package components.tree { import mx.collections.ArrayCollection; import mx.collections.ICollectionView; import mx.collections.XMLListCollection; import mx.collections.IViewCursor; import mx.collections.CursorBookmark; import mx.controls.treeClasses.ITreeDataDescriptor; /** * GenericDataDescriptor is an ITreeDataDescriptor that discribes XML our trees. */ public class GenericDataDescriptor implements ITreeDataDescriptor { /** * An Array that defines which elements are supposed to the treated as branches. If an object * is of type NodeTest the method doTest is called. Otherwise the object is compared (==) to * the result of the name() method of the XML object. */ public var branches:Array = null; public function getChildren(node:Object, model:Object=null):ICollectionView { if(hasChildren(node, model)) { return new XMLListCollection(XML(node).children()); } return new ArrayCollection([]); } public function hasChildren(node:Object, model:Object=null):Boolean { if(isBranch(node, model)) { return XML(node).children().length() > 0; } else { return false; } } public function isBranch(node:Object, model:Object=null):Boolean { if(branches != null && node is XML) { var name:Object = XML(node).name(); for each(var item:Object in branches) { if(item is NodeTest) { if(NodeTest(item).doTest(node)) { return true; } } if(name == item) { return true; } } } return false; } public function getData(node:Object, model:Object=null):Object { return node; } public function addChildAt(parent:Object, newChild:Object, index:int, model:Object=null):Boolean { if(parent is XML && newChild is XML) { var cursor:IViewCursor = getChildren(parent).createCursor(); cursor.seek(CursorBookmark.FIRST, index); cursor.insert(newChild); } return false; } public function removeChildAt(parent:Object, child:Object, index:int, model:Object=null):Boolean { if(parent is XML && child is XML) { var cursor:IViewCursor = getChildren(parent).createCursor(); cursor.seek(CursorBookmark.FIRST, index); cursor.remove(); return true; } return false; } } }