2008-03-27 Carlos Alberto Cortez <calberto.cortez@gmail.com>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / ControlBindingsCollection.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 //      Jackson Harper (jackson@ximian.com)
24
25
26 using System;
27 using System.Collections;
28 using System.ComponentModel;
29 using System.Reflection;
30
31
32 namespace System.Windows.Forms {
33         [DefaultEvent("CollectionChanged")]
34         [Editor("System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing, typeof(System.Drawing.Design.UITypeEditor))]
35         [TypeConverter("System.Windows.Forms.Design.ControlBindingsConverter, " + Consts.AssemblySystem_Design)]
36         public class ControlBindingsCollection : BindingsCollection {
37                 #region Fields
38                 private Control control;
39 #if NET_2_0
40                 private IBindableComponent bindable_component;
41                 private DataSourceUpdateMode default_datasource_update_mode;
42 #endif
43                 #endregion      // Fields
44
45                 #region Constructors
46                 internal ControlBindingsCollection (Control control) {
47                         this.control = control;
48 #if NET_2_0
49                         bindable_component = control as IBindableComponent;
50                         default_datasource_update_mode = DataSourceUpdateMode.OnValidation;
51 #endif
52                 }
53
54 #if NET_2_0
55                 public ControlBindingsCollection (IBindableComponent control)
56                 {
57                         bindable_component = control;
58                         control = control as Control;
59                         default_datasource_update_mode = DataSourceUpdateMode.OnValidation;
60                 }
61 #endif
62                 #endregion      // Constructors
63
64                 #region Public Instance Properties
65                 public Control Control {
66                         get {
67                                 return control;
68                         }
69                 }
70
71                 public Binding this[string propertyName] {
72                         get {
73                                 foreach (Binding b in base.List) {
74                                         if (b.PropertyName == propertyName) {
75                                                 return b;
76                                         }
77                                 }
78                                 return null;
79                         }
80                 }
81
82 #if NET_2_0
83                 public IBindableComponent BindableComponent {
84                         get {
85                                 return bindable_component;
86                         }
87                 }
88
89                 public DataSourceUpdateMode DefaultDataSourceUpdateMode {
90                         get {
91                                 return default_datasource_update_mode;
92                         }
93                         set {
94                                 default_datasource_update_mode = value;
95                         }
96                 }
97 #endif
98                 #endregion      // Public Instance Properties
99
100                 #region Public Instance Methods
101                 public new void Add (Binding binding)
102                 {
103                         AddCore (binding);
104                         OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Add, binding));
105                 }
106
107                 public Binding Add (string propertyName, object dataSource, string dataMember)
108                 {
109                         if (dataSource == null)
110                                 throw new ArgumentNullException ("dataSource");
111
112                         Binding res = new Binding (propertyName, dataSource, dataMember);
113 #if NET_2_0
114                         res.DataSourceUpdateMode = default_datasource_update_mode;
115 #endif
116                         Add (res);
117                         return res;
118                 }
119
120 #if NET_2_0
121                 public Binding Add (string propertyName, object dataSource, string dataMember, bool formattingEnabled)
122                 {
123                         return Add (propertyName, dataSource, dataMember, formattingEnabled, default_datasource_update_mode, null, String.Empty, null);
124                 }
125
126                 public Binding Add (string propertyName, object dataSource, string dataMember, bool formattingEnabled, DataSourceUpdateMode updateMode)
127                 {
128                         return Add (propertyName, dataSource, dataMember, formattingEnabled, updateMode, null, String.Empty, null);
129                 }
130
131                 public Binding Add (string propertyName, object dataSource, string dataMember, bool formattingEnabled, DataSourceUpdateMode updateMode,
132                         object nullValue)
133                 {
134                         return Add (propertyName, dataSource, dataMember, formattingEnabled, updateMode, nullValue, String.Empty, null);
135                 }
136
137                 public Binding Add (string propertyName, object dataSource, string dataMember, bool formattingEnabled, DataSourceUpdateMode updateMode,
138                         object nullValue, string formatString)
139                 {
140                         return Add (propertyName, dataSource, dataMember, formattingEnabled, updateMode, nullValue, formatString, null);
141                 }
142
143                 public Binding Add (string propertyName, object dataSource, string dataMember, bool formattingEnabled, DataSourceUpdateMode updateMode,
144                         object nullValue, string formatString, IFormatProvider formatInfo)
145                 {
146                         if (dataSource == null)
147                                 throw new ArgumentNullException ("dataSource");
148
149                         Binding res = new Binding (propertyName, dataSource, dataMember);
150                         res.FormattingEnabled = formattingEnabled;
151                         res.DataSourceUpdateMode = updateMode;
152                         res.NullValue = nullValue;
153                         res.FormatString = formatString;
154                         res.FormatInfo = formatInfo;
155
156                         Add (res);
157                         return res;
158                 }
159 #endif
160
161                 public new void Clear() {
162                         base.Clear();
163                 }
164
165                 public new void Remove (Binding binding) {
166                         if (binding == null)
167                                 throw new NullReferenceException("The binding is null");
168
169                         base.Remove(binding);
170                 }
171
172                 public new void RemoveAt(int index) {
173                         if (index < 0 || index >= base.List.Count)
174                                 throw new ArgumentOutOfRangeException("index");
175
176                         base.RemoveAt(index);
177                 }
178                 #endregion      // Public Instance Methods
179
180                 #region Protected Instance Methods
181                 protected override void AddCore (Binding dataBinding)
182                 {
183                         if (dataBinding == null)
184                                 throw new ArgumentNullException ("dataBinding");
185
186 #if NET_2_0
187                         if (dataBinding.Control != null && dataBinding.BindableComponent != bindable_component)
188 #else
189                         if (dataBinding.Control != null && dataBinding.Control != control)
190 #endif
191                                 throw new ArgumentException ("dataBinding belongs to another BindingsCollection");
192
193                         for (int i = 0; i < Count; i++) {
194                                 Binding bnd = this [i];
195                                 if (bnd == null || bnd.PropertyName.Length == 0 || dataBinding.PropertyName.Length == 0) {
196                                         continue;
197                                 }
198
199                                 if (String.Compare (bnd.PropertyName, dataBinding.PropertyName, true) == 0) {
200                                         throw new ArgumentException ("The binding is already in the collection");
201                                 }
202                         }
203
204 #if NET_2_0
205                         dataBinding.SetControl (bindable_component);
206 #else
207                         dataBinding.SetControl (control);
208 #endif
209                         dataBinding.Check ();
210                         base.AddCore (dataBinding);
211                 }
212
213                 protected override void ClearCore() {
214                         base.ClearCore ();
215                 }
216
217                 protected override void RemoveCore (Binding dataBinding) {
218                         if (dataBinding == null)
219                                 throw new ArgumentNullException ("dataBinding");
220
221                         base.RemoveCore (dataBinding);
222                 }
223                 #endregion      // Protected Instance Methods
224         }
225 }
226