Mono exception to SocketException
[mono.git] / mcs / class / referencesource / System.Activities.Presentation / System.Activities.Core.Presentation / System / ServiceModel / Activities / Presentation / CorrelationInitializerDesigner.xaml.cs
1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //----------------------------------------------------------------
4 namespace System.ServiceModel.Activities.Presentation
5 {
6     using System.Activities;
7     using System.Activities.Presentation;
8     using System.Activities.Presentation.Model;
9     using System.Activities.Presentation.View;
10     using System.Collections;
11     using System.Collections.ObjectModel;
12     using System.Collections.Specialized;
13     using System.Runtime;
14     using System.Windows;
15     using System.Windows.Input;
16     using System.Globalization;
17     using System.Collections.Generic;
18
19     internal partial class CorrelationInitializerDesigner 
20     {
21         DataGridHelper correlationInitializerDGHelper;
22
23         const string CorrelationInitializersKey = "CorrelationInitializers";
24
25         public static readonly DependencyProperty ActivityProperty = DependencyProperty.Register(
26             "Activity", 
27             typeof(ModelItem), 
28             typeof(CorrelationInitializerDesigner), 
29             new UIPropertyMetadata(OnActivityChanged));
30
31         static readonly ICommand AddNewInitializerCommand = new RoutedCommand();
32
33         public CorrelationInitializerDesigner()
34         {
35             this.InitializeComponent();
36         }
37
38         public ModelItem Activity
39         {
40             get { return (ModelItem)GetValue(ActivityProperty); }
41             set { SetValue(ActivityProperty, value); }
42         }
43
44         ModelItemCollection CorrelationInitializers
45         {
46             get { return this.Activity.Properties[CorrelationInitializersKey].Collection; }
47         }
48
49         protected override void OnInitialized(EventArgs args)
50         {
51             base.OnInitialized(args);
52
53             this.CommandBindings.Add(new CommandBinding(AddNewInitializerCommand, this.OnAddNewInitializerExecuted));
54
55             //create data grid helper
56             this.correlationInitializerDGHelper = new DataGridHelper(this.correlationInitializers, this);
57             this.correlationInitializerDGHelper.ShowValidationErrorAsToolTip = true;
58             this.correlationInitializerDGHelper.AddNewRowContent = (string)this.FindResource("addNewInitializer");
59             this.correlationInitializerDGHelper.AddNewRowCommand = CorrelationInitializerDesigner.AddNewInitializerCommand;            
60         }
61
62         void OnAddNewInitializerExecuted(object sender, ExecutedRoutedEventArgs e)
63         {
64             var initializer = (CanUseQueryCorrelationInitializer(this.Activity) ? 
65                 (CorrelationInitializer)new QueryCorrelationInitializer() : (CorrelationInitializer)new ContextCorrelationInitializer());
66             var result = this.CorrelationInitializers.Add(initializer);
67             var wrapper = new CorrelationInitializerEntry(result);
68             this.correlationInitializerDGHelper.Source<IList>().Add(wrapper);
69             this.correlationInitializerDGHelper.BeginRowEdit(wrapper);
70         }
71
72         static bool CanUseQueryCorrelationInitializer(ModelItem activity)
73         {
74             bool result = true;
75             if (null != activity)
76             {
77                 if (activity.IsAssignableFrom<Receive>() || activity.IsAssignableFrom<Send>())
78                 {
79                     ModelItem serializationOption;
80                     activity.TryGetPropertyValue(out serializationOption, "SerializerOption");
81                     result = SerializerOption.XmlSerializer != (SerializerOption)serializationOption.GetCurrentValue();
82                 }
83                 else if (activity.IsAssignableFrom<SendReply>() || activity.IsAssignableFrom<ReceiveReply>())
84                 {
85                     ModelItem request;
86                     activity.TryGetPropertyValue(out request, "Request");
87                     result = CanUseQueryCorrelationInitializer(request);
88                 }
89             }
90             return result;
91         }
92
93         void OnDataCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
94         {
95             switch (e.Action)
96             {
97                 case NotifyCollectionChangedAction.Remove:
98                     foreach (CorrelationInitializerEntry entry in e.OldItems)
99                     {
100                         this.CorrelationInitializers.Remove(entry.ReflectedObject);
101                         entry.Dispose();
102                     }
103                     break;
104             }
105         }
106
107
108         internal void CleanupObjectMap()
109         {
110         }
111
112         void OnActivityChanged()
113         {
114             if (null != this.Activity)
115             {
116                 var source = new ObservableCollection<CorrelationInitializerEntry>();
117
118                 foreach (var entry in this.CorrelationInitializers)
119                 {
120                     var wrapper = new CorrelationInitializerEntry(entry);
121                     source.Add(wrapper);
122                 }
123
124                 this.correlationInitializers.ItemsSource = source;
125                 source.CollectionChanged += this.OnDataCollectionChanged;
126             }
127         }
128
129         static void OnActivityChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
130         {
131             var activity = e.NewValue as ModelItem;
132             if (null != activity && !activity.IsMessagingActivity())
133             {
134                 throw FxTrace.Exception.AsError(new NotSupportedException(activity.ItemType.FullName));
135             }
136             ((CorrelationInitializerDesigner)sender).OnActivityChanged();
137         }
138
139         void OnEditingControlLoaded(object sender, RoutedEventArgs args)
140         {
141             DataGridHelper.OnEditingControlLoaded(sender, args);
142         }
143
144         void OnEditingControlUnloaded(object sender, RoutedEventArgs args)
145         {
146             DataGridHelper.OnEditingControlUnloaded(sender, args);
147         }
148
149         internal sealed class CorrelationInitializerEntry : DesignObjectWrapper
150         {
151             internal static readonly string HandleProperty = "CorrelationHandle";
152             internal static readonly string CorrelationTypeProperty = "CorrelationType";
153             internal static readonly string MessageQuerySetModelPropertyProperty = "MessageQuerySet";
154
155             static readonly string[] Properties = new string[] { HandleProperty, CorrelationTypeProperty, MessageQuerySetModelPropertyProperty };
156
157             #region Initialize type properties code
158             public static PropertyDescriptorData[] InitializeTypeProperties()
159             {
160                 return new PropertyDescriptorData[]
161                 {
162                     new PropertyDescriptorData()
163                     {
164                         PropertyName = HandleProperty,
165                         PropertyType = typeof(InArgument),
166                         PropertySetter = (instance, newValue) =>
167                             {
168                                 ((CorrelationInitializerEntry)instance).SetHandle(newValue);
169                             },
170                         PropertyGetter = (instance) => (((CorrelationInitializerEntry)instance).GetHandle()),
171                     },
172                     new PropertyDescriptorData()
173                     {
174                         PropertyName = CorrelationTypeProperty,
175                         PropertyType = typeof(Type),
176                         PropertyValidator = (instance, newValue, errors) => (((CorrelationInitializerEntry)instance).ValidateCorrelationType(newValue, errors)),
177                         PropertySetter = (instance, newValue) =>
178                             {
179                                 ((CorrelationInitializerEntry)instance).SetCorrelationType(newValue);                                
180                             },
181                         PropertyGetter = (instance) => (((CorrelationInitializerEntry)instance).GetCorrelationType()),
182                     },
183                     new PropertyDescriptorData()
184                     {
185                         PropertyName = MessageQuerySetModelPropertyProperty,
186                         PropertyType = typeof(ModelProperty),
187                         PropertyGetter = (instance) => (((CorrelationInitializerEntry)instance).GetMessageQuerySetModelProperty()),
188                     },
189                 };
190             }
191             #endregion
192
193
194             public CorrelationInitializerEntry()
195             {
196                 throw FxTrace.Exception.AsError(new NotSupportedException());
197             }
198
199             public CorrelationInitializerEntry(ModelItem initializer) : base(initializer)
200             {
201             }
202
203             protected override string AutomationId
204             {
205                 get { return ((ModelItemCollection)this.ReflectedObject.Parent).IndexOf(this.ReflectedObject).ToString(CultureInfo.InvariantCulture); }
206             }
207
208             internal object GetHandle()
209             {
210                 return this.ReflectedObject.Properties[HandleProperty].ComputedValue;
211             }
212
213             void SetHandle(object value)
214             {
215                 InArgument handle = (InArgument)(value is ModelItem ? ((ModelItem)value).GetCurrentValue() : value);
216                 this.ReflectedObject.Properties[HandleProperty].SetValue(handle);
217             }
218
219             internal Type GetCorrelationType()
220             {
221                 return this.ReflectedObject.ItemType;
222             }
223
224             void SetCorrelationType(object value)
225             {
226                 Type type = (Type)(value is ModelItem ? ((ModelItem)value).GetCurrentValue() : value);
227                 var source = (ModelItemCollection)this.ReflectedObject.Parent;
228                 int index = source.IndexOf(this.ReflectedObject);
229                 var oldInitalizer = (CorrelationInitializer)this.ReflectedObject.GetCurrentValue();
230                 var newInitializer = (CorrelationInitializer)Activator.CreateInstance(type);
231                 newInitializer.CorrelationHandle = oldInitalizer.CorrelationHandle;
232                 this.Dispose();
233                 source.RemoveAt(index);
234                 this.Initialize(source.Insert(index, newInitializer));
235                 this.RaisePropertyChangedEvent(MessageQuerySetModelPropertyProperty);
236             }
237
238             bool ValidateCorrelationType(object value, List<string> errors)
239             {
240                 Type type = (Type)(value is ModelItem ? ((ModelItem)value).GetCurrentValue() : value);
241                 var activity = this.ReflectedObject.Parent.Parent;
242                 if (typeof(QueryCorrelationInitializer).IsAssignableFrom(type) && !CorrelationInitializerDesigner.CanUseQueryCorrelationInitializer(activity))
243                 {
244                     errors.Add(System.Activities.Core.Presentation.SR.CorrelationInitializerNotSupported);
245                 }
246                 return 0 == errors.Count;
247             }
248
249             internal ModelProperty GetMessageQuerySetModelProperty()
250             {
251                 return this.ReflectedObject.Properties[MessageQuerySetModelPropertyProperty];
252             }
253         }
254     }
255 }