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)
24 // Ivan N. Zlatev (contact@i-nz.net)
29 using System.Reflection;
30 using System.Collections;
31 using System.ComponentModel;
33 namespace System.Windows.Forms {
34 public class CurrencyManager : BindingManagerBase {
36 protected int listposition;
37 protected Type finalType;
40 private bool binding_suspended;
42 private object data_source;
46 internal CurrencyManager ()
50 internal CurrencyManager (object data_source)
52 SetDataSource (data_source);
59 public override object Current {
61 if (listposition == -1 || listposition >= list.Count) {
62 // Console.WriteLine ("throwing exception from here");
63 // Console.WriteLine (Environment.StackTrace);
64 throw new IndexOutOfRangeException ("list position");
66 return list [listposition];
70 public override int Count {
71 get { return list.Count; }
74 public override int Position {
81 if (value >= list.Count)
82 value = list.Count - 1;
83 if (listposition == value)
86 if (listposition != -1)
90 OnCurrentChanged (EventArgs.Empty);
91 OnPositionChanged (EventArgs.Empty);
95 internal void SetDataSource (object data_source)
97 if (this.data_source is IBindingList)
98 ((IBindingList)this.data_source).ListChanged -= new ListChangedEventHandler (ListChangedHandler);
100 if (data_source is IListSource)
101 data_source = ((IListSource)data_source).GetList();
103 this.data_source = data_source;
104 if (data_source != null)
105 this.finalType = data_source.GetType();
108 if (this.data_source is IBindingList)
109 ((IBindingList)this.data_source).ListChanged += new ListChangedEventHandler (ListChangedHandler);
111 list = (IList)data_source;
113 // XXX this is wrong. MS invokes OnItemChanged directly, which seems to call PushData.
114 ListChangedHandler (null, new ListChangedEventArgs (ListChangedType.Reset, -1));
117 public override PropertyDescriptorCollection GetItemProperties ()
119 return ListBindingHelper.GetListItemProperties (list);
122 public override void RemoveAt (int index)
124 list.RemoveAt (index);
127 public override void SuspendBinding ()
129 binding_suspended = true;
132 public override void ResumeBinding ()
134 binding_suspended = false;
137 internal override bool IsSuspended {
139 // Always return true if we don't have items
143 return binding_suspended;
147 internal bool AllowNew {
149 if (list is IBindingList)
150 return ((IBindingList)list).AllowNew;
159 internal bool AllowRemove {
164 if (list is IBindingList)
165 return ((IBindingList)list).AllowRemove;
171 internal bool AllowEdit {
173 if (list is IBindingList)
174 return ((IBindingList)list).AllowEdit;
180 public override void AddNew ()
182 IBindingList ibl = list as IBindingList;
185 throw new NotSupportedException ();
189 bool validate = (Position != (list.Count - 1));
190 ChangeRecordState (list.Count - 1, validate, validate, true, true);
196 IEditableObject editable = Current as IEditableObject;
198 if (editable != null) {
200 editable.BeginEdit ();
204 /* swallow exceptions in IEditableObject.BeginEdit () */
209 public override void CancelCurrentEdit ()
211 if (listposition == -1)
214 IEditableObject editable = Current as IEditableObject;
216 if (editable != null) {
218 editable.CancelEdit ();
219 OnItemChanged (new ItemChangedEventArgs (Position));
221 if (list is ICancelAddNew)
222 ((ICancelAddNew)list).CancelNew (listposition);
225 public override void EndCurrentEdit ()
227 if (listposition == -1)
230 IEditableObject editable = Current as IEditableObject;
232 if (editable != null) {
237 if (list is ICancelAddNew)
238 ((ICancelAddNew)list).EndNew (listposition);
241 public void Refresh ()
243 ListChangedHandler (null, new ListChangedEventArgs (ListChangedType.Reset, -1));
246 protected void CheckEmpty ()
248 if (list == null || list.Count < 1)
249 throw new Exception ("List is empty.");
253 protected internal override void OnCurrentChanged (EventArgs e)
255 if (onCurrentChangedHandler != null) {
256 onCurrentChangedHandler (this, e);
259 // don't call OnCurrentItemChanged here, as it can be overridden
260 if (onCurrentItemChangedHandler != null) {
261 onCurrentItemChangedHandler (this, e);
266 protected override void OnCurrentItemChanged (EventArgs e)
268 if (onCurrentItemChangedHandler != null) {
269 onCurrentItemChangedHandler (this, e);
273 protected virtual void OnItemChanged (ItemChangedEventArgs e)
275 if (ItemChanged != null)
276 ItemChanged (this, e);
278 transfering_data = true;
280 transfering_data = false;
283 void OnListChanged (ListChangedEventArgs args)
285 if (ListChanged != null)
286 ListChanged (this, args);
289 protected virtual void OnPositionChanged (EventArgs e)
291 if (onPositionChangedHandler != null)
292 onPositionChangedHandler (this, e);
295 protected internal override string GetListName (ArrayList listAccessors)
297 if (list is ITypedList) {
298 PropertyDescriptor [] pds = null;
299 if (listAccessors != null) {
300 pds = new PropertyDescriptor [listAccessors.Count];
301 listAccessors.CopyTo (pds, 0);
303 return ((ITypedList) list).GetListName (pds);
305 else if (finalType != null) {
306 return finalType.Name;
311 protected override void UpdateIsBinding ()
315 foreach (Binding binding in Bindings)
316 binding.UpdateIsBinding ();
318 ChangeRecordState (listposition, false, false, true, false);
320 OnItemChanged (new ItemChangedEventArgs (-1));
323 private void ChangeRecordState (int newPosition,
326 bool firePositionChanged,
332 int old_index = listposition;
334 listposition = newPosition;
336 if (listposition >= list.Count)
337 listposition = list.Count - 1;
339 if (old_index != -1 && listposition != -1)
340 OnCurrentChanged (EventArgs.Empty);
342 if (firePositionChanged)
343 OnPositionChanged (EventArgs.Empty);
346 private void UpdateItem ()
348 // Probably should be validating or something here
349 if (!transfering_data && listposition == -1 && list.Count > 0) {
355 internal object this [int index] {
356 get { return list [index]; }
359 private PropertyDescriptorCollection GetBrowsableProperties (Type t)
361 Attribute [] att = new System.Attribute [1];
362 att [0] = new BrowsableAttribute (true);
363 return TypeDescriptor.GetProperties (t, att);
366 protected void OnMetaDataChanged (EventArgs e)
368 if (MetaDataChanged != null)
369 MetaDataChanged (this, e);
372 private void ListChangedHandler (object sender, ListChangedEventArgs e)
374 switch (e.ListChangedType) {
375 case ListChangedType.PropertyDescriptorAdded:
376 case ListChangedType.PropertyDescriptorDeleted:
377 case ListChangedType.PropertyDescriptorChanged:
378 OnMetaDataChanged (EventArgs.Empty);
381 case ListChangedType.ItemDeleted:
382 if (list.Count == 0) {
383 /* the last row was deleted */
387 OnPositionChanged (EventArgs.Empty);
388 OnCurrentChanged (EventArgs.Empty);
390 else if (e.NewIndex <= listposition) {
391 /* the deleted row was either the current one, or one earlier in the list.
392 Update the index and emit PositionChanged, CurrentChanged, and ItemChanged. */
393 ChangeRecordState (listposition+1,
394 false, false, e.NewIndex != listposition, false);
397 /* the deleted row was after the current one, so we don't
398 need to update bound controls for Position/Current changed */
401 OnItemChanged (new ItemChangedEventArgs (-1));
404 case ListChangedType.ItemAdded:
405 if (list.Count == 1) {
406 /* it's the first one we've added */
407 ChangeRecordState (e.NewIndex,
408 false, false, true, false);
410 OnItemChanged (new ItemChangedEventArgs (-1));
414 if (e.NewIndex <= listposition) {
415 ChangeRecordState (e.NewIndex,
416 false, false, false, false);
417 OnItemChanged (new ItemChangedEventArgs (-1));
419 OnPositionChanged (EventArgs.Empty);
422 OnItemChanged (new ItemChangedEventArgs (-1));
428 case ListChangedType.ItemChanged:
430 if (e.NewIndex == listposition)
431 OnCurrentItemChanged (EventArgs.Empty);
432 OnItemChanged (new ItemChangedEventArgs (e.NewIndex));
436 case ListChangedType.Reset:
447 public event ListChangedEventHandler ListChanged;
448 public event ItemChangedEventHandler ItemChanged;
449 public event EventHandler MetaDataChanged;