* Binding.cs: Implement IsBinding.
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / Binding.cs
1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
8 // 
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 // 
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 //
20 // Copyright (c) 2004-2005 Novell, Inc.
21 //
22 // Authors:
23 //      Peter Bartok    pbartok@novell.com
24 //      Jackson Harper  jackson@ximian.com
25 //
26
27
28 using System.ComponentModel;
29
30 namespace System.Windows.Forms {
31
32         [TypeConverter (typeof (ListBindingConverter))]
33         public class Binding {
34
35                 private string property_name;
36                 private object data_source;
37                 private string data_member;
38                 private BindingMemberInfo binding_member_info;
39                 private Control control;
40
41                 private BindingManagerBase manager;
42                 private PropertyDescriptor prop_desc;
43                 private PropertyDescriptor is_null_desc;
44
45                 private EventDescriptor changed_event;
46                 private EventHandler property_value_changed_handler;
47                 private object event_current; // The manager.Current as far as the changed_event knows
48
49                 private object data;
50                 private Type data_type;
51
52                 #region Public Constructors
53                 public Binding (string propertyName, object dataSource, string dataMember)
54                 {
55                         property_name = propertyName;
56                         data_source = dataSource;
57                         data_member = dataMember;
58                         binding_member_info = new BindingMemberInfo (dataMember);
59                 }
60                 #endregion      // Public Constructors
61
62                 #region Public Instance Properties
63                 public BindingManagerBase BindingManagerBase {
64                         get {
65                                 return manager;
66                         }
67                 }
68
69                 public BindingMemberInfo BindingMemberInfo {
70                         get {
71                                 return binding_member_info;
72                         }
73                 }
74
75                 [DefaultValue (null)]
76                 public Control Control {
77                         get {
78                                 return control;
79                         }
80                 }
81
82                 public object DataSource {
83                         get {
84                                 return data_source;
85                         }
86                 }
87
88                 public bool IsBinding {
89                         get {
90                                 if (control == null || !control.Created)
91                                         return false;
92                                 if (manager == null || manager.IsSuspended)
93                                         return false;
94                                 return true;
95                         }
96                 }
97
98                 [DefaultValue ("")]
99                 public string PropertyName {
100                         get {
101                                 return property_name;
102                         }
103                 }
104                 #endregion      // Public Instance Properties
105
106                 #region Protected Instance Methods
107                 protected virtual void OnFormat (ConvertEventArgs cevent)
108                 {
109                         if (Format!=null)
110                                 Format (this, cevent);
111                 }
112
113                 protected virtual void OnParse (ConvertEventArgs cevent)
114                 {
115                         if (Parse!=null)
116                                 Parse (this, cevent);
117                 }
118                 #endregion      // Protected Instance Methods
119
120                 
121                 internal void SetControl (Control control)
122                 {
123                         if (control == this.control)
124                                 return;
125
126                         prop_desc = TypeDescriptor.GetProperties (control).Find (property_name, false);
127                         data_type = prop_desc.PropertyType; // Getting the PropertyType is kinda slow and it should never change, so it is cached
128                         
129                         if (prop_desc == null)
130                                 throw new ArgumentException (String.Concat ("Cannot bind to property '", property_name, "' on target control."));
131                         if (prop_desc.IsReadOnly)
132                                 throw new ArgumentException (String.Concat ("Cannot bind to property '", property_name, "' because it is read only."));
133
134                         control.Validating += new CancelEventHandler (ControlValidatingHandler);
135
136                         this.control = control;
137                 }
138
139                 internal void Check (BindingContext binding_context)
140                 {
141                         if (control == null)
142                                 return;
143
144                         manager = control.BindingContext [data_source, property_name];
145                         manager.AddBinding (this);
146
147                         WirePropertyValueChangedEvent ();
148
149                         is_null_desc = TypeDescriptor.GetProperties (manager.Current).Find (property_name + "IsNull", false);
150
151                         PullData ();
152                 }
153
154                 internal void PushData ()
155                 {
156                         data = prop_desc.GetValue (control);
157                         data = FormatData (data);
158                         SetPropertyValue (data);
159                 }
160
161                 internal void PullData ()
162                 {
163                         if (is_null_desc != null) {
164                                 bool is_null = (bool) is_null_desc.GetValue (manager.Current);
165                                 if (is_null) {
166                                         data = Convert.DBNull;
167                                         return;
168                                 }
169                         }
170
171                         if (data_member != null) {
172                                 PropertyDescriptor pd = TypeDescriptor.GetProperties (manager.Current).Find (data_member, true);
173                                 object pulled = pd.GetValue (manager.Current);
174                                 data = ParseData (pulled, pd.PropertyType);
175                         } else {
176                                 object pulled = manager.Current;
177                                 data = ParseData (pulled, pulled.GetType ());
178                         }
179
180                         data = FormatData (data);
181                         SetControlValue (data);
182                 }
183
184                 internal void UpdateIsBinding ()
185                 {
186                         PushData ();
187                 }
188
189                 private void SetControlValue (object data)
190                 {
191                         prop_desc.SetValue (control, data);
192                 }
193
194                 private void SetPropertyValue (object data)
195                 {
196                         PropertyDescriptor pd = TypeDescriptor.GetProperties (manager.Current).Find (data_member, true);
197                         if (pd.IsReadOnly)
198                                 return;
199                         pd.SetValue (manager.Current, data);
200                 }
201
202                 private void CurrentChangedHandler ()
203                 {
204                         if (changed_event != null) {
205                                 changed_event.RemoveEventHandler (event_current, property_value_changed_handler);
206                                 WirePropertyValueChangedEvent ();
207                         }
208                 }
209
210                 private void WirePropertyValueChangedEvent ()
211                 {
212                         EventDescriptor changed_event = TypeDescriptor.GetEvents (manager.Current).Find (property_name + "Changed", false);
213                         if (changed_event == null)
214                                 return;
215                         property_value_changed_handler = new EventHandler (PropertyValueChanged);
216                         changed_event.AddEventHandler (manager.Current, property_value_changed_handler);
217
218                         event_current = manager.Current;
219                 }
220
221                 private void PropertyValueChanged (object sender, EventArgs e)
222                 {
223                         PullData ();
224                 }
225
226                 private void ControlValidatingHandler (object sender, CancelEventArgs e)
227                 {
228                         PullData ();
229                 }
230
231                 private object ParseData (object data, Type data_type)
232                 {
233                         ConvertEventArgs e = new ConvertEventArgs (data, data_type);
234
235                         OnParse (e);
236                         if (data_type.IsAssignableFrom (e.Value.GetType ()))
237                                 return e.Value;
238                         if (e.Value == Convert.DBNull)
239                                 return e.Value;
240
241                         return ConvertData (e.Value, data_type);
242                 }
243
244                 private object FormatData (object data)
245                 {
246                         if (data_type == typeof (object)) 
247                                 return data;
248
249                         ConvertEventArgs e = new ConvertEventArgs (data, data_type);
250
251                         OnFormat (e);
252                         if (data_type.IsAssignableFrom (e.Value.GetType ()))
253                                 return e.Value;
254
255                         return ConvertData (data, data_type);
256                 }
257
258                 private object ConvertData (object data, Type data_type)
259                 {
260                         TypeConverter converter = TypeDescriptor.GetConverter (data.GetType ());
261                         if (converter != null && converter.CanConvertTo (data_type))
262                                 return converter.ConvertTo (data, data_type);
263
264                         if (data is IConvertible) {
265                                 object res = Convert.ChangeType (data, data_type);
266                                 if (data_type.IsAssignableFrom (res.GetType ()))
267                                         return res;
268                         }
269
270                         return null;
271                 }
272
273                 #region Events
274                 public event ConvertEventHandler Format;
275                 public event ConvertEventHandler Parse;
276                 #endregion      // Events
277         }
278 }