public FormCondition(Word.ContentControl cc, ConditionsPartEntry cpe, condition existingCondition)
        {
            InitializeComponent();

            // NET 4 way; see http://msdn.microsoft.com/en-us/library/microsoft.office.tools.word.extensions.aspx
            FabDocxState fabDocxState = (FabDocxState)Globals.Factory.GetVstoObject(Globals.ThisAddIn.Application.ActiveDocument).Tag;

            // NET 3.5 way, which requires using Microsoft.Office.Tools.Word.Extensions
            //FabDocxState fabDocxState = (FabDocxState)Globals.ThisAddIn.Application.ActiveDocument.GetVstoObject(Globals.Factory).Tag;
            this.model = fabDocxState.model;
            xppe = new XPathsPartEntry(model);

            this.cc = cc;

            this.cpe = cpe;
            this.existingCondition = existingCondition;

            this.questionsPart = model.questionsPart;
            questionnaire = new questionnaire();
            questionnaire.Deserialize(questionsPart.XML, out questionnaire);

            questionListHelper = new Helpers.QuestionListHelperForConditionsForm(model, xppe, questionnaire, cc);
            questionListHelper.listBoxTypeFilter = listBoxTypeFilter;
            questionListHelper.listBoxQuestions = listBoxQuestions;
            questionListHelper.checkBoxScope = checkBoxScope;

            questionListHelper.comboBoxValues = comboBoxValues;
            questionListHelper.listBoxPredicate = listBoxPredicate;

            this.listBoxQuestions.SelectedIndexChanged += new System.EventHandler(questionListHelper.listBoxQuestions_SelectedIndexChanged);
            this.listBoxTypeFilter.SelectedIndexChanged += new System.EventHandler(questionListHelper.listBoxTypeFilter_SelectedIndexChanged);

            question existingQuestion = null;
            string matchResponse = null;
            if (existingCondition != null)
            {
                // Use the question associated with it, to pre-select
                // the correct entries in the dialog.

                // Re-label the window, so user can see what the condition was about
                this.Text = "Editing Condition:   " + cc.Title;

                //List<xpathsXpath> xpaths = ConditionsPartEntry.getXPathsUsedInCondition(existingCondition, xppe);
                List<xpathsXpath> xpaths = new List<xpathsXpath>();
                existingCondition.listXPaths(xpaths, cpe.conditions, xppe.getXPaths());

                if (xpaths.Count > 1)
                {
                    // TODO: use complex conditions editor
                }
                xpathsXpath xpathObj = xpaths[0];

                String xpathVal = xpathObj.dataBinding.xpath;

                if (xpathVal.StartsWith("/"))
                {
                    // simple
                    //System.out.println("question " + xpathObj.getQuestionID()
                    //        + " is in use via boolean condition " + conditionId);

                    existingQuestion = this.questionnaire.getQuestion(xpathObj.questionID);
                    matchResponse = xpathVal;
                }
                else if (xpathVal.Contains("position"))
                {
                    // TODO
                }
                else
                {
                    //System.out.println(xpathVal);

                    String qid = xpathVal.Substring(
                        xpathVal.LastIndexOf("@id") + 5);
                    //						System.out.println("Got qid: " + qid);
                    qid = qid.Substring(0, qid.IndexOf("'"));
                    //						System.out.println("Got qid: " + qid);

                    //System.out.println("question " + qid
                    //        + " is in use via condition " + conditionId);

                    existingQuestion = this.questionnaire.getQuestion(qid);
                    matchResponse = xpathVal;

                }

            }

            questionListHelper.populateTypeFilter(true);

            if (existingQuestion == null)
            {
                // for init, populate with all questions
                questionListHelper.populateQuestions(null);
            }
            else
            {
                // Just show the existing question
                listBoxQuestions.Items.Add(existingQuestion);
            }

            if (this.listBoxQuestions.Items.Count == 0) // Never happens if in a repeat, and nor do we want it to, since user might just want to use "repeat pos" stuff
            {
                // Try including out of scope
                this.checkBoxScope.Checked = true;
                questionListHelper.populateQuestions(null);
                if (this.listBoxQuestions.Items.Count == 0)
                {
                    MessageBox.Show("You can't define a condition until you have set up at least one question. Let's do that now. ");

                    FormQA formQA = new FormQA(cc, false);
                    formQA.ShowDialog();
                    formQA.Dispose();

                    // Refresh these
                    xppe = new XPathsPartEntry(model);
                    questionnaire.Deserialize(questionsPart.XML, out questionnaire);

                    questionListHelper.filterAction();

                    return;
                }
            }
            // value
            question q;
            if (existingQuestion == null)
            {
                // for init, populate with all questions
                q = (question)this.listBoxQuestions.Items[0];
            }
            else
            {
                q = existingQuestion;
            }

            this.listBoxQuestions.SelectedItem = q;
            if (q.response.Item is responseFixed)
            {
                questionListHelper.populateValues((responseFixed)q.response.Item, matchResponse);
            }

            // predicate =
            questionListHelper.populatePredicates(q);  // TODO: set this correctly in editing mode
        }
        public void buttonBind_Click(Office.IRibbonControl control)
        {
            try
            {
                Word.Document document = Globals.ThisAddIn.Application.ActiveDocument;

                //Model model = Model.ModelFactory(document);

                FabDocxState fabDocxState = getState();
                fabDocxState.inPlutextAdd = true;

                if (majorVersion>=14)
                {
                    getWordApp().UndoRecord.StartCustomRecord("FabDocx Insert Question");
                }

                // Create control
                Word.ContentControl cc = null;
                object missing = System.Type.Missing;
                try
                {
                    cc = document.ContentControls.Add(Word.WdContentControlType.wdContentControlText, ref missing);
                    cc.MultiLine = true;
                }
                catch (System.Exception e)
                {
                    if (majorVersion>=14)
                    {
                        getWordApp().UndoRecord.EndCustomRecord();
                    }
                    log.Error(e);
                    MessageBox.Show("Selection must be either part of a single paragraph, or one or more whole paragraphs");
                    fabDocxState.inPlutextAdd = false;
                    return;
                }

                // Ask question - form to have button which says "use existing Q/A"
                // or a combobox presenting
                // a default id, or choice from drop down?
                // Since we only want to create a new answer if it is a new question,
                // present the question form first.
                FormQA formQA = new FormQA(cc);
                formQA.ShowDialog();
                formQA.Dispose();

                if (majorVersion >= 14)
                {
                    getWordApp().UndoRecord.EndCustomRecord();
                }

                // It also makes the answer format more friendly if the id we give
                // an answer happens to be the same as the relevant question
                // (though not mandatory, since it is the xpath entry which ties
                //  the 2 together)

                if (fabDocxState.TaskPane.Visible)
                {
                    Controls.LogicTaskPaneUserControl ltp = (Controls.LogicTaskPaneUserControl)fabDocxState.TaskPane.Control;
                    ltp.populateLogicInUse();
                }
            }
            catch (System.Exception e2)
            {
                log.Error(e2);
                MessageBox.Show("Something went wrong.  Check the logs.");
            }
        }
        private void buttonQuestionAdd_Click(object sender, EventArgs e)
        {
            FormQA formQA = new FormQA(cc, false);
            formQA.ShowDialog();
            formQA.Dispose();

            // Refresh these
            xppe = new XPathsPartEntry(model);
            questionnaire.Deserialize(questionsPart.XML, out questionnaire);

            questionListHelper.filterAction();
        }