public SavedRouteNode GetNode(TreeView treeView, bool getParent = false) { TreeNode selectedNode = treeView.SelectedNode; if (selectedNode == null) { return(null); } // get TV selection path List <string> path = new List <string>(); TreeNode tvNode = selectedNode; while (tvNode.Parent != null) { path.Insert(0, tvNode.Parent.Text); tvNode = tvNode.Parent; } if (!getParent) { path.Add(selectedNode.Text); } // find equivalent node in config List <SavedRouteNode> target = this.Nodes; foreach (string pathNode in path.Take(path.Count - 1)) { target = ((SavedRouteGroup)target.Find((i) => i.Name == pathNode && i is SavedRouteGroup)).Nodes; } SavedRouteNode getNode = null; foreach (SavedRouteNode node in target) { if (node.Name == path.Last() && ((getParent ? selectedNode.Parent.ImageIndex == 0 : selectedNode.ImageIndex == 0) ? node.GetType() == typeof(SavedRouteGroup) : node.GetType() == typeof(SavedRouteItem))) { getNode = node; break; } } return(getNode); }
/// <summary> /// Recursively receives the routes from a node into a list /// </summary> /// <param name="node"></param> /// <returns></returns> public List<SavedRouteItem> GetRoutes(SavedRouteNode node) { List<SavedRouteItem> routes = new List<SavedRouteItem>(); if (node is SavedRouteGroup) foreach (SavedRouteNode subNode in ((SavedRouteGroup)node).Nodes) routes.AddRange(GetRoutes(subNode)); else if (node is SavedRouteItem) routes.Add((SavedRouteItem)node); return routes; }
/// <summary> /// For rename = true, if node already exists and source is a group it renames it without changing its subnodes, /// and if it's an item it replaces it.<para/> /// Returns:<para/> /// 0: success<para/> /// 1: invlid destination<para/> /// 2: name already exists /// </summary> /// <param name="treeView"></param> /// <param name="name"></param> /// <param name="rename">Rename group/edit item</param> /// <returns></returns> public int AddNode(TreeView treeView, SavedRouteNode node, bool rename = false) { TreeNode selectedNode = treeView.SelectedNode; if (selectedNode == null) return 1; // get TV selection path List<string> path = new List<string>(); TreeNode tvNode = selectedNode; while (tvNode.Parent != null) { path.Insert(0, tvNode.Parent.Text); tvNode = tvNode.Parent; } if (selectedNode.ImageIndex == 0) path.Add(selectedNode.Text); // find equivalent node in config List<SavedRouteNode> destination = this.Nodes; foreach (string pathNode in path.Take(path.Count - Convert.ToInt32(rename) + selectedNode.ImageIndex)) destination = ((SavedRouteGroup)destination.Find((i) => i.Name == pathNode && i is SavedRouteGroup)).Nodes; // find if name already used at destination if (destination.Find((i) => i.Name == node.Name && i.GetType() == node.GetType()) != null && (!rename || node.Name != selectedNode.Text)) return 2; if (rename) { if (selectedNode.ImageIndex == 0) { if (destination.Find((i) => i.Name == selectedNode.Text && i.GetType() == node.GetType()) != null) destination.Find((i) => i.Name == selectedNode.Text && i.GetType() == node.GetType()).Name = node.Name; path.Remove(path.Last()); } else { destination.Remove(destination.Find((i) => i.Name == selectedNode.Text && i.GetType() == node.GetType())); destination.Add(node); } } else destination.Add(node); path.Add(node.Name); // update TV Global.Config.Save(); Global.Config.SavedRoutes.Populate(treeView, path); // raise event if (NodesChanged != null) NodesChanged(this, new EventArgs()); return 0; }
/// <summary> /// For rename = true, if node already exists and source is a group it renames it without changing its subnodes, /// and if it's an item it replaces it.<para/> /// Returns:<para/> /// 0: success<para/> /// 1: invlid destination<para/> /// 2: name already exists /// </summary> /// <param name="treeView"></param> /// <param name="name"></param> /// <param name="rename">Rename group/edit item</param> /// <returns></returns> public int AddNode(TreeView treeView, SavedRouteNode node, bool rename = false) { TreeNode selectedNode = treeView.SelectedNode; if (selectedNode == null) { return(1); } // get TV selection path List <string> path = new List <string>(); TreeNode tvNode = selectedNode; while (tvNode.Parent != null) { path.Insert(0, tvNode.Parent.Text); tvNode = tvNode.Parent; } if (selectedNode.ImageIndex == 0) { path.Add(selectedNode.Text); } // find equivalent node in config List <SavedRouteNode> destination = this.Nodes; foreach (string pathNode in path.Take(path.Count - Convert.ToInt32(rename) + selectedNode.ImageIndex)) { destination = ((SavedRouteGroup)destination.Find((i) => i.Name == pathNode && i is SavedRouteGroup)).Nodes; } // find if name already used at destination if (destination.Find((i) => i.Name == node.Name && i.GetType() == node.GetType()) != null && (!rename || node.Name != selectedNode.Text)) { return(2); } if (rename) { if (selectedNode.ImageIndex == 0) { if (destination.Find((i) => i.Name == selectedNode.Text && i.GetType() == node.GetType()) != null) { destination.Find((i) => i.Name == selectedNode.Text && i.GetType() == node.GetType()).Name = node.Name; } path.Remove(path.Last()); } else { destination.Remove(destination.Find((i) => i.Name == selectedNode.Text && i.GetType() == node.GetType())); destination.Add(node); } } else { destination.Add(node); } path.Add(node.Name); // update TV Global.Config.Save(); Global.Config.SavedRoutes.Populate(treeView, path); // raise event if (NodesChanged != null) { NodesChanged(this, new EventArgs()); } return(0); }