public FragmentUnit CreateFragment(TextBox box)
        {
            int index = Container.Children.IndexOf(box);

            if (box.SelectionLength == 0 || box.SelectionLength == box.Text.Length)
            {
                return(null);
            }
            int          start  = box.SelectionStart;
            int          length = box.SelectionLength;
            string       text   = box.SelectedText;
            FragmentUnit unit   = new FragmentUnit(_viewer, text);

            if (start == 0)
            {
                box.Text = box.Text.Substring(length, box.Text.Length - length);
                Container.Children.Insert(index, unit);
            }
            else if (start + length == box.Text.Length)
            {
                box.Text = box.Text.Substring(0, start);
                Container.Children.Insert(index + 1, unit);
            }
            else
            {
                string  prefix = box.Text.Substring(0, start);
                string  suffix = box.Text.Substring(start + length, box.Text.Length - start - length);
                TextBox newbox = CreateTextBox(suffix);
                box.Text = prefix;
                Container.Children.Insert(index + 1, unit);
                Container.Children.Insert(index + 2, newbox);
            }
            unit.DeleteFragment += new DeleteFragmentHandler(unit_DeleteFragment);
            return(unit);
        }
        private void TriggerViewerFocusChanged(FragmentUnit unit)
        {
            ViewerFocusChangedHandler handler = ViewerFocusChanged;

            if (handler != null)
            {
                handler(unit);
            }
        }
        public void Initialize(SymbolManager manager)
        {
            _manager = manager;
            FragmentUnit unit = new FragmentUnit(this);
            TextBox      box  = unit.SetText("");

            box.Text   = "<Please input your sample>";
            unit.Label = "Symbol" + _index++;
            Symbol symbol = _manager.RegisterSymbol(unit.Label);

            symbol.CreateExpression();
            unit.Symbol  = symbol;
            Layout.Child = unit;
            //Layout.Children.Add(unit);
            SetFocus(null, unit);
        }
        public void Partition()
        {
            if (_focusTextBox == null || _focusUnit == null)
            {
                return;
            }
            FragmentUnit unit = _focusUnit.CreateFragment(_focusTextBox);

            if (unit == null)
            {
                return;
            }
            unit.Label = "Symbol" + _index++;
            Symbol symbol = _manager.RegisterSymbol(unit.Label);

            symbol.CreateExpression();
            unit.Symbol = symbol;
        }
 public void SetFocus(TextBox box, FragmentUnit unit)
 {
     _focusTextBox = box;
     _focusUnit    = unit;
     TriggerViewerFocusChanged(unit);
 }
        void unit_DeleteFragment(FragmentUnit sender)
        {
            int position = this.Container.Children.IndexOf(sender);
            int total    = this.Container.Children.Count;

            if (position == 0)
            {
                if (total > 1 && this.Container.Children[1] is TextBox)
                {
                    string origin = (this.Container.Children[1] as TextBox).Text;
                    (this.Container.Children[1] as TextBox).Text = sender.GetContent() + origin;
                    this.Container.Children.RemoveAt(0);
                }
                else
                {
                    TextBox tb = CreateTextBox(sender.GetContent());
                    this.Container.Children.RemoveAt(0);
                    this.Container.Children.Insert(0, tb);
                }
            }
            else if (position == total - 1)
            {
                if (total > 1 && this.Container.Children[total - 2] is TextBox)
                {
                    (this.Container.Children[total - 2] as TextBox).Text += sender.GetContent();
                    this.Container.Children.RemoveAt(position);
                }
                else
                {
                    TextBox tb = CreateTextBox(sender.GetContent());
                    this.Container.Children.RemoveAt(position);
                    this.Container.Children.Insert(position, tb);
                }
            }
            else
            {
                if (this.Container.Children[position - 1] is TextBox)
                {
                    if (this.Container.Children[position + 1] is TextBox)
                    {
                        (this.Container.Children[position - 1] as TextBox).Text += sender.GetContent() + (this.Container.Children[position + 1] as TextBox).Text;
                        this.Container.Children.RemoveAt(position + 1);
                        this.Container.Children.RemoveAt(position);
                    }
                    else
                    {
                        (this.Container.Children[position - 1] as TextBox).Text += sender.GetContent();
                        this.Container.Children.RemoveAt(position);
                    }
                }
                else if (this.Container.Children[position + 1] is TextBox)
                {
                    string origin = (this.Container.Children[position + 1] as TextBox).Text;
                    (this.Container.Children[position + 1] as TextBox).Text = sender.GetContent() + origin;
                    this.Container.Children.RemoveAt(position);
                }
                else
                {
                    TextBox tb = CreateTextBox(sender.GetContent());
                    this.Container.Children.RemoveAt(position);
                    this.Container.Children.Insert(position, tb);
                }
            }
            //_viewer.SetFocus(null, this);
        }
 void Partitioner_ViewerFocusChanged(Editor.FragmentUnit unit)
 {
     Editor.SetSymbol(unit.Symbol);
 }