Access keys

TraverseArrayEnumerator

Kind of class: class
Inherits from: ArrayEnumerator < Enumerator
Author: Arthur Clemens
Classpath: org.asapframework.data.array.TraverseArrayEnumerator
File last modified: Thursday, 12 October 2006, 11:08:42
Enhanced array enumerator, with the option to loop.
TraverseArrayEnumerator sends out traverse events of type playground.classes.data.array.TraverseArrayEnumeratorEvent.UPDATE.

A TraverseArrayEnumerator can be used with a paging controller to navigate through a list of thumbs or search result pages (see example below).
Usage:
Example for a enumerator that handles a row of thumbs.
private function PagingController (inTimeline:MovieClip) {
    // Create thumb image list:
    var thumbList:Array = createThumbs();
    // Create traverse enumerator for this list
    mThumbPager = new TraverseArrayEnumerator(thumbList);
    // Subscribe to update events so the previous and next buttons can be updated:
    mThumbPager.addEventListener(TraverseArrayEnumeratorEvent.UPDATE,
EventDelegate.create(this, handleThumbUpdate));
    // Highlight first thumb and load corresponding image:		
    activateThumb(mThumbPager.getNextObject());
}
In this example the previous, next and thumb image buttons are subclasses of org.asapframework.ui.buttons.EventButton:
public function onEventButtonPress ( e:EventButtonEvent ) : Void {
    if (e.target instanceof ThumbImage) {
        var thumb:ThumbImage = ThumbImage(e.target);
        var oldThumb:ThumbImage = mThumbPager.getCurrentObject();
        mThumbPager.setCurrentObject(thumb); // will update the enumerator			
        activateThumb(thumb, oldThumb);
    }
    if (e.target instanceof NextButton) {
        var oldThumb:ThumbImage = mThumbPager.getCurrentObject();
        var newThumb:ThumbImage;
        if (e.target._name == "next_btn") {
            newThumb = mThumbPager.getNextObject();
        }
        if (e.target._name == "previous_btn") {
            newThumb = mThumbPager.getPreviousObject();
        }
        activateThumb(newThumb, oldThumb);
    }
}
Update the previous and next button with each change:
private function handleThumbUpdate (e:TraverseArrayEnumeratorEvent) : Void {	
    next_btn.setEnabled(mThumbPager.hasNextObject());
    previous_btn.setEnabled(mThumbPager.hasPreviousObject());
}
Thumb update function:
private function activateThumb (inThumb:ThumbImage, inOldThumb:ThumbImage) : Void {
    if (inOldThumb) {
        inOldThumb.setSelected(false);
    }
    inThumb.setSelected(true);
    loadImage(inThumb.getId()); 
}
Events broadcast to listeners:
TraverseArrayEnumeratorEvent with type: UPDATE If the delegate validation method exists and only if the delegate method returns true.

Constructor

TraverseArrayEnumerator

function TraverseArrayEnumerator (
inArray:Array, inDoLoop:Boolean)
Creates a new array enumerator. Optionally stores a pointer to array inArray.
Parameters:
inArray :
(optional) the array to enumerate
inDoLoop:
if true, the enumerator will loop past the end of the array to the start (and back when traversing backwards)

Instance properties

traverseOptions

traverseOptions:Number
(read,write)

Instance methods

getNextObject

function getNextObject (
inTraverseOptions:Number)
Increments the location pointer by one and returns the object from the array at that location.
Returns:
(Deliberately untyped) The object at the new location. Returns null if the location pointer has moved past the end of the array and inTraverseOptions is not set to playground.classes.data.array.TraverseArrayOptions.LOOP.
Implementation note:
Calls update.

getPreviousObject

function getPreviousObject (
inTraverseOptions:Number)
Decrements the location pointer by one and returns the object from the array at that location.
Returns:
(Deliberately untyped) The object at the new location. Returns null if the location pointer has moved past the end of the array and inTraverseOptions is not set to playground.classes.data.array.TraverseArrayOptions.LOOP.
Implementation note:
Calls update.

hasNextObject

function hasNextObject (
inTraverseOptions:Number) : Boolean
Checks if there is an object after the current object.
Returns:
True: there is a next object; false: the current object is the last.

hasPreviousObject

function hasPreviousObject (
inTraverseOptions:Number) : Boolean
Checks if there is an object before the current object.
Returns:
True: there is a next object; false: the current object is the first.

setDelegate

function setDelegate (
inDelegateObject:Object, inDelegateMethod:Object) : Void
A delegate validation method is called in update when a delegate object is set. The delegate's validation method is called to evaluate the new node before it is set.
Parameters:
inDelegateObject:
the owner of the delegate method
inDelegateMethod:
Node validation method (method name or function reference). This method should accept an Object as parameter and return a Boolean to indicate the item's validity.

setLoop

function setLoop (
inDoLoop:Boolean) : Void
Set the looping property of the enumerator.
Parameters:
inDoLoop:
if true, the enumerator will loop past the end of the array to the start (and back when traversing backwards)