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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
|
namespace System.Workflow.Activities
{
#region Imports
using System;
using System.Text;
using System.Reflection;
using System.Collections;
using System.CodeDom;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.ComponentModel.Design.Serialization;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.Activities.Common;
#endregion
[SRDescription(SR.EventHandlingScopeActivityDescription)]
[ToolboxItem(typeof(ActivityToolboxItem))]
[ToolboxBitmap(typeof(EventHandlingScopeActivity), "Resources.Sequence.png")]
[ActivityValidator(typeof(EventHandlingScopeValidator))]
[Designer(typeof(EventHandlingScopeDesigner), typeof(IDesigner))]
[Obsolete("The System.Workflow.* types are deprecated. Instead, please use the new types from System.Activities.*")]
public sealed class EventHandlingScopeActivity : CompositeActivity, IActivityEventListener<ActivityExecutionStatusChangedEventArgs>
{
public EventHandlingScopeActivity()
{
}
public EventHandlingScopeActivity(string name)
: base(name)
{
}
protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
{
if (executionContext == null)
throw new ArgumentNullException("executionContext");
Activity bodyActivity = this.BodyActivity;
if (bodyActivity == null)
return ActivityExecutionStatus.Closed;
// run EventHandlers
EventHandlersActivity eventHandlers = this.EventHandlersActivity;
if (eventHandlers != null)
{
eventHandlers.RegisterForStatusChange(Activity.ClosedEvent, this);
executionContext.ExecuteActivity(eventHandlers);
}
// run body
bodyActivity.RegisterForStatusChange(Activity.ClosedEvent, this);
executionContext.ExecuteActivity(bodyActivity);
// return the status
return this.ExecutionStatus;
}
protected override ActivityExecutionStatus Cancel(ActivityExecutionContext executionContext)
{
if (executionContext == null)
throw new ArgumentNullException("executionContext");
Activity bodyActivity = this.BodyActivity;
EventHandlersActivity eventHandlers = this.EventHandlersActivity;
if (bodyActivity == null && eventHandlers == null)
return ActivityExecutionStatus.Closed;
bool cancelScheduled = false;
// check the status of body
if (bodyActivity != null && bodyActivity.ExecutionStatus == ActivityExecutionStatus.Executing)
{
executionContext.CancelActivity(bodyActivity);
cancelScheduled = true;
}
//Check the status of EventHandlers
if (eventHandlers != null && eventHandlers.ExecutionStatus == ActivityExecutionStatus.Executing)
{
executionContext.CancelActivity(eventHandlers);
cancelScheduled = true;
}
if (cancelScheduled ||
(bodyActivity != null && (bodyActivity.ExecutionStatus == ActivityExecutionStatus.Faulting ||
bodyActivity.ExecutionStatus == ActivityExecutionStatus.Canceling)) ||
(eventHandlers != null && (eventHandlers.ExecutionStatus == ActivityExecutionStatus.Faulting ||
eventHandlers.ExecutionStatus == ActivityExecutionStatus.Canceling))
)
{
return this.ExecutionStatus;
}
return ActivityExecutionStatus.Closed;
}
#region IActivityEventListener<ActivityExecutionStatusChangedEventArgs> Members
void IActivityEventListener<ActivityExecutionStatusChangedEventArgs>.OnEvent(object sender, ActivityExecutionStatusChangedEventArgs e)
{
ActivityExecutionContext context = sender as ActivityExecutionContext;
if (context == null)
throw new ArgumentException();
e.Activity.UnregisterForStatusChange(Activity.ClosedEvent, this);
if (e.Activity is EventHandlersActivity)
{
if (this.BodyActivity.ExecutionStatus == ActivityExecutionStatus.Closed)
{
context.CloseActivity();
}
//else Eventhandlers faulted, let exception propagate up.
}
else
{
EventHandlersActivity eventHandlers = this.EventHandlersActivity;
if (eventHandlers == null || eventHandlers.ExecutionStatus == ActivityExecutionStatus.Closed)
{
context.CloseActivity();
}
else
{
eventHandlers.UnsubscribeAndClose();
}
}
}
#endregion
internal EventHandlersActivity EventHandlersActivity
{
get
{
EventHandlersActivity eventHandlers = null;
foreach (Activity child in this.EnabledActivities)
{
if (child is EventHandlersActivity)
eventHandlers = child as EventHandlersActivity;
}
return eventHandlers;
}
}
internal Activity BodyActivity
{
get
{
Activity body = null;
foreach (Activity child in this.EnabledActivities)
{
if (!(child is EventHandlersActivity))
body = child;
}
return body;
}
}
#region Dynamic Update Methods
[NonSerialized]
private bool eventHandlersRemovedInDynamicUpdate = false;
[NonSerialized]
private bool bodyActivityRemovedInDynamicUpdate = false;
protected override void OnActivityChangeRemove(ActivityExecutionContext executionContext, Activity removedActivity)
{
base.OnActivityChangeRemove(executionContext, removedActivity);
if (removedActivity is EventHandlersActivity)
eventHandlersRemovedInDynamicUpdate = true;
else
bodyActivityRemovedInDynamicUpdate = true;
}
protected override void OnWorkflowChangesCompleted(ActivityExecutionContext executionContext)
{
base.OnWorkflowChangesCompleted(executionContext);
switch (this.ExecutionStatus)
{
case ActivityExecutionStatus.Executing:
if (bodyActivityRemovedInDynamicUpdate)
{
if (EventHandlersActivity == null || EventHandlersActivity.ExecutionStatus == ActivityExecutionStatus.Closed)
executionContext.CloseActivity();
else if (EventHandlersActivity.ExecutionStatus == ActivityExecutionStatus.Executing)
EventHandlersActivity.UnsubscribeAndClose();
}
if (eventHandlersRemovedInDynamicUpdate)
{
if (BodyActivity == null || BodyActivity.ExecutionStatus == ActivityExecutionStatus.Closed)
executionContext.CloseActivity();
}
break;
default:
break;
}
eventHandlersRemovedInDynamicUpdate = false;
bodyActivityRemovedInDynamicUpdate = false;
}
#endregion
}
#region Class EventHandlingScopeValidator
internal sealed class EventHandlingScopeValidator : CompositeActivityValidator
{
public override ValidationErrorCollection Validate(ValidationManager manager, object obj)
{
ValidationErrorCollection validationErrors = base.Validate(manager, obj);
EventHandlingScopeActivity compositeActivity = obj as EventHandlingScopeActivity;
if (compositeActivity == null)
throw new ArgumentException(SR.GetString(SR.Error_UnexpectedArgumentType, typeof(EventHandlingScopeActivity).FullName), "obj");
//we only allow one activity to be inserted
int childCount = 0;
int eventHandlersCount = 0;
foreach (Activity child in compositeActivity.EnabledActivities)
{
if (child is EventHandlersActivity)
eventHandlersCount++;
else
childCount++;
}
// check if more than two activities inside the collection
if (childCount > 1)
validationErrors.Add(new ValidationError(SR.GetString(SR.Error_MoreThanTwoActivitiesInEventHandlingScope, compositeActivity.QualifiedName), ErrorNumbers.Error_MoreThanTwoActivitiesInEventHandlingScope));
// check if more than one EventHandlers
if (eventHandlersCount > 1)
validationErrors.Add(new ValidationError(SR.GetString(SR.Error_MoreThanOneEventHandlersDecl, compositeActivity.GetType().Name), ErrorNumbers.Error_ScopeMoreThanOneEventHandlersDecl));
return validationErrors;
}
public override ValidationError ValidateActivityChange(Activity activity, ActivityChangeAction action)
{
if (activity == null)
throw new ArgumentNullException("activity");
if (action == null)
throw new ArgumentNullException("action");
if (activity.ExecutionStatus != ActivityExecutionStatus.Initialized &&
activity.ExecutionStatus != ActivityExecutionStatus.Executing &&
activity.ExecutionStatus != ActivityExecutionStatus.Closed)
{
return new ValidationError(SR.GetString(SR.Error_DynamicActivity2, activity.QualifiedName, Enum.GetName(typeof(ActivityExecutionStatus), activity.ExecutionStatus), activity.GetType().FullName), ErrorNumbers.Error_DynamicActivity2);
}
if (activity.ExecutionStatus == ActivityExecutionStatus.Executing && action is AddedActivityAction)
{
return new ValidationError(SR.GetString(SR.Error_DynamicActivity3, activity.QualifiedName, Enum.GetName(typeof(ActivityExecutionStatus), activity.ExecutionStatus), activity.GetType().FullName), ErrorNumbers.Error_DynamicActivity2);
}
return null;
}
}
#endregion
#region Class EventHandlingScopeDesigner
[ActivityDesignerTheme(typeof(EventHandlingScopeActivityDesignerTheme))]
internal sealed class EventHandlingScopeDesigner : SequentialActivityDesigner
{
#region Properties and Methods
public override bool CanExpandCollapse
{
get
{
return false;
}
}
#endregion
public override bool CanInsertActivities(HitTestInfo insertLocation, System.Collections.ObjectModel.ReadOnlyCollection<Activity> activitiesToInsert)
{
//we only allow one activity to be inserted
int childCount = 0;
foreach (Activity child in ((EventHandlingScopeActivity)this.Activity).Activities)
{
if (!Helpers.IsFrameworkActivity(child) &&
!(child is EventHandlersActivity))
childCount++;
}
if (childCount > 0)
return false;
return base.CanInsertActivities(insertLocation, activitiesToInsert);
}
}
#endregion
#region EventHandlingScopeActivityDesignerTheme
internal sealed class EventHandlingScopeActivityDesignerTheme : CompositeDesignerTheme
{
public EventHandlingScopeActivityDesignerTheme(WorkflowTheme theme)
: base(theme)
{
this.ShowDropShadow = false;
this.ConnectorStartCap = LineAnchor.None;
this.ConnectorEndCap = LineAnchor.ArrowAnchor;
this.ForeColor = Color.FromArgb(0xFF, 0x00, 0x00, 0x00);
this.BorderColor = Color.FromArgb(0xFF, 0xE0, 0xE0, 0xE0);
this.BorderStyle = DashStyle.Dash;
this.BackColorStart = Color.FromArgb(0x00, 0x00, 0x00, 0x00);
this.BackColorEnd = Color.FromArgb(0x00, 0x00, 0x00, 0x00);
}
}
#endregion
}
|