* Binding.cs: Support <Property>IsNull checks. Also bind to the
[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                 private object data;
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                 #region Public Constructors
51                 public Binding (string propertyName, object dataSource, string dataMember)
52                 {
53                         property_name = propertyName;
54                         data_source = dataSource;
55                         data_member = dataMember;
56                         binding_member_info = new BindingMemberInfo (dataMember);
57                 }
58                 #endregion      // Public Constructors
59
60                 #region Public Instance Properties
61                 public BindingManagerBase BindingManagerBase {
62                         get {
63                                 return manager;
64                         }
65                 }
66
67                 public BindingMemberInfo BindingMemberInfo {
68                         get {
69                                 return binding_member_info;
70                         }
71                 }
72
73                 [DefaultValue (null)]
74                 public Control Control {
75                         get {
76                                 return control;
77                         }
78                 }
79
80                 public object DataSource {
81                         get {
82                                 return data_source;
83                         }
84                 }
85
86                 [MonoTODO]
87                 public bool IsBinding {
88                         get {
89                                 return false;
90                         }
91                 }
92
93                 [DefaultValue ("")]
94                 public string PropertyName {
95                         get {
96                                 return property_name;
97                         }
98                 }
99                 #endregion      // Public Instance Properties
100
101                 #region Protected Instance Methods
102                 protected virtual void OnFormat (ConvertEventArgs cevent)
103                 {
104                         if (Format!=null)
105                                 Format (this, cevent);
106                 }
107
108                 protected virtual void OnParse (ConvertEventArgs cevent)
109                 {
110                         if (Parse!=null)
111                                 Parse (this, cevent);
112                 }
113                 #endregion      // Protected Instance Methods
114
115                 
116                 internal void SetControl (Control control)
117                 {
118                         if (control == this.control)
119                                 return;
120
121                         prop_desc = TypeDescriptor.GetProperties (control).Find (property_name, false);
122
123                         if (prop_desc == null)
124                                 throw new ArgumentException (String.Concat ("Cannot bind to property '", property_name, "' on target control."));
125                         if (prop_desc.IsReadOnly)
126                                 throw new ArgumentException (String.Concat ("Cannot bind to property '", property_name, "' because it is read only."));
127
128                         control.Validating += new CancelEventHandler (ControlValidatingHandler);
129
130                         this.control = control;
131                 }
132
133                 internal void Check (BindingContext binding_context)
134                 {
135                         if (control == null)
136                                 return;
137
138                         manager = control.BindingContext [data_source, property_name];
139                         manager.AddBinding (this);
140
141                         WirePropertyValueChangedEvent ();
142
143                         is_null_desc = TypeDescriptor.GetProperties (manager.Current).Find (property_name + "IsNull", false);
144
145                         PullData ();
146                         PushData ();
147                 }
148
149                 internal void PushData ()
150                 {
151                         prop_desc.SetValue (control, data);
152                 }
153
154                 internal void PullData ()
155                 {
156                         if (is_null_desc != null) {
157                                 bool is_null = (bool) is_null_desc.GetValue (manager.Current);
158                                 if (is_null) {
159                                         data = Convert.DBNull;
160                                         return;
161                                 }
162                         }
163
164                         PropertyDescriptor pd = TypeDescriptor.GetProperties (manager.Current).Find (data_member, true);
165                         data = pd.GetValue (manager.Current);
166                 }
167
168                 internal void UpdateIsBinding ()
169                 {
170                         PushData ();
171                 }
172
173                 private void CurrentChangedHandler ()
174                 {
175                         if (changed_event != null) {
176                                 changed_event.RemoveEventHandler (event_current, property_value_changed_handler);
177                                 WirePropertyValueChangedEvent ();
178                         }
179                 }
180
181                 private void WirePropertyValueChangedEvent ()
182                 {
183                         EventDescriptor changed_event = TypeDescriptor.GetEvents (manager.Current).Find (property_name + "Changed", false);
184                         if (changed_event == null)
185                                 return;
186                         property_value_changed_handler = new EventHandler (PropertyValueChanged);
187                         changed_event.AddEventHandler (manager.Current, property_value_changed_handler);
188
189                         event_current = manager.Current;
190                 }
191
192                 private void PropertyValueChanged (object sender, EventArgs e)
193                 {
194                         PullData ();
195                         PushData ();
196                 }
197
198                 private void ControlValidatingHandler (object sender, CancelEventArgs e)
199                 {
200                         PullData ();
201                 }
202
203                 #region Events
204                 public event ConvertEventHandler Format;
205                 public event ConvertEventHandler Parse;
206                 #endregion      // Events
207         }
208 }