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
))
72 protected override ArrayList List
{
76 int ICollection
.Count
{
77 get { return items.Count;}
80 bool ICollection
.IsSynchronized
{
84 object ICollection
.SyncRoot
{
88 bool IList
.IsFixedSize
{
92 bool IList
.IsReadOnly
{
96 object IList
.this [int index
] {
97 get { return items[index]; }
98 set { throw new NotSupportedException (); }
101 #endregion Public Instance Properties
103 #region Private Instance Properties
104 internal bool FireEvents
{
105 get { return fire_event;}
106 set { fire_event = value;}
108 #endregion Private Instance Properties
110 #region Public Instance Methods
111 public virtual int Add (DataGridColumnStyle column
)
113 // TODO: MS allows duplicate columns. How they diferenciate between them?
114 if (FromColumnNameToIndex (column
.MappingName
) != -1) {
115 throw new ArgumentException ("The ColumnStyles collection already has a column with this mapping name");
118 column
.TableStyle
= owner
;
119 column
.SetDataGridInternal (owner
.DataGrid
);
120 ConnectColumnEvents (column
);
121 int cnt
= items
.Add (column
);
122 OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction
.Add
, column
));
126 public void AddRange (DataGridColumnStyle
[] columns
)
128 foreach (DataGridColumnStyle mi
in columns
)
135 OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction
.Refresh
, null));
138 public bool Contains (DataGridColumnStyle column
)
140 return (FromColumnNameToIndex (column
.MappingName
) != -1);
143 public bool Contains (PropertyDescriptor propertyDescriptor
)
145 return (this [propertyDescriptor
] != null);
148 public bool Contains (string name
)
150 return (FromColumnNameToIndex (name
) != -1);
153 void ICollection
.CopyTo (Array array
, int index
)
155 items
.CopyTo (array
, index
);
158 IEnumerator IEnumerable
.GetEnumerator ()
160 return items
.GetEnumerator ();
163 int IList
.Add (object value)
165 return Add ((DataGridColumnStyle
)value);
173 bool IList
.Contains (object value)
175 return Contains ((DataGridColumnStyle
)value);
178 int IList
.IndexOf (object value)
180 return IndexOf ((DataGridColumnStyle
)value);
183 void IList
.Insert (int index
, object value)
185 throw new NotSupportedException ();
188 void IList
.Remove (object value)
190 Remove ((DataGridColumnStyle
)value);
193 void IList
.RemoveAt (int index
)
198 public int IndexOf (DataGridColumnStyle element
)
200 return items
.IndexOf (element
);
203 protected void OnCollectionChanged (CollectionChangeEventArgs e
)
205 if (fire_event
&& CollectionChanged
!= null)
206 CollectionChanged (this, e
);
209 public void Remove (DataGridColumnStyle column
)
211 items
.Remove (column
);
212 DisconnectColumnEvents (column
);
213 OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction
.Remove
, column
));
216 public void RemoveAt (int index
)
218 DataGridColumnStyle item
= (DataGridColumnStyle
)items
[index
];
219 items
.RemoveAt (index
);
220 DisconnectColumnEvents (item
);
221 OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction
.Remove
, item
));
224 public void ResetPropertyDescriptors ()
226 for (int i
= 0; i
< items
.Count
; i
++) {
227 DataGridColumnStyle column
= (DataGridColumnStyle
) items
[i
];
228 if (column
.PropertyDescriptor
!= null) {
229 column
.PropertyDescriptor
= null;
234 #endregion Public Instance Methods
237 public event CollectionChangeEventHandler CollectionChanged
;
240 #region Private Instance Methods
241 void ConnectColumnEvents (DataGridColumnStyle col
)
243 col
.AlignmentChanged
+= new EventHandler (ColumnAlignmentChangedEvent
);
244 col
.FontChanged
+= new EventHandler (ColumnFontChangedEvent
);
245 col
.HeaderTextChanged
+= new EventHandler (ColumnHeaderTextChanged
);
246 col
.MappingNameChanged
+= new EventHandler (ColumnMappingNameChangedEvent
);
247 col
.NullTextChanged
+= new EventHandler (ColumnNullTextChangedEvent
);
248 col
.PropertyDescriptorChanged
+= new EventHandler (ColumnPropertyDescriptorChanged
);
249 col
.ReadOnlyChanged
+= new EventHandler (ColumnReadOnlyChangedEvent
);
250 col
.WidthChanged
+= new EventHandler (ColumnWidthChangedEvent
);
253 void DisconnectColumnEvents (DataGridColumnStyle col
)
255 col
.AlignmentChanged
-= new EventHandler (ColumnAlignmentChangedEvent
);
256 col
.FontChanged
-= new EventHandler (ColumnFontChangedEvent
);
257 col
.HeaderTextChanged
-= new EventHandler (ColumnHeaderTextChanged
);
258 col
.MappingNameChanged
-= new EventHandler (ColumnMappingNameChangedEvent
);
259 col
.NullTextChanged
-= new EventHandler (ColumnNullTextChangedEvent
);
260 col
.PropertyDescriptorChanged
-= new EventHandler (ColumnPropertyDescriptorChanged
);
261 col
.ReadOnlyChanged
-= new EventHandler (ColumnReadOnlyChangedEvent
);
262 col
.WidthChanged
-= new EventHandler (ColumnWidthChangedEvent
);
265 void ColumnAlignmentChangedEvent (object sender
, EventArgs e
)
267 // XXX should this do a CollectionChangedEvent (Refresh, sender)?
270 void ColumnFontChangedEvent (object sender
, EventArgs e
)
272 // XXX should this do a CollectionChangedEvent (Refresh, sender)?
275 void ColumnHeaderTextChanged (object sender
, EventArgs e
)
277 // XXX should this do a CollectionChangedEvent (Refresh, sender)?
280 void ColumnMappingNameChangedEvent (object sender
, EventArgs e
)
282 // XXX should this do a CollectionChangedEvent (Refresh, sender)?
285 void ColumnNullTextChangedEvent (object sender
, EventArgs e
)
287 // XXX should this do a CollectionChangedEvent (Refresh, sender)?
290 void ColumnPropertyDescriptorChanged (object sender
, EventArgs e
)
292 // XXX should this do a CollectionChangedEvent (Refresh, sender)?
293 OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction
.Refresh
, sender
));
296 void ColumnReadOnlyChangedEvent (object sender
, EventArgs e
)
298 // XXX should this do a CollectionChangedEvent (Refresh, sender)?
301 void ColumnWidthChangedEvent (object sender
, EventArgs e
)
303 // XXX should this do a CollectionChangedEvent (Refresh, sender)?
306 private int FromColumnNameToIndex (string columnName
)
308 for (int i
= 0; i
< items
.Count
; i
++) {
309 DataGridColumnStyle column
= (DataGridColumnStyle
) items
[i
];
311 if (column
.MappingName
== null || column
.MappingName
== string.Empty
)
314 if (String
.Compare (column
.MappingName
, columnName
, true) == 0) {
322 #endregion Private Instance Methods