2008-03-27 Carlos Alberto Cortez <calberto.cortez@gmail.com>
[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
39                 private bool is_binding;
40                 private bool checked_isnull;
41
42                 private BindingMemberInfo binding_member_info;
43 #if NET_2_0
44                 private IBindableComponent control;
45 #else
46                 private Control control;
47 #endif
48
49                 private BindingManagerBase manager;
50                 private PropertyDescriptor control_property;
51                 private PropertyDescriptor is_null_desc;
52
53                 private object data;
54                 private Type data_type;
55
56 #if NET_2_0
57                 private DataSourceUpdateMode datasource_update_mode;
58                 private ControlUpdateMode control_update_mode;
59                 private object datasource_null_value;
60                 private object null_value;
61                 private IFormatProvider format_info;
62                 private string format_string;
63                 private bool formatting_enabled;
64 #endif
65                 #region Public Constructors
66 #if NET_2_0
67                 public Binding (string propertyName, object dataSource, string dataMember) 
68                         : this (propertyName, dataSource, dataMember, false, DataSourceUpdateMode.OnValidation, null, string.Empty, null)
69                 {
70                 }
71                 
72                 public Binding (string propertyName, object dataSource, string dataMember, bool formattingEnabled)
73                         : this (propertyName, dataSource, dataMember, formattingEnabled, DataSourceUpdateMode.OnValidation, null, string.Empty, null)
74                 {
75                 }
76                 
77                 public Binding (string propertyName, object dataSource, string dataMember, bool formattingEnabled, DataSourceUpdateMode dataSourceUpdateMode)
78                         : this (propertyName, dataSource, dataMember, formattingEnabled, dataSourceUpdateMode, null, string.Empty, null)
79                 {
80                 }
81                 
82                 public Binding (string propertyName, object dataSource, string dataMember, bool formattingEnabled, DataSourceUpdateMode dataSourceUpdateMode, object nullValue)
83                         : this (propertyName, dataSource, dataMember, formattingEnabled, dataSourceUpdateMode, nullValue, string.Empty, null)
84                 {
85                 }
86
87                 public Binding (string propertyName, object dataSource, string dataMember, bool formattingEnabled, DataSourceUpdateMode dataSourceUpdateMode, object nullValue, string formatString)
88                         : this (propertyName, dataSource, dataMember, formattingEnabled, dataSourceUpdateMode, nullValue, formatString, null)
89                 {
90                 }
91
92                 public Binding (string propertyName, object dataSource, string dataMember, bool formattingEnabled, DataSourceUpdateMode dataSourceUpdateMode, object nullValue, string formatString, IFormatProvider formatInfo)
93                 {
94                         property_name = propertyName;
95                         data_source = dataSource;
96                         data_member = dataMember;
97                         binding_member_info = new BindingMemberInfo (dataMember);
98                         datasource_update_mode = dataSourceUpdateMode;
99                         null_value = nullValue;
100                         format_string = formatString;
101                         format_info = formatInfo;
102
103                         EventDescriptor prop_changed_event = GetPropertyChangedEvent (data_source, binding_member_info.BindingField);
104                         if (prop_changed_event != null)
105                                 prop_changed_event.AddEventHandler (data_source, new EventHandler (SourcePropertyChangedHandler));
106                 }
107 #else
108                 public Binding (string propertyName, object dataSource, string dataMember)
109                 {
110                         property_name = propertyName;
111                         data_source = dataSource;
112                         data_member = dataMember;
113                         binding_member_info = new BindingMemberInfo (dataMember);
114                                 
115                         EventDescriptor prop_changed_event = GetPropertyChangedEvent (data_source, binding_member_info.BindingField);
116                         if (prop_changed_event != null)
117                                 prop_changed_event.AddEventHandler (data_source, new EventHandler (SourcePropertyChangedHandler));
118                 }               
119 #endif
120                 #endregion      // Public Constructors
121
122                 #region Public Instance Properties
123 #if NET_2_0
124                 [DefaultValue (null)]
125                 public IBindableComponent BindableComponent {
126                         get {
127                                 return control;
128                         }
129                 }
130 #endif
131                 public BindingManagerBase BindingManagerBase {
132                         get {
133                                 return manager;
134                         }
135                 }
136
137                 public BindingMemberInfo BindingMemberInfo {
138                         get {
139                                 return binding_member_info;
140                         }
141                 }
142
143                 [DefaultValue (null)]
144                 public Control Control {
145                         get {
146 #if NET_2_0
147                                 return control as Control;
148 #else
149                                 return control;
150 #endif
151                         }
152                 }
153
154 #if NET_2_0
155                 [DefaultValue (ControlUpdateMode.OnPropertyChanged)]
156                 public ControlUpdateMode ControlUpdateMode {
157                         get {
158                                 return control_update_mode;
159                         }
160                         set {
161                                 control_update_mode = value;
162                         }
163                 }
164 #endif
165
166                 public object DataSource {
167                         get {
168                                 return data_source;
169                         }
170                 }
171
172 #if NET_2_0
173                 [DefaultValue (DataSourceUpdateMode.OnValidation)]
174                 public DataSourceUpdateMode DataSourceUpdateMode {
175                         get {
176                                 return datasource_update_mode;
177                         }
178                         set {
179                                 datasource_update_mode = value;
180                         }
181                 }
182
183                 public object DataSourceNullValue {
184                         get {
185                                 return datasource_null_value;
186                         }
187                         set {
188                                 datasource_null_value = value;
189                         }
190                 }
191
192                 [DefaultValue (false)]
193                 public bool FormattingEnabled {
194                         get {
195                                 return formatting_enabled;
196                         }
197                         set {
198                                 if (formatting_enabled == value)
199                                         return;
200
201                                 formatting_enabled = value;
202                                 PushData ();
203                         }
204                 }
205
206                 [DefaultValue (null)]
207                 public IFormatProvider FormatInfo {
208                         get {
209                                 return format_info;
210                         }
211                         set {
212                                 if (value == format_info)
213                                         return;
214
215                                 format_info = value;
216                                 if (formatting_enabled)
217                                         PushData ();
218                         }
219                 }
220
221                 public string FormatString {
222                         get {
223                                 return format_string;
224                         }
225                         set {
226                                 if (value == null)
227                                         value = String.Empty;
228                                 if (value == format_string)
229                                         return;
230
231                                 format_string = value;
232                                 if (formatting_enabled)
233                                         PushData ();
234                         }
235                 }
236 #endif
237
238                 public bool IsBinding {
239                         get {
240                                 return is_binding;
241                         }
242                 }
243
244 #if NET_2_0
245                 public object NullValue {
246                         get {
247                                 return null_value;
248                         }
249                         set {
250                                 if (value == null_value)
251                                         return;
252
253                                 null_value = value;
254                                 if (formatting_enabled)
255                                         PushData ();
256                         }
257                 }
258 #endif
259
260                 [DefaultValue ("")]
261                 public string PropertyName {
262                         get {
263                                 return property_name;
264                         }
265                 }
266                 #endregion      // Public Instance Properties
267
268 #if NET_2_0
269                 public void ReadValue ()
270                 {
271                         PushData (true);
272                 }
273
274                 public void WriteValue ()
275                 {
276                         PullData (true);
277                 }
278 #endif
279
280                 #region Protected Instance Methods
281 #if NET_2_0
282                 protected virtual void OnBindingComplete (BindingCompleteEventArgs e)
283                 {
284                         if (BindingComplete != null)
285                                 BindingComplete (this, e);
286                 }
287 #endif
288
289                 protected virtual void OnFormat (ConvertEventArgs cevent)
290                 {
291                         if (Format!=null)
292                                 Format (this, cevent);
293                 }
294
295                 protected virtual void OnParse (ConvertEventArgs cevent)
296                 {
297                         if (Parse!=null)
298                                 Parse (this, cevent);
299                 }
300                 #endregion      // Protected Instance Methods
301
302                 internal string DataMember {
303                         get { return data_member; }
304                 }
305                 
306 #if NET_2_0
307                 internal void SetControl (IBindableComponent control)
308 #else
309                 internal void SetControl (Control control)
310 #endif
311                 {
312                         if (control == this.control)
313                                 return;
314
315                         control_property = TypeDescriptor.GetProperties (control).Find (property_name, true);                   
316
317                         if (control_property == null)
318                                 throw new ArgumentException (String.Concat ("Cannot bind to property '", property_name, "' on target control."));
319                         if (control_property.IsReadOnly)
320                                 throw new ArgumentException (String.Concat ("Cannot bind to property '", property_name, "' because it is read only."));
321                                 
322                         data_type = control_property.PropertyType; // Getting the PropertyType is kinda slow and it should never change, so it is cached
323
324                         if (control is Control)
325                                 ((Control)control).Validating += new CancelEventHandler (ControlValidatingHandler);
326
327 #if NET_2_0
328                         EventDescriptor prop_changed_event = GetPropertyChangedEvent (control, property_name);
329                         if (prop_changed_event != null)
330                                 prop_changed_event.AddEventHandler (control, new EventHandler (ControlPropertyChangedHandler));
331 #endif
332                         this.control = control;
333                 }
334
335                 internal void Check ()
336                 {
337                         if (control == null || control.BindingContext == null)
338                                 return;
339
340                         if (manager == null) {
341                                 manager = control.BindingContext [data_source];
342                                 manager.AddBinding (this);
343                                 manager.PositionChanged += new EventHandler (PositionChangedHandler);
344                         }
345
346                         if (manager.Position == -1)
347                                 return;
348
349                         if (!checked_isnull) {
350                                 is_null_desc = TypeDescriptor.GetProperties (manager.Current).Find (property_name + "IsNull", false);
351                                 checked_isnull = true;
352                         }
353
354                         PushData ();
355                 }
356
357                 internal bool PullData ()
358                 {
359                         return PullData (false);
360                 }
361
362                 // Return false ONLY in case of error - and return true even in cases
363                 // where no update was possible
364                 bool PullData (bool force)
365                 {
366                         if (IsBinding == false || manager.Current == null)
367                                 return true;
368 #if NET_2_0
369                         if (!force && datasource_update_mode == DataSourceUpdateMode.Never)
370                                 return true;
371 #endif
372
373                         data = control_property.GetValue (control);
374
375                         try {
376                                 SetPropertyValue (data);
377                         } catch (Exception e) {
378 #if NET_2_0
379                                 if (formatting_enabled) {
380                                         FireBindingComplete (BindingCompleteContext.DataSourceUpdate, e, e.Message);
381                                         return false;
382                                 }
383 #endif
384                                 throw e;
385                         }
386
387 #if NET_2_0
388                         if (formatting_enabled)
389                                 FireBindingComplete (BindingCompleteContext.DataSourceUpdate, null, null);
390 #endif
391                         return true;
392                 }
393
394                 internal void PushData ()
395                 {
396                         PushData (false);
397                 }
398
399                 void PushData (bool force)
400                 {
401                         if (manager == null || manager.IsSuspended || manager.Current == null)
402                                 return;
403 #if NET_2_0
404                         if (!force && control_update_mode == ControlUpdateMode.Never)
405                                 return;
406 #endif
407
408                         if (is_null_desc != null) {
409                                 bool is_null = (bool) is_null_desc.GetValue (manager.Current);
410                                 if (is_null) {
411                                         data = Convert.DBNull;
412                                         return;
413                                 }
414                         }
415
416                         PropertyDescriptor pd = TypeDescriptor.GetProperties (manager.Current).Find (binding_member_info.BindingField, true);
417                         if (pd == null) {
418                                 data = manager.Current;
419                         } else {
420                                 data = pd.GetValue (manager.Current);
421                         }
422
423                         try {
424                                 data = FormatData (data);
425                                 SetControlValue (data);
426                         } catch (Exception e) {
427 #if NET_2_0
428                                 if (formatting_enabled) {
429                                         FireBindingComplete (BindingCompleteContext.ControlUpdate, e, e.Message);
430                                         return;
431                                 }
432 #endif
433                                 throw e;
434                         }
435
436 #if NET_2_0
437                         if (formatting_enabled)
438                                 FireBindingComplete (BindingCompleteContext.ControlUpdate, null, null);
439 #endif
440                 }
441
442                 internal void UpdateIsBinding ()
443                 {
444                         is_binding = false;
445 #if NET_2_0
446                         if (control == null || (control is Control && !((Control)control).Created))
447 #else
448                         if (control == null && !control.Created)
449 #endif
450                                 return;
451                         if (manager == null || manager.IsSuspended)
452                                 return;
453
454                         is_binding = true;
455                         PushData ();
456                 }
457
458                 private void SetControlValue (object data)
459                 {
460                         control_property.SetValue (control, data);
461                 }
462
463                 private void SetPropertyValue (object data)
464                 {
465                         PropertyDescriptor pd = TypeDescriptor.GetProperties (manager.Current).Find (binding_member_info.BindingField, true);
466                         if (pd.IsReadOnly)
467                                 return;
468                         data = ParseData (data, pd.PropertyType);
469                         pd.SetValue (manager.Current, data);
470                 }
471
472                 private void ControlValidatingHandler (object sender, CancelEventArgs e)
473                 {
474 #if NET_2_0
475                         if (datasource_update_mode != DataSourceUpdateMode.OnValidation)
476                                 return;
477 #endif
478
479                         bool ok = true;
480                         // If the data doesn't seem to be valid (it can't be converted,
481                         // is the wrong type, etc, we reset to the old data value.
482                         // If Formatting is enabled, no exception is fired, but we get a false value
483                         try {
484                                 ok = PullData ();
485                         } catch {
486                                 ok = false;
487                         }
488
489                         e.Cancel = !ok;
490                 }
491
492                 private void PositionChangedHandler (object sender, EventArgs e)
493                 {
494                         Check ();
495                         PushData ();
496                 }
497
498                 EventDescriptor GetPropertyChangedEvent (object o, string property_name)
499                 {
500                         if (o == null || property_name == null || property_name.Length == 0)
501                                 return null;
502
503                         string event_name = property_name + "Changed";
504                         Type event_handler_type = typeof (EventHandler);
505
506                         EventDescriptor prop_changed_event = null;
507                         foreach (EventDescriptor event_desc in TypeDescriptor.GetEvents (o)) {
508                                 if (event_desc.Name == event_name && event_desc.EventType == event_handler_type) {
509                                         prop_changed_event = event_desc;
510                                         break;
511                                 }
512                         }
513
514                         return prop_changed_event;
515                 }
516
517                 void SourcePropertyChangedHandler (object o, EventArgs args)
518                 {
519                         PushData ();
520                 }
521
522 #if NET_2_0
523                 void ControlPropertyChangedHandler (object o, EventArgs args)
524                 {
525                         if (datasource_update_mode != DataSourceUpdateMode.OnPropertyChanged)
526                                 return;
527
528                         PullData ();
529                 }
530 #endif
531
532                 private object ParseData (object data, Type data_type)
533                 {
534                         ConvertEventArgs e = new ConvertEventArgs (data, data_type);
535
536                         OnParse (e);
537                         if (data_type.IsInstanceOfType (e.Value))
538                                 return e.Value;
539                         if (e.Value == Convert.DBNull)
540                                 return e.Value;
541 #if NET_2_0
542                         if (e.Value == null)
543                                 return data_type.IsByRef ? null : Convert.DBNull;
544 #endif
545
546                         return ConvertData (e.Value, data_type);
547                 }
548
549                 private object FormatData (object data)
550                 {
551                         ConvertEventArgs e = new ConvertEventArgs (data, data_type);
552
553                         OnFormat (e);
554                         if (data_type.IsInstanceOfType (e.Value))
555                                 return e.Value;
556
557 #if NET_2_0
558                         if (formatting_enabled) {
559                                 if (e.Value == null || e.Value == Convert.DBNull) {
560                                         return null_value;
561                                 }
562                                 
563                                 if (e.Value is IFormattable && data_type == typeof (string)) {
564                                         IFormattable formattable = (IFormattable) e.Value;
565                                         return formattable.ToString (format_string, format_info);
566                                 }
567                         }
568 #endif
569                         if (e.Value == null && data_type == typeof (object))
570                                 return Convert.DBNull;
571
572                         return ConvertData (data, data_type);
573                 }
574
575                 private object ConvertData (object data, Type data_type)
576                 {
577                         if (data == null)
578                                 return null;
579
580                         TypeConverter converter = TypeDescriptor.GetConverter (data.GetType ());
581                         if (converter != null && converter.CanConvertTo (data_type))
582                                 return converter.ConvertTo (data, data_type);
583
584                         if (data is IConvertible) {
585                                 object res = Convert.ChangeType (data, data_type);
586                                 if (data_type.IsInstanceOfType (res))
587                                         return res;
588                         }
589
590                         return null;
591                 }
592 #if NET_2_0
593                 void FireBindingComplete (BindingCompleteContext context, Exception exc, string error_message)
594                 {
595                         BindingCompleteEventArgs args = new BindingCompleteEventArgs (this, 
596                                         exc == null ? BindingCompleteState.Success : BindingCompleteState.Exception,
597                                         context);
598                         if (exc != null) {
599                                 args.SetException (exc);
600                                 args.SetErrorText (error_message);
601                         }
602
603                         OnBindingComplete (args);
604                 }
605 #endif
606
607                 #region Events
608                 public event ConvertEventHandler Format;
609                 public event ConvertEventHandler Parse;
610 #if NET_2_0
611                 public event BindingCompleteEventHandler BindingComplete;
612 #endif
613                 #endregion      // Events
614         }
615 }