* Binding.cs: Format and parse data correctly. Use ASsignableFrom
[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                 [MonoTODO]
89                 public bool IsBinding {
90                         get {
91                                 return false;
92                         }
93                 }
94
95                 [DefaultValue ("")]
96                 public string PropertyName {
97                         get {
98                                 return property_name;
99                         }
100                 }
101                 #endregion      // Public Instance Properties
102
103                 #region Protected Instance Methods
104                 protected virtual void OnFormat (ConvertEventArgs cevent)
105                 {
106                         if (Format!=null)
107                                 Format (this, cevent);
108                 }
109
110                 protected virtual void OnParse (ConvertEventArgs cevent)
111                 {
112                         if (Parse!=null)
113                                 Parse (this, cevent);
114                 }
115                 #endregion      // Protected Instance Methods
116
117                 
118                 internal void SetControl (Control control)
119                 {
120                         if (control == this.control)
121                                 return;
122
123                         prop_desc = TypeDescriptor.GetProperties (control).Find (property_name, false);
124                         data_type = prop_desc.PropertyType; // Getting the PropertyType is kinda slow and it should never change, so it is cached
125                         
126                         if (prop_desc == null)
127                                 throw new ArgumentException (String.Concat ("Cannot bind to property '", property_name, "' on target control."));
128                         if (prop_desc.IsReadOnly)
129                                 throw new ArgumentException (String.Concat ("Cannot bind to property '", property_name, "' because it is read only."));
130
131                         control.Validating += new CancelEventHandler (ControlValidatingHandler);
132
133                         this.control = control;
134                 }
135
136                 internal void Check (BindingContext binding_context)
137                 {
138                         if (control == null)
139                                 return;
140
141                         manager = control.BindingContext [data_source, property_name];
142                         manager.AddBinding (this);
143
144                         WirePropertyValueChangedEvent ();
145
146                         is_null_desc = TypeDescriptor.GetProperties (manager.Current).Find (property_name + "IsNull", false);
147
148                         PullData ();
149                 }
150
151                 internal void PushData ()
152                 {
153                         SetControlValue (data);
154                 }
155
156                 internal void PullData ()
157                 {
158                         if (is_null_desc != null) {
159                                 bool is_null = (bool) is_null_desc.GetValue (manager.Current);
160                                 if (is_null) {
161                                         data = Convert.DBNull;
162                                         return;
163                                 }
164                         }
165
166                         PropertyDescriptor pd = TypeDescriptor.GetProperties (manager.Current).Find (data_member, true);
167                         object pulled = pd.GetValue (manager.Current);
168                         data = ParseData (pulled, pd.PropertyType);
169
170                         data = FormatData (data);
171                         SetControlValue (data);
172                 }
173
174                 internal void UpdateIsBinding ()
175                 {
176                         PushData ();
177                 }
178
179                 private void SetControlValue (object data)
180                 {
181                         prop_desc.SetValue (control, data);
182                 }
183
184                 private void CurrentChangedHandler ()
185                 {
186                         if (changed_event != null) {
187                                 changed_event.RemoveEventHandler (event_current, property_value_changed_handler);
188                                 WirePropertyValueChangedEvent ();
189                         }
190                 }
191
192                 private void WirePropertyValueChangedEvent ()
193                 {
194                         EventDescriptor changed_event = TypeDescriptor.GetEvents (manager.Current).Find (property_name + "Changed", false);
195                         if (changed_event == null)
196                                 return;
197                         property_value_changed_handler = new EventHandler (PropertyValueChanged);
198                         changed_event.AddEventHandler (manager.Current, property_value_changed_handler);
199
200                         event_current = manager.Current;
201                 }
202
203                 private void PropertyValueChanged (object sender, EventArgs e)
204                 {
205                         PullData ();
206                 }
207
208                 private void ControlValidatingHandler (object sender, CancelEventArgs e)
209                 {
210                         PullData ();
211                 }
212
213                 private object ParseData (object data, Type data_type)
214                 {
215                         ConvertEventArgs e = new ConvertEventArgs (data, data_type);
216
217                         OnParse (e);
218                         if (e.Value.GetType ().IsAssignableFrom (data_type))
219                                 return e.Value;
220                         if (e.Value == Convert.DBNull)
221                                 return e.Value;
222
223                         return ConvertData (e.Value, data_type);
224                 }
225
226                 private object FormatData (object data)
227                 {
228                         if (data_type == typeof (object)) 
229                                 return data;
230
231                         ConvertEventArgs e = new ConvertEventArgs (data, data_type);
232
233                         OnFormat (e);
234                         if (e.Value.GetType ().IsAssignableFrom (data_type))
235                                 return e.Value;
236
237                         return ConvertData (data, data_type);
238                 }
239
240                 private object ConvertData (object data, Type data_type)
241                 {
242                         TypeConverter converter = TypeDescriptor.GetConverter (data.GetType ());
243                         if (converter != null && converter.CanConvertTo (data_type))
244                                 return converter.ConvertTo (data, data_type);
245
246                         if (data is IConvertible) {
247                                 object res = Convert.ChangeType (data, data_type);
248                                 if (res.GetType ().IsAssignableFrom (data_type))
249                                         return res;
250                         }
251
252                         return null;
253                 }
254
255                 #region Events
256                 public event ConvertEventHandler Format;
257                 public event ConvertEventHandler Parse;
258                 #endregion      // Events
259         }
260 }