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] {
56 get { return (DataGridColumnStyle) items[index]; }
61 public DataGridColumnStyle this [PropertyDescriptor propertyDesciptor] {
63 PropertyDescriptor propDesc = propertyDesciptor;
65 public DataGridColumnStyle this [PropertyDescriptor propDesc] {
68 for (int i = 0; i < items.Count; i++) {
69 DataGridColumnStyle column = (DataGridColumnStyle) items[i];
70 if (column.PropertyDescriptor.Equals (propDesc))
78 protected override ArrayList List {
82 int ICollection.Count {
83 get { return items.Count;}
86 bool ICollection.IsSynchronized {
90 object ICollection.SyncRoot {
94 bool IList.IsFixedSize {
98 bool IList.IsReadOnly {
102 object IList.this [int index] {
103 get { return items[index]; }
104 set { throw new NotSupportedException (); }
107 #endregion Public Instance Properties
109 #region Private Instance Properties
110 internal bool FireEvents {
111 get { return fire_event;}
112 set { fire_event = value;}
114 #endregion Private Instance Properties
116 #region Public Instance Methods
117 public virtual int Add (DataGridColumnStyle column)
119 // TODO: MS allows duplicate columns. How they diferenciate between them?
120 if (FromColumnNameToIndex (column.MappingName) != -1) {
121 throw new ArgumentException ("The ColumnStyles collection already has a column with this mapping name");
124 column.TableStyle = owner;
125 column.SetDataGridInternal (owner.DataGrid);
126 ConnectColumnEvents (column);
127 int cnt = items.Add (column);
128 OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Add, column));
132 public void AddRange (DataGridColumnStyle[] columns)
134 foreach (DataGridColumnStyle mi in columns)
141 OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Refresh , null));
144 public bool Contains (DataGridColumnStyle column)
146 return (FromColumnNameToIndex (column.MappingName) != -1);
150 public bool Contains (PropertyDescriptor propertyDescriptor )
152 PropertyDescriptor propDesc = propertyDescriptor;
154 public bool Contains (PropertyDescriptor propDesc)
157 return (this [propDesc] != null);
160 public bool Contains (string name)
162 return (FromColumnNameToIndex (name) != -1);
165 void ICollection.CopyTo (Array array, int index)
167 items.CopyTo (array, index);
170 IEnumerator IEnumerable.GetEnumerator ()
172 return items.GetEnumerator ();
175 int IList.Add (object value)
177 return Add ((DataGridColumnStyle)value);
185 bool IList.Contains (object value)
187 return Contains ((DataGridColumnStyle)value);
190 int IList.IndexOf (object value)
192 return IndexOf ((DataGridColumnStyle)value);
195 void IList.Insert (int index, object value)
197 throw new NotSupportedException ();
200 void IList.Remove (object value)
202 Remove ((DataGridColumnStyle)value);
205 void IList.RemoveAt (int index)
210 public int IndexOf (DataGridColumnStyle element)
212 return items.IndexOf (element);
216 protected void OnCollectionChanged (CollectionChangeEventArgs e)
218 CollectionChangeEventArgs ccevent = e;
220 protected void OnCollectionChanged (CollectionChangeEventArgs ccevent)
223 if (fire_event && CollectionChanged != null)
224 CollectionChanged (this, ccevent);
227 public void Remove (DataGridColumnStyle column)
229 items.Remove (column);
230 DisconnectColumnEvents (column);
231 OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Remove, column));
234 public void RemoveAt (int index)
236 DataGridColumnStyle item = (DataGridColumnStyle)items[index];
237 items.RemoveAt (index);
238 DisconnectColumnEvents (item);
239 OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Remove, item));
242 public void ResetPropertyDescriptors ()
244 for (int i = 0; i < items.Count; i++) {
245 DataGridColumnStyle column = (DataGridColumnStyle) items[i];
246 if (column.PropertyDescriptor != null) {
247 column.PropertyDescriptor = null;
252 #endregion Public Instance Methods
255 public event CollectionChangeEventHandler CollectionChanged;
258 #region Private Instance Methods
259 void ConnectColumnEvents (DataGridColumnStyle col)
261 col.AlignmentChanged += new EventHandler (ColumnAlignmentChangedEvent);
262 col.FontChanged += new EventHandler (ColumnFontChangedEvent);
263 col.HeaderTextChanged += new EventHandler (ColumnHeaderTextChanged);
264 col.MappingNameChanged += new EventHandler (ColumnMappingNameChangedEvent);
265 col.NullTextChanged += new EventHandler (ColumnNullTextChangedEvent);
266 col.PropertyDescriptorChanged += new EventHandler (ColumnPropertyDescriptorChanged);
267 col.ReadOnlyChanged += new EventHandler (ColumnReadOnlyChangedEvent);
268 col.WidthChanged += new EventHandler (ColumnWidthChangedEvent);
271 void DisconnectColumnEvents (DataGridColumnStyle col)
273 col.AlignmentChanged -= new EventHandler (ColumnAlignmentChangedEvent);
274 col.FontChanged -= new EventHandler (ColumnFontChangedEvent);
275 col.HeaderTextChanged -= new EventHandler (ColumnHeaderTextChanged);
276 col.MappingNameChanged -= new EventHandler (ColumnMappingNameChangedEvent);
277 col.NullTextChanged -= new EventHandler (ColumnNullTextChangedEvent);
278 col.PropertyDescriptorChanged -= new EventHandler (ColumnPropertyDescriptorChanged);
279 col.ReadOnlyChanged -= new EventHandler (ColumnReadOnlyChangedEvent);
280 col.WidthChanged -= new EventHandler (ColumnWidthChangedEvent);
283 void ColumnAlignmentChangedEvent (object sender, EventArgs e)
285 // XXX should this do a CollectionChangedEvent (Refresh, sender)?
288 void ColumnFontChangedEvent (object sender, EventArgs e)
290 // XXX should this do a CollectionChangedEvent (Refresh, sender)?
293 void ColumnHeaderTextChanged (object sender, EventArgs e)
295 // XXX should this do a CollectionChangedEvent (Refresh, sender)?
298 void ColumnMappingNameChangedEvent (object sender, EventArgs e)
300 // XXX should this do a CollectionChangedEvent (Refresh, sender)?
303 void ColumnNullTextChangedEvent (object sender, EventArgs e)
305 // XXX should this do a CollectionChangedEvent (Refresh, sender)?
308 void ColumnPropertyDescriptorChanged (object sender, EventArgs e)
310 // XXX should this do a CollectionChangedEvent (Refresh, sender)?
311 OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Refresh, sender));
314 void ColumnReadOnlyChangedEvent (object sender, EventArgs e)
316 // XXX should this do a CollectionChangedEvent (Refresh, sender)?
319 void ColumnWidthChangedEvent (object sender, EventArgs e)
321 // XXX should this do a CollectionChangedEvent (Refresh, sender)?
324 private int FromColumnNameToIndex (string columnName)
326 for (int i = 0; i < items.Count; i++) {
327 DataGridColumnStyle column = (DataGridColumnStyle) items[i];
329 if (column.MappingName == null || column.MappingName == string.Empty)
332 if (String.Compare (column.MappingName, columnName, true) == 0) {
340 #endregion Private Instance Methods