Mono exception to SocketException
[mono.git] / mcs / class / referencesource / System.Activities.Presentation / System.Activities.Core.Presentation / System / Activities / Core / Presentation / FlowDecisionDesigner.xaml.cs
1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //----------------------------------------------------------------
4
5 namespace System.Activities.Core.Presentation
6 {
7     using System.Activities.Presentation;
8     using System.Activities.Presentation.Annotations;
9     using System.Activities.Presentation.Metadata;
10     using System.Activities.Presentation.View;
11     using System.Activities.Presentation.View.OutlineView;
12     using System.Activities.Statements;
13     using System.ComponentModel;
14     using System.Windows;
15     using System.Windows.Automation.Peers;
16     using System.Windows.Controls;
17     using System.Windows.Data;
18     using System.Windows.Documents;
19     using System.Windows.Media;
20     using Microsoft.Activities.Presentation;
21
22     partial class FlowDecisionDesigner
23     {
24         public static readonly DependencyProperty ExpressionButtonVisibilityProperty =
25             DependencyProperty.Register("ExpressionButtonVisibility", typeof(Visibility), typeof(FlowDecisionDesigner));
26
27         public static readonly DependencyProperty ExpressionButtonColorProperty =
28             DependencyProperty.Register("ExpressionButtonColor", typeof(Brush), typeof(FlowDecisionDesigner));
29
30         static readonly DependencyProperty ShowAllConditionsProperty =
31             DependencyProperty.Register("ShowAllConditions", typeof(bool), typeof(FlowDecisionDesigner),
32             new UIPropertyMetadata(new PropertyChangedCallback(OnShowAllConditionsChanged)));
33
34         bool isPinned;
35         bool expressionShown = false;
36
37         private AnnotationManager annotationManager;
38
39         public FlowDecisionDesigner()
40         {
41             InitializeComponent();
42             this.Loaded += (sender, e) =>
43             {
44                 //UnRegistering because of 137896: Inside tab control multiple Loaded events happen without an Unloaded event.
45                 this.ModelItem.PropertyChanged -= OnModelItemPropertyChanged;
46                 this.ModelItem.PropertyChanged += OnModelItemPropertyChanged;
47                 OnModelItemPropertyChanged(this.ModelItem, new PropertyChangedEventArgs("Condition"));
48
49                 SetupBinding();
50
51                 if (this.Context.Services.GetService<DesignerConfigurationService>().TargetFrameworkName.IsLessThan45())
52                 {
53                     this.displayNameTextBox.IsReadOnly = true;
54                 }
55
56                 this.annotationManager.Initialize();
57             };
58             this.Unloaded += (sender, e) =>
59             {
60                 this.ModelItem.PropertyChanged -= OnModelItemPropertyChanged;
61                 this.annotationManager.Uninitialize();
62             };
63             this.MouseEnter += (sender, e) =>
64             {
65                 Update();
66             };
67             this.MouseLeave += (sender, e) =>
68             {
69                 Update();
70             };
71
72             this.InitializeAnnotation();
73         }
74
75         private void InitializeAnnotation()
76         {
77             this.annotationManager = new AnnotationManager(this);
78             this.annotationManager.AnnotationVisualProvider = new FlowDecisionDesignerAnnotationVisualProvider(this);
79         }
80
81         void SetupBinding()
82         {
83             Binding showAllConditionsBinding = new Binding();
84             showAllConditionsBinding.RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(FlowchartDesigner), 1);
85             showAllConditionsBinding.Path = new PropertyPath(FlowchartDesigner.ShowAllConditionsProperty);
86             showAllConditionsBinding.Mode = BindingMode.OneWay;
87
88             BindingOperations.SetBinding(this, FlowDecisionDesigner.ShowAllConditionsProperty, showAllConditionsBinding);
89         }
90
91         public Visibility ExpressionButtonVisibility
92         {
93             get { return (Visibility)GetValue(ExpressionButtonVisibilityProperty); }
94             set { SetValue(ExpressionButtonVisibilityProperty, value); }
95         }
96
97         public Brush ExpressionButtonColor
98         {
99             get { return (Brush)GetValue(ExpressionButtonColorProperty); }
100             set { SetValue(ExpressionButtonColorProperty, value); }
101         }
102
103         public bool ExpressionShown
104         {
105             get { return this.expressionShown; }
106         }
107
108         public static void RegisterMetadata(AttributeTableBuilder builder)
109         {
110             Type type = typeof(FlowDecision);
111             builder.AddCustomAttributes(type, new DesignerAttribute(typeof(FlowDecisionDesigner)));
112             builder.AddCustomAttributes(type, type.GetProperty("True"), BrowsableAttribute.No);
113             builder.AddCustomAttributes(type, type.GetProperty("False"), BrowsableAttribute.No);
114             builder.AddCustomAttributes(type, type.GetProperty("Condition"), new HidePropertyInOutlineViewAttribute());
115             builder.AddCustomAttributes(type, new FeatureAttribute(typeof(FlowDecisionLabelFeature)));
116             builder.AddCustomAttributes(type, new ActivityDesignerOptionsAttribute
117             {
118                 AllowDrillIn = false,
119                 OutlineViewIconProvider = (modelItem) =>
120                 {
121                     if (modelItem != null)
122                     {
123                         ResourceDictionary icons = EditorResources.GetIcons();
124                         if (icons.Contains("FlowDecisionIcon") && icons["FlowDecisionIcon"] is DrawingBrush)
125                         {
126                             return (DrawingBrush)icons["FlowDecisionIcon"];
127                         }
128                     }
129
130                     return null;
131                 }
132             });
133         }
134
135         protected override AutomationPeer OnCreateAutomationPeer()
136         {
137             return new FlowchartExpressionAutomationPeer(this, base.OnCreateAutomationPeer());
138         }
139
140
141         void OnExpressionButtonClicked(object sender, RoutedEventArgs e)
142         {
143             this.isPinned = !this.isPinned;
144             Update();
145         }
146
147         void OnModelItemPropertyChanged(object sender, PropertyChangedEventArgs e)
148         {
149             if (e.PropertyName == "Condition")
150             {
151                 Update();
152             }
153         }
154
155         void Update()
156         {
157             Activity expressionActivity = this.ModelItem.Properties["Condition"].ComputedValue as Activity;
158             string expressionString = ExpressionHelper.GetExpressionString(expressionActivity, this.ModelItem);
159             bool expressionSpecified = !string.IsNullOrEmpty(expressionString);
160             if (!expressionSpecified)
161             {
162                 this.isPinned = false;
163             }
164
165             this.ExpressionButtonVisibility = expressionSpecified ? Visibility.Visible : Visibility.Collapsed;
166
167             if (this.isPinned)
168             {
169                 this.ExpressionButtonColor = WorkflowDesignerColors.FlowchartExpressionButtonPressedBrush;
170             }
171             else if (this.IsMouseOver)
172             {
173                 this.ExpressionButtonColor = WorkflowDesignerColors.FlowchartExpressionButtonMouseOverBrush;
174             }
175             else
176             {
177                 this.ExpressionButtonColor = WorkflowDesignerColors.FlowchartExpressionButtonBrush;
178             }
179             expressionShown = false;
180             AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(this);
181             if (adornerLayer != null)
182             {
183                 Adorner[] adorners = adornerLayer.GetAdorners(this);
184                 if (adorners != null)
185                 {
186                     foreach (Adorner adorner in adorners)
187                     {
188                         if (adorner is FlowchartExpressionAdorner)
189                         {
190                             adornerLayer.Remove(adorner);
191                         }
192                     }
193                 }
194                 if ((this.IsMouseOver && expressionSpecified) || this.isPinned)
195                 {
196                     expressionShown = true;
197                     adornerLayer.Add(new FlowchartExpressionAdorner(this));
198                 }
199             }
200         }
201
202         static void OnShowAllConditionsChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
203         {
204             if (e.NewValue != DependencyProperty.UnsetValue)
205             {
206                 FlowDecisionDesigner designer = obj as FlowDecisionDesigner;
207                 designer.OnShowAllConditionsChanged((bool)e.NewValue);
208             }
209         }
210
211         void OnShowAllConditionsChanged(bool isOpen)
212         {
213             this.isPinned = isOpen;
214             Update();
215         }
216
217         protected internal override void OnEditAnnotation()
218         {
219             this.annotationManager.OnEditAnnotation();
220         }
221
222         private class FlowDecisionDesignerAnnotationVisualProvider : IAnnotationVisualProvider
223         {
224             private FlowDecisionDesigner designer;
225             private IAnnotationIndicator indicator;
226             private IFloatingAnnotation floatingAnnotation;
227             private IDockedAnnotation dockedAnnotation;
228
229             public FlowDecisionDesignerAnnotationVisualProvider(FlowDecisionDesigner designer)
230             {
231                 this.designer = designer;
232             }
233
234             public IAnnotationIndicator GetAnnotationIndicator()
235             {
236                 if (this.indicator == null)
237                 {
238                     this.indicator = new UIElementToAnnotationIndicatorAdapter(this.designer.defaultAnnotationIndicator);
239                 }
240
241                 return this.indicator;
242             }
243
244             public IFloatingAnnotation GetFloatingAnnotation()
245             {
246                 if (this.floatingAnnotation == null)
247                 {
248                     this.floatingAnnotation = new FloatingAnnotationView();
249                 }
250
251                 return this.floatingAnnotation;
252             }
253
254             public IDockedAnnotation GetDockedAnnotation()
255             {
256                 if (this.dockedAnnotation == null)
257                 {
258                     DockedAnnotationView view = new DockedAnnotationView();
259                     Binding binding = new Binding("ModelItem.AnnotationText");
260                     view.SetBinding(DockedAnnotationView.AnnotationTextProperty, binding);
261                     view.Visibility = Visibility.Collapsed;
262                     Grid.SetRow(view, 0);
263
264                     this.dockedAnnotation = view;
265                     this.designer.rootGrid.Children.Insert(0, view);
266                 }
267
268                 return this.dockedAnnotation;
269             }
270         }
271     }
272 }