Monday, December 3, 2012

Image Base64Encoder


<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
  xmlns:s="library://ns.adobe.com/flex/spark"
  xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
<s:layout>
<s:VerticalLayout/>
</s:layout>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.utils.Base64Encoder;
import mx.utils.ObjectUtil;

import spark.components.mediaClasses.VolumeBar;

public var fileReference:FileReference=new FileReference();
private function btn_click(evt:MouseEvent):void {
var arr:Array = [];
arr.push(new FileFilter("Images", ".gif;*.jpeg;*.jpg;*.png"));
fileReference.addEventListener(Event.COMPLETE, fileReference_complete);
fileReference.addEventListener(Event.SELECT, fileReference_select);
fileReference.browse(arr);
}

private function fileReference_select(evt:Event):void {
fileReference.load();
}

private function fileReference_complete(evt:Event):void {
img.source = fileReference.data;
var ba:ByteArray = fileReference.data;//['data'];
var b64encoder:Base64Encoder = new Base64Encoder();
b64encoder.reset();
b64encoder.encodeBytes(ba);
//var _schoolkey:int=3009;
TA.text=b64encoder.flush();
Alert.show(ObjectUtil.toString(fileReference));
}

]]>
</fx:Script>

<mx:VBox width="100%" height="100%" paddingLeft="5" paddingRight="5" paddingBottom="5" paddingTop="5" borderColor="#040404">


<mx:Panel id="panel"
 layout="absolute"
 width="100%"
 height="100%" title="Base64Encoder">
<mx:Image id="img" scaleContent="false"/>
<mx:ControlBar>
<mx:Button id="btn"
  label="Browse and preview..."
  click="btn_click(event);" />
<mx:Button label="Upload..."
  enabled="false" />
</mx:ControlBar>
</mx:Panel>
<s:TextArea width="100%" height="100%" id="TA"/>
</mx:VBox>
</s:Application>














Monday, November 5, 2012

Advanced Data Grid With Alive PDF

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
  xmlns:s="library://ns.adobe.com/flex/spark" creationComplete="initData()"
  xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
<fx:Declarations>

</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.controls.dataGridClasses.DataGridColumn;

import org.alivepdf.colors.IColor;
import org.alivepdf.colors.RGBColor;
import org.alivepdf.data.Grid;
import org.alivepdf.data.GridColumn;
import org.alivepdf.display.Display;
import org.alivepdf.fonts.CoreFont;
import org.alivepdf.layout.Orientation;
import org.alivepdf.layout.Size;
import org.alivepdf.layout.Unit;
import org.alivepdf.pdf.PDF;
import org.alivepdf.saving.Download;
import org.alivepdf.saving.Method;
[Bindable]private var dpFlat:ArrayCollection = new ArrayCollection([
{Region:"Southwest", Territory:"Arizona",
Territory_Rep:"Barbara Jennings", Actual:38865, Estimate:40000},
{Region:"Southwest", Territory:"Arizona",
Territory_Rep:"Dana Binn", Actual:29885, Estimate:30000},
{Region:"Southwest", Territory:"Central California",
Territory_Rep:"Joe Smith", Actual:29134, Estimate:30000},
{Region:"Southwest", Territory:"Nevada",
Territory_Rep:"Bethany Pittman", Actual:52888, Estimate:45000},
{Region:"Southwest", Territory:"Northern California",
Territory_Rep:"Lauren Ipsum", Actual:38805, Estimate:40000},
{Region:"Southwest", Territory:"Northern California",
Territory_Rep:"T.R. Smith", Actual:55498, Estimate:40000},
{Region:"Southwest", Territory:"Southern California",
Territory_Rep:"Alice Treu", Actual:44985, Estimate:45000},
{Region:"Southwest", Territory:"Southern California",
Territory_Rep:"Jane Grove", Actual:44913, Estimate:45000},
{Region:"NorthEast", Territory:"New York",
Territory_Rep:"Jose Rodriguez", Actual:26992, Estimate:30000},
{Region:"NorthEast", Territory:"New York",
Territory_Rep:"lisa Sims", Actual:47885, Estimate:50000},
{Region:"NorthEast", Territory:"Massachusetts",
Territory_Rep:"kelly o'connell", Actual:172911, Estimate:20000},
{Region:"NorthEast", Territory:"Pennsylvania",
Territory_Rep:"John Barnes", Actual:32105, Estimate:30000},
{Region:"MidWest", Territory:"Illinois",
Territory_Rep:"Seth Brown", Actual:42511, Estimate:40000}
]);

private var dgArray:Array = [
{Artist:'Pavement', Album:'Slanted and Enchanted', Price:11.99},
{Artist:'Pavement', Album:'Brighten the Corners', Price:11.99},
{Artist:'test2', Album:'junk1', Price:11.99},
{Artist:'test2', Album:'junk2', Price:22.99},
{Artist:'test3', Album:'junk3', Price:11.99},
{Artist:'test3', Album:'junk4', Price:11.99},
{Artist:'test4', Album:'junk1 and Enchanted', Price:35.99},
{Artist:'test4', Album:'Slanted and junk1', Price:6.99},
{Artist:'test5', Album:'Slanted junk1', Price:89.99},
{Artist:'test5', Album:'junk1 Enchanted', Price:33.12}];

