/// <summary>
 /// Creates an instance of StateMachine Activity with an initial State.
 /// </summary>
 /// <param name="target">Not used.</param>
 /// <returns>An instance of StateMachine Activity with an initial State.</returns>
 public Activity Create(DependencyObject target)
 {
     State state = new State()
     {
         DisplayName = StateContainerEditor.DefaultStateDisplayName + "1"
     };
     return new StateMachine()
     {
         States = { state },
         InitialState = state
     };
 }
Пример #2
0
        /// <summary>
        /// The create state machine.
        /// </summary>
        /// <returns>
        /// </returns>
        private static StateMachine CreateStateMachine()
        {
            var state1 = new State { DisplayName = "state1" };
            var state2 = new State { DisplayName = "state2" };
            var state3 = new State { IsFinal = true, DisplayName = "state3" };
            state1.Transitions.Add(new Transition { To = state2 });
            state2.Transitions.Add(new Transition { To = state3 });

            var stateMachine = new StateMachine { InitialState = state1, States = { state1, state2, state3 } };

            return stateMachine;
        }
        public void OnItemsPasted(List<object> itemsToPaste, List<object> metaData, Point pastePoint, WorkflowViewElement pastePointReference)
        {
            if (this.ModelItem.ItemType == typeof(State))
            {
                WorkflowViewElement view = VisualTreeUtils.FindVisualAncestor<WorkflowViewElement>(this);
                if (view != null)
                {
                    StateContainerEditor container = (StateContainerEditor)DragDropHelper.GetCompositeView(view);
                    container.OnItemsPasted(itemsToPaste, metaData, pastePoint, pastePointReference);
                }

                return;
            }

            if (itemsToPaste.Count == 1 && itemsToPaste.First() is Transition)
            {
                if (metaData == null || metaData.Count != 1 || !(metaData.First() is string))
                {
                    ShowMessageBox(SR.PasteTransitionWithoutDestinationState);
                    return;
                }

                ModelItem destinationState = FindState(metaData.First() as string);

                if (destinationState == null)
                {
                    ShowMessageBox(SR.PasteTransitionWithoutDestinationState);
                    return;
                }

                this.PopulateVirtualizingContainer(destinationState);

                ModelItem[] selectedItems = this.Context.Items.GetValue<Selection>().SelectedObjects.ToArray();
                string errorMessage;
                if (!CanPasteTransition(destinationState, out errorMessage, selectedItems))
                {
                    ShowMessageBox(errorMessage);
                    return;
                }

                Transition pastedTransition = itemsToPaste.First() as Transition;
                Fx.Assert(pastedTransition != null, "Copied Transition should not be null.");

                using (EditingScope es = (EditingScope)this.ModelItem.BeginEdit(System.Activities.Presentation.SR.PropertyChangeEditingScopeDescription))
                {
                    string displayName = pastedTransition.DisplayName;
                    bool isFirst = true;
                    foreach (ModelItem selectedItem in selectedItems)
                    {
                        if (!isFirst)
                        {
                            StringReader reader = new StringReader(XamlServices.Save(pastedTransition));
                            pastedTransition = (Transition)XamlServices.Load(reader);
                        }

                        ModelItem transitionModelItem = this.Context.Services.GetRequiredService<ModelTreeManager>().WrapAsModelItem(pastedTransition);
                        ModelItem sourceState = selectedItem;
                        sourceState.Properties[StateDesigner.TransitionsPropertyName].Collection.Add(transitionModelItem);
                        transitionModelItem.Properties[TransitionDesigner.ToPropertyName].SetValue(destinationState);

                        if (isFirst)
                        {
                            this.ViewStateService.RemoveViewState(transitionModelItem, ConnectorLocationViewStateKey);
                            this.ViewStateService.RemoveViewState(transitionModelItem, SrcConnectionPointIndexStateKey);
                            this.ViewStateService.RemoveViewState(transitionModelItem, DestConnectionPointIndexStateKey);
                            isFirst = false;
                        }
                    }

                    es.Complete();
                }
            }
            else
            {
                List<ModelItem> modelItemsPasted = new List<ModelItem>();
                List<State> states = new List<State>();

                foreach (object obj in itemsToPaste)
                {
                    State state;
                    if (obj is FinalState)
                    {
                        state = new State() { DisplayName = DefaultFinalStateDisplayName, IsFinal = true };
                    }
                    else
                    {
                        state = (State)obj;
                        if (state.DisplayName == null)
                        {
                            state.DisplayName = DefaultStateDisplayName;
                        }
                    }
                    states.Add(state);
                }

                RemoveDanglingTransitions(states);

                using (EditingScope es = (EditingScope)this.ModelItem.BeginEdit(
                    System.Activities.Presentation.SR.CollectionAddEditingScopeDescription))
                {
                    // Fix 157591 by storing the height and width of the container "before" the new states are added to the
                    // panel, and group the insertion inside one editing scope - such that Undo will also restore the 
                    // size of the StateMachineContainer to pre-insert size.
                    StoreShapeSizeWithUndoRecursively(this.ModelItem);

                    foreach (State state in states)
                    {
                        ModelItem stateModelItem =
                            (this.ModelItem.ItemType == typeof(StateMachine)) ?
                            this.ModelItem.Properties[StateMachineDesigner.StatesPropertyName].Collection.Add(state) :
                            GetStateMachineModelItem(this.ModelItem).Properties[StateMachineDesigner.StatesPropertyName].Collection.Add(state);
                        modelItemsPasted.Add(stateModelItem);
                    }

                    es.Complete();
                }

                if (modelItemsPasted.Count > 0)
                {
                    // translate location view states to be in the coordinate system of the pasting target
                    Fx.Assert(this.ModelItem.ItemType == typeof(StateMachine), "Only StateMachine contain the StateContainerEditor.");

                    this.UpdateLocationViewStatesByMetaData(modelItemsPasted, metaData, this);

                    if (pastePoint.X > 0 && pastePoint.Y > 0)
                    {
                        if (pastePointReference != null)
                        {
                            pastePoint = pastePointReference.TranslatePoint(pastePoint, this.panel);
                            pastePoint.X = pastePoint.X < 0 ? 0 : pastePoint.X;
                            pastePoint.Y = pastePoint.Y < 0 ? 0 : pastePoint.Y;
                        }
                        this.UpdateLocationViewStatesByPoint(modelItemsPasted, pastePoint);
                    }
                    // If paste point is not available, paste the items to the top left corner.
                    else
                    {
                        this.UpdateLocationViewStatesToAvoidOverlap(modelItemsPasted);
                    }
                }

                this.Dispatcher.BeginInvoke(() =>
                {
                    if (modelItemsPasted.Count > 0 && modelItemsPasted[0] != null)
                    {
                        Keyboard.Focus(modelItemsPasted[0].View as IInputElement);
                    }
                    this.Context.Items.SetValue(new Selection(modelItemsPasted));
                },
                DispatcherPriority.ApplicationIdle
                );
            }
        }
