PDA

View Full Version : Using iconFunction with mx:Tree


webninja
04-02-2006, 09:32 AM
I'm sorry to keep asking what should be simple questions, but I have killed hours on this. Even the debugger is no help.

I have a situation where I need to change the icon of a Tree branch or node depending upon what it is. I created a Tree and populated it with XML data and that all works great. I tried to figure out how to specify the icon in the XML but no joy.

So I wrote an iconFunction and assigmed it to the proper Property in the Tree.

Here is some of the XML:


<mx:XML id="sitree">
<PATPID label="PAT PID 0x0000">
<PMTPID label="PMTPID 0x0010 - Network">
<SDT label="VNUS0"></SDT>
</PMTPID>
<PMTPID label="PMTPID 0x0020 - Program 750">
<ESPID label="ES PID 0x1022">
<CA label="CA Descriptor"></CA>
</ESPID>
</PMTPID>
<PMTPID label="PMTPID 0x0020 - Program 751">
<SDT label="VNUS0"></SDT>
</PMTPID>

</PATPID>
</mx:XML>


I put a breakpoint in my iconFunction and so I know its being called. I just don't know how to make it work. Here it is:


<mx:Script>
<![CDATA[

[Embed("c:/zanalyzer/client/icons/si_base.png")] var icoSiBase:Class;
[Embed("c:/zanalyzer/client/icons/log_warn.png")] var icoLogWarn:Class;

public function siTreeIcon(item:Object):Class {

if( /* SOME TEST I CANT FIGURE OUT */ )
{
return icoSiBase;
}

return icoLogWarn;
}
]]>
</mx:Script>


So what is it I am supposed to be testing for? It seems that the object passed in to my function would be of type PATPID (in the case of the root node) and the debugger shows that item contains the xml I wrote, but I can't figure out how to test for that.

Thanks very much...

Jim

hangalot
04-02-2006, 01:31 PM
i had serious problems with the tree in 1.5 to the extent that subclassed it and its components and basically rebuild it to do what i want. adobe has said that the tree in 2 is much better, but i have not used it yet.

i need to see more code, to help you btw.

webninja
04-02-2006, 06:05 PM
I'm sorry for the imcomplete code. I'll paste a complete app below.

The function I coded to return the custom icon is being called, it's just that I don't know what to test for. I found an example on the Macromedia user forum here:


http://www.macromedia.com/cfusion/webforums/forum/messageview.cfm?catid=346&threadid=1131501&enterthread=y


Their solution does not seem to work. The String they extract from the Object passed in is always an empty string, at least accroding to the debugger. Maybe I set up the XML wrong, but I even tried adding another field to it to test. No luck there.

The docs on iconFunction say that it's there to do exactly this, but they don't give an example of how to use it.

I hope this is enough to show what's happening. If not I'll add more.

Thanks again!



<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns="*" layout="absolute">
<!-- Simple example to demonstrate the Tree control -->
<mx:XML id="myxml">
<Company label="Macromedia">
<Branch label="Newton">
<Department label="Flex Doc">
<Title label="Intern">
<Name label="Kapil Virdi"></Name>
</Title>
</Department>
</Branch>
</Company>
</mx:XML>

<mx:Panel id="treePanel" title="Tree Trace Panel" paddingTop="10">
<mx:VBox>
<mx:Tree id="EmployeeTrace" width="285" height="196"
iconFunction="siTreeIcon" showRoot="false" labelField="@label">

<mx:dataProvider>
{myxml}
</mx:dataProvider>

<mx:Script>
<![CDATA[

[Embed("c:/zanalyzer/client/icons/si_base.png")] var icoSiBase:Class;
[Embed("c:/zanalyzer/client/icons/log_warn.png")] var icoLogWarn:Class;

public function siTreeIcon(item:Object):Class {

// I can't figure out what to test for in this if statement.
// I tried several things, all with no luck.


var sNodeName:String;
sNodeName = item.attributes.RecordType;

// At this point, the debugger shows that sNodeName is an empty string

if(sNodeName == "PMTPID")
// if(true) // If uncommented this line returns the icoSiBase icon
{
return icoSiBase;
}

return icoLogWarn;
}
]]>
</mx:Script>

</mx:Tree>
</mx:VBox>
</mx:Panel>

</mx:Application>

hangalot
04-02-2006, 06:55 PM
ok. so the siTreeIcon receoives the xmlnode that it must decide about. so inspecting the attributes of that xmlnode will sort you out.

webninja
04-02-2006, 08:17 PM
ok. so the siTreeIcon receoives the xmlnode that it must decide about. so inspecting the attributes of that xmlnode will sort you out.

Okay, but how? Is it just a matter of string manipulation at that point? I was under the impression that I would cast the object to some type that would represent the XML or the Tree in some way. If that's up to me to do, fine, I just don't know what's in that object.

hangalot
04-02-2006, 11:41 PM
if you look in the debugger its an xml node in some form. so cast it usign the as operator [var p:ObjectType = variable as ObjectType]if that doesn't work try (just maybe a explicit case [var p:ObjectType = ObjectType(variable)])otherwise use it as a string and make your own xmlnode from it. then use e4x or whatever other ways extracting attributes from xml in as3 is. its not going to be difficult.

webninja
04-03-2006, 08:00 AM
Ah, okay, I did that. I was able to cast it to an XML object with an explicit cast. From that you can extract the first node to an XMLList object and then convert it to a string, or just convert the XML object itself to a string.

The only problem is when the iconFunction is called for a leaf as opposed to a branch. In that case I still get the XML but the toString() and toXMLString() methods (from the XML object and XMLList object) return null. I'm not sure if that's a feature or a bug, but I'll do some testing and limit it down to a small app.

If it's a bug I'll let you know.

Thanks again...

Jim

SmackMe
08-20-2007, 01:23 PM
itemFunction is a very cool way to set custom icons according to whatever actions you perform with the tree.
You can test whether the item is opened or if it is a branch and return the apropriate icon.


[Embed(source="bin/assets/fcdu.png")]
private var fcdu:Class;

[Embed(source="bin/assets/fodu.png")]
private var fodu:Class;
public function treeIconFunction(item:Object):Class {
if(myTree.isItemOpen(item)) {
return fodu;
}
else
return fcdu;
}