// --------------------------- // ClassTree相关功能逻辑 /// <summary> /// 从当前数据信息创建TreeNode /// </summary> public TreeNode ToTreeNode() { TreeNode node = new TreeNode(); node.Name = this.Name; node.Text = Name; node.Tag = this.ToString(); // 设置图标 if (parent == null) { node.ImageIndex = 2; } else { node.ImageIndex = isDir ? 0 : 1; } node.SelectedImageIndex = node.ImageIndex; foreach (string key in childs.Keys) { ClassTree child = childs[key]; TreeNode childNode = child.ToTreeNode(); node.Nodes.Add(childNode); } return(node); }
/// <summary> /// childPath形如:Lcom/ltsdk_167_xiaomihaiwai/v1_0_5/R$string; /// </summary> /// <param name="childPath"></param> private void AddChildByPath(String childPath) { // 获取所有子节点名称信息 String[] A = childPath.Split('/'); // 逐级添加子节点 ClassTree tree = this; for (int i = 0; i < A.Length; i++) { string name = A[i]; if (i == A.Length - 1) { name += ".class"; } tree.AddChild(name); tree = tree.childs[name]; if (i == A.Length - 1) { tree.isDir = false; // 最后一个设置为 } } }
public ClassesForm(List <string> classes) { InitializeComponent(); ClassTree tree = new ClassTree(classes, "class.dex"); tree.ShowIn(treeView1); }
/// <summary> /// 删除指定名称路径下的child /// </summary> private void RemoveChildByPath(String childPath) { ClassTree treeNode = GetChildByPath(childPath); // 获取路径下的 if (treeNode != null && treeNode.parent != null) { treeNode.parent.RemoveChild(treeNode.Name); } }
/// <summary> /// 添加子节点 /// </summary> /// <param name="childName"></param> /// <param name="isDir"></param> public void AddChild(String childName, bool isDir = true) { if (childName.Contains("/")) { AddChildByPath(childName); } else { if (!childs.ContainsKey(childName)) { ClassTree child = new ClassTree(childName, isDir); childs.Add(childName, child); // 添加至当前节点的子节点 childs[childName].parent = this; // 设置当前节点为父节点 } } }
/// <summary> /// 添加tree至当前节点的子节点中 /// </summary> /// <param name="tree"></param> public void AddChild(ClassTree tree) { // 若子节点中不含有该节点,则直接添加 if (!childs.ContainsKey(tree.Name)) { childs.Add(tree.Name, tree); tree.parent = this; } // 若子节点中已含有该节点,则合并至同名子节点 else { ClassTree child = childs[tree.Name]; // 获取子节点 foreach (ClassTree sChild in tree.childs.Values) // 添加tree的所有子节点 { child.AddChild(sChild); } } }
/// <summary> /// 获取指定名称路径下的Tree节点 /// </summary> public ClassTree GetChildByPath(String childPath) { ClassTree tree = this; // 获取所有子节点名称信息 String[] A = childPath.Split('/'); foreach (string name in A) { if (tree.childs.ContainsKey(name)) { tree = tree.childs[name]; } else { tree = null; } } return(tree); }