Mono exception to SocketException
[mono.git] / mcs / class / referencesource / System.Activities.Presentation / System.Activities.Core.Presentation / System / ServiceModel / Activities / Presentation / BindingEditor.xaml.cs
1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //----------------------------------------------------------------
4 namespace System.ServiceModel.Activities.Presentation
5 {
6     using System.Collections.Generic;
7     using System.Linq;
8     using System.ServiceModel;
9     using System.ServiceModel.Channels;
10     using System.Windows;
11     using System.Windows.Controls;
12     using System.Activities.Presentation.Model;
13     using System.Configuration;
14     using System.ServiceModel.Configuration;
15     using System.Activities.Presentation;
16     
17     partial class BindingEditor
18     {
19         public static readonly DependencyProperty BindingProperty =
20             DependencyProperty.Register("Binding",
21             typeof(object),
22             typeof(BindingEditor),
23             new PropertyMetadata(OnBindingChanged));
24
25         List<BindingDescriptor> bindingElements = new List<BindingDescriptor>();
26
27         bool isInitializing;
28
29         public BindingEditor()
30         {
31             InitializeComponent();
32         }
33
34         public object Binding
35         {
36             get { return GetValue(BindingProperty); }
37             set { SetValue(BindingProperty, value); }
38         }
39
40         void LoadBindings()
41         {
42             try
43             {
44                 this.bindingElements.Add(new BindingDescriptor { BindingName = (string)(this.TryFindResource("bindingEditorEmptyBindingLabel") ?? "none"), Value = null });
45                 Configuration machineConfig = ConfigurationManager.OpenMachineConfiguration();              
46                 ServiceModelSectionGroup section = ServiceModelSectionGroup.GetSectionGroup(machineConfig);
47                 if (null != section && null != section.Bindings)
48                 {
49                     this.bindingElements.AddRange(section.Bindings.BindingCollections
50                         .OrderBy(p => p.BindingName)
51                         .Select<BindingCollectionElement, BindingDescriptor>(p => new BindingDescriptor() { BindingName = p.BindingName, Value = p }));
52                 }
53             }
54             catch (ConfigurationErrorsException err)
55             {
56                 ErrorReporting.ShowErrorMessage(err.Message);
57             }
58         }
59
60         protected override void OnInitialized(EventArgs e)
61         {
62             base.OnInitialized(e);
63             this.isInitializing = true;
64             LoadBindings();
65             this.ItemsSource = this.bindingElements;
66             this.SelectedIndex = 0;
67             this.isInitializing = false;
68         }
69
70         protected override void OnSelectionChanged(SelectionChangedEventArgs e)
71         {
72             base.OnSelectionChanged(e);
73             if (!this.isInitializing)
74             {
75
76                 BindingDescriptor entry = (BindingDescriptor)e.AddedItems[0];
77                 if (null == entry.Value)
78                 {
79                     Binding = null;
80                 }
81                 // try to avoid blowing away any binding that has been custom-tweaked in XAML.
82                 else if (Binding == null || !(Binding is ModelItem) || !((ModelItem)Binding).ItemType.Equals(entry.Value.BindingType))
83                 {
84                     Binding instance = (Binding)Activator.CreateInstance(entry.Value.BindingType);
85                     instance.Name = entry.BindingName;
86                     Binding = instance;
87                 }
88             }
89         }
90
91         static void OnBindingChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
92         {
93             BindingEditor editor = (BindingEditor)sender;
94             object newValue = args.NewValue;
95
96             Type bindingType = null;
97             ModelItem item = newValue as ModelItem;
98             string bindingName = null;
99             if (item != null)
100             {
101                 bindingType = item.ItemType;
102                 bindingName = (string)item.Properties["Name"].ComputedValue;
103             }
104             else if (newValue != null)
105             {
106                 bindingType = newValue.GetType();
107                 if (typeof(Binding).IsAssignableFrom(bindingType))
108                 {
109                     bindingName = ((Binding)newValue).Name;
110                 }
111             }
112
113             // Make combo appear empty if the binding is not one of the ones known to us, e.g., has been custom-tweaked in XAML.
114             BindingDescriptor toSelect = null;
115             Func<BindingDescriptor, bool> where = p => null != p.Value && p.Value.BindingType == bindingType;
116             if (editor.bindingElements.Count(where) > 1)
117             {
118                 toSelect = editor.bindingElements.Where(where).Where(p => string.Equals(p.BindingName, bindingName)).FirstOrDefault();
119             }
120             else
121             {
122                 toSelect = editor.bindingElements.Where(where).FirstOrDefault();
123             }
124             //prevent OnSelectionChanged now - the binding is set directly to the object, no need to set again through event handler
125             editor.isInitializing = true;
126             if (null != toSelect)
127             {
128                 editor.SelectedItem = toSelect;
129             }
130             else
131             {
132                 editor.SelectedIndex = 0;
133             }
134             //allow selection changed events to be consumed again
135             editor.isInitializing = false;
136         }
137
138         sealed class BindingDescriptor 
139         {
140             public string BindingName
141             {
142                 get;
143                 internal set;
144             }
145
146             public BindingCollectionElement Value
147             {
148                 get;
149                 internal set;
150             }
151
152             public override string ToString()
153             {
154                 return BindingName;
155             }
156         }
157     }
158 }