Posts Tagged ‘ Navigation ’

Using IViewCursor for XML Traversing

The source code for using IViewCursor for XMLListCollection Navigation is here.

Advertisements

Using IViewCursor for Navigation

The Below code is example for using IViewCursor for ArrayCollection Navigation

<?xml version=”1.0″?>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml&#8221; initialize=”run();”>
<mx:Script>
<![CDATA[
import mx.collections.*;
private var myCursor:IViewCursor;
[Bindable]
public var myAC:ArrayCollection;
public var myArray:Array = [ { label:1, data:”One” }, { label:2, data:”Two” }, { label:3, data:”Three” }, { label:4, data:”Four” }, { label:5, data:”Five” },
{ label:6, data:”Six” }, { label:7, data:”Seven” }, { label:8, data:”Eight” }, { label:9, data:”Nine” }, {label:10, data:”Ten”}];

public function run():void {
myAC = new ArrayCollection(myArray);
myCursor=myAC.createCursor();
var sort:Sort = new Sort();
sort.fields = [new SortField(“label”)];
myAC.sort=sort;
myAC.refresh();
first.enabled = false;
back.enabled = false;
}
public function countLast(theCursor:IViewCursor):int {
var counter:int=0;
var mark:CursorBookmark=theCursor.bookmark;
while (theCursor.moveNext()) {
counter++;
}
theCursor.seek(mark);
return counter;
}
public function countFromSelection():void {
myCursor.findAny(myCB.selectedItem);
var count:int = countLast(myCursor);
ta1.text = “cursor is at: ” + myCursor.current.label;
enableDisableButtons();
}
public function nextCollection():void {
if(! myCursor.afterLast) {
myCursor.moveNext();
ta1.text=”cursor is at: ” + myCursor.current.label;
}
}
public function backCollection():void {
if(!myCursor.beforeFirst) {
myCursor.movePrevious();
ta1.text=”cursor is at: ” + myCursor.current.label;
}
}
public function firstCollection():void {
myCursor.seek(CursorBookmark.FIRST);
ta1.text=”cursor is at: ” + myCursor.current.label;
}
public function lastCollection():void {
myCursor.seek(CursorBookmark.LAST);
ta1.text=”cursor is at: ” + myCursor.current.label;
}
public function navigate(event:MouseEvent):void {
switch(event.currentTarget.label) {
case ‘first’:
firstCollection();
break;
case ‘back’:
backCollection();
break;
case ‘next’:
nextCollection();
break;
case ‘last’:
lastCollection();
break;
}
enableDisableButtons()
}
public function enableDisableButtons():void{
var firstInCollection:Boolean = myAC.getItemAt(0) == myCursor.current;
first.enabled = back.enabled = !firstInCollection
var lastInCollection:Boolean = myAC.getItemAt(myAC.length – 1) == myCursor.current;
last.enabled = next.enabled = !lastInCollection;
}
]]>
</mx:Script>
<mx:ComboBox id=”myCB” dataProvider=”{myAC}” change=”countFromSelection();”/>
<mx:TextArea id=”ta1″ height=”200″ width=”175″/>
<mx:HBox>
<mx:Button label=”first” id=”first” click=”navigate(event)”/>
<mx:Button label=”back” id=”back” click=”navigate(event)”/>
<mx:Button label=”next” id=”next” click=”navigate(event)”/>
<mx:Button label=”last” id=”last” click=”navigate(event)”/>
</mx:HBox>
</mx:Application>