Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 04-02-2006, 09:32 AM   #1
webninja
Registered User
 
Join Date: Mar 2006
Location: Rhode Island
Posts: 51
Default Using iconFunction with mx:Tree

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:

Code:
	
   <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:

Code:
<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
webninja is offline   Reply With Quote
Old 04-02-2006, 01:31 PM   #2
hangalot
lala
 
hangalot's Avatar
 
Join Date: Feb 2002
Location: on the road
Posts: 2,858
Default

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.
__________________
oi poloi
http://www.memorphic.com/news/
hangalot is offline   Reply With Quote
Old 04-02-2006, 06:05 PM   #3
webninja
Registered User
 
Join Date: Mar 2006
Location: Rhode Island
Posts: 51
Default More code here...

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/we...&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!


Code:
<?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>
webninja is offline   Reply With Quote
Old 04-02-2006, 06:55 PM   #4
hangalot
lala
 
hangalot's Avatar
 
Join Date: Feb 2002
Location: on the road
Posts: 2,858
Default

ok. so the siTreeIcon receoives the xmlnode that it must decide about. so inspecting the attributes of that xmlnode will sort you out.
__________________
oi poloi
http://www.memorphic.com/news/
hangalot is offline   Reply With Quote
Old 04-02-2006, 08:17 PM   #5
webninja
Registered User
 
Join Date: Mar 2006
Location: Rhode Island
Posts: 51
Default

Quote:
Originally Posted by hangalot
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.
webninja is offline   Reply With Quote
Old 04-02-2006, 11:41 PM   #6
hangalot
lala
 
hangalot's Avatar
 
Join Date: Feb 2002
Location: on the road
Posts: 2,858
Default

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.
__________________
oi poloi
http://www.memorphic.com/news/
hangalot is offline   Reply With Quote
Old 04-03-2006, 08:00 AM   #7
webninja
Registered User
 
Join Date: Mar 2006
Location: Rhode Island
Posts: 51
Default

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
webninja is offline   Reply With Quote
Old 08-20-2007, 01:23 PM   #8
SmackMe
smackmyworld
 
Join Date: May 2005
Location: Timisoara, Romania
Posts: 44
Default

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.

ActionScript Code:
[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; }
SmackMe is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
iconFunction in Tree Object farsider Components 1 05-10-2005 08:29 PM


All times are GMT. The time now is 06:56 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.