Merge pull request #268 from pcc/menudeactivate
[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                 private IBindableComponent bindable_component;
40                 private DataSourceUpdateMode default_datasource_update_mode;
41                 #endregion      // Fields
42
43                 #region Constructors
44                 internal ControlBindingsCollection (Control control) {
45                         this.control = control;
46                         bindable_component = control as IBindableComponent;
47                         default_datasource_update_mode = DataSourceUpdateMode.OnValidation;
48                 }
49
50                 public ControlBindingsCollection (IBindableComponent control)
51                 {
52                         bindable_component = control;
53                         control = control as Control;
54                         default_datasource_update_mode = DataSourceUpdateMode.OnValidation;
55                 }
56                 #endregion      // Constructors
57
58                 #region Public Instance Properties
59                 public Control Control {
60                         get {
61                                 return control;
62                         }
63                 }
64
65                 public Binding this[string propertyName] {
66                         get {
67                                 foreach (Binding b in base.List) {
68                                         if (b.PropertyName == propertyName) {
69                                                 return b;
70                                         }
71                                 }
72                                 return null;
73                         }
74                 }
75
76                 public IBindableComponent BindableComponent {
77                         get {
78                                 return bindable_component;
79                         }
80                 }
81
82                 public DataSourceUpdateMode DefaultDataSourceUpdateMode {
83                         get {
84                                 return default_datasource_update_mode;
85                         }
86                         set {
87                                 default_datasource_update_mode = value;
88                         }
89                 }
90                 #endregion      // Public Instance Properties
91
92                 #region Public Instance Methods
93                 public new void Add (Binding binding)
94                 {
95                         AddCore (binding);
96                         OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Add, binding));
97                 }
98
99                 public Binding Add (string propertyName, object dataSource, string dataMember)
100                 {
101                         if (dataSource == null)
102                                 throw new ArgumentNullException ("dataSource");
103
104                         Binding res = new Binding (propertyName, dataSource, dataMember);
105                         res.DataSourceUpdateMode = default_datasource_update_mode;
106                         Add (res);
107                         return res;
108                 }
109
110                 public Binding Add (string propertyName, object dataSource, string dataMember, bool formattingEnabled)
111                 {
112                         return Add (propertyName, dataSource, dataMember, formattingEnabled, default_datasource_update_mode, null, String.Empty, null);
113                 }
114
115                 public Binding Add (string propertyName, object dataSource, string dataMember, bool formattingEnabled, DataSourceUpdateMode updateMode)
116                 {
117                         return Add (propertyName, dataSource, dataMember, formattingEnabled, updateMode, null, String.Empty, null);
118                 }
119
120                 public Binding Add (string propertyName, object dataSource, string dataMember, bool formattingEnabled, DataSourceUpdateMode updateMode,
121                         object nullValue)
122                 {
123                         return Add (propertyName, dataSource, dataMember, formattingEnabled, updateMode, nullValue, String.Empty, null);
124                 }
125
126                 public Binding Add (string propertyName, object dataSource, string dataMember, bool formattingEnabled, DataSourceUpdateMode updateMode,
127                         object nullValue, string formatString)
128                 {
129                         return Add (propertyName, dataSource, dataMember, formattingEnabled, updateMode, nullValue, formatString, null);
130                 }
131
132                 public Binding Add (string propertyName, object dataSource, string dataMember, bool formattingEnabled, DataSourceUpdateMode updateMode,
133                         object nullValue, string formatString, IFormatProvider formatInfo)
134                 {
135                         if (dataSource == null)
136                                 throw new ArgumentNullException ("dataSource");
137
138                         Binding res = new Binding (propertyName, dataSource, dataMember);
139                         res.FormattingEnabled = formattingEnabled;
140                         res.DataSourceUpdateMode = updateMode;
141                         res.NullValue = nullValue;
142                         res.FormatString = formatString;
143                         res.FormatInfo = formatInfo;
144
145                         Add (res);
146                         return res;
147                 }
148
149                 public new void Clear() {
150                         base.Clear();
151                 }
152
153                 public new void Remove (Binding binding) {
154                         if (binding == null)
155                                 throw new NullReferenceException("The binding is null");
156
157                         base.Remove(binding);
158                 }
159
160                 public new void RemoveAt(int index) {
161                         if (index < 0 || index >= base.List.Count)
162                                 throw new ArgumentOutOfRangeException("index");
163
164                         base.RemoveAt(index);
165                 }
166                 #endregion      // Public Instance Methods
167
168                 #region Protected Instance Methods
169                 protected override void AddCore (Binding dataBinding)
170                 {
171                         if (dataBinding == null)
172                                 throw new ArgumentNullException ("dataBinding");
173
174                         if (dataBinding.Control != null && dataBinding.BindableComponent != bindable_component)
175                                 throw new ArgumentException ("dataBinding belongs to another BindingsCollection");
176
177                         for (int i = 0; i < Count; i++) {
178                                 Binding bnd = this [i];
179                                 if (bnd == null || bnd.PropertyName.Length == 0 || dataBinding.PropertyName.Length == 0) {
180                                         continue;
181                                 }
182
183                                 if (String.Compare (bnd.PropertyName, dataBinding.PropertyName, true) == 0) {
184                                         throw new ArgumentException ("The binding is already in the collection");
185                                 }
186                         }
187
188                         dataBinding.SetControl (bindable_component);
189                         dataBinding.Check ();
190                         base.AddCore (dataBinding);
191                 }
192
193                 protected override void ClearCore() {
194                         base.ClearCore ();
195                 }
196
197                 protected override void RemoveCore (Binding dataBinding) {
198                         if (dataBinding == null)
199                                 throw new ArgumentNullException ("dataBinding");
200
201                         base.RemoveCore (dataBinding);
202                 }
203                 #endregion      // Protected Instance Methods
204         }
205 }
206