2008-03-27 Carlos Alberto Cortez <calberto.cortez@gmail.com>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / BindingManagerBase.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) 2004-2005 Novell, Inc.
21 //
22 // Authors:
23 //      Peter Bartok    pbartok@novell.com
24 //      Jackson Harper  jackson@ximian.com
25 //
26
27
28 // NOT COMPLETE
29
30 using System.ComponentModel;
31 using System.Collections;
32
33 namespace System.Windows.Forms
34 {
35         public abstract class BindingManagerBase
36         {
37                 private BindingsCollection      bindings;
38                 internal bool transfering_data; /* true if we're pushing or pulling data */
39
40                 #region Public Constructors
41                 public BindingManagerBase()
42                 {
43                 }
44                 #endregion      // Public Constructors
45
46                 #region Protected Instance Fields
47                 protected EventHandler onCurrentChangedHandler;
48                 protected EventHandler onPositionChangedHandler;
49 #if NET_2_0
50                 internal EventHandler onCurrentItemChangedHandler;
51 #endif
52                 #endregion      // Protected Instance Fields
53
54                 #region Public Instance Properties
55                 public BindingsCollection Bindings {
56                         get {
57                                 if (bindings == null) {
58                                         bindings = new BindingsCollection ();
59                                 }
60                                 return bindings;
61                         }
62                 }
63
64                 public abstract int Count {
65                         get;
66                 }
67
68                 public abstract object Current {
69                         get;
70                 }
71
72 #if NET_2_0
73                 public bool IsBindingSuspended {
74                         get {
75                                 return IsSuspended;
76                         }
77                 }
78 #endif
79
80                 public abstract int Position {
81                         get; set;
82                 }
83                 #endregion      // Public Instance Properties
84
85                 #region Public Instance Methods
86                 public abstract void AddNew();
87
88                 public abstract void CancelCurrentEdit();
89
90                 public abstract void EndCurrentEdit();
91
92 #if NET_2_0
93                 public virtual PropertyDescriptorCollection GetItemProperties()
94                 {
95                         throw new NotImplementedException ();
96                 }
97 #else
98                 public abstract PropertyDescriptorCollection GetItemProperties();
99 #endif
100
101                 public abstract void RemoveAt(int index);
102
103                 public abstract void ResumeBinding();
104
105                 public abstract void SuspendBinding();
106                 #endregion      // Public Instance Methods
107
108                 internal virtual bool IsSuspended {
109                         get {
110                                 return false;
111                         }
112                 }
113
114                 #region Protected Instance Methods
115                 [MonoTODO]
116                 protected internal virtual PropertyDescriptorCollection GetItemProperties (ArrayList dataSources, ArrayList listAccessors)
117                 {
118                         throw new NotImplementedException();
119                 }
120
121                 [MonoTODO]
122                 protected virtual PropertyDescriptorCollection GetItemProperties (Type listType, int offset, ArrayList dataSources, ArrayList listAccessors)
123                 {
124                         throw new NotImplementedException();
125                 }
126
127                 protected internal abstract string GetListName (ArrayList listAccessors);
128
129                 protected internal abstract void OnCurrentChanged (EventArgs e);
130
131                 protected void PullData()
132                 {
133                         try {
134                                 if (!transfering_data) {
135                                         transfering_data = true;
136                                         UpdateIsBinding ();
137                                 }
138                                 foreach (Binding binding in Bindings) {
139                                         binding.PullData ();
140                                 }
141                         } finally {
142                                 transfering_data = false;
143                         }
144                 }
145
146                 protected void PushData()
147                 {
148                         try {
149                                 if (!transfering_data) {
150                                         transfering_data = true;
151                                         UpdateIsBinding ();
152                                 }
153                                 foreach (Binding binding in Bindings) {
154                                         binding.PushData ();
155                                 }
156                         } finally {
157                                 transfering_data = false;
158                         }
159                 }
160
161
162 #if NET_2_0
163                 protected void OnBindingComplete (BindingCompleteEventArgs args)
164                 {
165                         if (BindingComplete != null)
166                                 BindingComplete (this, args);
167                 }
168
169                 protected abstract void OnCurrentItemChanged (EventArgs e);
170
171                 protected void OnDataError (Exception e)
172                 {
173                         if (DataError != null)
174                                 DataError (this, new BindingManagerDataErrorEventArgs (e));
175                 }
176 #endif
177
178                 protected abstract void UpdateIsBinding();
179                 #endregion      // Protected Instance Methods
180
181                 internal void AddBinding (Binding binding)
182                 {
183                         if (Bindings.Contains (binding))
184                                 return;
185                         Bindings.Add (binding);
186                 }
187
188                 #region Events
189                 public event EventHandler CurrentChanged {
190                         add { onCurrentChangedHandler += value; }
191                         remove { onCurrentChangedHandler -= value; }
192                 }
193
194                 public event EventHandler PositionChanged {
195                         add { onPositionChangedHandler += value; }
196                         remove { onPositionChangedHandler -= value; }
197                 }
198
199 #if NET_2_0
200                 public event EventHandler CurrentItemChanged {
201                         add { onCurrentItemChangedHandler += value; }
202                         remove { onCurrentItemChangedHandler -= value; }
203                 }
204
205                 public event BindingCompleteEventHandler BindingComplete;
206                 public event BindingManagerDataErrorEventHandler DataError;
207 #endif
208                 #endregion      // Events
209         }
210 }