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, string data_member)
44 if (data_source is IList) {
45 list = (IList) data_source;
46 } else if (data_source is IListSource) {
47 list = ((IListSource) data_source).GetList ();
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;
69 DataSet dataset = data_source as DataSet;
70 int sp = data_member.IndexOf ('.');
72 data_member = data_member.Substring (0, sp);
74 if (dataset != null) {
75 table = dataset.Tables [data_member];
80 list = new DataView (table);
81 table.Columns.CollectionChanged += new CollectionChangeEventHandler (MetaDataChangedHandler);
82 table.ChildRelations.CollectionChanged += new CollectionChangeEventHandler (MetaDataChangedHandler);
83 table.ParentRelations.CollectionChanged += new CollectionChangeEventHandler (MetaDataChangedHandler);
84 table.Constraints.CollectionChanged += new CollectionChangeEventHandler (MetaDataChangedHandler);
92 public override object Current {
94 return list [listposition];
98 public override int Count {
99 get { return list.Count; }
102 public override int Position {
109 if (value == list.Count)
110 value = list.Count - 1;
111 if (listposition == value)
113 listposition = value;
114 OnCurrentChanged (EventArgs.Empty);
115 OnPositionChanged (EventArgs.Empty);
119 internal string ListName {
121 ITypedList typed = list as ITypedList;
124 return finalType.Name;
126 return typed.GetListName (null);
131 public override PropertyDescriptorCollection GetItemProperties ()
133 ITypedList typed = list as ITypedList;
136 Type element = list.GetType ().GetElementType ();
137 return TypeDescriptor.GetProperties (element);
141 return typed.GetItemProperties (null);
144 PropertyInfo [] props = finalType.GetProperties ();
145 for (int i = 0; i < props.Length; i++) {
146 if (props [i].Name == "Item") {
147 Type t = props [i].PropertyType;
148 if (t == typeof (object))
150 return GetBrowsableProperties (t);
154 if (list.Count > 0) {
155 return GetBrowsableProperties (list [0].GetType ());
158 return new PropertyDescriptorCollection (null);
161 public override void RemoveAt (int index)
163 list.RemoveAt (index);
166 public override void SuspendBinding ()
168 binding_suspended = true;
171 public override void ResumeBinding ()
173 binding_suspended = false;
176 internal override bool IsSuspended {
177 get { return binding_suspended; }
180 internal bool CanAddRows {
182 if (list as IBindingList == null) {
190 public override void AddNew ()
192 if (list as IBindingList == null)
193 throw new NotSupportedException ();
195 (list as IBindingList).AddNew ();
198 public override void CancelCurrentEdit ()
200 IEditableObject editable = Current as IEditableObject;
202 if (editable == null)
204 editable.CancelEdit ();
205 OnItemChanged (new ItemChangedEventArgs (Position));
208 public override void EndCurrentEdit ()
210 IEditableObject editable = Current as IEditableObject;
212 if (editable == null)
217 public void Refresh ()
222 [MonoTODO ("This is just a guess, as I can't figure out how to test this method")]
223 protected void CheckEmpty ()
225 if (list == null || list.Count < 1)
226 throw new Exception ("List is empty.");
230 protected internal override void OnCurrentChanged (EventArgs e)
234 if (onCurrentChangedHandler != null) {
235 onCurrentChangedHandler (this, e);
239 protected virtual void OnItemChanged (ItemChangedEventArgs e)
243 if (ItemChanged != null)
244 ItemChanged (this, e);
247 protected virtual void OnPositionChanged (EventArgs e)
249 if (onPositionChangedHandler == null)
251 onPositionChangedHandler (this, e);
254 protected internal override string GetListName (ArrayList accessors)
256 if (list is ITypedList) {
257 PropertyDescriptor [] pds;
258 pds = new PropertyDescriptor [accessors.Count];
259 accessors.CopyTo (pds, 0);
260 return ((ITypedList) list).GetListName (pds);
265 [MonoTODO ("Not totally sure how this works, its doesn't seemt to do a pull/push like i originally assumed")]
266 protected override void UpdateIsBinding ()
270 foreach (Binding binding in Bindings)
271 binding.UpdateIsBinding ();
274 private void UpdateItem ()
276 // Probably should be validating or something here
280 internal object GetItem (int index)
285 private PropertyDescriptorCollection GetBrowsableProperties (Type t)
287 Attribute [] att = new System.Attribute [1];
288 att [0] = new BrowsableAttribute (true);
289 return TypeDescriptor.GetProperties (t, att);
292 private void MetaDataChangedHandler (object sender, CollectionChangeEventArgs e)
294 if (MetaDataChanged != null)
295 MetaDataChanged (this, EventArgs.Empty);
298 public event ItemChangedEventHandler ItemChanged;
299 public event EventHandler MetaDataChanged;