Dave
Dave

Reputation: 21924

Flex custom TreeItemRenderer that changes based on type of object?

I have a Flex tree control that holds a tree full of various types of objects. I'd like to change the label of the item based on this type (and other properties). I'd prefer to do this in a custom TreeItemRenderer rather than via label or labelfunction.

It seems that whatever I do, I cannot typecheck nor cast the objects and thus I get [Object object] in the nodes of my tree. Here is my renderer:

public class MyCustomTreeItemRenderer extends TreeItemRenderer {
    // Overriding to set the text for each tree node.      
    override protected function updateDisplayList(unscaledWidth:Number, 
                                                  unscaledHeight:Number):void {

        super.updateDisplayList(unscaledWidth, unscaledHeight);

        if (super.data) {
            trace("Rendering node:");

            if (super.data is MyClassA) { 
                trace("             MyClassA");
                super.label.text =  MyClassA(super.data).name
            }

            if (super.data is MyClassB) { 
                trace("             MyClassB");
                super.label.text = MyClassB(super.data).id;
            }
        }
    }

    public function NavigateTreeItemRenderer() {
        super();
    }

}

Examining the trace shows I am rendering the node, but I never end up inside either of the two if statements. When I run in the debugger, I can actually the properties on the "data" that correspond to MyClassA and MyClassB!

Upvotes: 0

Views: 1493

Answers (2)

Dave
Dave

Reputation: 21924

Turns out I was bitten by a subtle issue whereby the ActionScript3 compiler automatically dropped my classes from my remote services SWC because it did not detect any direct reference to those classes. Because those objects are instantiated by RemoteObject result handlers and placed into an child ArrayCollection, there was no reference to them that the compiler could detect.

When this happens, rather than throwing an exception, Flex's RemoteObject implementation simply (and quietly) hydrates them into a generic Object, thus negating the value of their strongly typed interface.

The workaround was to declare a variable within each derived service class for every type of value object that could ever appear within the their object graphs.

Upvotes: 0

Stan Reshetnyk
Stan Reshetnyk

Reputation: 2036

  1. According your conditions you could use another way:

    override public function set data(value : Object) { super.data = value; label.text = ... }

  2. But much simple is write labelFunction

Upvotes: 1

Related Questions