private void myTreeView_AfterLabelEdit()

in src/XmlNotepad/XmlTreeView.cs [292:383]


        private void myTreeView_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
        {
            try
            {
                XmlTreeNode xn = (XmlTreeNode)e.Node;
                XmlNode n = xn.Node;
                if (e.CancelEdit) return; // it's being cancelled.

                if (e.Label == null || StringHelper.IsNullOrEmpty(e.Label.Trim()))
                {

                    string arg = null;
                    if (xn.NodeImage == NodeImage.Attribute)
                        arg = "attributes";
                    else if (xn.NodeImage == NodeImage.Element || xn.NodeImage == NodeImage.OpenElement || xn.NodeImage == NodeImage.Leaf)
                        arg = "elements";

                    if (arg != null && n == null && MessageBox.Show(this,
                        SR.XmlNameEmptyPrompt, SR.XmlNameErrorCaption,
                        MessageBoxButtons.YesNo, MessageBoxIcon.Error) == DialogResult.No)
                    {
                        e.Node.BeginEdit();
                        e.CancelEdit = true;
                    }
                    return;
                }
                Command cmd = null;
                if (n == null)
                {
                    TreeNode parent = e.Node.Parent;
                    XmlNode context = (parent == null) ? this._model.Document : ((XmlTreeNode)parent).Node;
                    cmd = this.UndoManager.Peek();
                    try
                    {
                        InsertNode inode = cmd as InsertNode;
                        if (inode != null)
                        {
                            if (inode.RequiresName)
                            {
                                inode.XmlNode = inode.CreateNode(context, e.Label);
                                // Cause selection event to be triggered so that menu state
                                // is recalculated.
                                this._myTreeView.SelectedNode = null;
                                this.OnNodeInserted(inode.NewNode);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(this, ex.Message,
                            SR.XmlNameErrorCaption, MessageBoxButtons.OK, MessageBoxIcon.Error);
                        this._myTreeView.SelectedNode = e.Node;
                        e.CancelEdit = true;
                        xn.Label = e.Label.Trim();
                        e.Node.BeginEdit();
                        return;
                    }
                    e.Node.Label = e.Label;
                    this._myTreeView.SelectedNode = e.Node;
                    this._nodeTextView.Invalidate(e.Node);
                    this._nodeTextView.FocusBeginEdit(null);
                    return; // one undoable unit.
                }
                switch (n != null ? n.NodeType : XmlNodeType.None)
                {
                    case XmlNodeType.Comment:
                    case XmlNodeType.Text:
                    case XmlNodeType.CDATA:
                        e.CancelEdit = true;
                        // actually it would be cool to change the node type at this point.
                        break;
                    case XmlNodeType.Attribute:
                        cmd = new EditAttributeName(n as XmlAttribute, e);
                        break;
                    case XmlNodeType.Element:
                        cmd = new EditElementName(n as XmlElement, e);
                        break;
                    case XmlNodeType.ProcessingInstruction:
                        cmd = new EditProcessingInstructionName(n as XmlProcessingInstruction, e);
                        break;
                }
                if (cmd != null)
                {
                    this.UndoManager.Push(cmd);
                }
            }
            catch (Exception ex)
            {
                e.CancelEdit = true;
                MessageBox.Show(this, ex.Message, SR.EditErrorCaption, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }