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 ((DataView) list).ListChanged += new ListChangedEventHandler (ListChangedHandler);
82 table.Columns.CollectionChanged += new CollectionChangeEventHandler (MetaDataChangedHandler);
83 table.ChildRelations.CollectionChanged += new CollectionChangeEventHandler (MetaDataChangedHandler);
84 table.ParentRelations.CollectionChanged += new CollectionChangeEventHandler (MetaDataChangedHandler);
85 table.Constraints.CollectionChanged += new CollectionChangeEventHandler (MetaDataChangedHandler);
93 public override object Current {
97 return list [listposition];
101 public override int Count {
102 get { return list.Count; }
105 public override int Position {
112 if (value == list.Count)
113 value = list.Count - 1;
114 if (listposition == value)
116 listposition = value;
117 OnCurrentChanged (EventArgs.Empty);
118 OnPositionChanged (EventArgs.Empty);
122 internal string ListName {
124 ITypedList typed = list as ITypedList;
127 return finalType.Name;
129 return typed.GetListName (null);
134 public override PropertyDescriptorCollection GetItemProperties ()
136 ITypedList typed = list as ITypedList;
139 Type element = list.GetType ().GetElementType ();
140 return TypeDescriptor.GetProperties (element);
144 return typed.GetItemProperties (null);
147 PropertyInfo [] props = finalType.GetProperties ();
148 for (int i = 0; i < props.Length; i++) {
149 if (props [i].Name == "Item") {
150 Type t = props [i].PropertyType;
151 if (t == typeof (object))
153 return GetBrowsableProperties (t);
157 if (list.Count > 0) {
158 return GetBrowsableProperties (list [0].GetType ());
161 return new PropertyDescriptorCollection (null);
164 public override void RemoveAt (int index)
166 list.RemoveAt (index);
169 public override void SuspendBinding ()
171 binding_suspended = true;
174 public override void ResumeBinding ()
176 binding_suspended = false;
179 internal override bool IsSuspended {
180 get { return binding_suspended; }
183 internal bool CanAddRows {
185 if (list as IBindingList == null) {
193 public override void AddNew ()
195 if (list as IBindingList == null)
196 throw new NotSupportedException ();
198 (list as IBindingList).AddNew ();
201 public override void CancelCurrentEdit ()
203 IEditableObject editable = Current as IEditableObject;
205 if (editable == null)
207 editable.CancelEdit ();
208 OnItemChanged (new ItemChangedEventArgs (Position));
211 public override void EndCurrentEdit ()
213 IEditableObject editable = Current as IEditableObject;
215 if (editable == null)
220 public void Refresh ()
225 protected void CheckEmpty ()
227 if (list == null || list.Count < 1)
228 throw new Exception ("List is empty.");
232 protected internal override void OnCurrentChanged (EventArgs e)
236 if (onCurrentChangedHandler != null) {
237 onCurrentChangedHandler (this, e);
241 protected virtual void OnItemChanged (ItemChangedEventArgs e)
245 if (ItemChanged != null)
246 ItemChanged (this, e);
249 protected virtual void OnPositionChanged (EventArgs e)
251 if (onPositionChangedHandler == null)
253 onPositionChangedHandler (this, e);
256 protected internal override string GetListName (ArrayList accessors)
258 if (list is ITypedList) {
259 PropertyDescriptor [] pds;
260 pds = new PropertyDescriptor [accessors.Count];
261 accessors.CopyTo (pds, 0);
262 return ((ITypedList) list).GetListName (pds);
267 protected override void UpdateIsBinding ()
271 foreach (Binding binding in Bindings)
272 binding.UpdateIsBinding ();
275 private void UpdateItem ()
277 // Probably should be validating or something here
281 internal object GetItem (int index)
286 private PropertyDescriptorCollection GetBrowsableProperties (Type t)
288 Attribute [] att = new System.Attribute [1];
289 att [0] = new BrowsableAttribute (true);
290 return TypeDescriptor.GetProperties (t, att);
293 private void MetaDataChangedHandler (object sender, CollectionChangeEventArgs e)
295 if (MetaDataChanged != null)
296 MetaDataChanged (this, EventArgs.Empty);
299 private void ListChangedHandler (object sender, ListChangedEventArgs e)
304 public event ItemChangedEventHandler ItemChanged;
305 public event EventHandler MetaDataChanged;