Thursday, October 16, 2008

Selecting the right value from a ComboBox or List

I know, I promised Degrafa skinning, and I'm skipping out. It'll be next, if I can find the time!

In other news, there's been some talk as there always is regarding the difficulty some have been having due to the fact ComboBox (and others like it) work with identity rather than contents when choosing which item within the dataProvider is the "selected" value. There are various solutions, usually involving subclassing ComboBox or something along those lines. Well, that's kinda unnecessary. I maintained that you could cook up a util to achieve the same thing across any list-style control, and by golly I like to stand up to my word.

You can download the source for the util here. I'll add it to SVN soon, I'm kinda busy atm tho. Lousy sleep eating up 8 hours of productivity a day!

For an example of how to use the <FindIndex> code:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:pkg="pkg.*">

    <mx:ArrayCollection id="src1">
        <mx:Object label="Ken" objid="1"/>
        <mx:Object label="Ryu" objid="2"/>
        <mx:Object label="Chun-Li" objid="3"/>
        <mx:Object label="Dhalsim" objid="4"/>
        <mx:Object label="E. Honda" objid="5"/>
        <mx:Object label="Blanka" objid="6"/>
        <mx:Object label="Zangief" objid="7"/>
        <mx:Object label="Guile" objid="8"/>
    </mx:ArrayCollection>

    <mx:ArrayCollection id="src2">
        <mx:Object label="Ken" objid="1"/>
        <mx:Object label="Ryu" objid="2"/>
        <mx:Object label="Chun-Li" objid="3"/>
        <mx:Object label="Dhalsim" objid="4"/>
        <mx:Object label="E. Honda" objid="5"/>
        <mx:Object label="Blanka" objid="6"/>
        <mx:Object label="Zangief" objid="7"/>
        <mx:Object label="Guile" objid="8"/>
    </mx:ArrayCollection>

    <pkg:FindIndex source="{src2}" keyField="objid" searchFor="{ left.selectedItem }" id="fi"/>

    <mx:HBox horizontalCenter="0" verticalCenter="0">

        <mx:List id="left" dataProvider="{src1}" width="120"/>

        <mx:List id="right" dataProvider="{src2}" selectedIndex="{ fi.matchingIndex }" width="120"/>

    </mx:HBox>
</mx:Application>

Degrafa skinning next week! Honest!

Thursday, October 9, 2008

Typesafe ActionScript Enums

A busy week, with some client work and a 4-day weekend away (starting in about 12 hours, woot!), so a quick post.

Typesafe Enums are something we take for granted these days in a lot of programming languages, but unfortunately ActionScript isn't yet one of them. However, there are a few solutions out there to provide this functionality, and of course since none of them met my needs exatly, this is mine!

First, let's jump into how we use the damn things - here's how you would create a new Enum type:

package demo
{
    import info.joshmcdonald.barra.utils.Enum;

    public class CompassPoint extends Enum
    {
        public static const NORTH      : CompassPoint = Enum.create(CompassPoint, "N",  "Towards Santa");
        public static const NORTH_EAST : CompassPoint = Enum.create(CompassPoint, "NE", "North East");
        public static const EAST       : CompassPoint = Enum.create(CompassPoint, "E",  "East");
        public static const SOUTH_EAST : CompassPoint = Enum.create(CompassPoint, "SE", "South East");
        public static const SOUTH      : CompassPoint = Enum.create(CompassPoint, "S",  "Towards Penguins");
        public static const SOUTH_WEST : CompassPoint = Enum.create(CompassPoint, "SW", "South West");
        public static const WEST       : CompassPoint = Enum.create(CompassPoint, "W",  "West");
        public static const NORTH_WEST : CompassPoint = Enum.create(CompassPoint, "NW", "North West").close();
    }
}

Seems simple enough, right?

Wait, hold on, "what the hell is that on the end?" I hear you cry? Why, I'm terribly glad you asked! That my friends, combined with using Enum.create() is how we stop people from instantiating new instances willy-nilly, which of course leads to anarchy! When you call close(), no more instances of Compass can be instantiated, and if you try, you'll get a run-time error.

Now, some "client" code that makes use of our new CompassPoint class:

<mx:Script>
    <![CDATA[
        import info.joshmcdonald.barra.utils.Enum;
        import demo.CompassPoint;

        private function cc() : void
        {
            var towardsPerth : CompassPoint = CompassPoint.SOUTH_WEST;

            trace("Direction to Perth is", towardsPerth);

            trace("What are all the Compass points?");
            trace("   * " + Enum.valuesOf(CompassPoint).join("\n   * "));
        }

    ]]>
</mx:Script>
</mx:Application>

As usual, I've added Enum.as to my blog's google code repository.

Next week: Skinning with Degrafa!

Thursday, October 2, 2008

Announcing Smartypants IOC, Dependency Injection for Flex!

I'm happy to announce the public release of the Smartypants IOC dependency-injection framework for Flex! Inspired by the likes of Guice, it features annotations to specify dependencies, and a simple internal DSL to wire everything together.

Dependency Injection, Flex style!

Normally, dependency injection is a static thing. Once your object has been populated and handed over, you and your injection framework are through. However, Smartypants' live injection functionality acts as a mediator allowing bindings between un-coupled classes, without keeping any hard-references to either that can lead to troublesome memory leaks.

Grab the source or binary over at Google Code, check out the API reference, join the discussion group and hack away! I'm looking forward to your feedback, opinions, and thoughts on the direction in which you'd like to see Smartypants IOC heading in the future?

This is

  • Tales of Flex
  • From Brisbane, Australia
  • Opinions on Flex development
  • Tips and FAQs
  • Shameless self-promotion

I am

  • Twitterer
  • Flexcoder
  • Maroon
  • Designer
  • Java lover
  • That loud-mouthed Aussie yob
  • Blogger
  • Problem solver
  • Contributor
  • Cricket Fan
  • Lousy photographer
  • Great cook

I read



Subscribe via RSS to receive updates!