Rename Managed.Windows.Forms to System.Windows.Forms for consistency.
[mono.git] / mcs / class / System.Windows.Forms / System.Windows.Forms / PropertyManager.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) 2005 Novell, Inc.
21 //
22 // Authors:
23 //      Jackson Harper  jackson@ximian.com
24
25 using System;
26 using System.Collections;
27 using System.ComponentModel;
28
29 namespace System.Windows.Forms {
30
31         public class PropertyManager : BindingManagerBase {
32
33                 internal string property_name;
34                 private PropertyDescriptor prop_desc;
35                 private object data_source;
36                 private EventDescriptor changed_event;
37                 private EventHandler property_value_changed_handler;
38
39                 public PropertyManager() {
40                 }
41
42                 internal PropertyManager (object data_source)
43                 {
44                         SetDataSource (data_source);
45                 }
46
47                 internal PropertyManager (object data_source, string property_name)
48                 {
49                         this.property_name = property_name;
50
51                         SetDataSource (data_source);
52                 }
53
54                 internal void SetDataSource (object new_data_source)
55                 {
56                         if (changed_event != null)
57                                 changed_event.RemoveEventHandler (data_source, property_value_changed_handler);
58
59                         data_source = new_data_source;
60
61                         if (property_name != null) {
62                                 prop_desc = TypeDescriptor.GetProperties (data_source).Find (property_name, true);
63
64                                 if (prop_desc == null)
65                                         return;
66
67                                 changed_event = TypeDescriptor.GetEvents (data_source).Find (property_name + "Changed", false);
68                                 if (changed_event != null) {
69                                         property_value_changed_handler = new EventHandler (PropertyValueChanged);
70                                         changed_event.AddEventHandler (data_source, property_value_changed_handler);
71                                 }
72                         }
73                 }
74
75                 void PropertyValueChanged (object sender, EventArgs args)
76                 {
77                         OnCurrentChanged (args);
78                 }
79
80                 public override object Current {
81                         get { return prop_desc == null ? data_source : prop_desc.GetValue (data_source); }
82                 }
83
84                 public override int Position {
85                         get { return 0; }
86                         set { /* Doesn't do anything on MS" */ }
87                 }
88
89                 public override int Count {
90                         get { return 1; }
91                 }
92
93                 public override void AddNew ()
94                 {
95                         throw new NotSupportedException ("AddNew is not supported for property to property binding");
96                 }
97
98                 public override void CancelCurrentEdit ()
99                 {
100                         IEditableObject editable = data_source as IEditableObject;
101                         if (editable == null)
102                                 return;
103                         editable.CancelEdit ();
104
105                         PushData ();
106                 }
107
108                 public override void EndCurrentEdit ()
109                 {
110                         PullData ();
111
112                         IEditableObject editable = data_source as IEditableObject;
113                         if (editable == null)
114                                 return;
115                         editable.EndEdit ();
116                 }
117
118                 // Hide this method from the 2.0 public API
119                 internal override PropertyDescriptorCollection GetItemPropertiesInternal ()
120                 {
121                         return TypeDescriptor.GetProperties (data_source);
122                 }
123
124                 public override void RemoveAt (int index)
125                 {
126                         throw new NotSupportedException ("RemoveAt is not supported for property to property binding");
127                 }
128
129                 public override void ResumeBinding ()
130                 {
131                 }
132
133                 public override void SuspendBinding ()
134                 {
135                 }
136
137                 internal override bool IsSuspended {
138                         get { return data_source == null; }
139                 }
140
141                 protected internal override string GetListName (ArrayList listAccessors)
142                 {
143                         return String.Empty;
144                 }
145
146                 [MonoTODO ("Stub, does nothing")]
147                 protected override void UpdateIsBinding ()
148                 {
149                 }
150
151                 protected internal override void OnCurrentChanged (EventArgs ea)
152                 {
153                         PushData ();
154
155                         if (onCurrentChangedHandler != null) {
156                                 onCurrentChangedHandler (this, ea);
157                         }
158                 }
159
160                 protected override void OnCurrentItemChanged (EventArgs ea)
161                 {
162                         throw new NotImplementedException ();
163                 }
164         }
165 }
166