private void addTransitionMenuItem_Click(object sender, EventArgs e)
        {
            if (StateMachine.States.Count > 1)
            {
                DataDictionary.ObjectFactory factory = (DataDictionary.ObjectFactory)DataDictionary.Generated.acceptor.getFactory();
                DataDictionary.Rules.Rule    rule    = (DataDictionary.Rules.Rule)factory.createRule();
                rule.Name = "Rule" + (StateMachine.Rules.Count + 1);

                DataDictionary.Rules.RuleCondition ruleCondition = (DataDictionary.Rules.RuleCondition)factory.createRuleCondition();
                ruleCondition.Name = "RuleCondition" + (rule.RuleConditions.Count + 1);
                rule.appendConditions(ruleCondition);

                DataDictionary.Rules.Action action = (DataDictionary.Rules.Action)factory.createAction();
                action.Expression = "THIS <- " + ((State)StateMachine.States[1]).LiteralName;
                ruleCondition.appendActions(action);

                if (MDIWindow.DataDictionaryWindow != null)
                {
                    DataDictionaryView.StateTreeNode stateNode = MDIWindow.DataDictionaryWindow.FindNode((State)StateMachine.States[0]) as DataDictionaryView.StateTreeNode;
                    DataDictionaryView.RuleTreeNode  ruleNode  = stateNode.Rules.AddRule(rule);
                }

                StateContainerPanel.RefreshControl();
                StateContainerPanel.Refresh();

                TransitionControl control = StateContainerPanel.getTransitionControl(ruleCondition);
                Select(control);
            }
        }
 /// <summary>
 /// Selects a model element
 /// </summary>
 /// <param name="model"></param>
 public void Select(object model)
 {
     Selected = model;
     if (model is StateControl)
     {
         StateControl control = model as StateControl;
         propertyGrid.SelectedObject = new StateEditor(control);
         descriptionRichTextBox.ResetText();
         descriptionRichTextBox.Rtf = control.State.getExplain(false);
         MDIWindow.Select(control.State);
     }
     else if (model is TransitionControl)
     {
         TransitionControl control = model as TransitionControl;
         propertyGrid.SelectedObject = new TransitionEditor(control);
         descriptionRichTextBox.ResetText();
         if (control.Transition.RuleCondition != null)
         {
             descriptionRichTextBox.Rtf = control.Transition.RuleCondition.getExplain(true);
         }
         else
         {
             descriptionRichTextBox.Rtf = "";
         }
         MDIWindow.Select(control.Transition.RuleCondition);
     }
     else
     {
         propertyGrid.SelectedObject = null;
     }
 }
        /// <summary>
        /// Provides the transition control which corresponds to the transition provided
        /// </summary>
        /// <param name="transition"></param>
        /// <returns></returns>
        public TransitionControl getTransitionControl(DataDictionary.Rules.Transition transition)
        {
            TransitionControl retVal = null;

            if (transitions.ContainsKey(transition))
            {
                retVal = transitions[transition];
            }

            return(retVal);
        }
        /// <summary>
        /// Tries to slide the transition up following the transition arrow to avoid any collision
        /// with the already allocated bounding boxes
        /// </summary>
        /// <param name="transition"></param>
        /// <param name="center"></param>
        /// <returns></returns>
        private Point Slide(TransitionControl transition, Point center, TransitionControl.SlideDirection direction)
        {
            Point     retVal    = center;
            Rectangle colliding = AllocatedBoxes.Intersects(transition.getTextBoundingBox(retVal));

            while (colliding != Rectangle.Empty)
            {
                retVal    = transition.Slide(retVal, colliding, direction);
                colliding = AllocatedBoxes.Intersects(transition.getTextBoundingBox(retVal));
            }

            return(retVal);
        }
        /// <summary>
        /// Provides the transition control which corresponds to the rule
        /// </summary>
        /// <param name="ruleCondition"></param>
        /// <returns></returns>
        public TransitionControl getTransitionControl(DataDictionary.Rules.RuleCondition ruleCondition)
        {
            TransitionControl retVal = null;

            foreach (TransitionControl control in transitions.Values)
            {
                if (control.Transition.RuleCondition == ruleCondition)
                {
                    retVal = control;
                    break;
                }
            }

            return(retVal);
        }
