Exemplo n.º 1
0
 public override void Do()
 {
     if (this.parent == null) {
         this.pos = e.Index;
         this.parent = new TreeParent(e.TreeView, doc, e);
     }
     parent.Remove(e);
 }
Exemplo n.º 2
0
        public override void Undo()
        {
            // Cannot use this.sourceParent because this points to the old source position
            // not the current position.
            TreeParent parent = new TreeParent(this.sourceParent.View, this.sourceParent.Document, this.source);
            parent.Remove(this.source);

            // If the node was not in the tree, then undo just removes it, it does not
            // have to re-insert back in a previous position, because it was a new node
            // (probably inserted via drag/drop).
            if (this.sourceParent.IsNodeInTree) {
                this.sourceParent.Insert(this.sourcePosition, InsertPosition.Before, source, true);
                if (source.Parent != null && source.Parent.Nodes.Count == 1) {
                    source.Parent.Expand();
                }
                source.TreeView.SelectedNode = source;
            } else {
                this.target.TreeView.SelectedNode = target;
            }
        }
Exemplo n.º 3
0
 public CutCommand(XmlTreeView view, XmlTreeNode node)
 {
     this.node = node;
     this.parent = new TreeParent(view, view.Model.Document, node);
     index = node.Index;
 }
Exemplo n.º 4
0
        /// <summary>
        /// Move or copy a node from one place to another place in the tree.
        /// </summary>
        /// <param name="view">The MyTreeView that we are inserting into</param>
        /// <param name="source">The node that we are moving.  This node may not be in the tree
        /// and that is ok, so it might be a node that is being cut&paste from another process
        /// for example</param>
        /// <param name="target">The existing node that establishes where in the tree we want
        /// to move the source node to</param>
        /// <param name="where">The position relative to the target node (before or after)</param>
        /// <param name="copy">Whether we are moving or copying the source node</param>
        public MoveNode(XmlTreeView view, XmlTreeNode source, XmlTreeNode target, InsertPosition where, bool copy)
        {
            XmlNode sn = source.Node;
            XmlNode dn = target.Node;

            this.copy = copy;
            TreeView tv = view.TreeView;
            XmlDocument doc = view.Model.Document;
            this.view = view;
            this.sourcePosition = source.Index;

            view.Model.BeginUpdate();
            try {
                if (copy) {
                    this.wasExpanded = source.IsExpanded;
                    XmlTreeNode newSource = view.CreateTreeNode();
                    if (sn != null) {
                        sn = sn.CloneNode(true);
                        newSource.Node = sn;
                    }
                    source = newSource;
                }

                this.sourceParent = new TreeParent(tv, doc, source);
                this.tp = new TreeParent(tv, doc, target);

                // normalize destination based on source node type.
                // for example, if source is an attribute, then it can only be
                // inserted amongst attributes of another node.
                if (tp.IsRoot && where != InsertPosition.Child) {
                    if (sn is XmlAttribute)
                        throw new Exception(SR.RootLevelAttributes);
                    if (sn is XmlText || sn is XmlCDataSection)
                        throw new Exception(SR.RootLevelText);
                    if (sn is XmlElement && sn.OwnerDocument.DocumentElement != null && sn.OwnerDocument.DocumentElement != sn)
                        throw new Exception(SR.RootLevelElements);
                    if (dn is XmlDeclaration && where == InsertPosition.Before)
                        throw new Exception(SR.RootLevelBeforeXmlDecl);
                }
                if (where != InsertPosition.Child) {
                    if (sn is XmlAttribute) {
                        if (!(dn is XmlAttribute)) {
                            if (tp.AttributeCount != 0) {
                                // move target to valid location for attributes.
                                target = tp.GetChild(tp.AttributeCount - 1);
                                where = InsertPosition.After;
                            } else {
                                // append the attribute.
                                where = InsertPosition.Child;
                                target = (XmlTreeNode)target.Parent;
                            }

                        }
                    } else if (dn is XmlAttribute) {
                        if (!(sn is XmlAttribute)) {
                            int skip = tp.AttributeCount;
                            if (tp.Count > skip) {
                                // Move non-attribute down to beginning of child elements.
                                target = tp.GetChild(skip);
                                where = InsertPosition.Before;
                            } else {
                                // append the node.
                                where = InsertPosition.Child;
                                target = (XmlTreeNode)target.Parent;
                            }
                        }
                    }
                }
                this.source = source;
                this.target = target;
                this.where = where;
                this.tp = new TreeParent(tv, doc, target);

                if (where == InsertPosition.Child) {
                    this.tp.SetParent(target);
                }
            } finally {
                view.Model.EndUpdate();
            }
        }
