The Tree
component allows you to display a tree structure in an easy-to-navigate,
graphical format. The standard appearance of the Tree component should
be familiar to you since it is essentially identical to the control you
encounter each time you navigate to a directory on your hard drive to
open a document.
Create a new FLA and set the Stage dimensions to 400px by 200px. Use
the following screenshot as a guide when laying out your components
in the next few steps.
Create two new layers. Name the layers, starting with the top one
as Actions, Tree Component and Text Area Component.
Select Frame 1 of the Tree Component layer and drag an instance of
the Tree component on Stage. Using the Property Inspector (PI), resize
it to 172px by 180px and give it the instance name myTree.
Select Frame 1 of the Text Area Component layer and drag an instance
of the Text Area component on Stage. Using the PI, resize it to 188px
by 180px and give it the instance name myStatusArea. Set its html parameter
to true, since you will be displaying the status messages with HTML
formatting. Make sure that wordWrap is also set to true (it is, by default.)
Open up tree_source.xml from your downloads in a text editor and
look at the structure of the tree data or see below. Note that how it
is comprised of nodes with label attributes. Nodes that contain other
nodes display as folders and those that do not display as documents
within the default skin of the tree component.
Enter the following code in Frame 1 of the Actions layer:
// load and assign data source
myTreeDataProvider = new XML();
// ignore whitespace in XML (important)
myTreeDataProvider.ignoreWhite = true;
// load external XML file
myTreeDataProvider.load("tree_source.xml");
// onLoad handler for XML data
myTreeDataProvider.onLoad = function()
{
myTree.dataProvider = myTreeDataProvider;
}
The code above loads in an external XML file (tree_source.xml) and sets
it as the data provider for the Tree component.
Continue adding the following code to frame:
// set up tree listener
myTreeListener = new Object();
// nodeOpen event handler
myTreeListener.nodeOpen = function(eventObject)
{
myStatusArea.text += "<li>"+ eventObject.node.attributes.label+ " opened.</li>";
}
Here, you create an event listener object for the tree and an event
handler for the nodeOpen event. The nodeOpen event gets fired when the
user opens a branch node (a folder) in a tree.
Here you are creating a nodeClose event handler, just like you did previously
for nodeOpen.
Continue adding the following code to frame:
// change event handler
myTreeListener.change = function(eventObject)
{
// the selected node
var theSelectedNode = eventObject.target.selectedNode;
// the label of the selected node
var theSelectedNodeLabel = theSelectedNode.attributes.label;
// add a status message to the status message text area component
myStatusArea.text += "<li>"+ theSelectedNodeLabel+ " <b>selected</b>.</li>";
}
You may remember the change event from some of the previous component
examples. For the Tree component, the change event gets called when
a node is selected (as opposed to opened.)
This code tells your Tree component instance that it should broadcast
the stated events to the myTreeListener event listener. Note that
you have to register for each event individually. You cannot register
to receive all events at once.
Test the movie. Open some folders and click on some of the nodes
to see the status messages appear in the Text Area component. You may
notice that as you dig deeper into the tree, some of the labels start
getting cut off horizontally. Unfortunately, it appears that the Tree
component does not have a functional horizontal scroller. The Tree component
extends the List component that you saw earlier and thus should have
a horizontal scrollbar but turning it on using the following code does
not work.
// unfortunately, this does not work
//(bug with component or undocumented limitation) // myTree.setHScrollPolicy ("on");
You can interact with the finished version of the
application below.