File: SetState.cs

package info (click to toggle)
mono 4.6.2.7%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (193 lines) | stat: -rw-r--r-- 7,727 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
namespace System.Workflow.Activities
{
    using System;
    using System.Text;
    using System.Reflection;
    using System.Collections;
    using System.CodeDom;
    using System.ComponentModel;
    using System.ComponentModel.Design;
    using System.Diagnostics;
    using System.Drawing;
    using System.Drawing.Design;
    using System.Security;
    using System.Security.Permissions;
    using System.Workflow.ComponentModel;
    using System.Workflow.ComponentModel.Design;
    using System.Collections.Generic;
    using System.Workflow.ComponentModel.Compiler;
    using System.Workflow.Activities.Common;

    [SRDescription(SR.SetStateActivityDescription)]
    [ToolboxItem(typeof(ActivityToolboxItem))]
    [Designer(typeof(SetStateDesigner), typeof(IDesigner))]
    [ToolboxBitmap(typeof(SetStateActivity), "Resources.SetStateActivity.png")]
    [ActivityValidator(typeof(SetStateValidator))]
    [SRCategory(SR.Standard)]
    [System.Runtime.InteropServices.ComVisible(false)]
    [Obsolete("The System.Workflow.* types are deprecated.  Instead, please use the new types from System.Activities.*")]
    public sealed class SetStateActivity : Activity
    {
        internal const string TargetStateNamePropertyName = "TargetStateName";

        //metadata property
        public static readonly DependencyProperty TargetStateNameProperty = DependencyProperty.Register(TargetStateNamePropertyName, typeof(string), typeof(SetStateActivity), new PropertyMetadata("", DependencyPropertyOptions.Metadata, new ValidationOptionAttribute(ValidationOption.Optional)));

        #region Constructors

        public SetStateActivity()
        {
        }

        public SetStateActivity(string name)
            : base(name)
        {
        }

        #endregion
        [SRDescription(SR.TargetStateDescription)]
        [Editor(typeof(StateDropDownEditor), typeof(UITypeEditor))]
        [DefaultValue((string)null)]
        public string TargetStateName
        {
            get
            {
                return base.GetValue(TargetStateNameProperty) as string;
            }
            set
            {
                base.SetValue(TargetStateNameProperty, value);
            }
        }

        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            if (executionContext == null)
                throw new ArgumentNullException("executionContext");

            StateActivity parentState = StateMachineHelpers.FindEnclosingState(executionContext.Activity);
            StateActivity rootState = StateMachineHelpers.GetRootState(parentState);
            StateMachineExecutionState executionState = StateMachineExecutionState.Get(rootState);
            executionState.NextStateName = this.TargetStateName;
            return ActivityExecutionStatus.Closed;
        }
    }

    [System.Runtime.InteropServices.ComVisible(false)]
    internal sealed class SetStateValidator : ActivityValidator
    {
        public override ValidationErrorCollection Validate(ValidationManager manager, object obj)
        {
            ValidationErrorCollection validationErrors = new ValidationErrorCollection(base.Validate(manager, obj));

            SetStateActivity setState = obj as SetStateActivity;
            if (setState == null)
                throw new ArgumentException(SR.GetString(SR.Error_UnexpectedArgumentType, typeof(StateActivity).FullName), "obj");

            if (!SetStateContainment.Validate(setState, validationErrors))
                return validationErrors; // could not find a valid parent

            if (String.IsNullOrEmpty(setState.TargetStateName))
            {
                validationErrors.Add(new ValidationError(
                    SR.GetString(SR.Error_PropertyNotSet, SetStateActivity.TargetStateNamePropertyName),
                    ErrorNumbers.Error_PropertyNotSet, false,
                    SetStateActivity.TargetStateNamePropertyName));
            }
            else
            {
                StateActivity enclosingState = StateMachineHelpers.FindEnclosingState(setState);
                Debug.Assert(enclosingState != null); // this should be caught by the SetStateContainment.Validate call above

                StateActivity rootState = StateMachineHelpers.GetRootState(enclosingState);

                StateActivity targetActivity = StateMachineHelpers.FindStateByName(
                    rootState,
                    setState.TargetStateName);
                StateActivity targetState = targetActivity as StateActivity;
                if (targetState == null)
                {
                    validationErrors.Add(new ValidationError(SR.GetError_SetStateMustPointToAState(), ErrorNumbers.Error_SetStateMustPointToAState, false, SetStateActivity.TargetStateNamePropertyName));
                }
                else
                {
                    if (!StateMachineHelpers.IsLeafState(targetState))
                    {
                        validationErrors.Add(new ValidationError(SR.GetError_SetStateMustPointToALeafNodeState(), ErrorNumbers.Error_SetStateMustPointToALeafNodeState, false, SetStateActivity.TargetStateNamePropertyName));
                    }
                }
            }

            return validationErrors;
        }

        #region SetStateContainement

        private class SetStateContainment
        {
            private bool validParentFound = true;
            private bool validParentStateFound;

            private SetStateContainment()
            {
            }

            public static bool Validate(SetStateActivity setState, ValidationErrorCollection validationErrors)
            {
                SetStateContainment containment = new SetStateContainment();
                ValidateContainment(containment, setState);

                if (!containment.validParentFound ||
                    !containment.validParentStateFound)
                {
                    validationErrors.Add(new ValidationError(SR.GetError_SetStateOnlyWorksOnStateMachineWorkflow(), ErrorNumbers.Error_SetStateOnlyWorksOnStateMachineWorkflow));
                    return false;
                }
                return true;
            }

            private static void ValidateContainment(SetStateContainment containment, Activity activity)
            {
                Debug.Assert(activity != null);
                if (activity.Parent == null || activity.Parent == activity)
                {
                    containment.validParentFound = false;
                    return;
                }

                if (SetStateValidator.IsValidContainer(activity.Parent))
                {
                    ValidateParentState(containment, activity.Parent);
                    return;
                }

                ValidateContainment(containment, activity.Parent);
            }

            private static void ValidateParentState(SetStateContainment containment, CompositeActivity activity)
            {
                Debug.Assert(activity != null);
                if (activity.Parent == null)
                    return;

                StateActivity state = activity.Parent as StateActivity;
                if (state != null)
                {
                    containment.validParentStateFound = true;
                    return;
                }

                ValidateParentState(containment, activity.Parent);
            }
        }

        #endregion

        #region Helper methods
        static internal bool IsValidContainer(CompositeActivity activity)
        {
            return (activity is EventDrivenActivity || activity is StateInitializationActivity);
        }
        #endregion
    }
}