merging the Mainsoft branch to the trunk
[mono.git] / mcs / class / Managed.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                 private object data_source;
34                 private string property_name;
35                 private PropertyDescriptor prop_desc;
36                 private bool binding_suspended;
37
38                 public PropertyManager() {
39                 }
40
41                 internal PropertyManager (object data_source, string property_name)
42                 {
43                         this.data_source = data_source;
44                         this.property_name = property_name;
45
46                         prop_desc = TypeDescriptor.GetProperties (data_source).Find (property_name, true);
47
48                         if (prop_desc == null)
49                                 return;
50
51                         prop_desc.AddValueChanged (data_source, new EventHandler (PropertyChangedHandler));
52                 }
53
54                 public override object Current {
55                         get { return data_source; }
56                 }
57
58                 public override int Position {
59                         get { return 0; }
60                         set { /* Doesn't do anything on MS" */ }
61                 }
62
63                 public override int Count {
64                         get { return 1; }
65                 }
66
67                 public override void AddNew ()
68                 {
69                         throw new NotSupportedException ("AddNew is not supported for property to property binding");
70                 }
71
72                 public override void CancelCurrentEdit ()
73                 {
74                         IEditableObject editable = data_source as IEditableObject;
75                         if (editable == null)
76                                 return;
77                         editable.CancelEdit ();
78
79                         PushData ();
80                 }
81
82                 public override void EndCurrentEdit ()
83                 {
84                         PullData ();
85
86                         IEditableObject editable = data_source as IEditableObject;
87                         if (editable == null)
88                                 return;
89                         editable.EndEdit ();
90                 }
91
92                 public override PropertyDescriptorCollection GetItemProperties ()
93                 {
94                         return TypeDescriptor.GetProperties (data_source);
95                 }
96
97                 public override void RemoveAt (int idx)
98                 {
99                         throw new NotSupportedException ("RemoveAt is not supported for property to property binding");
100                 }
101
102                 public override void ResumeBinding ()
103                 {
104                         binding_suspended = false;
105                 }
106
107                 public override void SuspendBinding ()
108                 {
109                         binding_suspended = true;
110                 }
111
112                 internal override bool IsSuspended {
113                         get { return binding_suspended; }
114                 }
115
116                 protected internal override string GetListName (ArrayList list)
117                 {
118                         return String.Empty;
119                 }
120
121                 [MonoTODO]
122                 protected override void UpdateIsBinding ()
123                 {
124                 }
125
126                 protected internal override void OnCurrentChanged (EventArgs e)
127                 {
128                         PullData ();
129
130                         if (onCurrentChangedHandler != null) {
131                                 onCurrentChangedHandler (this, e);
132                         }
133                 }
134
135                 private void PropertyChangedHandler (object sender, EventArgs e)
136                 {
137                         OnCurrentChanged (EventArgs.Empty);
138                 }
139         }
140 }
141