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. (http://www.novell.com)
23 // Jordi Mas i Hernandez <jordi@ximian.com>
27 using System.Collections;
28 using System.ComponentModel;
30 namespace System.Windows.Forms
32 [Editor("System.Windows.Forms.Design.DataGridColumnCollectionEditor, " + Consts.AssemblySystem_Design, typeof(System.Drawing.Design.UITypeEditor))]
34 public class GridColumnStylesCollection : BaseCollection, IList
36 private ArrayList items;
37 private DataGridTableStyle owner;
38 private bool fire_event;
40 internal GridColumnStylesCollection (DataGridTableStyle tablestyle)
42 items = new ArrayList ();
47 #region Public Instance Properties
48 public DataGridColumnStyle this [string columnName] {
50 int idx = FromColumnNameToIndex (columnName);
51 return idx == -1 ? null : this [idx];
55 public DataGridColumnStyle this [int index] {
57 return (DataGridColumnStyle) items[index];
62 public DataGridColumnStyle this [PropertyDescriptor propDesc] {
64 for (int i = 0; i < items.Count; i++) {
65 DataGridColumnStyle column = (DataGridColumnStyle) items[i];
66 if (column.PropertyDescriptor.Equals (propDesc)) {
75 protected override ArrayList List {
79 int ICollection.Count {
80 get { return items.Count;}
83 bool ICollection.IsSynchronized {
87 object ICollection.SyncRoot {
91 bool IList.IsFixedSize {
95 bool IList.IsReadOnly {
99 object IList.this [int index] {
104 throw new NotSupportedException ();
108 #endregion Public Instance Properties
110 #region Private Instance Properties
111 internal bool FireEvents {
112 get { return fire_event;}
113 set { fire_event = value;}
115 #endregion Private Instance Properties
117 #region Public Instance Methods
118 public virtual int Add (DataGridColumnStyle column)
120 int cnt = AddInternal (column);
124 public void AddRange (DataGridColumnStyle[] columns)
126 foreach (DataGridColumnStyle mi in columns)
133 OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Refresh , null));
136 public bool Contains (DataGridColumnStyle column)
138 return (FromColumnNameToIndex (column.MappingName) != -1);
141 public bool Contains (PropertyDescriptor propDesc)
143 return (this [propDesc] != null);
146 public bool Contains (string name)
148 return (FromColumnNameToIndex (name) != -1);
151 void ICollection.CopyTo (Array dest, int index)
153 items.CopyTo (dest, index);
156 IEnumerator IEnumerable.GetEnumerator ()
158 return items.GetEnumerator ();
161 int IList.Add (object value)
163 return AddInternal ((DataGridColumnStyle)value);
169 OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Refresh , null));
172 bool IList.Contains (object value)
174 return items.Contains (value);
177 int IList.IndexOf (object value)
179 return items.IndexOf (value);
182 void IList.Insert (int index, object value)
184 throw new NotSupportedException ();
187 void IList.Remove (object value)
189 items.Remove (value);
192 void IList.RemoveAt (int index)
194 object item = items[index];
196 items.RemoveAt (index);
197 OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Remove, item));
200 public int IndexOf (DataGridColumnStyle element)
202 return items.IndexOf (element);
205 protected void OnCollectionChanged (CollectionChangeEventArgs ccevent)
207 if (fire_event == true && CollectionChanged != null) {
208 CollectionChanged (this, ccevent);
212 public void Remove (DataGridColumnStyle column)
214 items.Remove (column);
215 OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Remove, column));
218 public void RemoveAt (int index)
220 object item = items[index];
221 items.RemoveAt (index);
222 OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Remove, item));
225 public void ResetPropertyDescriptors ()
227 for (int i = 0; i < items.Count; i++) {
228 DataGridColumnStyle column = (DataGridColumnStyle) items[i];
229 if (column.PropertyDescriptor != null) {
230 column.PropertyDescriptor = null;
235 #endregion Public Instance Methods
238 public event CollectionChangeEventHandler CollectionChanged;
241 #region Private Instance Methods
242 private int AddInternal (DataGridColumnStyle column)
244 // TODO: MS allows duplicate columns. How they diferenciate between them?
245 if (FromColumnNameToIndex (column.MappingName) != -1) {
246 throw new ArgumentException ("The ColumnStyles collection already has a column with this mapping name");
249 column.TableStyle = owner;
250 int cnt = items.Add (column);
251 OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Add, column));
255 private int FromColumnNameToIndex (string columnName)
257 for (int i = 0; i < items.Count; i++) {
258 DataGridColumnStyle column = (DataGridColumnStyle) items[i];
260 if (column.MappingName == null || column.MappingName == string.Empty)
263 if (String.Compare (column.MappingName, columnName, true) == 0) {
271 #endregion Private Instance Methods