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]; }
60 public DataGridColumnStyle this [PropertyDescriptor propertyDesciptor] {
62 for (int i = 0; i < items.Count; i++) {
63 DataGridColumnStyle column = (DataGridColumnStyle) items[i];
64 if (column.PropertyDescriptor.Equals (propertyDesciptor)) {
73 protected override ArrayList List {
77 int ICollection.Count {
78 get { return items.Count;}
81 bool ICollection.IsSynchronized {
85 object ICollection.SyncRoot {
89 bool IList.IsFixedSize {
93 bool IList.IsReadOnly {
97 object IList.this [int index] {
98 get { return items[index]; }
99 set { throw new NotSupportedException (); }
102 #endregion Public Instance Properties
104 #region Private Instance Properties
105 internal bool FireEvents {
106 get { return fire_event;}
107 set { fire_event = value;}
109 #endregion Private Instance Properties
111 #region Public Instance Methods
112 public virtual int Add (DataGridColumnStyle column)
114 // TODO: MS allows duplicate columns. How they diferenciate between them?
115 if (FromColumnNameToIndex (column.MappingName) != -1) {
116 throw new ArgumentException ("The ColumnStyles collection already has a column with this mapping name");
119 column.TableStyle = owner;
120 column.SetDataGridInternal (owner.DataGrid);
121 ConnectColumnEvents (column);
122 int cnt = items.Add (column);
123 OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Add, column));
127 public void AddRange (DataGridColumnStyle[] columns)
129 foreach (DataGridColumnStyle mi in columns)
136 OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Refresh , null));
139 public bool Contains (DataGridColumnStyle column)
141 return (FromColumnNameToIndex (column.MappingName) != -1);
144 public bool Contains (PropertyDescriptor propertyDescriptor)
146 return (this[propertyDescriptor] != null);
149 public bool Contains (string name)
151 return (FromColumnNameToIndex (name) != -1);
154 void ICollection.CopyTo (Array dest, int index)
156 items.CopyTo (dest, index);
159 IEnumerator IEnumerable.GetEnumerator ()
161 return items.GetEnumerator ();
164 int IList.Add (object value)
166 return Add ((DataGridColumnStyle)value);
174 bool IList.Contains (object value)
176 return Contains ((DataGridColumnStyle)value);
179 int IList.IndexOf (object value)
181 return IndexOf ((DataGridColumnStyle)value);
184 void IList.Insert (int index, object value)
186 throw new NotSupportedException ();
189 void IList.Remove (object value)
191 Remove ((DataGridColumnStyle)value);
194 void IList.RemoveAt (int index)
199 public int IndexOf (DataGridColumnStyle element)
201 return items.IndexOf (element);
204 protected void OnCollectionChanged (CollectionChangeEventArgs e)
206 if (fire_event == true && CollectionChanged != null) {
207 CollectionChanged (this, e);
211 public void Remove (DataGridColumnStyle column)
213 items.Remove (column);
214 DisconnectColumnEvents (column);
215 OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Remove, column));
218 public void RemoveAt (int index)
220 DataGridColumnStyle item = (DataGridColumnStyle)items[index];
221 items.RemoveAt (index);
222 DisconnectColumnEvents (item);
223 OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Remove, item));
226 public void ResetPropertyDescriptors ()
228 for (int i = 0; i < items.Count; i++) {
229 DataGridColumnStyle column = (DataGridColumnStyle) items[i];
230 if (column.PropertyDescriptor != null) {
231 column.PropertyDescriptor = null;
236 #endregion Public Instance Methods
239 public event CollectionChangeEventHandler CollectionChanged;
242 #region Private Instance Methods
243 void ConnectColumnEvents (DataGridColumnStyle col)
245 col.AlignmentChanged += new EventHandler (ColumnAlignmentChangedEvent);
246 col.FontChanged += new EventHandler (ColumnFontChangedEvent);
247 col.HeaderTextChanged += new EventHandler (ColumnHeaderTextChanged);
248 col.MappingNameChanged += new EventHandler (ColumnMappingNameChangedEvent);
249 col.NullTextChanged += new EventHandler (ColumnNullTextChangedEvent);
250 col.PropertyDescriptorChanged += new EventHandler (ColumnPropertyDescriptorChanged);
251 col.ReadOnlyChanged += new EventHandler (ColumnReadOnlyChangedEvent);
252 col.WidthChanged += new EventHandler (ColumnWidthChangedEvent);
255 void DisconnectColumnEvents (DataGridColumnStyle col)
257 col.AlignmentChanged -= new EventHandler (ColumnAlignmentChangedEvent);
258 col.FontChanged -= new EventHandler (ColumnFontChangedEvent);
259 col.HeaderTextChanged -= new EventHandler (ColumnHeaderTextChanged);
260 col.MappingNameChanged -= new EventHandler (ColumnMappingNameChangedEvent);
261 col.NullTextChanged -= new EventHandler (ColumnNullTextChangedEvent);
262 col.PropertyDescriptorChanged -= new EventHandler (ColumnPropertyDescriptorChanged);
263 col.ReadOnlyChanged -= new EventHandler (ColumnReadOnlyChangedEvent);
264 col.WidthChanged -= new EventHandler (ColumnWidthChangedEvent);
267 void ColumnAlignmentChangedEvent (object sender, EventArgs e)
269 // XXX should this do a CollectionChangedEvent (Refresh, sender)?
272 void ColumnFontChangedEvent (object sender, EventArgs e)
274 // XXX should this do a CollectionChangedEvent (Refresh, sender)?
277 void ColumnHeaderTextChanged (object sender, EventArgs e)
279 // XXX should this do a CollectionChangedEvent (Refresh, sender)?
282 void ColumnMappingNameChangedEvent (object sender, EventArgs e)
284 // XXX should this do a CollectionChangedEvent (Refresh, sender)?
287 void ColumnNullTextChangedEvent (object sender, EventArgs e)
289 // XXX should this do a CollectionChangedEvent (Refresh, sender)?
292 void ColumnPropertyDescriptorChanged (object sender, EventArgs e)
294 // XXX should this do a CollectionChangedEvent (Refresh, sender)?
295 OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Refresh, sender));
298 void ColumnReadOnlyChangedEvent (object sender, EventArgs e)
300 // XXX should this do a CollectionChangedEvent (Refresh, sender)?
303 void ColumnWidthChangedEvent (object sender, EventArgs e)
305 // XXX should this do a CollectionChangedEvent (Refresh, sender)?
309 private int FromColumnNameToIndex (string columnName)
311 for (int i = 0; i < items.Count; i++) {
312 DataGridColumnStyle column = (DataGridColumnStyle) items[i];
314 if (column.MappingName == null || column.MappingName == string.Empty)
317 if (String.Compare (column.MappingName, columnName, true) == 0) {
325 #endregion Private Instance Methods