1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //----------------------------------------------------------------
4 namespace System.Workflow.Activities
5 {
6     using System;
7     using System.Xml;
8     using System.ComponentModel;
9     using System.ComponentModel.Design;
10     using System.ComponentModel.Design.Serialization;
11     using System.Reflection;
12     using System.Collections;
13     using System.Collections.Generic;
14     using System.Collections.Specialized;
15     using System.Diagnostics;
16     using System.Runtime.Serialization;
17     using System.Workflow.ComponentModel;
18     using System.Workflow.ComponentModel.Design;
19     using System.Workflow.ComponentModel.Serialization;
20     using System.Workflow.Runtime;
21     using System.Globalization;
22 
23     internal sealed class ChannelTokenTypeConverter : ExpandableObjectConverter
24     {
CanConvertFrom(ITypeDescriptorContext context, Type sourceType)25         public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
26         {
27             return (sourceType == typeof(string));
28         }
29 
CanConvertTo(ITypeDescriptorContext context, Type destinationType)30         public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
31         {
32             return (destinationType == typeof(string));
33         }
34 
ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)35         public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
36         {
37             object convertedValue = null;
38             string endpointName = value as String;
39             if (!String.IsNullOrEmpty(endpointName))
40             {
41                 foreach (object obj in GetStandardValues(context))
42                 {
43                     ChannelToken endpoint = obj as ChannelToken;
44                     if (endpoint != null && endpoint.Name == endpointName)
45                     {
46                         convertedValue = endpoint;
47                         break;
48                     }
49                 }
50 
51                 if (convertedValue == null)
52                 {
53                     convertedValue = new ChannelToken(endpointName);
54                 }
55             }
56 
57             return convertedValue;
58         }
59 
ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)60         public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
61         {
62             object convertedValue = null;
63             ChannelToken endpoint = value as ChannelToken;
64             if (destinationType == typeof(string) && endpoint != null)
65             {
66                 convertedValue = endpoint.Name;
67             }
68             return convertedValue;
69         }
70 
GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)71         public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
72         {
73             PropertyDescriptorCollection properties = base.GetProperties(context, value, attributes);
74             ArrayList props = new ArrayList(properties);
75             return new PropertyDescriptorCollection((PropertyDescriptor[]) props.ToArray(typeof(PropertyDescriptor)));
76         }
77 
GetStandardValues(ITypeDescriptorContext context)78         public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
79         {
80             ArrayList values = new ArrayList();
81             Activity activity = context.Instance as Activity;
82             if (activity != null)
83             {
84                 foreach (Activity preceedingActivity in GetPreceedingActivities(activity))
85                 {
86                     PropertyDescriptor endpointProperty = TypeDescriptor.GetProperties(preceedingActivity)["ChannelToken"] as PropertyDescriptor;
87                     if (endpointProperty != null)
88                     {
89                         ChannelToken endpoint = endpointProperty.GetValue(preceedingActivity) as ChannelToken;
90                         if (endpoint != null && !values.Contains(endpoint))
91                         {
92                             values.Add(endpoint);
93                         }
94                     }
95                 }
96             }
97             return new StandardValuesCollection(values);
98         }
99 
GetStandardValuesExclusive(ITypeDescriptorContext context)100         public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
101         {
102             return false;
103         }
104 
GetStandardValuesSupported(ITypeDescriptorContext context)105         public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
106         {
107             return true;
108         }
109 
GetContainedActivities(CompositeActivity activity)110         private IEnumerable GetContainedActivities(CompositeActivity activity)
111         {
112             if (!activity.Enabled)
113             {
114                 yield break;
115             }
116 
117             foreach (Activity containedActivity in activity.Activities)
118             {
119                 if (containedActivity.Enabled)
120                 {
121                     yield return containedActivity;
122 
123                     if (containedActivity is CompositeActivity)
124                     {
125                         foreach (Activity nestedActivity in GetContainedActivities((CompositeActivity) containedActivity))
126                         {
127                             if (nestedActivity.Enabled)
128                             {
129                                 yield return nestedActivity;
130                             }
131                         }
132                     }
133                 }
134             }
135             yield break;
136         }
137 
GetPreceedingActivities(Activity startActivity)138         private IEnumerable GetPreceedingActivities(Activity startActivity)
139         {
140             Activity currentActivity = null;
141             Stack<Activity> activityStack = new Stack<Activity>();
142             activityStack.Push(startActivity);
143 
144             while ((currentActivity = activityStack.Pop()) != null)
145             {
146                 if (currentActivity.Parent != null)
147                 {
148                     foreach (Activity siblingActivity in currentActivity.Parent.Activities)
149                     {
150                         if (siblingActivity == currentActivity)
151                         {
152                             continue;
153                         }
154 
155                         if (siblingActivity.Enabled)
156                         {
157                             yield return siblingActivity;
158 
159                             if (siblingActivity is CompositeActivity)
160                             {
161                                 foreach (Activity containedActivity in GetContainedActivities((CompositeActivity) siblingActivity))
162                                 {
163                                     yield return containedActivity;
164                                 }
165                             }
166                         }
167                     }
168                 }
169                 activityStack.Push(currentActivity.Parent);
170             }
171             yield break;
172         }
173     }
174 }
175