Flex vs Silverlight: Debate at Cal Poly Pomona

Posted in AIR, Flex, Miscellaneous on February 16th, 2009 by Kalen Gibbons

If you haven’t seen it yet, check out the debate between Adobe’s Kevin Hoyt and Microsoft’s Sam Stokes, which was presented last week at Cal Poly Pomona. Although my perspective is admittedly a bit skewed, seeing as I love Flex and hate Microsoft, I think the victor is fairly obvious.

The Microsoft presentation was very weak, relying heavily on the strength of one image manipulation library and a couple “non-Silverlight” services. Even I expected more from Microsoft. Give the video a watch and see if you agree.

http://video.csupomona.edu/streaming/Events/FutureOfComputing.html


Tags: , , , ,

Tour de Flex: An amazing learning tool

Posted in AIR, Flex, Miscellaneous on December 30th, 2008 by Kalen Gibbons

If you haven’t checked out Tour de Flex I suggest you do. It is an Air app that contains over 200 working applications, all with source code. Everything from simple Flex components to advanced filters and working with Air is covered, so there’s something for everyone, beginners to advanced developers alike.

 


Tags:

How to drag, drop, and parse a CSV file in AIR

Posted in ActionScript, AIR, Flex, How-To on November 26th, 2008 by Kalen Gibbons

Drag and Drop a CSV file in Adobe AIRI’ve been playing around with Adobe AIR lately and I’ve really been enjoying it. So I thought I’d put out this quick tutorial on how to drag and drop a file into your AIR application; in this case we will be using a CSV file, and then we will parse that file so that its data can be used in a datagrid. You can download the full source code here.

Dragging a file into AIR:

First, we need to set up handlers to listen for the drag events. We are going to set up two listeners, one for when an item is dragged over the app and one for when that item is dropped.

this.addEventListener(NativeDragEvent.NATIVE_DRAG_ENTER, dragEnterHandler);
this.addEventListener(NativeDragEvent.NATIVE_DRAG_DROP, dragDropHandler);

Then we’ll set up the two handler function. The dragEnterHandler function determines if the item is an acceptable file type. You could use the different constants of the ClipboardFormats class to only accept specific file formats, but for now we will accept any valid file.

private function dragEnterHandler(evt:NativeDragEvent):void{

if(evt.clipboard.hasFormat(ClipboardFormats.FILE_LIST_FORMAT)){

NativeDragManager.acceptDragDrop(this);

}

}

Next, when the user drops the file, we will copy it from the clipboard, read its contents, and parse its values into a more usable data structure.

private function dragDropHandler(evt:NativeDragEvent):void{

NativeDragManager.dropAction = NativeDragActions.COPY;
//get an array of the files dropped in
var dropFiles:Array = evt.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array;
//get the content of the file
var fileContent:String = getFileContent(dropFiles[0]);
//parse it to get an array of Orders
orders = parseCSV(fileContent);

}

Reading the content of the dropped file:

For this we will use the FileStream class. The getFileContent function takes a generic file as its only parameter; it opens a FileStream, reads the contents of the file and returns that value as a string.

private function getFileContent(_file:File):String{

//open a fileStream to read the content of the file
var fileStream:FileStream = new FileStream();
fileStream.open(_file, FileMode.READ);
var fileContent:String = fileStream.readUTFBytes(fileStream.bytesAvailable);
fileStream.close();
return fileContent;

}

Parsing the CSV file:

Finally, we are going to take the content of the CSV file, which is now in the form of a string, and parse out its data to populate an Array. We start by using the split() function to create an Array of line items. We use character code 10, which represents carriage returns, and character code 13, which represents new lines. to separate each line item. Then we loop through the newly created array of line items and pull out each comma separated value. At this point we have the information we need to populate the Order object and add that to our Array.

private function parseCSV(_content:String):Array{

//create temporary array to store the Orders
var csvArray:Array = new Array();
//break the csv into individual lines
var csvLines:Array = _content.split(String.fromCharCode(13,10));
//remove title row
csvLines.splice(0,1);
//loop over each line
for each(var s:String in csvLines){

var lineItems:Array = s.split(“,”);
var transaction:Order = new Order( lineItems[0],

new Date(lineItems[1]),
lineItems[2],
lineItems[3],
lineItems[4]);

csvArray.push(transaction);

}
return csvArray;

}

The data pulled from the CSV file is now in a more manageable form and can be used as a dataProvider for components or to populate AIR’s built in SQLite database. Of course, in this tutorial, for simplicity’s sake, we assume that the file dropped into the application is a CSV file. In your application you should have some trapping to determine the file type and process it accordingly.


Tags: , , , , , ,

Connecting Abobe AIR (beta) to Coldfusion via mx:RemoteObject

Posted in AIR, Flex, How-To on August 17th, 2007 by Kalen Gibbons

While Flex 3 and AIR are still in beta, there are lots of little bugs and quirks that need to be worked around.  Today I was porting over a Flex application to AIR, and received an error whenever using mx:RemoteObject.  It looked something like the following:

[RPC Fault faultString="Send failed" faultCode="Client.Error.MessageSend" faultDetail="Channel.Connect.Failed error NetConnection.Call.Failed: HTTP: Failed: url: 'http://myApp-debug.swf/flex2gateway/'"]

I searched for hours and hours trying to find out why my application worked correctly as a regular Flex application but not as an AIR app.  I finally stumbled upon the solution and I thought I would share… in order to save everyone else the same headache.  The key is to explicitly specify the endpoint attribute in the RemoteObject tag.  If you are testing locally it may look something like this:

<mx:RemoteObject

id="AIR_Service"
destination="ColdFusion"
showBusyCursor="true"
endpoint="http://localhost:8500/flex2gateway"
source="myApp.cfc.cfcName" />

This is something I hope Adobe will fix before the official release of both these applications, but in the meantime, here is your workaround.

Update: After working with AIR for a while, I realize, of course, that this is not actually a bug. AIR does not have the same server context that Flex has, and therefore, an endpoint is required. The thing to remember when porting an application from Flex to AIR is not to forget the endpoint, or AIR won’t know what server it is supposed to connect to.


Tags: ,