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)
28 using System.Reflection;
29 using System.Collections;
30 using System.ComponentModel;
32 namespace System.Windows.Forms {
33 [DefaultMember("Item")]
34 public class CurrencyManager : BindingManagerBase {
36 protected Type finalType;
37 protected int listposition;
40 private bool binding_suspended;
42 internal CurrencyManager (object data_source)
44 if (data_source is IListSource) {
45 list = ((IListSource) data_source).GetList ();
46 } else if (data_source is IList) {
47 list = (IList) data_source;
49 throw new Exception ("Attempted to create currency manager " +
50 "from invalid type: " + data_source.GetType ());
53 if (data_source as ArrayList != null) {
54 finalType = ((ArrayList)data_source).GetType ();
56 if (data_source as Array != null) {
57 finalType = ((Array) data_source).GetType ();
63 DataTable table = data_source as DataTable;
64 if (table == null && data_source is DataView)
65 table = ((DataView) data_source).Table;
68 table.Columns.CollectionChanged += new CollectionChangeEventHandler (MetaDataChangedHandler);
69 table.ChildRelations.CollectionChanged += new CollectionChangeEventHandler (MetaDataChangedHandler);
70 table.ParentRelations.CollectionChanged += new CollectionChangeEventHandler (MetaDataChangedHandler);
71 table.Constraints.CollectionChanged += new CollectionChangeEventHandler (MetaDataChangedHandler);
79 public override object Current {
81 return list [listposition];
85 public override int Count {
86 get { return list.Count; }
89 public override int Position {
96 if (value == list.Count)
97 value = list.Count - 1;
98 if (listposition == value)
100 listposition = value;
101 OnCurrentChanged (EventArgs.Empty);
102 OnPositionChanged (EventArgs.Empty);
106 internal string ListName {
108 ITypedList typed = list as ITypedList;
111 return finalType.Name;
113 return typed.GetListName (null);
118 public override PropertyDescriptorCollection GetItemProperties ()
120 ITypedList typed = list as ITypedList;
123 Type element = list.GetType ().GetElementType ();
124 return TypeDescriptor.GetProperties (element);
128 return typed.GetItemProperties (null);
131 PropertyInfo [] props = finalType.GetProperties ();
132 for (int i = 0; i < props.Length; i++) {
133 if (props [i].Name == "Item") {
134 Type t = props [i].PropertyType;
135 if (t == typeof (object))
137 return GetBrowsableProperties (t);
141 if (list.Count > 0) {
142 return GetBrowsableProperties (list [0].GetType ());
145 return new PropertyDescriptorCollection (null);
148 public override void RemoveAt (int index)
150 list.RemoveAt (index);
153 public override void SuspendBinding ()
155 binding_suspended = true;
158 public override void ResumeBinding ()
160 binding_suspended = false;
163 internal override bool IsSuspended {
164 get { return binding_suspended; }
167 internal bool CanAddRows {
169 if (list as IBindingList == null) {
177 public override void AddNew ()
179 if (list as IBindingList == null)
180 throw new NotSupportedException ();
182 (list as IBindingList).AddNew ();
185 public override void CancelCurrentEdit ()
187 IEditableObject editable = Current as IEditableObject;
189 if (editable == null)
191 editable.CancelEdit ();
192 OnItemChanged (new ItemChangedEventArgs (Position));
195 public override void EndCurrentEdit ()
197 IEditableObject editable = Current as IEditableObject;
199 if (editable == null)
204 public void Refresh ()
209 [MonoTODO ("This is just a guess, as I can't figure out how to test this method")]
210 protected void CheckEmpty ()
212 if (list == null || list.Count < 1)
213 throw new Exception ("List is empty.");
217 protected internal override void OnCurrentChanged (EventArgs e)
221 if (onCurrentChangedHandler != null) {
222 onCurrentChangedHandler (this, e);
226 protected virtual void OnItemChanged (ItemChangedEventArgs e)
230 if (ItemChanged != null)
231 ItemChanged (this, e);
234 protected virtual void OnPositionChanged (EventArgs e)
236 if (onPositionChangedHandler == null)
238 onPositionChangedHandler (this, e);
241 protected internal override string GetListName (ArrayList accessors)
243 if (list is ITypedList) {
244 PropertyDescriptor [] pds;
245 pds = new PropertyDescriptor [accessors.Count];
246 accessors.CopyTo (pds, 0);
247 return ((ITypedList) list).GetListName (pds);
252 [MonoTODO ("Not totally sure how this works, its doesn't seemt to do a pull/push like i originally assumed")]
253 protected override void UpdateIsBinding ()
257 foreach (Binding binding in Bindings)
258 binding.UpdateIsBinding ();
261 private void UpdateItem ()
263 // Probably should be validating or something here
267 internal object GetItem (int index)
272 private PropertyDescriptorCollection GetBrowsableProperties (Type t)
274 Attribute [] att = new System.Attribute [1];
275 att [0] = new BrowsableAttribute (true);
276 return TypeDescriptor.GetProperties (t, att);
279 private void MetaDataChangedHandler (object sender, CollectionChangeEventArgs e)
281 if (MetaDataChanged != null)
282 MetaDataChanged (this, EventArgs.Empty);
285 public event ItemChangedEventHandler ItemChanged;
286 public event EventHandler MetaDataChanged;