Thursday, March 24, 2011

Create a log application using XML and AIR: Part 3

In this tutorial we will add the ability to delete the selected log in our application.

First of all create a new button on stage and call it deleteEntryButton. Place it somewhere near the description text area. By now you should have your stage set up like this:



Now, the coding.

The first thing we need to do is add a listener for the button:

deleteEntryButton.addEventListener(MouseEvent.CLICK, deleteEntry);

We want this button to only be visible when a log is selected, so edit the selectEntry function like this:

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

We know that the selection will be reset every time we call the updateList function, so that is where we should make the button invisible:

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]});
}
deleteEntryButton.visible = false;
myDescription.text = "";
}

Now, the deletion function. We need to perform 3 actions when deleting a log. Firstly, we need to delete the said log from the localXML object. Secondly, we need to update the list. And thirdly, we need to update the real XML file on the hard drive.

To remove the node from our local XML object, we'll use the keyword "delete" and find the needed node by getting the selectedIndex property from the list.

function deleteEntry(evt:MouseEvent):void
{
delete localXML.logentry[myList.selectedIndex];
updateList();
saveXML();
}

Here is 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);
deleteEntryButton.addEventListener(MouseEvent.CLICK, deleteEntry);
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;
deleteEntryButton.visible = true;
}

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]});
}
deleteEntryButton.visible = false;
myDescription.text = "";
}

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

function deleteEntry(evt:MouseEvent):void
{
delete localXML.logentry[myList.selectedIndex];
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 2
Create a log application using XML and AIR: Part 4
Files and directories in AIR: Part 12

No comments:

Post a Comment