public void init(MultTreeNode pNode = null) { if (childList == null) { childList = new List <MultTreeNode>(); } parentId = 0; selfId = 0; nodeName = ""; obj = null; if (pNode != null) { nodeName = pNode.getNodeName(); if (pNode.getObj() != null) { obj = new Hashtable((Hashtable)pNode.getObj()); } parentNode = pNode.getParentNode(); if (pNode.getChildList() != null) { childList = new List <MultTreeNode>(pNode.getChildList()); } } }
/// <summary> /// insert child node to one node(this). /// </summary> /// <param name="name">new child node name.</param> /// <param name="path"></param> /// <param name="newnode">can be null. no use I think.</param> /// <returns></returns> public virtual MultTreeNode InsertNode(string name, string path = "", MultTreeNode newnode = null) { MultTreeNode pntnode = this; if (newnode == null) { newnode = new MultTreeNode(); } if (path.Length > 0) { pntnode = findNodeByPath(path); } if (pntnode == null) { return(null); } // check if has same node with same name. // can not insert two same name node under one parent node. // 2016-03-04 09:38:33 if (pntnode.ExistChildNode(name)) { return(pntnode); } newnode.setNodeName(name); newnode.parentNode = pntnode; pntnode.InsertChildNode(newnode); return(newnode); }
/** 找到一颗树中某个节点 按节点ID */ public MultTreeNode findNodeById(int id) { if (this.selfId == id) { return(this); } if (isLeaf()) { return(null); } else { int childNumber = childList.Count; for (int i = 0; i < childNumber; i++) { MultTreeNode child = childList.ElementAt(i); MultTreeNode resultNode = child.findNodeById(id); if (resultNode != null) { return(resultNode); } } return(null); } }
/// <summary> /// TreeNodeData返回TreeNode /// </summary> /// <returns></returns> public MultTreeNode ToTreeNode() { MultTreeNode ToTreeNode = new MultTreeNode(); // ToTreeNode.Checked = this.Checked; // ToTreeNode.setObj(this.Tag); ToTreeNode.Obj = this.Tag; ToTreeNode.NodeName = this.Text; ToTreeNode.SelfId = this.Name; // ToTreeNode.setNodeName(this.Name); //if (this.Expanded) //{ // ToTreeNode.Expand(); //} if (this.Nodes == null && this.Nodes.Length == 0) { return(null); } if (ToTreeNode != null && this.Nodes.Length == 0) { return(ToTreeNode); } for (int i = 0; i <= this.Nodes.Length - 1; i++) { ToTreeNode.getChildList().Add(this.Nodes[i].ToTreeNode()); } return(ToTreeNode); }
/// <summary> /// TreeNode构造函数 /// </summary> /// <param name="node"></param> public MultTreeNodeData(MultTreeNode node) { //this.Text = node.getNodeName(); //this.Name = node.getSelfId(); this.Text = node.NodeName; this.Name = node.SelfId; // this.ImageIndex = node.ImageIndex; // this.SelectedImageIndex = node.SelectedImageIndex; // this.Checked = node.Checked; // this.Expanded = node.IsExpanded; this.Nodes = new MultTreeNodeData[node.getChildList().Count]; if ((!(node.Obj == null)) && node.Obj.GetType().IsSerializable) { this.Tag = node.Obj; } else { this.Tag = null; } if (node.getChildList().Count == 0) { return; } for (int i = 0; i <= node.getChildList().Count - 1; i++) { Nodes[i] = new MultTreeNodeData(node.getChildList()[i]); } }
/** 插入一个child节点到当前节点中 */ public virtual void InsertChildNode(MultTreeNode treeNode) { if (childList == null) { childList = new List <MultTreeNode>(); } treeNode.setParentNode(this); childList.Add(treeNode); }
/** 删除节点和它下面的晚辈 */ public void deleteNode() { MultTreeNode parentNode = this.getParentNode(); int id = this.getSelfId(); if (parentNode != null) { parentNode.deleteChildNode(id); } }
/** 删除当前节点的某个子节点 * childId=0时删除所有子节点 */ public void deleteChildNode(int childId = 0) { List <MultTreeNode> childList = this.getChildList(); int childNumber = childList.Count; for (int i = 0; i < childNumber; i++) { MultTreeNode child = childList.ElementAt(i); if (child.getSelfId() == childId || childId == 0) { childList.RemoveAt(i); return; } } }
/** 返回当前节点的父辈节点集合 形成路径使用 */ public List <MultTreeNode> getElders() { ListExtA <MultTreeNode> elderList = new ListExtA <MultTreeNode>(); MultTreeNode parentNode = this.getParentNode(); if (parentNode == null) { return(elderList); } else { elderList.AddAll(parentNode.getElders()); elderList.Add(parentNode); return(elderList); } }
/// <summary> /// TreeNodeData返回TreeNode /// </summary> /// <returns></returns> public MultTreeNode ToTreeNode() { #region 无用代码 //TreeNode ToTreeNode = new TreeNode(this.Text, this.ImageIndex, this.SelectedImageIndex); #endregion MultTreeNode ToTreeNode = new MultTreeNode(); #region 无用代码 // ToTreeNode.Checked = this.Checked; #endregion ToTreeNode.Obj = this.Tag; ToTreeNode.SelfId = this.Name; ToTreeNode.NodeName = this.Text; //ToTreeNode.setNodeName(this.Text); // ToTreeNode.setSelfId(this.Name); #region 无用代码 #region 无用代码 //ToTreeNode.setParent(this.Parent); //ToTreeNode.setParentNode(this.parentNodes); #endregion #region 无用代码 //if (this.Expanded) //{ // ToTreeNode.Expand(); //} #endregion #endregion if (this.Nodes == null && this.Nodes.Length == 0) { return(null); } if (ToTreeNode != null && this.Nodes.Length == 0) { return(ToTreeNode); } for (int i = 0; i <= this.Nodes.Length - 1; i++) { #region 无用代码 // ToTreeNode.Nodes.Add(this.Nodes[i].ToTreeNode()); // ToTreeNode.getChildList().Add(this.Nodes[i].ToTreeNode()); #endregion ToTreeNode.InsertChildNode(this.Nodes[i].ToTreeNode()); } return(ToTreeNode); }
/// <summary> /// TreeNode构造函数 /// </summary> /// <param name="node"></param> public TreeNodeData(MultTreeNode node) { //this.Text = node.getNodeName(); //this.Name = node.getSelfId(); this.Text = node.NodeName; this.Name = node.SelfId; #region 无用代码 // this.Parent = node.getParentId(); // this.ImageIndex = node.ImageIndex; //this.SelectedImageIndex = node.SelectedImageIndex; // this.Checked = node.Checked; // this.Expanded = node.IsExpanded; #endregion this.Nodes = new TreeNodeData[node.getChildNodeCount()]; if ((!(node.Obj == null)) && node.Obj.GetType().IsSerializable) { this.Tag = node.Obj; } else { this.Tag = null; } #region 无用代码 //if (node.getParentNode() != null) //{ // this.parentNodes = node.getParentNode(); //} //else //{ // this.parentNodes = null; //} #endregion if (node.getChildNodeCount() == 0) { return; } for (int i = 0; i <= node.getChildNodeCount() - 1; i++) { Nodes[i] = new TreeNodeData(node.getChildList()[i]); } }
/** 遍历一棵树,深度遍历 */ public void depthtraverse(OneStepTravers stepbef, OneStepTravers stepafter) { if (stepbef != null) { stepbef(this); } if (childList != null) { int childNumber = childList.Count; for (int i = 0; i < childNumber; i++) { MultTreeNode child = childList.ElementAt(i); child.depthtraverse(stepbef, stepafter); } } if (stepafter != null) { stepafter(this); } }
/** 返回当前节点的晚辈集合 */ public List <MultTreeNode> getJuniors() { ListExtA <MultTreeNode> juniorList = new ListExtA <MultTreeNode>(); List <MultTreeNode> childList = this.getChildList(); if (childList == null) { return(juniorList); } else { int childNumber = childList.Count; for (int i = 0; i < childNumber; i++) { MultTreeNode junior = childList.ElementAt(i); juniorList.Add(junior); juniorList.AddAll(junior.getJuniors()); } return(juniorList); } }
public void setParentNode(MultTreeNode parentNode) { this.parentNode = parentNode; }
public MultTreeNode(MultTreeNode parentNode) { init(parentNode); }
/// <summary> /// find one note in tree. /// 2016-03-04 16:21:34 /// modify when empty path .return this instead of null. /// </summary> /// <param name="path"></param> /// <returns></returns> public MultTreeNode findNodeByPath(string path) { if (string.IsNullOrEmpty(path)) { return(this); } //path = StrUtils.FormatPath(path); //path = StrUtils.GetSDirString(path); string[] pathArr = path.Split(new char[] { '\\' }); //StrUtils.Split(path, "\\"); //path.Split('\\'); if (pathArr[0] == null) { pathArr[0] = path; } // make sure this is root path. if (nodeName != pathArr[0]) { return(null); } MultTreeNode node = this; for (int i = 0; i < pathArr.Count(); i++) { // 说明:注释。不知道为什么要加这句 可能会影响代码生成。需要注意 // 修改为 break; => continue; \ 查找具有 相同节点名称的 子节点 逻辑 // 短暂测试是正常的。 // 修改日期:2014-8-5 14:20:28 if (string.IsNullOrEmpty(pathArr[i])) { List <MultTreeNode> list = node.getChildList(); if (i < pathArr.Count() - 1) { for (int j = 0; j < list.Count; j++) { if (list[j].getNodeName() == pathArr[i + 1]) { node = list[j]; } } } continue; } // use path to avoid error like 0\0\0 // 2016-07-11 21:34:54 string nodePath = node.getPath(); string testPath = StrUtils.FromArr(ArrayUtils.SplitArray <string>(pathArr, 0, i - 1), "\\"); if (node.getNodeName() == pathArr[i] && nodePath == testPath) { List <MultTreeNode> list = node.getChildList(); for (int j = 0; j < list.Count; j++) { if (i + 1 < pathArr.Length && list[j].getNodeName() == pathArr[i + 1]) { node = list[j]; } } } else { node = null; break; } } return(node); }