/// <summary>
        /// Insert statement with a combobox into current document within richtextbox at current caret position.
        /// </summary>
        /// <param name="precedeStatement"></param>
        public void InsertStatementWithComboBox(Statement precedeStatement)
        {
            //get position where element should be added
            TextPointer insertionPosition = CaretPosition.GetInsertionPosition(LogicalDirection.Forward);

            ComboBoxStatement statementBox = new ComboBoxStatement(precedeStatement);

            //attach event
            statementBox.ComboBoxDropDownOpened += OnComboBoxStatementDropDownOpened;
            statementBox.ComboBoxDropDownClosed += OnComboBoxStatementDropDownClosed;

            InlineUIContainer myInlineUIContainer = new InlineUIContainer(statementBox, insertionPosition);

            //move caret position
            TextPointer newCaretPosition = insertionPosition.GetNextInsertionPosition(LogicalDirection.Forward);

            if (newCaretPosition != null)
            {
                CaretPosition = newCaretPosition;
            }
        }
        /// <summary>
        /// Searches for all chidren comboboxes and deattaches event handlers from them.
        /// </summary>
        private void DettachEventHandlersFromAllComboBoxes(FlowDocument document)
        {
            System.Diagnostics.Debug.WriteLine("Detaching eventhandlers from combobox statements");

            for (TextPointer position = document.ContentStart;
                 position != null && position.CompareTo(document.ContentEnd) <= 0;
                 position = position.GetNextContextPosition(LogicalDirection.Forward))
            {
                if (position.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.ElementEnd)
                {
                    // process only UiElements. all other elements are ignored
                    InlineUIContainer uiContainer;
                    if ((uiContainer = position.Parent as InlineUIContainer) != null)
                    {
                        ComboBoxStatement statementBox = uiContainer.Child as ComboBoxStatement;
                        statementBox.ComboBoxDropDownOpened -= OnComboBoxStatementDropDownOpened;
                        statementBox.ComboBoxDropDownClosed -= OnComboBoxStatementDropDownClosed;
                    }
                }
            }
        }
        /// <summary>
        /// Appends given line to the given paragraph
        /// </summary>
        /// <param name="currentParagraph"></param>
        /// <param name="line"></param>
        public void AppendTextToParagraph(Paragraph currentParagraph, string line)
        {
            foreach (string token in Regex.Split(line, regexStatementsPattern))
            {
                if (token.StartsWith("Load") || token.StartsWith("Select"))
                {
                    //match the selected item
                    Match m = Regex.Match(token, @"""(.*)""");
                    string selectedItem = m.Groups[1].Value;

                    ComboBoxStatement statementBox;
                    if (token.StartsWith("Load"))
                    {
                        statementBox = new ComboBoxStatement(Statement.Load, selectedItem);
                    }
                    else
                    {
                        statementBox = new ComboBoxStatement(Statement.Select, selectedItem);
                    }

                    InlineUIContainer myInlineUIContainer = new InlineUIContainer(statementBox);
                    currentParagraph.Inlines.Add(myInlineUIContainer);
                }
                else
                {
                    currentParagraph.Inlines.Add(new Run(token));
                }
            }
        }
        /// <summary>
        /// Insert statement with a combobox into current document within richtextbox at current caret position.
        /// </summary>
        /// <param name="precedeStatement"></param>
        public void InsertStatementWithComboBox(Statement precedeStatement)
        {
            //get position where element should be added
            TextPointer insertionPosition = CaretPosition.GetInsertionPosition(LogicalDirection.Forward);

            ComboBoxStatement statementBox = new ComboBoxStatement(precedeStatement);

            //attach event
            statementBox.ComboBoxDropDownOpened += OnComboBoxStatementDropDownOpened;
            statementBox.ComboBoxDropDownClosed += OnComboBoxStatementDropDownClosed;
            
            InlineUIContainer myInlineUIContainer = new InlineUIContainer(statementBox, insertionPosition);

            //move caret position
            TextPointer newCaretPosition = insertionPosition.GetNextInsertionPosition(LogicalDirection.Forward);
            if (newCaretPosition != null)
                CaretPosition = newCaretPosition;
        }