ActionScript实现TreeView数据结构

     由于ActionScript语言的动态特性,flex中所有树形结构数据都是直接使用xml进行处理的,虽然非常灵活,但是在使用时也会感到不方便,因此不妨实现一个Treeview的数据结构,并以此数据结构作为mxml中的数据源,使用起来就会方便很多了。

TreeNode的代码:

package com.sample.Control

{

	import mx.collections.ArrayCollection;

	

	[Bindable]

	public dynamic class TreeNode

	{

		public var text:String="";

		public var value:String="";

		public var url:String="";

		public var Target:String="";

		public var ShowCheckBox:Boolean=false;

		public var Checked:Boolean=false;

		public var Depth:int=0;

		public var Expanded:Boolean=false;

		public var ImageTooltip:String="";

		public var ImageUrl:String="";

		public var Selected:Boolean=false;

		public var Tooltip:String="";

		public var Children:ArrayCollection=null;

		

		public function TreeNode(xml:XML)

		{

			if(xml==null)

				return;

			this.text = xml.TEXT||xml.@TEXT||xml.@text||xml.text;

			this.value = xml.VALUE||xml.@VALUE||xml.@value||xml.value;

			this.url = xml.URL||xml.@URL||xml.@url||xml.url;

			this.Target = xml.TARGET||xml.@TARGET||xml.@Target||xml.Target;

			var showCheckBox:String = xml.SHOWCHECKBOX||xml.@SHOWCHECKBOX||

										xml.@ShowCheckBox||xml.ShowCheckBox;

			this.ShowCheckBox = showCheckBox=="true";

			var checked:String = xml.CHECKED||xml.@CHECKED||xml.@Checked||xml.Checked;

			this.Checked = checked=="true";

			var expand:String = xml.EXPANDED||xml.@EXPANDED||

							xml.@Expanded||xml.Expanded;

			this.Expanded = expand=="true";

			if(xml.DEPTH!=undefined){

				this.Depth = xml.DEPTH;

			}

			this.ImageTooltip = xml.IMAGETOOLTIP||xml.@IMAGETOOLTIP||

								xml.@ImageTooltip||xml.ImageTooltip;

								

			this.ImageUrl = xml.IMAGEURL||xml.@IMAGEURL||

								xml.@ImageUrl||xml.ImageUrl;

			if(xml.SELECTED!=undefined){

				this.Selected = xml.SELECTED=="true";

			}

			if(xml.TOOLTIP!=undefined){

				this.Tooltip = xml.TOOLTIP;

			}

			if(xml.CHILDNODES!=undefined && xml.CHILDNODES.length()>0){

				var cNodes:ArrayCollection = new ArrayCollection();

				for(var index:int=0;index<xml.CHILDNODES.children().length();index++){

						var xNode:XML = xml.CHILDNODES.child(index)[0];

						if(xNode!=null){

			 				cNodes.addItem(new TreeNode(xNode));

			 		}

			 	}

				this.Children = cNodes;

			}

		}

	

		///转换为XML

		public function ToXML():XML{

			var node:XML = new XML("<TreeNode/>");

			node.@text = this.text;

			node.@value = this.value;

			node.@Checked = this.Checked;

			node.@Depth = this.Depth;

			node.@Expanded = this.Expanded;

			node.@ImageTooltip = this.ImageTooltip;

			node.@ImageUrl = this.ImageUrl;

			node.@Selected = this.Selected;

			node.@ShowCheckBox = this.ShowCheckBox;

			node.@Target = this.Target;

			node.@Tooltip = this.Tooltip;

			node.@url = this.url;

			if(this.Children!=null && this.Children.length>0){

				for each(var cNode:TreeNode in this.Children){

					node.appendChild(cNode.ToXML());

				}

			}

			return node;

		}

	}

}
TreeView的代码:
package com.sample.Control

{

	import mx.collections.ArrayCollection;

	

	///Tree控件的数据源容器

	public dynamic class TreeView

	{

		private var m_Nodes:ArrayCollection;

		

		///构造,参数可以为TreeNode、XML、XMLList

		public function TreeView(nodes:Object=null)

		{

			m_Nodes=new ArrayCollection();

			if(nodes!=null){

				if(nodes is TreeNode){

					m_Nodes.addItem(nodes);

				}

				else if(nodes is XML){

					this.createFromXML(nodes as XML);

				}else if(nodes is XMLList){

					this.createFromXMLList(nodes as XMLList);

				}

			}

		}

		//从XML构造

		private function createFromXML(xml:XML):void{

			if(xml.@text!=undefined || xml.text!=undefined||

				xml.@TEXT!=undefined || xml.TEXT!=undefined){

				m_Nodes.addItem(new TreeNode(xml));

			}else if(xml.children().length()>0){

				createFromXMLList(xml.children());

			}

		}

		//从XMLList构造

		private function createFromXMLList(xmlList:XMLList):void{

			if(xmlList.length()>0){

				for(var index:int=0; index<xmlList.length();index++){

					var xml:XML = xmlList[index];

					m_Nodes.addItem(new TreeNode(xml));

				}

			}

		}

		

		///转换为XML

		public function ToXML():XML{

			var xml:XML = new XML("<TreeView />");

			if(m_Nodes.length>0){

				for each(var node:TreeNode in m_Nodes){

					xml.appendChild(node.ToXML());

				}

			}

			return xml;

		}

		

		

		static public function GetCheckedTreeNode(Data:XMLList):ArrayCollection{

			if(Data==null)return null;

			var array:ArrayCollection = new ArrayCollection();

			for(var index:int =0;index<Data.children().length();index++){

				var xNode:XML = Data.child(index)[0];

				GetCheckedTreeNodeByNode(xNode,array);

			}

			return array;

		}

		static private function GetCheckedTreeNodeByNode(Node:XML,array:ArrayCollection):void{

			if(Node.@Checked=="true"){//本节点已经勾选

				array.addItem(Node);

			}

			if(Node.children().length()>0){//开始检测子节点

				for(var index:int =0;index<Node.children().length();index++){

					var cNode:XML = Node.child(index)[0];

					GetCheckedTreeNodeByNode(cNode,array);

				}

			}

		}

	}

}

 

原发布于ActionScript实现TreeView数据结构(2010-11-10 13:24),现转于此!

你可能感兴趣的:(actionscript)