Пример #4
0
 /// <summary>
 ///   Creates a StateMachine that will fault
 /// </summary>
 /// <returns> A StateMachine </returns>
 internal static Activity CreateStateMachineThatFaults()
 {
     var finalState = new State { DisplayName = "FinalState", IsFinal = true };
     var initialState = new State
         {
             DisplayName = "Initial State",
             Entry = new Throw { Exception = new InArgument<Exception>(ctx => new InvalidOperationException()) },
             Transitions = {
                              new Transition { To = finalState }
                           }
         };
     return new StateMachine { InitialState = initialState, States = { initialState, finalState } };
 }
Пример #5
0
        /// <summary>
        /// Creates a large StateMachine
        /// </summary>
        /// <param name="stateCount">
        /// The state Count. 
        /// </param>
        /// <returns>
        /// A StateMachine 
        /// </returns>
        internal static Activity CreateLargeStateMachine(int stateCount)
        {
            var initialState = new State { DisplayName = "State1", };

            var largeStateMachine = new StateMachine { InitialState = initialState, States = { initialState } };

            var prevState = initialState;
            for (var i = 1; i < stateCount; i++)
            {
                var state = new State { DisplayName = string.Format("State{0}", i + 1), IsFinal = i == stateCount - 1 };
                largeStateMachine.States.Add(state);
                prevState.Transitions.Add(new Transition { DisplayName = string.Format("T{0}", i), To = state });
                prevState = state;
            }

            return largeStateMachine;
        }
Пример #6
0
 public MyState(XElement e)
 {
     _elem = e;
     
     _state = new State();
     _state.DisplayName = _elem.Attribute("name").Value;
 }