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:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
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.
20 // Copyright (c) 2005 Novell, Inc.
23 // Jackson Harper (jackson@ximian.com)
27 using System.Collections;
28 using System.ComponentModel;
30 namespace System.Windows.Forms {
32 public class CurrencyManager : BindingManagerBase {
34 protected Type finalType;
35 protected int listposition;
38 private IBindingList binding_list;
40 private bool binding_suspended;
42 internal CurrencyManager (object data_source)
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;
50 throw new Exception ("Attempted to create currency manager " +
51 "from invalid type: " + data_source.GetType ());
59 public override object Current {
61 return list [listposition];
65 public override int Count {
66 get { return list.Count; }
69 public override int Position {
76 if (value == list.Count)
77 value = list.Count - 1;
78 if (listposition == value)
81 OnCurrentChanged (EventArgs.Empty);
82 OnPositionChanged (EventArgs.Empty);
86 public override PropertyDescriptorCollection GetItemProperties ()
88 ITypedList typed = list as ITypedList;
92 return typed.GetItemProperties (null);
95 public override void RemoveAt (int index)
97 list.RemoveAt (index);
100 public override void SuspendBinding ()
102 binding_suspended = true;
105 public override void ResumeBinding ()
107 binding_suspended = false;
110 public override void AddNew ()
112 if (binding_list == null)
113 throw new NotSupportedException ();
114 binding_list.AddNew ();
117 public override void CancelCurrentEdit ()
119 IEditableObject editable = Current as IEditableObject;
121 if (editable == null)
123 editable.CancelEdit ();
124 OnItemChanged (new ItemChangedEventArgs (Position));
127 public override void EndCurrentEdit ()
129 IEditableObject editable = Current as IEditableObject;
131 if (editable == null)
136 protected internal override void OnCurrentChanged (EventArgs e)
140 if (onCurrentChangedHandler != null) {
141 onCurrentChangedHandler (this, e);
145 protected virtual void OnItemChanged (ItemChangedEventArgs e)
149 if (ItemChanged != null)
150 ItemChanged (this, e);
153 protected virtual void OnPositionChanged (EventArgs e)
155 if (onPositionChangedHandler == null)
157 onPositionChangedHandler (this, e);
160 protected internal override string GetListName (ArrayList accessors)
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);
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 ()
176 foreach (Binding binding in Bindings)
177 binding.UpdateIsBinding ();
180 private void UpdateItem ()
182 // Probably should be validating or something here
186 public event ItemChangedEventHandler ItemChanged;