Hi, I'm Ben. This is my blog. I write about Flex,
Flash, software development, and other miscellany.

Cairngen: Create Sequence Use Existing Delegate

I've created an additional task that can be added to Eric Feminella's super handy Cairngen project. The task is a slight modification of the out-of-the-box "create sequence" tasks. Create Sequence Use Existing Delegate does just what the name implies, and allows you to specify an existing Delegate class to wire your newly generated Command class to. This is the most common repetitive task I use when coding Cairngorm-based Flex applications as any Commands that call the same remote service will utilize the same Delegate class. To use this task you simply need to add CommandUseExistingDelegate.tpl to your templates directory and add the target node below to your existing Cairngen build.xml file.

Note that I've added a couple of input nodes to the beginning which will cause Eclipse to open a dialog box asking you to enter the sequence name and full class name (e.g. MyCoolDelegate) of the Delegate you wish to use. You could easily strip these out and have the task get these values from your properties file if you so desire. Hopefully someone out there finds this useful!

<!-- create cairngen use existing delegate sequence, generates: Event, Command -->
<target name="create-sequence-use-existing-delegate" depends="log">
    <input message="Sequence name: " addproperty="seq" />
    <input message="Delegate class: " addproperty="del" />
    <echo>${log.sequence.exclude.delegate.message}</echo>
    <copy file="${cairngorm.templates.dir}/${cairngorm.framework.name}${cairngorm.version}/Event.tpl" tofile="${project-uri}/${events.dir}/${seq}Event.as" overwrite="${overwrite.existing.files}">
        <filterchain>
           <replacetokens>
                <token key="sequence"  value="${seq}" />
                <token key="namespace" value="${namespace}"     />
                <token key="events"    value="${events.dir}"    />
            </replacetokens>
        </filterchain>
    </copy>
    <copy file="${cairngorm.templates.dir}/${cairngorm.framework.name}${cairngorm.version}/CommandUseExistingDelegate.tpl" tofile="${project-uri}/${commands.dir}/${seq}Command.as" overwrite="${overwrite.existing.files}">
        <filterchain>
           <replacetokens>
                <token key="sequence"  value="${seq}" />
                <token key="namespace" value="${namespace}"     />
                <token key="events"    value="${events.dir}"    />
                <token key="commands"  value="${commands.dir}"  />
                <token key="business"  value="${business.dir}"  />
             <token key="delegate"  value="${del}"  />
            </replacetokens>
        </filterchain>
    </copy>
</target>

"Apollo" to be called Adobe AIR?

Everyone knows that Apollo is just an interim name for Adobe's upcoming platform for desktop RIAs. Today Ted Patrick concluded his week of posts revealing features of Flex 3 with a post on "Apollo". Aside from the liberal use of quotes, he also blurred out what we can assume is the official name that the product will ship with in the screenshots he provided. Below you will find a magnified portion of one of the images that seems to clearly show the name beginning with an A, and being very short. The final letter also has what looks an awful lot like the descender of a capital R. I suppose AIR would make a pretty suitable name since the application files the platform generates are .air files, and of course its RIA backwards. What do you think?

Apollo name revealed?

ArrayCollection for AS2

Did you know that AS2 includes rudimentary implementations of collections and iterators? I didn't until a few months back, but they do come in handy from time to time. An added bonus is that they make me less sad about developing in AS2 and missing out on all that AS3 goodness when the project requires FP8 compatibility.

Anyhow, the default implementation still leaves a bit to be desired, especially for those of us who spend the majority of our time coding AS3. The built-in class not only bears the unfortunate name CollectionImpl (which implements the Collection interface, both of which reside in the mx.utils package), but it also lacks some methods I've grown to appreciate in AS3's ArrayCollection class. Those methods are as follows:

  • length (getter)
  • addItemAt()
  • contains()
  • getItemIndex()
  • removeAll()
  • removeItemAt()

The following class provides those methods. I know, nothing fancy or impressive, but useful (in my opinion) nonetheless. CollectionImpl isn't all bad though, it does provide us with most of the basic collection-related functionality you would expect, including the removeItem() method that was left off of AS3's ArrayCollection class. As such, this class inherits from CollectionImpl and attempts to fill in the blanks.

import mx.utils.Collection;
import mx.utils.CollectionImpl;

class com.returnundefined.utils.ArrayCollection extends CollectionImpl implements Collection
{
    private var _items:Array;

    public function ArrayCollection(arr:Array)
    {
        super();
        _items = (arr == null) ? new Array() : arr;
    }

    public function get length():Number
    {
        return _items.length;
    }

    public function addItemAt(item:Object, index:Number):Boolean
    {
        var result:Boolean = false;

        if(item != null)
        {
            _items.splice(index, 0, item);
            result = true;
        }

        return result;
    }

    public function contains(item:Object):Boolean
    {
        return getItemIndex> -1;
    }

    public function getItemIndex(item:Object):Number
    {
        var index:Number = -1;
        var len:Number = length;
        for(var i:Number = 0; i <len; i++)
        {
            if (_items[i] == item)
            {
                index = i;
                break;
            }
        }
        return index;
    }

    public function removeAll():Void
    {
        _items = new Array();
    }

    public function removeItemAt(index:Number):Boolean
    {
        return removeItem(getItemAt(index));
    }
}

Feel free to alter the package path here and insert this class into your library wherever you want.