New test.
[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 BindingMemberInfo binding_member_info;
40                 private Control control;
41
42                 private BindingManagerBase manager;
43                 private PropertyDescriptor control_property;
44                 private PropertyDescriptor is_null_desc;
45
46                 private EventDescriptor changed_event;
47                 private EventHandler property_value_changed_handler;
48                 private object event_current; // The manager.Current as far as the changed_event knows
49
50                 private object data;
51                 private Type data_type;
52
53                 #region Public Constructors
54                 public Binding (string propertyName, object dataSource, string dataMember)
55                 {
56                         property_name = propertyName;
57                         data_source = dataSource;
58                         data_member = dataMember;
59                         binding_member_info = new BindingMemberInfo (dataMember);
60                 }
61                 #endregion      // Public Constructors
62
63                 #region Public Instance Properties
64                 public BindingManagerBase BindingManagerBase {
65                         get {
66                                 return manager;
67                         }
68                 }
69
70                 public BindingMemberInfo BindingMemberInfo {
71                         get {
72                                 return binding_member_info;
73                         }
74                 }
75
76                 [DefaultValue (null)]
77                 public Control Control {
78                         get {
79                                 return control;
80                         }
81                 }
82
83                 public object DataSource {
84                         get {
85                                 return data_source;
86                         }
87                 }
88
89                 public bool IsBinding {
90                         get {
91                                 if (control == null || !control.Created)
92                                         return false;
93                                 if (manager == null || manager.IsSuspended)
94                                         return false;
95                                 return true;
96                         }
97                 }
98
99                 [DefaultValue ("")]
100                 public string PropertyName {
101                         get {
102                                 return property_name;
103                         }
104                 }
105                 #endregion      // Public Instance Properties
106
107                 #region Protected Instance Methods
108                 protected virtual void OnFormat (ConvertEventArgs cevent)
109                 {
110                         if (Format!=null)
111                                 Format (this, cevent);
112                 }
113
114                 protected virtual void OnParse (ConvertEventArgs cevent)
115                 {
116                         if (Parse!=null)
117                                 Parse (this, cevent);
118                 }
119                 #endregion      // Protected Instance Methods
120
121                 
122                 internal void SetControl (Control control)
123                 {
124                         if (control == this.control)
125                                 return;
126
127                         control_property = TypeDescriptor.GetProperties (control).Find (property_name, true);                   
128
129                         if (control_property == null)
130                                 throw new ArgumentException (String.Concat ("Cannot bind to property '", property_name, "' on target control."));
131                         if (control_property.IsReadOnly)
132                                 throw new ArgumentException (String.Concat ("Cannot bind to property '", property_name, "' because it is read only."));
133                                 
134                         data_type = control_property.PropertyType; // Getting the PropertyType is kinda slow and it should never change, so it is cached
135                         control.Validating += new CancelEventHandler (ControlValidatingHandler);
136
137                         this.control = control;
138                         control.DataBindings.Add (this);
139                 }
140
141                 internal void Check (BindingContext binding_context)
142                 {
143                         if (control == null || control.BindingContext == null)
144                                 return;
145
146                         manager = control.BindingContext [data_source, data_member];
147
148                         manager.AddBinding (this);
149                         manager.PositionChanged += new EventHandler (PositionChangedHandler);
150                         manager.CurrentChanged += new EventHandler (CurrentChangedHandler);
151
152                         is_null_desc = TypeDescriptor.GetProperties (manager.Current).Find (property_name + "IsNull", false);
153
154                         PushData ();
155                 }
156
157                 internal void PullData ()
158                 {
159                         if (IsBinding == false || manager.Current == null)
160                                 return;
161
162                         data = control_property.GetValue (control);
163                         SetPropertyValue (data);
164                 }
165
166                 internal void PushData ()
167                 {
168                         if (manager == null || manager.IsSuspended || manager.Current == null)
169                                 return;
170
171                         if (is_null_desc != null) {
172                                 bool is_null = (bool) is_null_desc.GetValue (manager.Current);
173                                 if (is_null) {
174                                         data = Convert.DBNull;
175                                         return;
176                                 }
177                         }
178
179                         PropertyDescriptor pd = TypeDescriptor.GetProperties (manager.Current).Find (binding_member_info.BindingField, true);
180                         if (pd == null) {
181                                 data = ParseData (manager.Current, manager.Current.GetType ());
182                         } else {
183                                 data = ParseData (pd.GetValue (manager.Current), pd.PropertyType);
184                         }
185
186                         data = FormatData (data);
187                         SetControlValue (data);
188                 }
189
190                 internal void UpdateIsBinding ()
191                 {
192                         PushData ();
193                 }
194
195                 private void SetControlValue (object data)
196                 {
197                         control_property.SetValue (control, data);
198                 }
199
200                 private void SetPropertyValue (object data)
201                 {
202                         PropertyDescriptor pd = TypeDescriptor.GetProperties (manager.Current).Find (binding_member_info.BindingField, true);
203                         if (pd.IsReadOnly)
204                                 return;
205                         data = ParseData (data, pd.PropertyType);
206                         pd.SetValue (manager.Current, data);
207                 }
208
209                 private void CurrentChangedHandler (object sender, EventArgs e)
210                 {
211                         PushData ();
212                 }
213
214                 private void ControlValidatingHandler (object sender, CancelEventArgs e)
215                 {
216                         object old_data = data;
217
218                         // If the data doesn't seem to be valid (it can't be converted,
219                         // is the wrong type, etc, we reset to the old data value.
220                         try {
221                                 PullData ();
222                         } catch {
223                                 data = old_data;
224                                 SetControlValue (data);
225                         }
226                 }
227
228                 private void PositionChangedHandler (object sender, EventArgs e)
229                 {
230                         PushData ();
231                 }
232
233                 private object ParseData (object data, Type data_type)
234                 {
235                         ConvertEventArgs e = new ConvertEventArgs (data, data_type);
236
237                         OnParse (e);
238                         if (data_type.IsInstanceOfType (e.Value))
239                                 return e.Value;
240                         if (e.Value == Convert.DBNull)
241                                 return e.Value;
242
243                         return ConvertData (e.Value, data_type);
244                 }
245
246                 private object FormatData (object data)
247                 {
248                         if (data_type == typeof (object)) 
249                                 return data;
250
251                         ConvertEventArgs e = new ConvertEventArgs (data, data_type);
252
253                         OnFormat (e);
254                         if (data_type.IsInstanceOfType (e.Value))
255                                 return e.Value;
256
257                         return ConvertData (data, data_type);
258                 }
259
260                 private object ConvertData (object data, Type data_type)
261                 {
262                         TypeConverter converter = TypeDescriptor.GetConverter (data.GetType ());
263                         if (converter != null && converter.CanConvertTo (data_type))
264                                 return converter.ConvertTo (data, data_type);
265
266                         if (data is IConvertible) {
267                                 object res = Convert.ChangeType (data, data_type);
268                                 if (data_type.IsInstanceOfType (res))
269                                         return res;
270                         }
271
272                         return null;
273                 }
274
275                 #region Events
276                 public event ConvertEventHandler Format;
277                 public event ConvertEventHandler Parse;
278                 #endregion      // Events
279         }
280 }