Merge remote branch 'upstream/master'
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / CurrencyManager.cs
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:
8 // 
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 // 
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.
19 //
20 // Copyright (c) 2005 Novell, Inc.
21 //
22 // Authors:
23 //      Jackson Harper (jackson@ximian.com)
24 //      Ivan N. Zlatev (contact@i-nz.net)
25 //
26
27 using System;
28 using System.Data;
29 using System.Reflection;
30 using System.Collections;
31 using System.ComponentModel;
32
33 namespace System.Windows.Forms {
34         public class CurrencyManager : BindingManagerBase {
35
36                 protected int listposition;
37                 protected Type finalType;
38
39                 private IList list;
40                 private bool binding_suspended;
41
42                 private object data_source;
43
44                 bool editing;
45
46                 internal CurrencyManager ()
47                 {
48                 }
49
50                 internal CurrencyManager (object data_source)
51                 {
52                         SetDataSource (data_source);
53                 }
54
55                 public IList List {
56                         get { return list; }
57                 }
58
59                 public override object Current {
60                         get {
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");
65                                 }
66                                 return list [listposition];
67                         }
68                 }
69
70                 public override int Count {
71                         get { return list.Count; }
72                 }
73
74                 public override int Position {
75                         get {
76                                 return listposition;
77                         } 
78                         set {
79                                 if (value < 0)
80                                         value = 0;
81                                 if (value >= list.Count)
82                                         value = list.Count - 1;
83                                 if (listposition == value)
84                                         return;
85
86                                 if (listposition != -1)
87                                         EndCurrentEdit ();
88
89                                 listposition = value;
90                                 OnCurrentChanged (EventArgs.Empty);
91                                 OnPositionChanged (EventArgs.Empty);
92                         }
93                 }
94
95                 internal void SetDataSource (object data_source)
96                 {
97                         if (this.data_source is IBindingList)
98                                 ((IBindingList)this.data_source).ListChanged -= new ListChangedEventHandler (ListChangedHandler);
99
100                         if (data_source is IListSource)
101                                 data_source = ((IListSource)data_source).GetList();
102
103                         this.data_source = data_source;
104                         if (data_source != null)
105                                 this.finalType = data_source.GetType();
106
107                         listposition = -1;
108                         if (this.data_source is IBindingList)
109                                 ((IBindingList)this.data_source).ListChanged += new ListChangedEventHandler (ListChangedHandler);
110
111                         list = (IList)data_source;
112
113                         // XXX this is wrong.  MS invokes OnItemChanged directly, which seems to call PushData.
114                         ListChangedHandler (null, new ListChangedEventArgs (ListChangedType.Reset, -1));
115                 }
116
117                 public override PropertyDescriptorCollection GetItemProperties ()
118                 {
119                         return ListBindingHelper.GetListItemProperties (list);
120                 }
121
122                 public override void RemoveAt (int index)
123                 {
124                         list.RemoveAt (index);
125                 }
126
127                 public override void SuspendBinding ()
128                 {
129                         binding_suspended = true;
130                 }
131                 
132                 public override void ResumeBinding ()
133                 {
134                         binding_suspended = false;
135                 }
136
137                 internal override bool IsSuspended {
138                         get {
139                                 // Always return true if we don't have items
140                                 if (Count == 0)
141                                         return true;
142
143                                 return binding_suspended;
144                         }
145                 }
146
147                 internal bool AllowNew {
148                         get {
149                                 if (list is IBindingList)
150                                         return ((IBindingList)list).AllowNew;
151
152                                 if (list.IsReadOnly)
153                                         return false;
154
155                                 return false;
156                         }
157                 }
158
159                 internal bool AllowRemove {
160                         get {
161                                 if (list.IsReadOnly)
162                                         return false;
163
164                                 if (list is IBindingList)
165                                         return ((IBindingList)list).AllowRemove;
166
167                                 return false;
168                         }
169                 }
170
171                 internal bool AllowEdit {
172                         get {
173                                 if (list is IBindingList)
174                                         return ((IBindingList)list).AllowEdit;
175
176                                 return false;
177                         }
178                 }
179
180                 public override void AddNew ()
181                 {
182                         IBindingList ibl = list as IBindingList;
183
184                         if (ibl == null)
185                                 throw new NotSupportedException ();
186
187                         ibl.AddNew ();
188
189                         bool validate = (Position != (list.Count - 1));
190                         ChangeRecordState (list.Count - 1, validate, validate, true, true);
191                 }
192
193
194                 void BeginEdit ()
195                 {
196                         IEditableObject editable = Current as IEditableObject;
197
198                         if (editable != null) {
199                                 try {
200                                         editable.BeginEdit ();
201                                         editing = true;
202                                 }
203                                 catch {
204                                         /* swallow exceptions in IEditableObject.BeginEdit () */
205                                 }
206                         }
207                 }
208
209                 public override void CancelCurrentEdit ()
210                 {
211                         if (listposition == -1)
212                                 return;
213
214                         IEditableObject editable = Current as IEditableObject;
215
216                         if (editable != null) {
217                                 editing = false;
218                                 editable.CancelEdit ();
219                                 OnItemChanged (new ItemChangedEventArgs (Position));
220                         }
221                         if (list is ICancelAddNew)
222                                 ((ICancelAddNew)list).CancelNew (listposition);
223                 }
224                 
225                 public override void EndCurrentEdit ()
226                 {
227                         if (listposition == -1)
228                                 return;
229
230                         IEditableObject editable = Current as IEditableObject;
231
232                         if (editable != null) {
233                                 editing = false;
234                                 editable.EndEdit ();
235                         }
236
237                         if (list is ICancelAddNew)
238                                 ((ICancelAddNew)list).EndNew (listposition);
239                 }
240
241                 public void Refresh ()
242                 {
243                         ListChangedHandler (null, new ListChangedEventArgs (ListChangedType.Reset, -1));
244                 }
245
246                 protected void CheckEmpty ()
247                 {
248                         if (list == null || list.Count < 1)
249                                 throw new Exception ("List is empty.");
250                                 
251                 }
252
253                 protected internal override void OnCurrentChanged (EventArgs e)
254                 {
255                         if (onCurrentChangedHandler != null) {
256                                 onCurrentChangedHandler (this, e);
257                         }
258
259                         // don't call OnCurrentItemChanged here, as it can be overridden
260                         if (onCurrentItemChangedHandler != null) {
261                                 onCurrentItemChangedHandler (this, e);
262                         }
263
264                 }
265
266                 protected override void OnCurrentItemChanged (EventArgs e)
267                 {
268                         if (onCurrentItemChangedHandler != null) {
269                                 onCurrentItemChangedHandler (this, e);
270                         }
271                 }
272
273                 protected virtual void OnItemChanged (ItemChangedEventArgs e)
274                 {
275                         if (ItemChanged != null)
276                                 ItemChanged (this, e);
277
278                         transfering_data = true;
279                         PushData ();
280                         transfering_data = false;
281                 }
282
283                 void OnListChanged (ListChangedEventArgs args)
284                 {
285                         if (ListChanged != null)
286                                 ListChanged (this, args);
287                 }
288
289                 protected virtual void OnPositionChanged (EventArgs e)
290                 {
291                         if (onPositionChangedHandler != null)
292                                 onPositionChangedHandler (this, e);
293                 }
294
295                 protected internal override string GetListName (ArrayList listAccessors)
296                 {
297                         if (list is ITypedList) {
298                                 PropertyDescriptor [] pds = null;
299                                 if (listAccessors != null) {
300                                         pds = new PropertyDescriptor [listAccessors.Count];
301                                         listAccessors.CopyTo (pds, 0);
302                                 }
303                                 return ((ITypedList) list).GetListName (pds);
304                         }
305                         else if (finalType != null) {
306                                 return finalType.Name;
307                         }
308                         return String.Empty;
309                 }
310
311                 protected override void UpdateIsBinding ()
312                 {
313                         UpdateItem ();
314
315                         foreach (Binding binding in Bindings)
316                                 binding.UpdateIsBinding ();
317
318                         ChangeRecordState (listposition, false, false, true, false);
319
320                         OnItemChanged (new ItemChangedEventArgs (-1));
321                 }
322
323                 private void ChangeRecordState (int newPosition,
324                                                 bool validating,
325                                                 bool endCurrentEdit,
326                                                 bool firePositionChanged,
327                                                 bool pullData)
328                 {
329                         if (endCurrentEdit)
330                                 EndCurrentEdit ();
331
332                         int old_index = listposition;
333
334                         listposition = newPosition;
335
336                         if (listposition >= list.Count)
337                                 listposition = list.Count - 1;
338
339                         if (old_index != -1 && listposition != -1)
340                                 OnCurrentChanged (EventArgs.Empty);
341
342                         if (firePositionChanged)
343                                 OnPositionChanged (EventArgs.Empty);
344                 }
345
346                 private void UpdateItem ()
347                 {
348                         // Probably should be validating or something here
349                         if (!transfering_data && listposition == -1 && list.Count > 0) {
350                                 listposition = 0;
351                                 BeginEdit ();
352                         }
353                 }
354                 
355                 internal object this [int index] {
356                         get { return list [index]; }
357                 }               
358                 
359                 private PropertyDescriptorCollection GetBrowsableProperties (Type t)
360                 {
361                         Attribute [] att = new System.Attribute [1];
362                         att [0] = new BrowsableAttribute (true);
363                         return TypeDescriptor.GetProperties (t, att);
364                 }
365
366                 protected void OnMetaDataChanged (EventArgs e)
367                 {
368                         if (MetaDataChanged != null)
369                                 MetaDataChanged (this, e);
370                 }
371
372                 private void ListChangedHandler (object sender, ListChangedEventArgs e)
373                 {
374                         switch (e.ListChangedType) {
375                         case ListChangedType.PropertyDescriptorAdded:
376                         case ListChangedType.PropertyDescriptorDeleted:
377                         case ListChangedType.PropertyDescriptorChanged:
378                                 OnMetaDataChanged (EventArgs.Empty);
379                                 OnListChanged (e);
380                                 break;
381                         case ListChangedType.ItemDeleted:
382                                 if (list.Count == 0) {
383                                         /* the last row was deleted */
384                                         listposition = -1;
385                                         UpdateIsBinding ();
386
387                                         OnPositionChanged (EventArgs.Empty);
388                                         OnCurrentChanged (EventArgs.Empty);
389                                 }
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);
395                                 }
396                                 else {
397                                         /* the deleted row was after the current one, so we don't
398                                            need to update bound controls for Position/Current changed */
399                                 }
400
401                                 OnItemChanged (new ItemChangedEventArgs (-1));
402                                 OnListChanged (e);
403                                 break;
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);
409
410                                         OnItemChanged (new ItemChangedEventArgs (-1));
411                                         OnListChanged (e);
412                                 }
413                                 else {
414                                         if (e.NewIndex <= listposition) {
415                                                 ChangeRecordState (e.NewIndex,
416                                                                    false, false, false, false);
417                                                 OnItemChanged (new ItemChangedEventArgs (-1));
418                                                 OnListChanged (e);
419                                                 OnPositionChanged (EventArgs.Empty);
420                                         }
421                                         else {
422                                                 OnItemChanged (new ItemChangedEventArgs (-1));
423                                                 OnListChanged (e);
424                                         }
425                                 }
426
427                                 break;
428                         case ListChangedType.ItemChanged:
429                                 if (editing) {
430                                         if (e.NewIndex == listposition)
431                                                 OnCurrentItemChanged (EventArgs.Empty);
432                                         OnItemChanged (new ItemChangedEventArgs (e.NewIndex));
433                                 }
434                                 OnListChanged (e);
435                                 break;
436                         case ListChangedType.Reset:
437                                 PushData();
438                                 UpdateIsBinding();
439                                 OnListChanged (e);
440                                 break;
441                         default:
442                                 OnListChanged (e);
443                                 break;
444                         }
445                 }
446
447                 public event ListChangedEventHandler ListChanged;
448                 public event ItemChangedEventHandler ItemChanged;
449                 public event EventHandler MetaDataChanged;
450         }
451 }
452