Chronicles in flex land (the tree)
Cool so we have a flex project. Making a UI in flex for a .net application. Just what I love, the flash player and .net. I had to make a wrapper framework which allowed the flex UI to be developed separately to a .Net API. so external interface commands are mapped to a dynamically loaded API dll. was very happy with how this all worked out and it allowed me the room to set standard Exception handleing from .net to be passed to flex for presentation.Wicked
so I am wandering along in flex land all is well ….la la la.. then I find I have to add functionality to the existing tree control. The dark clouds loom in and I begin panicing.
Ok so its not quite that bad but there are a few things to be learnt from where I have gone. If you are going to be developing a flex app that you know will make heavy use of the tree view or will require some extrafunctionailty there are a couple of things that will help you right off the bat.
- Try your very hardest to own or at least have rights to modify the data source.
- Make sure you client knows that as soon as they step away from the current functionality there is more time involved in developing thus features.
We had a problem on this project in that the data source (xml) was owned by the .net app. Next we will look at XSLT transforms for the flash input but for now we were stuck. So if your plugging into someone elses server for the xml….consider pulling that xml from their server to yours then to flash. This will allow you to tweak it server side first.
There are also a few classes to extend when you want to play with the tree, and at first it seems a little overwhelming, but press on as it soon becomes clear how things are working, just be sure there are no surprises for your client.
The tree … the one control that by itself can show multidimensional data hierarchiesies (spelling?). It serves the purpose well and is understood by most users. In flex here is how it works (feel free to correct me in the comments).
You place the tree component on the stage which creates an instance of the tree class. mx.control.Tree;
Now its a good idea to check out how the flexteam built the tree and see whats going on under the hood. The source is included ( on my computer: "C:\Program Files\Adobe\Flex Builder 2\Flex SDK 2\frameworks\source\mx\controls\Tree.as" ). Notice that the class extends List. Well the tree view is really just lots of lists (uh basically) a list for each level. So alot of the functionality is brought from the List class.
So you set your dataprovider and your away laughing. Opps no hang on I dont want some of those node to display. Well you could try a filter function but you would soon learn that it isnt implemented on the tree control. Bugger. This is where you need to step into creating your own DefaultDataDescriptor. The DefaultDataDescriptor class provides a default implementation for accessing and manipulating data for use in controls such as Tree and Menu. Cool. so now you can override methods in your own class that extends the DefaultDataDescriptor and doo all sorts of funky things with the xml before it reaches the tree. Dont forget to set it in your tree class though, this.dataDescriptor = new MySuperSpecialDataDescriptor(); You might want to override the getChildren method to show only certain things etc. One cool thing would be to use namespaces in your xml for lanuage support.
I wrote a static TreeFilter class that the DataDescriptor basically asked to filter the xml before sending it onto the tree for display.
Hmm ok so our tree is showing everything right but you want to change the way your nodes look based on the information in the xml?
Well enter the TreeItemRenderer which extends UIComponent. You can create your own that overrides methods in the base class and assign that to the tree as before. What the TreeItemRender does is for each node on the tree, it renders it. Well duh. So buy over-ridding the updateDisplayList method you could make the text bold if that xml node had an attribute bold="true",
var nodeXML:XML = XML(TreeListData(super.listData).item);
if (nodeXML.attribute("bold") == true){
setStyle("fontWeight", 'bold');
}
You could really draw anything in here, even other components like buttons etc using the
var indent:Number = TreeListData(super.listData).indent ;
indent to place the buttons at the right place.
Also if you want to evaluate the nodes on dragging a node around there are a few things youll need to do but ill leave that for another post.
So if your going to start extending the trees functionality its a good idea to look through the base mx classes first and get your head around whats going on and the variables available to you, then set out on you own tree adventure in flex land. If you get stuck give us a yell and ill see if I can help you out.
Also if you look through the FlexTeams blog youll see that they are releasing all sorts of goodie components over the next wee while, and seeing the tree is such an important control that is the only one that displays many levels I think its safe to say we will see a few updates around this control.

Hi Campbell,
Thanks for the info. I’ve recently been playing around with exactly the same stuff.
I would add the that the filter function is implemented for the Tree control, as it is for any dataprovider. It just isn’t very useful for hierarchical data since it only filters the top level of the dataprovider.
Another way to solve this problem is to subclass the XMLListCollection and override the refresh method so that it recurses through the all of the data and filters out the items that you do not want. Then you just need to use this subclass as the dataprovider for your Tree,. Then you can use the filter function as you normally would.
-Derek
Hey Derek, Yeah I found it strange no one else had hit this problem before. Thought it would be a biggie if you didnt own the datasource.
Is there a way you can give some more advice on the TreeFilter. Maybe some samples…
Thanks in advance..
Hey Maikel, replyed to your email. Im going to continue to post a few more examples but some specifics about your situation would help.
Hi!
I ve read that you are hosting a flash player in a .NET application that does all the stuff that Flash/Flex can’t do. That sounds kinda interesting to me. I am doing the same thing with a product called “Zinc”. It hosts a flash player and I can put my swf in it plus write some dlls to do the native stuff (MIDI, Database, file processing, etc…) and call those functions from Actionscript. The cool thing of this Zinc-Projector is that the end user does not have to have Flash Player OCX installed. The downside is that this Zinc Projector is kinda buggy in some regards, which costs me some sleepless nights. Does not feel to good having 80,000 CD-Roms fabricated and theres a bug in the software…
.
So I was wondering what kind of Flash Player you use in your project… is it the ActiveX one? or is it a different thing? I mean since they included the neat ExternalInterface thingy its quite possible for everyone to build a little bridge between Flash/Flex player and some custom C++, .NET or whatever function… just that the Flash-Player ActiveX control would still have to be installed, wouldn’t it? Well… if not that would be great news…
Cheers from Germany
Andi
Hey Andi, Yes there are a few options available to you. I have sent you an email outlining the details.
Re sorting ArrayCollections:
Sorry to bother you, but I’m wondering:
Have you ever run into a problem w/ Flex 2 where (having properly set up your SortFields), the myArrayCollection.refresh() call appends a null element to your AC and the refresh blows up? I have that problem. I have isolated it and can replicate it using Adobe’s example (by adding a null array element into an array which is subsequently turned into an AC).
Any pointers you could offer would be much appreciated.
Best Regards,
Lisa
Hi Campbell,
Thank you for your article!
I am interested in using your TreeFilter.
Would you please send me a sample? I would really appreciate it!!
Thank you!!,
Adrian
I will do a blog post containing the code for people to see