* ImageList.cs: When the image stream is set pull all the images
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / CurrencyManager.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
26 using System;
27 using System.Collections;
28 using System.ComponentModel;
29
30 namespace System.Windows.Forms {
31
32         public class CurrencyManager : BindingManagerBase {
33
34                 protected Type finalType;
35                 protected int listposition;
36
37                 private IList list;
38                 private IBindingList binding_list;
39
40                 private bool binding_suspended;
41
42                 internal CurrencyManager (object data_source)
43                 {
44                         binding_list = data_source as IBindingList;
45                         if (data_source is IListSource) {
46                                 list = ((IListSource) data_source).GetList ();
47                         } else if (data_source is IList) {
48                                 list = (IList) data_source;
49                         } else {
50                                 throw new Exception ("Attempted to create currency manager " +
51                                         "from invalid type: " + data_source.GetType ());
52                         }
53                 }               
54
55                 public IList List {
56                         get { return list; }
57                 }
58
59                 public override object Current {
60                         get {
61                                 return list [listposition];
62                         }
63                 }
64
65                 public override int Count {
66                         get { return list.Count; }
67                 }
68
69                 public override int Position {
70                         get {
71                                 return listposition;
72                         } 
73                         set {
74                                 if (value < 0)
75                                         value = 0;
76                                 if (value == list.Count)
77                                         value = list.Count - 1;
78                                 if (listposition == value)
79                                         return;
80                                 listposition = value;
81                                 OnCurrentChanged (EventArgs.Empty);
82                                 OnPositionChanged (EventArgs.Empty);
83                         }
84                 }
85
86                 public override PropertyDescriptorCollection GetItemProperties ()
87                 {
88                         ITypedList typed = list as ITypedList;
89
90                         if (typed == null)
91                                 return null;
92                         return typed.GetItemProperties (null);
93                 }
94
95                 public override void RemoveAt (int index)
96                 {
97                         list.RemoveAt (index);
98                 }
99
100                 public override void SuspendBinding ()
101                 {
102                         binding_suspended = true;
103                 }
104                 
105                 public override void ResumeBinding ()
106                 {
107                         binding_suspended = false;
108                 }
109
110                 public override void AddNew ()
111                 {
112                         if (binding_list == null)
113                                 throw new NotSupportedException ();
114                         binding_list.AddNew ();
115                 }
116
117                 public override void CancelCurrentEdit ()
118                 {
119                         IEditableObject editable = Current as IEditableObject;
120
121                         if (editable == null)
122                                 return;
123                         editable.CancelEdit ();
124                         OnItemChanged (new ItemChangedEventArgs (Position));
125                 }
126                 
127                 public override void EndCurrentEdit ()
128                 {
129                         IEditableObject editable = Current as IEditableObject;
130
131                         if (editable == null)
132                                 return;
133                         editable.EndEdit ();
134                 }
135
136                 protected internal override void OnCurrentChanged (EventArgs e)
137                 {
138                         PullData ();
139
140                         if (onCurrentChangedHandler != null) {
141                                 onCurrentChangedHandler (this, e);
142                         }
143                 }
144
145                 protected virtual void OnItemChanged (ItemChangedEventArgs e)
146                 {
147                         PushData ();
148
149                         if (ItemChanged != null)
150                                 ItemChanged (this, e);
151                 }
152
153                 protected virtual void OnPositionChanged (EventArgs e)
154                 {
155                         if (onPositionChangedHandler == null)
156                                 return;
157                         onPositionChangedHandler (this, e);
158                 }
159
160                 protected internal override string GetListName (ArrayList accessors)
161                 {
162                         if (list is ITypedList) {
163                                 PropertyDescriptor [] pds;
164                                 pds = new PropertyDescriptor [accessors.Count];
165                                 accessors.CopyTo (pds, 0);
166                                 return ((ITypedList) list).GetListName (pds);
167                         }
168                         return String.Empty;
169                 }
170
171                 [MonoTODO ("Not totally sure how this works, its doesn't seemt to do a pull/push like i originally assumed")]
172                 protected override void UpdateIsBinding ()
173                 {
174                         UpdateItem ();
175
176                         foreach (Binding binding in Bindings)
177                                 binding.UpdateIsBinding ();
178                 }
179
180                 private void UpdateItem ()
181                 {
182                         // Probably should be validating or something here
183                         EndCurrentEdit ();
184                 }
185
186                 public event ItemChangedEventHandler ItemChanged;
187         }
188 }
189