[Bindable] public var dgArrayColl:ArrayCollection;

private function initData():void
{
dgArrayColl=new ArrayCollection(dgArray);
}

private function myLabelFunc(data:Object, column:DataGridColumn):String
{
return "$" + data.Price;
}

private function sendToPDF():void
{
var PDFcolumns:Array = getPDFColumns(myADG);

var resultsArray:Array = myADG.dataProvider.toArray();
formatPDFGridData(resultsArray, myADG);

var pdf:PDF  = new PDF(Orientation.PORTRAIT,Unit.MM,Size.A4);
pdf.addPage();
pdf.setAutoPageBreak(false,1);
pdf.setDisplayMode ( Display.FULL_PAGE );

pdf.setFont(new CoreFont(),10);
pdf.textStyle(new RGBColor(0x000000));


var grid:Grid = new Grid(resultsArray,200, 120, new RGBColor ( 0xCCCCCC ), new RGBColor (0xFFFFFF), new RGBColor (0xFFFFFF), true, new RGBColor(0x000000));
grid.columns = PDFcolumns;
pdf.addGrid(grid,25,10);
var bytes:ByteArray=pdf.save(Method.LOCAL);
var f:FileReference=new FileReference();

f.save(bytes,"Test 1.pdf");
/* pdf.save(Method.REMOTE, "http://mywebserver:8080/alivepdf/CreatePDFServlet", Download.ATTACHMENT, "parcelreport.pdf"); */
Alert.show('test');
} // end function sendToPrinter

private function getPDFColumns(datagrid:AdvancedDataGrid):Array
{


var PDFcolumns:Array = new Array;
var dataGridColumn:AdvancedDataGridColumn;

for( var i:Number = 0; i < datagrid.columns.length; i++ )
{
dataGridColumn = datagrid.columns[i];
if (dataGridColumn.visible)
{
var gridColumn:GridColumn = new GridColumn(dataGridColumn.headerText,
dataGridColumn.dataField,
dataGridColumn.width * 0.35);
PDFcolumns.push(gridColumn);
}
}
return PDFcolumns;
}

private function formatPDFGridData(arr:Array, datagrid:AdvancedDataGrid):void
{
var dataGridColumn:AdvancedDataGridColumn;
for (var i:int = 0; i<arr.length; i++)
{
for( var iCols:Number = 0; iCols < datagrid.columns.length; iCols++ )
{
dataGridColumn = datagrid.columns[iCols];
if (dataGridColumn.labelFunction != null)
{
arr[i][dataGridColumn.dataField] = dataGridColumn.labelFunction(arr[i],dataGridColumn);
}
}
}
}
]]>
</fx:Script>

<!--<mx:DataGrid id="dg" dataProvider="{dgArrayColl}" width="420" x="10" y="87">
<mx:columns>
<mx:DataGridColumn dataField="Artist" width="100"/>
<mx:DataGridColumn dataField="Album" />
<mx:DataGridColumn dataField="Price" width="100" visible="true" labelFunction="myLabelFunc"/>
</mx:columns>
</mx:DataGrid>
--><mx:Button label="PDF" click="sendToPDF()" x="84" y="33"/>
<mx:AdvancedDataGrid id="myADG"
width="507" height="190"
color="0x323232"
dataProvider="{dpFlat}" x="438" y="87">

<mx:columns>

<mx:AdvancedDataGridColumn dataField="Region" />
<mx:AdvancedDataGridColumn dataField="Territory" />
<mx:AdvancedDataGridColumn dataField="Territory_Rep"
  headerText="Territory Rep"/>
 
<mx:AdvancedDataGridColumn dataField="Actual"/>
<mx:AdvancedDataGridColumn dataField="Estimate"/>
 

</mx:columns>
<mx:rendererProviders>
<mx:AdvancedDataGridRendererProvider dataField="Region" renderer="testred" columnIndex="11" rowSpan="5"/>
</mx:rendererProviders>
</mx:AdvancedDataGrid>


</s:Application>













DEMO 
DOWNLOAD SOURCE FILE

Wednesday, August 29, 2012

Cairngorm (Flex Framework)

Cairngorm is based on the MVC model. It is specifically designed to facilitate complex state and data synchronization between the client and the server, while keeping the programming of the View layer detached from the data implementation.
The role of the View layer in a Cairngorm application is to throw events and bind to data stored in the Model. Components on the View can bind to Value Objects or other properties in the Model (data) layer.
In a Cairngorm Model, related data are stored in Value Objects (VOs), while simple variables can be stored as direct properties of the ModelLocator class. A static reference to the ModelLocator singleton instance is used by the View layers to locate the required data.
The Controller is the most sophisticated part of the Cairngorm architecture. The Controller layer is implemented as a singleton FrontController. The FrontController instance, which receives every View-generated event, dispatches the events to the assigned Command class based on the event's declared type.

The Command class then processes the event by running the Command class' execute() method, which is an ICommand interface method. The event object may include additional data if required by the developer. The execute() method can update the central Model, as well as invoke a Service class which typically involves communication with a remote server. The IResponder interface, which is also implemented by the Command class, includes onResult and onFault methods to handle responses returned from the invoked remote service


DEMO APPLICATION HERE

1). Hello World Application
2). Demo Cairngorm Diagram

Thanks To
( AsFusion & Robsondesign)