Exemple #6
0
        /// <summary>
        /// Provides the IModelElement element from the graphic element
        /// </summary>
        /// <param name="element"></param>
        /// <returns></returns>
        protected override IModelElement GetModelFromGraphicElement(GraphicElement element)
        {
            IModelElement retVal;

            if (element is TransitionControl)
            {
                TransitionControl transition = element as TransitionControl;
                retVal = transition.TypedModel.RuleCondition;
            }
            else
            {
                retVal = base.GetModelFromGraphicElement(element);
            }

            return(retVal);
        }
        /// <summary>
        /// Refreshes the control according to the state machine
        /// </summary>
        public void RefreshControl()
        {
            try
            {
                refreshingControl = true;
                SuspendLayout();

                foreach (StateControl control in states.Values)
                {
                    control.Parent = null;
                }
                states.Clear();

                foreach (TransitionControl control in transitions.Values)
                {
                    control.Parent = null;
                }
                transitions.Clear();

                if (stateMachine != null)
                {
                    Text = stateMachine.Name;
                    foreach (State state in stateMachine.States)
                    {
                        StateControl stateControl = new StateControl();
                        states[state]       = stateControl;
                        stateControl.Parent = this;
                        stateControl.State  = state;
                    }
                    foreach (DataDictionary.Rules.Transition transition in stateMachine.Transitions)
                    {
                        TransitionControl transitionControl = new TransitionControl();
                        transitions[transition]      = transitionControl;
                        transitionControl.Parent     = this;
                        transitionControl.Transition = transition;
                    }
                    UpdateTransitionPosition();
                }
            }
            finally
            {
                refreshingControl = false;
                ResumeLayout(true);
            }

            Refresh();
        }
        /// <summary>
        /// Selects a model element
        /// </summary>
        /// <param name="model"></param>
        public void Select(object model)
        {
            if (StateDiagramWindow != null)
            {
                StateDiagramWindow.Select(model);
            }
            else
            {
                if (model is StateControl)
                {
                    StateControl control = model as StateControl;
                    MDIWindow.Select(control.State);
                }
                else if (model is TransitionControl)
                {
                    TransitionControl control = model as TransitionControl;
                    MDIWindow.Select(control.Transition.RuleCondition);
                }
            }

            Refresh();
        }
 void StatePanel_MouseDown(object sender, MouseEventArgs e)
 {
     if (e.Button == System.Windows.Forms.MouseButtons.Left)
     {
         Point clickPoint = new Point(e.X, e.Y);
         foreach (TransitionControl transition in transitions.Values)
         {
             if (around(transition.StartLocation, clickPoint))
             {
                 changingTransition        = transition;
                 changingTransition.Parent = this;   // I do not know why...
                 chaningTransitionAction   = ChangeAction.InitialState;
                 break;
             }
             if (around(transition.TargetLocation, clickPoint))
             {
                 changingTransition        = transition;
                 changingTransition.Parent = this;   // I do not know why...
                 chaningTransitionAction   = ChangeAction.TargetState;
                 break;
             }
         }
     }
 }
        private void deleteMenuItem1_Click(object sender, EventArgs e)
        {
            IModelElement model = null;

            if (Selected is StateControl)
            {
                model = (Selected as StateControl).State;
            }
            else if (Selected is TransitionControl)
            {
                TransitionControl control       = Selected as TransitionControl;
                RuleCondition     ruleCondition = control.Transition.RuleCondition;
                Rule rule = ruleCondition.EnclosingRule;
                if (rule.countConditions() == 1)
                {
                    model = rule;
                }
                else
                {
                    model = ruleCondition;
                }
            }

            if (MDIWindow.DataDictionaryWindow != null)
            {
                BaseTreeNode node = MDIWindow.DataDictionaryWindow.FindNode(model);
                if (node != null)
                {
                    node.Delete();
                }
            }
            Select(null);

            StateContainerPanel.RefreshControl();
            StateContainerPanel.Refresh();
        }
        /// <summary>
        /// Tries to slide the transition up following the transition arrow to avoid any collision
        /// with the already allocated bounding boxes
        /// </summary>
        /// <param name="transition"></param>
        /// <param name="center"></param>
        /// <returns></returns>
        private Point Slide(TransitionControl transition, Point center, TransitionControl.SlideDirection direction)
        {
            Point retVal = center;
            Rectangle colliding = AllocatedBoxes.Intersects(transition.getTextBoundingBox(retVal));

            while (colliding != Rectangle.Empty)
            {
                retVal = transition.Slide(retVal, colliding, direction);
                colliding = AllocatedBoxes.Intersects(transition.getTextBoundingBox(retVal));
            }

            return retVal;
        }
        /// <summary>
        /// Refreshes the control according to the state machine
        /// </summary>
        public void RefreshControl()
        {
            try
            {
                refreshingControl = true;
                SuspendLayout();

                foreach (StateControl control in states.Values)
                {
                    control.Parent = null;
                }
                states.Clear();

                foreach (TransitionControl control in transitions.Values)
                {
                    control.Parent = null;
                }
                transitions.Clear();

                if (stateMachine != null)
                {
                    Text = stateMachine.Name;
                    foreach (State state in stateMachine.States)
                    {
                        StateControl stateControl = new StateControl();
                        states[state] = stateControl;
                        stateControl.Parent = this;
                        stateControl.State = state;
                    }
                    foreach (DataDictionary.Rules.Transition transition in stateMachine.Transitions)
                    {
                        TransitionControl transitionControl = new TransitionControl();
                        transitions[transition] = transitionControl;
                        transitionControl.Parent = this;
                        transitionControl.Transition = transition;
                    }
                    UpdateTransitionPosition();
                }
            }
            finally
            {
                refreshingControl = false;
                ResumeLayout(true);
            }

            Refresh();
        }
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="control"></param>
 public TransitionEditor(TransitionControl control)
 {
     this.control = control;
 }
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="control"></param>
 public TransitionEditor(TransitionControl control)
 {
     this.control = control;
 }
 void StatePanel_MouseUp(object sender, MouseEventArgs e)
 {
     changingTransition      = null;
     chaningTransitionAction = ChangeAction.None;
 }
 void StatePanel_MouseDown(object sender, MouseEventArgs e)
 {
     if (e.Button == System.Windows.Forms.MouseButtons.Left)
     {
         Point clickPoint = new Point(e.X, e.Y);
         foreach (TransitionControl transition in transitions.Values)
         {
             if (around(transition.StartLocation, clickPoint))
             {
                 changingTransition = transition;
                 changingTransition.Parent = this;   // I do not know why...
                 chaningTransitionAction = ChangeAction.InitialState;
                 break;
             }
             if (around(transition.TargetLocation, clickPoint))
             {
                 changingTransition = transition;
                 changingTransition.Parent = this;   // I do not know why...
                 chaningTransitionAction = ChangeAction.TargetState;
                 break;
             }
         }
     }
 }
 void StatePanel_MouseUp(object sender, MouseEventArgs e)
 {
     changingTransition = null;
     chaningTransitionAction = ChangeAction.None;
 }
        /// <summary>
        /// Ensures that two transitions do not overlap by computing an offset between the transitions
        /// </summary>
        private void ComputeTransitionArrowPosition()
        {
            List <TransitionControl> workingSet = new List <TransitionControl>();

            workingSet.AddRange(transitions.Values);

            while (workingSet.Count > 1)
            {
                TransitionControl t1 = workingSet[0];
                workingSet.Remove(t1);

                // Compute the set of transitions overlapping with t1
                List <TransitionControl> overlap = new List <TransitionControl>();
                overlap.Add(t1);
                foreach (TransitionControl t in workingSet)
                {
                    if (t.Transition.InitialState == t1.Transition.InitialState &&
                        t.Transition.TargetState == t1.Transition.TargetState)
                    {
                        overlap.Add(t);
                    }
                    else if ((t.Transition.InitialState == t1.Transition.TargetState &&
                              t.Transition.TargetState == t1.Transition.InitialState))
                    {
                        overlap.Add(t);
                    }
                }

                // Remove all transitions of this overlap class from the working set
                foreach (TransitionControl t in overlap)
                {
                    workingSet.Remove(t);
                }

                // Shift transitions of this overlap set if they are overlapping (that is, if the set size > 1)
                if (overlap.Count > 1)
                {
                    Point shift;        // the shift to be applied to the current transition
                    Point offset;       // the offset to apply on all transitions of this overlap set

                    double angle = overlap[0].Angle;
                    if ((angle > Math.PI / 4 && angle < 3 * Math.PI / 4) ||
                        (angle < -Math.PI / 4 && angle > -3 * Math.PI / 4))
                    {
                        // Horizontal shift
                        shift  = new Point(-(overlap.Count - 1) * HORIZONTAL_SHIFT_SIZE / 2, 0);
                        offset = new Point(HORIZONTAL_SHIFT_SIZE, 0);
                    }
                    else
                    {
                        // Vertical shift
                        shift  = new Point(0, -(overlap.Count - 1) * VERTICAL_SHIFT_SIZE / 2);
                        offset = new Point(0, VERTICAL_SHIFT_SIZE);
                    }

                    int i = 0;
                    foreach (TransitionControl transition in overlap)
                    {
                        transition.Offset = shift;
                        shift.Offset(offset);

                        if (transition.TargetStateControl == null)
                        {
                            transition.EndOffset = new Point(0, VERTICAL_SHIFT_SIZE * i / 2);
                        }
                        i = i + 1;
                    }
                }
            }
        }