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