|
Bgx Xml Serialization Classes (Flash MX 2004) :
Complex Tree and DataGrid example
Flash output
This example corresponds with the file TreeExample.fla
that is supplied with the component. The following code loads the
XML file sample.xml,
parses it using the BgxDesObject class and then populates an instance
of the Macromedia Tree component called bgxTree with the
folder and page nodes of the XML.
It assigns click actions that display the label name and an active
hyperlink in case of page nodes, a DataGrid with the list of child
pages in case od folder nodes.
It further assigns double-click actions that expand/collapse folder
nodes and load the associated page for page nodes.
Note: The Tree component allows very easy population
of a tree by simply binding the XML directly to it, with XML elements
containing label, data and isBranch attributes.
Apart from the fact that it might not suit you to have those attributes
in the XML or it would mean previous recursive parsing of the XML
to add them, you cannot add complex data as treeNode data via a
data attribute. The code below adds the node objects themselves
as treeNode data. Therefore, if a node is clicked all data associated
with that node is instantly available (see the line
var newNode = treeNode.addTreeNode(nodeName,elementsArray[i]);)
Note also that within the setDisplay()
function the nodeName property, added automatically if
addNodeName is set to true, is used to determine whether
a treeNode corresponds to an element of type folder (see
if (dataObject.nodeName == "folder")).
The following is only one way how a tree can be populated. It will
depend on the actual structure of the XML data or other parameters,
like what kind of interaction is needed, do determine the best route
on a case by case basis.
[history]
import bgx.BgxDesObject;
import mx.controls.Tree;
dgPages._visible = false;
var SampleData:Object;
initDisplay();
var sampleXML:XML = new XML();
sampleXML.ignoreWhite = true;
sampleXML.onLoad = parseSampleData;
sampleXML.load("sample.xml");
function parseSampleData() {
var rootNode:XMLNode = sampleXML.firstChild;
SampleData = new BgxDesObject();
SampleData.xmlNode = rootNode;
SampleData.onParsed = populateTree;
SampleData.parse();
}
function populateTree(includeRoot:Boolean)
{
addTreeNodes(SampleData,bgxTree);
}
function addTreeNodes(elements:Object,treeNode:Tree)
{
for (var element in elements)
{
if (typeof elements[element]
== "object" &&
element
!= "bgx_cln_comment" &&
element
!= "exampleNode")
{
var
elementsArray = elements[element];
for
(var i = 0; i < elementsArray.length; i++)
{
var
nodeName = elementsArray[i].label;
if
(element == "page")
{
var
newNode = treeNode.addTreeNodeAt(0,nodeName,elementsArray[i]);
}
else
{
var
newNode = treeNode.addTreeNode(nodeName,elementsArray[i]);
}
var
isBranch = (element == "folder");
bgxTree.setIsBranch(newNode,isBranch);
addTreeNodes(elementsArray[i],newNode);
}
}
}
}
function initDisplay()
{
treeListenerObject = new Object();
treeListenerObject.change = function(evtObject){
setDisplay(bgxTree.selectedNode,evtObject.target.selectedItem.attributes.data);
}
bgxTree.addEventListener("change",
treeListenerObject);
dgPages.columnNames= ["label",
"URL"];
dgPages.getColumnAt(0).width = 170;
dgPages.getColumnAt(1).width = 320;
dgPages.getColumnAt(0).headerText = "Pages";
dgPages.getColumnAt(1).headerText = "URL";
var dgListener = new Object();
dgListener.cellPress = function(event) {
var itemData = dgPages.dataProvider[event.itemIndex];
if (checkDblClick(itemData))
getURL(itemData.URL,"_blank");
};
dgPages.addEventListener("cellPress",
dgListener);
}
function setDisplay(treeNode,dataObject)
{
if (checkDblClick(dataObject))
{
if (typeof dataObject.URL
!= "undefined")
{
getURL(dataObject.URL,"_blank");
}
if (bgxTree.getIsBranch(treeNode))
bgxTree.setIsOpen(treeNode,!bgxTree.getIsOpen(treeNode));
}
itemTitle.text = dataObject.label;
if (dataObject.nodeName == "folder")
{
dgPages.dataProvider
= dataObject.page;
dgPages._visible
= true;
itemURL.text = "";
}
else
{
dgPages._visible
= false;
}
if (dataObject.nodeName != "page")
{
itemURL.text = "";
}
else
{
itemURL.htmlText
= "<a href='" + dataObject.URL + "' target='_blank'><u>"
+ dataObject.URL + "</u></a>";
}
}
var lastClick = 0;
var lastSelected;
function checkDblClick(pObj):Boolean
{
var clickTime = getTimer();
var dClick = ((clickTime-lastClick<400)
&& (lastSelected == pObj));
lastClick = clickTime;
lastSelected = pObj;
return dClick;
}
//----End utility double-click
History
5 June 2004
Changed the datatype of treeNode in the line function
addTreeNodes(elements:Object,treeNode:Tree) from XML to Tree
(I couldn't find a type TreeNode for the Tree component). This also
requires to add import mx.controls.Tree;
at the top of the code.
Reason: This example works fine in both cases but I worked on other
tree examples since that require the identification of a Treenode's
parent. As the Tree component does not provide an interface for
that I use the parentNode attribute (added by the BgxDesObject
Class) of the object stored as data. If treeNode is typed as XML
this does not return the parentNode the BgxDesObject Class created.
Frankly the type should not have been XML in the first place.
4 January 2005
Changed code to list pages above folders. Thanks to James Jefferson
for the suggestion.
For further information please contact info@bgxcomponents.com.
|