Wednesday, March 23, 2011

Create a log application using XML and AIR: Part 2

In the second part of the tutorial, we will display the log data in our application.

Firstly, create a new List component on stage and call it myList. Create a dynamic text field next to it, call it myDescription. This is where we'll be able to select a log entry and read it.

We will introduce a new method called upateList(), and the first time we call it is right after we use the getXML() for the first time.

updateList();

Here is the function itself:

function updateList():void
{
myList.dataProvider = new DataProvider();
for (var log in localXML.logentry)
{
myList.addItem({label: "Log entry: " + localXML.logentry[log].@date, data: localXML.logentry[log]});
}
}

We also add a listener for the list to display the information about the log entry when an item in the list is clicked:

myList.addEventListener(Event.CHANGE, selectEntry);

The selectEntry function itself:

function selectEntry(evt:Event):void
{
myDescription.text = evt.target.selectedItem.data;
}

We need to call updateList every time we make changes to the XML, so we need to also include it in the writeEntry function:

function writeEntry(evt:MouseEvent):void
{
localXML.appendChild(<logentry date={new Date()}>{myText.text}</logentry>);
myText.text = "";
updateList();
saveXML();
}

Here's the full code:

import flash.filesystem.File;
import flash.filesystem.FileMode;
import flash.filesystem.FileStream;
import flash.events.MouseEvent;
import flash.events.Event;
import fl.data.DataProvider;

var myFile:File = File.documentsDirectory.resolvePath("Air Examples/mylog.xml");
var localXML:XML;
myText.text = "";

getXML();
updateList();

myWriteButton.addEventListener(MouseEvent.CLICK, writeEntry);
myList.addEventListener(Event.CHANGE, selectEntry);

function getXML():void
{
if (myFile.exists)
{
var myStream:FileStream = new FileStream();
myStream.open(myFile, FileMode.READ);
localXML = new XML(myStream.readUTFBytes(myStream.bytesAvailable));
myStream.close();
}
else
{
localXML = <mylog></mylog>;
}
}

function saveXML():void
{
var myStream:FileStream = new FileStream();
myStream.open(myFile, FileMode.WRITE);
myStream.writeUTFBytes(localXML.toXMLString());
myStream.close();
}

function selectEntry(evt:Event):void
{
myDescription.text = evt.target.selectedItem.data;
}

function updateList():void
{
myList.dataProvider = new DataProvider();
for (var log in localXML.logentry)
{
myList.addItem({label: "Log entry: " + localXML.logentry[log].@date, data: localXML.logentry[log]});
}
}

function writeEntry(evt:MouseEvent):void
{
localXML.appendChild(<logentry date={new Date()}>{myText.text}</logentry>);
myText.text = "";
updateList();
saveXML();
}

Thanks for reading!

Related:

Files and directories in AIR: Part 1
Files and directories in AIR: Part 2
Files and directories in AIR: Part 3
Files and directories in AIR: Part 4
Files and directories in AIR: Part 5
Files and directories in AIR: Part 6
Files and directories in AIR: Part 7
Files and directories in AIR: Part 8
Files and directories in AIR: Part 9
Files and directories in AIR: Part 10
Files and directories in AIR: Part 11
Create a log application using XML and AIR: Part 1
Create a log application using XML and AIR: Part 3
Create a log application using XML and AIR: Part 4
Files and directories in AIR: Part 12

2 comments:

Roberto Aprile said...
This comment has been removed by the author.
Roberto Aprile said...
This comment has been removed by the author.

Post a Comment