Exemplo n.º 5
0
 public override void Undo()
 {
     if (newNode.IsEditing) {
         newNode.EndEdit(true);
     }
     TreeParent np = new TreeParent(this.view, this.doc, newNode);
     np.Remove(newNode);
 }
Exemplo n.º 6
0
        public void Initialize(XmlTreeNode newNode, XmlTreeNode target, InsertPosition position)
        {
            this.newNode = newNode;
            this.position = position;

            if (target == null) {
                this.parent = new TreeParent(this.view.TreeView, this.doc);
            } else {
                this.parent = new TreeParent(this.view, this.doc, target);
                if (position == InsertPosition.Child) {
                    if (CanHaveChildren(target)) {
                        this.parent.SetParent(target);
                    } else {
                        // if it's not an element it cannot have children!
                        this.position = InsertPosition.After;
                    }
                }
            }
            if (position == InsertPosition.Child) {
                if (target == null) {
                    // inserting at rool level
                    this.pos = this.view.TreeView.Nodes.Count;
                } else {
                    if (!CanHaveChildren(target)) {
                        this.position = InsertPosition.After;
                    }
                    if (newNode.NodeImage == NodeImage.Attribute) {
                        this.pos = this.parent.AttributeCount;
                    } else if (target != null) {
                        this.pos = target.Nodes.Count;
                    }
                }
            }
            if (this.position != InsertPosition.Child) {
                if (target.Node is XmlAttribute ^ newNode.Node is XmlAttribute) {
                    pos = this.parent.AttributeCount;
                    this.position = InsertPosition.Before;
                } else if (target != null) {
                    this.pos = target.Index;
                }
            }
        }
Exemplo n.º 7
0
 // Returns false if the given insertion is illegal
 public bool Initialize(XmlTreeNode n, InsertPosition position, XmlNodeType type)
 {
     this.position = position;
     this.type = type;
     XmlNode xn = null;
     this.newNode.NodeType = type;
     if (n != null) {
         this.parent = new TreeParent(view, doc, n);
         xn = n.Node;
     } else {
         position = InsertPosition.Child; ;
         xn = view.Model.Document;
         this.parent = new TreeParent(view.TreeView, view.Model.Document);
     }
     bool result = CanInsertNode(position, type, xn);
     if (result) {
         if (position == InsertPosition.Child) {
             if (xn != null) parent.SetParent(n);
             pos = parent.AttributeCount;
             if (type != XmlNodeType.Attribute)
                 pos += parent.ChildCount;
         } else {
             if (type == XmlNodeType.Attribute ^ xn is XmlAttribute) {
                 pos = this.parent.AttributeCount;
                 this.position = InsertPosition.Before;
             } else if (n != null) {
                 pos = n.Index;
             }
         }
     }
     return result;
 }
Exemplo n.º 8
0
 public override void Undo()
 {
     this.view.BeginUpdate();
     try
     {
         if (newNode.IsEditing)
         {
             newNode.EndEdit(true);
         }
         TreeParent np = new TreeParent(this.view, this.doc, newNode);
         np.Remove(newNode);
     }
     finally
     {
         this.view.EndUpdate();
     }
 }