I had introduced the use of LinkList in my previous post. So what can you do with a LinkList? And how can it help you?
Let us take a look at a simple example.
If you are staying in a house with many rooms and each room have a few cupboards or shelves fully occupied with books, you probably will have a list like the following.
Click
here for the list.
I have created a simple flex application to sort all the data in a orderly manner.
Buy let us take a look at some parts of the codes first.
On top of the CustomLinkList class that I had introduced in the previous post, I had build a room class on top of the CustomLinkList class.
package List
{
public class RoomsList extends CustomLinkList
{
public function RoomsList()
{
super();
}
//Override the add Value function in the CustomLinkList class
override public function addValue(tempValue:*):void{
var newNode:Node = new RoomNode(tempValue);
var baseNode:Node = head;
var tempNode:Node;
if(baseNode == null){
head = newNode;
}else{
var hasCheck:Boolean = false;
while(hasCheck == false){
//While moving from one node to the next,
//do a comparison between the nodes
//and find the best place to place the new node
if(compareRoomName(baseNode, newNode)){
hasCheck = true;
if(baseNode.prev == null){
head = newNode;
}
tempNode = baseNode.prev;
if(tempNode != null){
tempNode.next = newNode;
}
newNode.prev = baseNode.prev;
newNode.next = baseNode;
baseNode.prev = newNode;
}else{
if(baseNode.next == null){
baseNode.next = newNode;
newNode.prev = baseNode;
hasCheck = true;
}else{
baseNode = baseNode.next;
}
}
}
}
count ++;
}
//Function to compare 2 node and if the new node that was pass into
//this function had a smaller value, it will return true, else false
//We are going to sort the room by their room_name value
public function compareRoomName(tempNode1:Node, tempNode2:Node):Boolean{
var name1:String = String(tempNode1.value.room_name).toLowerCase();
var name2:String = String(tempNode2.value.room_name).toLowerCase();
var tempArray:Array = new Array();
tempArray.push(name1);
tempArray.push(name2);
tempArray.sort();
if(name2 == tempArray[0]){
return true;
}else{
return false;
}
return false;
}
}
}
And a RoomNode class that builds on top of the Node class.
package List
{
public class RoomNode extends Node
{
private var shelvesList:ShelvesList = new ShelvesList();
//After passing in the value of this node for room,
//Pass in all the relevant data for creating the
//respective shelves
public function RoomNode(tempValue:*):void {
// constructor code
super(tempValue);
for(var i:int = 0; i < this.value.shelfs.shelf.length(); i ++){
shelvesList.addValue(this.value.shelfs.shelf[i]);
}
}
public function get roomContents():ShelvesList{
return shelvesList;
}
}
}
And this would be my main application class.
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute"
width="100%"
height="100%"
creationComplete="creationCompleteHandler(event)">
<mx:Style>
VBox{
paddingTop:5;
paddingBottom:5;
paddingLeft:5;
paddingRight:5;
}
</mx:Style>
<mx:Script>
<![CDATA[
import List.*;
import mx.events.FlexEvent;
//Embed a xml file that contents data
[Embed(source="xml/booksData.xml")]
private var dataXml:Class;
//Create a list for rooms
private var tempRoomList:RoomsList = new RoomsList();
protected function creationCompleteHandler(event:FlexEvent):void{
var strOutput:String = "";
var tempDataXML:XML = dataXml.data as XML;
//Pass all the data into the list for rooms
for(var i:int = 0; i < tempDataXML.room.length(); i ++){
tempRoomList.addValue(tempDataXML.room[i]);
}
var roomObject:RoomNode;
var shelfObject:ShelfNode;
//Display all the contents in the list for rooms
for(i = 0; i < tempRoomList.length(); i ++){
roomObject = tempRoomList.getNodeAt(i);
strOutput += "Room Name = " +
String(roomObject.value.room_name);
strOutput += "\n";
for(var j:int = 0; j < roomObject.roomContents.length(); j ++){
shelfObject = roomObject.roomContents.getNodeAt(j);
strOutput += "-> Shelf Name = " +
String(shelfObject.value.shelf_name);
strOutput += "\n";
for(var k:int = 0; k < shelfObject.shelfContents.length(); k ++){
strOutput += " -> Book Title = " +
String(shelfObject.shelfContents.getNodeAt(k).value.book_title);
strOutput += ", Written By " +
String(shelfObject.shelfContents.getNodeAt(k).value.author);
strOutput += "\n";
}
}
}
txtMessage.text = strOutput;
}
]]>
</mx:Script>
<mx:VBox width="100%" height="100%">
<mx:TextArea id="txtMessage" width="100%" height="100%"/>
</mx:VBox>
</mx:Application>
* Click
here to view the demo in this post.
^ Click
here for the source files of the demo
` Click
here to follow the other post on LinkList.