/// <summary> /// 子ノードの追加 /// </summary> /// <returns></returns> public Node AddChild() { var node = new Node(this); var old_value = children; var new_value = new List<Node>(children); new_value.Add(node); var cmd = new Command.DelegateCommand( () => { children = new_value; if (OnAfterAddNode != null) { OnAfterAddNode(this, new ChangedValueEventArgs(node, ChangedValueType.Execute)); } }, () => { children = old_value; if (OnAfterRemoveNode != null) { OnAfterRemoveNode(this, new ChangedValueEventArgs(node, ChangedValueType.Unexecute)); } }); Command.CommandManager.Execute(cmd); return node; }
public static bool MoveNode(Data.Node movedNode, Data.NodeBase targetParent, int targetIndex) { // Check if (movedNode.Parent == targetParent && targetIndex != int.MaxValue) { var index = targetParent.Children.Internal.Select((i, n) => Tuple.Create(i, n)).FirstOrDefault(_ => _.Item1 == movedNode).Item2; // Not changed. if (index == targetIndex || index + 1 == targetIndex) { return(false); } } if (movedNode == targetParent) { return(false); } Func <Data.Node, bool> isFound = null; isFound = (Data.Node n) => { if (n.Children.Internal.Any(_ => _ == targetParent)) { return(true); } foreach (var n_ in n.Children.Internal) { if (isFound(n_)) { return(true); } } return(false); }; if (isFound(movedNode)) { return(false); } // if (targetParent == movedNode.Parent && targetIndex != int.MaxValue) { var index = targetParent.Children.Internal.Select((i, n) => Tuple.Create(i, n)).FirstOrDefault(_ => _.Item1 == movedNode).Item2; if (index < targetIndex) { targetIndex -= 1; } } Command.CommandManager.StartCollection(); movedNode.Parent.RemoveChild(movedNode); targetParent.AddChild(movedNode, targetIndex); Command.CommandManager.EndCollection(); return(true); }
public static bool MoveNode(Data.Node movedNode, Data.NodeBase targetParent, int targetIndex) { Command.CommandManager.StartCollection(); movedNode.Parent.RemoveChild(movedNode); targetParent.AddChild(movedNode, targetIndex); Command.CommandManager.EndCollection(); return(true); }
/// <summary> /// 子ノードの破棄 /// </summary> /// <param name="node"></param> /// <returns></returns> public bool RemoveChild(Node node) { if (!children.Contains(node)) return false; var old_value = children; var new_value = new List<Node>(children); new_value.Remove(node); var cmd = new Command.DelegateCommand( () => { children = new_value; if (OnAfterRemoveNode != null) { OnAfterRemoveNode(this, new ChangedValueEventArgs(node, ChangedValueType.Execute)); } }, () => { children = old_value; if (OnAfterAddNode != null) { OnAfterAddNode(this, new ChangedValueEventArgs(node, ChangedValueType.Unexecute)); } }); Command.CommandManager.Execute(cmd); if (node == Core.SelectedNode) { Core.SelectedNode = null; } return true; }
/// <summary> /// 現在のノードの親とノードの間にノードを挿入 /// </summary> public Node InsertParent() { if (Parent == null) return null; var node = new Node(Parent); node.children.Add((Node)this); var old_value = Parent.children; var new_value = new List<Node>(Parent.children); for (int i = 0; i < new_value.Count; i++) { if (new_value[i] == this) { new_value[i] = node; break; } } var parent = Parent; var _this = this; var cmd = new Command.DelegateCommand( () => { parent.children = new_value; _this.Parent = node; if (parent.OnAfterRemoveNode != null) { parent.OnAfterRemoveNode(parent, new ChangedValueEventArgs(_this, ChangedValueType.Execute)); } if (parent.OnAfterAddNode != null) { parent.OnAfterAddNode(parent, new ChangedValueEventArgs(node, ChangedValueType.Execute)); } }, () => { parent.children = old_value; _this.Parent = parent; if (parent.OnAfterAddNode != null) { parent.OnAfterAddNode(this, new ChangedValueEventArgs(_this, ChangedValueType.Unexecute)); } if (parent.OnAfterRemoveNode != null) { parent.OnAfterRemoveNode(parent, new ChangedValueEventArgs(node, ChangedValueType.Unexecute)); } }); Command.CommandManager.Execute(cmd); return node; }
/// <summary> /// 子同士を交換 /// </summary> /// <param name="node1"></param> /// <param name="node2"></param> /// <returns></returns> public bool ExchangeChildren(Node node1, Node node2) { if (node1 == node2) return false; if (!children.Contains(node1)) return false; if (!children.Contains(node2)) return false; var old_value = children; var new_value = new List<Node>(children); var ind1 = new_value.FindIndex((n) => { return n == node1; }); var ind2 = new_value.FindIndex((n) => { return n == node2; }); new_value[ind1] = node2; new_value[ind2] = node1; var cmd = new Command.DelegateCommand( () => { Tuple<Node, Node> nodes = new Tuple<Node, Node>(node1, node2); children = new_value; if (OnAfterExchangeNodes != null) { OnAfterExchangeNodes(this, new ChangedValueEventArgs(nodes, ChangedValueType.Execute)); } }, () => { Tuple<Node, Node> nodes = new Tuple<Node, Node>(node2, node1); children = old_value; if (OnAfterExchangeNodes != null) { OnAfterExchangeNodes(this, new ChangedValueEventArgs(nodes, ChangedValueType.Unexecute)); } }); Command.CommandManager.Execute(cmd); return true; }