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