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.Reflection;
28 using System.Collections;
29 using System.ComponentModel;
31 namespace System.Windows.Forms {
32 [DefaultMember("Item")]
33 public class CurrencyManager : BindingManagerBase {
35 protected Type finalType;
36 protected int listposition;
39 private bool binding_suspended;
41 internal CurrencyManager (object data_source)
43 if (data_source is IListSource) {
44 list = ((IListSource) data_source).GetList ();
45 } else if (data_source is IList) {
46 list = (IList) data_source;
48 throw new Exception ("Attempted to create currency manager " +
49 "from invalid type: " + data_source.GetType ());
52 if (data_source as ArrayList != null) {
53 finalType = ((ArrayList)data_source).GetType ();
55 if (data_source as Array != null) {
56 finalType = ((Array) data_source).GetType ();
67 public override object Current {
69 return list [listposition];
73 public override int Count {
74 get { return list.Count; }
77 public override int Position {
84 if (value == list.Count)
85 value = list.Count - 1;
86 if (listposition == value)
89 OnCurrentChanged (EventArgs.Empty);
90 OnPositionChanged (EventArgs.Empty);
94 internal string ListName {
96 ITypedList typed = list as ITypedList;
99 return finalType.Name;
101 return typed.GetListName (null);
106 public override PropertyDescriptorCollection GetItemProperties ()
108 ITypedList typed = list as ITypedList;
111 Type element = list.GetType ().GetElementType ();
112 return TypeDescriptor.GetProperties (element);
116 return typed.GetItemProperties (null);
119 PropertyInfo [] props = finalType.GetProperties ();
120 for (int i = 0; i < props.Length; i++) {
121 if (props [i].Name == "Item") {
122 Type t = props [i].PropertyType;
123 if (t == typeof (object))
125 return GetBrowsableProperties (t);
129 if (list.Count > 0) {
130 return GetBrowsableProperties (list [0].GetType ());
133 return new PropertyDescriptorCollection (null);
136 public override void RemoveAt (int index)
138 list.RemoveAt (index);
141 public override void SuspendBinding ()
143 binding_suspended = true;
146 public override void ResumeBinding ()
148 binding_suspended = false;
151 internal override bool IsSuspended {
152 get { return binding_suspended; }
155 internal bool CanAddRows {
157 if (list as IBindingList == null) {
165 public override void AddNew ()
167 if (list as IBindingList == null)
168 throw new NotSupportedException ();
170 (list as IBindingList).AddNew ();
173 public override void CancelCurrentEdit ()
175 IEditableObject editable = Current as IEditableObject;
177 if (editable == null)
179 editable.CancelEdit ();
180 OnItemChanged (new ItemChangedEventArgs (Position));
183 public override void EndCurrentEdit ()
185 IEditableObject editable = Current as IEditableObject;
187 if (editable == null)
192 protected internal override void OnCurrentChanged (EventArgs e)
196 if (onCurrentChangedHandler != null) {
197 onCurrentChangedHandler (this, e);
201 protected virtual void OnItemChanged (ItemChangedEventArgs e)
205 if (ItemChanged != null)
206 ItemChanged (this, e);
209 protected virtual void OnPositionChanged (EventArgs e)
211 if (onPositionChangedHandler == null)
213 onPositionChangedHandler (this, e);
216 protected internal override string GetListName (ArrayList accessors)
218 if (list is ITypedList) {
219 PropertyDescriptor [] pds;
220 pds = new PropertyDescriptor [accessors.Count];
221 accessors.CopyTo (pds, 0);
222 return ((ITypedList) list).GetListName (pds);
227 [MonoTODO ("Not totally sure how this works, its doesn't seemt to do a pull/push like i originally assumed")]
228 protected override void UpdateIsBinding ()
232 foreach (Binding binding in Bindings)
233 binding.UpdateIsBinding ();
236 private void UpdateItem ()
238 // Probably should be validating or something here
242 internal object GetItem (int index)
247 private PropertyDescriptorCollection GetBrowsableProperties (Type t)
249 Attribute [] att = new System.Attribute [1];
250 att [0] = new BrowsableAttribute (true);
251 return TypeDescriptor.GetProperties (t, att);
254 public event ItemChangedEventHandler ItemChanged;