/// <summary>
        /// Replace the contents of the existing XML part.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void buttonReplaceXML_Click(Office.IRibbonControl control)
        {
            CustomTaskPane ctpPaneForThisWindow = Utilities.FindTaskPaneForCurrentWindow();

            Controls.ControlMain ccm = (Controls.ControlMain)ctpPaneForThisWindow.Control;

            Office.CustomXMLPart userPart = null;
            bool cancelled = false;

            while (userPart == null && !cancelled)
            {
                using (Forms.FormAddPart fap = new Forms.FormAddPart())
                {
                    if (fap.ShowDialog() == DialogResult.OK)
                    {
                        string requiredRoot = System.Configuration.ConfigurationManager.AppSettings["RootElement"];
                        if (string.IsNullOrWhiteSpace(requiredRoot))
                        {
                            ccm.EventHandlerAndOnChildren.NodeAfterReplaceDisconnect();
                            Office.CustomXMLPart existingPart = ccm.model.getUserPart(requiredRoot);
                            CustomXmlUtilities.replaceXmlDoc(existingPart, fap.XmlString);
                            userPart = existingPart;
                        }
                        else
                        {
                            Office.CustomXMLPart existingPart = ccm.model.getUserPart(requiredRoot);
                            if (existingPart.DocumentElement.BaseName.Equals(requiredRoot))
                            {
                                ccm.EventHandlerAndOnChildren.NodeAfterReplaceDisconnect();
                                CustomXmlUtilities.replaceXmlDoc(existingPart, fap.XmlString);
                                userPart = existingPart;
                            }
                            else
                            {
                                MessageBox.Show("You need to use root element: " + requiredRoot);
                            }
                        }
                    }
                    else
                    {
                        cancelled = true;
                    }
                }
            }

            if (!cancelled)
            {
                ccm.EventHandlerAndOnChildren.NodeAfterReplaceReconnect();
            }
            ccm.RefreshTreeControl(null);
        }
        private void toggleButtonMapping_Click(object sender, RibbonControlEventArgs e)
        {
            Word.Document document = Globals.ThisAddIn.Application.ActiveDocument;

            //get the ctp for this window (or null if there's not one)
            CustomTaskPane ctpPaneForThisWindow = Utilities.FindTaskPaneForCurrentWindow();

            if (toggleButtonMapping.Checked == false)
            {
                Debug.Assert(ctpPaneForThisWindow != null,
                    "how was the ribbon button pressed if there was a control?");

                //it's being unclicked
                if (ctpPaneForThisWindow != null)
                    ctpPaneForThisWindow.Visible = false;

            }
            else if (ctpPaneForThisWindow == null)
            {
                if (CustomXmlUtilities.areOpenDoPEPartsPresent(Globals.ThisAddIn.Application.ActiveDocument))
                {
                    log.Debug("OpenDoPE parts detected as present");
                }
                else
                {
                    log.Info("OpenDoPE parts not detected; adding now.");

                    Model model = Model.ModelFactory(document);
                    string requiredRoot = System.Configuration.ConfigurationManager.AppSettings["RootElement"];
                    Office.CustomXMLPart userPart = model.getUserPart(requiredRoot);

                    bool cancelled = false;
                    while (userPart == null && !cancelled)
                    {
                        using (Forms.FormAddPart fap = new Forms.FormAddPart())
                        {
                            //add a new stream from the XML retrieved from the Add New dialog
                            //otherwise, select the last selected item and populate with its xml
                            if (fap.ShowDialog() == DialogResult.OK)
                            {
                                object missing = System.Reflection.Missing.Value;
                                Office.CustomXMLPart newCxp = document.CustomXMLParts.Add(fap.XmlString, missing);

                                model = Model.ModelFactory(document);
                                userPart = model.getUserPart(requiredRoot);
                                if (userPart == null)
                                {
                                    newCxp.Delete();
                                    MessageBox.Show("You need to use root element: " + requiredRoot);
                                }
                            }
                            else
                            {
                                cancelled = true;
                            }
                        }
                    }
                    if (cancelled)
                    {
                        MessageBox.Show("For template authoring, you need to add your XML.");
                        toggleButtonMapping.Checked = false;
                        return;
                    }

                    InitialSetup init = new InitialSetup();
                    init.process();
                }
                launchTaskPane();
            }
            else
            {
                //it's built and being clicked, show it
                ctpPaneForThisWindow.Visible = true;
            }
        }
        /// <summary>
        /// Replace the contents of the existing XML part.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void buttonReplaceXML_Click(object sender, Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs e)
        {
            CustomTaskPane ctpPaneForThisWindow = Utilities.FindTaskPaneForCurrentWindow();
            Controls.ControlMain ccm = (Controls.ControlMain)ctpPaneForThisWindow.Control;

            Office.CustomXMLPart userPart = null;
            bool cancelled = false;
            while (userPart == null && !cancelled)
            {
                using (Forms.FormAddPart fap = new Forms.FormAddPart())
                {
                    if (fap.ShowDialog() == DialogResult.OK)
                    {

                        string requiredRoot = System.Configuration.ConfigurationManager.AppSettings["RootElement"];
                        if (string.IsNullOrWhiteSpace(requiredRoot))
                        {
                            ccm.EventHandlerAndOnChildren.NodeAfterReplaceDisconnect();
                            Office.CustomXMLPart existingPart = ccm.model.getUserPart(requiredRoot);
                            CustomXmlUtilities.replaceXmlDoc(existingPart, fap.XmlString);
                            userPart = existingPart;
                        }
                        else
                        {
                            Office.CustomXMLPart existingPart = ccm.model.getUserPart(requiredRoot);
                            if (existingPart.DocumentElement.BaseName.Equals(requiredRoot))
                            {
                                ccm.EventHandlerAndOnChildren.NodeAfterReplaceDisconnect();
                                CustomXmlUtilities.replaceXmlDoc(existingPart, fap.XmlString);
                                userPart = existingPart;
                            }
                            else
                            {
                                MessageBox.Show("You need to use root element: " + requiredRoot);
                            }
                        }
                    }
                    else
                    {
                        cancelled = true;
                    }

                }
            }

            if (!cancelled)
            {
                ccm.EventHandlerAndOnChildren.NodeAfterReplaceReconnect();
            }
            ccm.RefreshTreeControl(null);
        }
        public void toggleButtonMapping_Click(Office.IRibbonControl control, bool pressed)//, ref bool cancelDefault)
        {
            Word.Document document = Globals.ThisAddIn.Application.ActiveDocument;

            //get the ctp for this window (or null if there's not one)
            CustomTaskPane ctpPaneForThisWindow = Utilities.FindTaskPaneForCurrentWindow();

            if (pressed)
            {
                toggleButtonMappingChecked = true;
            }


            if (toggleButtonMappingChecked == false)
            {
                //Debug.Assert(ctpPaneForThisWindow != null,
                //    "how was the ribbon button pressed if there was a control?");

                //it's being unclicked
                if (ctpPaneForThisWindow != null)
                {
                    ctpPaneForThisWindow.Visible = false;
                }
            }
            else if (ctpPaneForThisWindow == null)
            {
                if (CustomXmlUtilities.areOpenDoPEPartsPresent(Globals.ThisAddIn.Application.ActiveDocument))
                {
                    log.Debug("OpenDoPE parts detected as present");
                }
                else
                {
                    log.Info("OpenDoPE parts not detected; adding now.");

                    Model  model                  = Model.ModelFactory(document);
                    string requiredRoot           = System.Configuration.ConfigurationManager.AppSettings["RootElement"];
                    Office.CustomXMLPart userPart = model.getUserPart(requiredRoot);

                    bool cancelled = false;
                    while (userPart == null && !cancelled)
                    {
                        using (Forms.FormAddPart fap = new Forms.FormAddPart())
                        {
                            //add a new stream from the XML retrieved from the Add New dialog
                            //otherwise, select the last selected item and populate with its xml
                            if (fap.ShowDialog() == DialogResult.OK)
                            {
                                object missing = System.Reflection.Missing.Value;
                                Office.CustomXMLPart newCxp = document.CustomXMLParts.Add(fap.XmlString, missing);

                                model    = Model.ModelFactory(document);
                                userPart = model.getUserPart(requiredRoot);
                                if (userPart == null)
                                {
                                    newCxp.Delete();
                                    MessageBox.Show("You need to use root element: " + requiredRoot);
                                }
                            }
                            else
                            {
                                cancelled = true;
                            }
                        }
                    }
                    if (cancelled)
                    {
                        MessageBox.Show("For template authoring, you need to add your XML.");
                        toggleButtonMappingChecked = false;
                        return;
                    }

                    InitialSetup init = new InitialSetup();
                    init.process();
                }
                launchTaskPane();
            }
            else
            {
                //it's built and being clicked, show it
                ctpPaneForThisWindow.Visible = true;
            }
        }