2 // System.Data.DataViewManager
\r
5 // Rodrigo Moya (rodrigo@ximian.com)
\r
6 // Tim Coleman (tim@timcoleman.com)
\r
7 // Atsushi Enomoto (atsushi@ximian.com)
\r
9 // (C) Ximian, Inc. 2002
\r
10 // Copyright (C) Tim Coleman, 2002
\r
11 // Copyright (C) 2005 Novell Inc,
\r
15 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
\r
17 // Permission is hereby granted, free of charge, to any person obtaining
\r
18 // a copy of this software and associated documentation files (the
\r
19 // "Software"), to deal in the Software without restriction, including
\r
20 // without limitation the rights to use, copy, modify, merge, publish,
\r
21 // distribute, sublicense, and/or sell copies of the Software, and to
\r
22 // permit persons to whom the Software is furnished to do so, subject to
\r
23 // the following conditions:
\r
25 // The above copyright notice and this permission notice shall be
\r
26 // included in all copies or substantial portions of the Software.
\r
28 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
\r
29 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
\r
30 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
\r
31 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
\r
32 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
\r
33 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
\r
34 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
38 using System.Collections;
\r
39 using System.ComponentModel;
\r
43 namespace System.Data
\r
46 /// Contains a default DataViewSettingCollection for each DataTable in a DataSet.
\r
49 [DesignerAttribute ("Microsoft.VSDesigner.Data.VS.DataViewManagerDesigner, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.ComponentModel.Design.IDesigner")]
\r
50 public class DataViewManager : MarshalByValueComponent, IBindingList, ICollection, IList, ITypedList, IEnumerable
\r
55 DataViewManagerListItemTypeDescriptor descriptor;
\r
56 DataViewSettingCollection settings;
\r
59 #endregion // Fields
\r
61 #region Constructors
\r
63 public DataViewManager ()
\r
68 public DataViewManager (DataSet ds)
\r
70 // Null argument is allowed here.
\r
74 #endregion // Constructors
\r
78 [DataSysDescription ("Indicates the source of data for this DataViewManager.")]
\r
79 [DefaultValue (null)]
\r
80 public DataSet DataSet {
\r
81 get { return dataSet; }
\r
84 throw new DataException ("Cannot set null DataSet.");
\r
89 public string DataViewSettingCollectionString {
\r
93 ParseSettingString (value);
\r
94 xml = BuildSettingString ();
\r
95 } catch (XmlException ex) {
\r
96 throw new DataException ("Cannot set DataViewSettingCollectionString.", ex);
\r
101 [DataSysDescription ("Indicates the sorting/filtering/state settings for any table in the corresponding DataSet.")]
\r
102 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
\r
103 public DataViewSettingCollection DataViewSettings {
\r
104 get { return settings; }
\r
107 int ICollection.Count {
\r
108 get { return settings.Count; }
\r
111 bool ICollection.IsSynchronized {
\r
113 get { throw new NotImplementedException (); }
\r
116 object ICollection.SyncRoot {
\r
118 get { throw new NotImplementedException (); }
\r
121 bool IList.IsFixedSize {
\r
122 get { return true; }
\r
125 bool IList.IsReadOnly {
\r
126 get { return true; }
\r
129 object IList.this [int index] {
\r
131 if (descriptor == null)
\r
132 descriptor = new DataViewManagerListItemTypeDescriptor (this);
\r
137 set { throw new ArgumentException ("Not modifiable"); }
\r
140 bool IBindingList.AllowEdit {
\r
142 get { throw new NotImplementedException (); }
\r
145 bool IBindingList.AllowNew {
\r
147 get { throw new NotImplementedException (); }
\r
150 bool IBindingList.AllowRemove {
\r
152 get { throw new NotImplementedException (); }
\r
155 bool IBindingList.IsSorted {
\r
157 get { throw new NotImplementedException (); }
\r
160 ListSortDirection IBindingList.SortDirection {
\r
162 get { throw new NotImplementedException (); }
\r
165 PropertyDescriptor IBindingList.SortProperty {
\r
167 get { throw new NotImplementedException (); }
\r
170 bool IBindingList.SupportsChangeNotification {
\r
172 get { throw new NotImplementedException (); }
\r
175 bool IBindingList.SupportsSearching {
\r
177 get { throw new NotImplementedException (); }
\r
180 bool IBindingList.SupportsSorting {
\r
182 get { throw new NotImplementedException (); }
\r
185 #endregion // Properties
\r
188 private void SetDataSet (DataSet ds)
\r
191 settings = new DataViewSettingCollection (this);
\r
192 xml = BuildSettingString ();
\r
195 private void ParseSettingString (string source)
\r
197 XmlTextReader xtr = new XmlTextReader (source,
\r
198 XmlNodeType.Element, null);
\r
201 if (xtr.Name != "DataViewSettingCollectionString")
\r
202 // easy way to throw the expected exception ;-)
\r
203 xtr.ReadStartElement ("DataViewSettingCollectionString");
\r
204 if (xtr.IsEmptyElement)
\r
205 return; // MS does not change the value.
\r
207 ArrayList result = new ArrayList ();
\r
210 xtr.MoveToContent ();
\r
211 if (xtr.NodeType == XmlNodeType.EndElement)
\r
213 if (xtr.NodeType == XmlNodeType.Element)
\r
214 ReadTableSetting (xtr);
\r
217 } while (!xtr.EOF);
\r
218 if (xtr.NodeType == XmlNodeType.EndElement)
\r
219 xtr.ReadEndElement ();
\r
222 private void ReadTableSetting (XmlReader reader)
\r
224 // Namespace is ignored BTW.
\r
225 DataTable dt = DataSet.Tables [XmlConvert.DecodeName (
\r
226 reader.LocalName)];
\r
227 // The code below might result in NullReference error.
\r
228 DataViewSetting s = settings [dt];
\r
229 string sort = reader.GetAttribute ("Sort");
\r
231 s.Sort = sort.Trim ();
\r
232 string ads = reader.GetAttribute ("ApplyDefaultSort");
\r
233 if (ads != null && ads.Trim () == "true")
\r
234 s.ApplyDefaultSort = true;
\r
235 string rowFilter = reader.GetAttribute ("RowFilter");
\r
236 if (rowFilter != null)
\r
237 s.RowFilter = rowFilter.Trim ();
\r
238 string rsf = reader.GetAttribute ("RowStateFilter");
\r
240 s.RowStateFilter = (DataViewRowState)
\r
241 Enum.Parse (typeof (DataViewRowState),
\r
246 private string BuildSettingString ()
\r
248 if (dataSet == null)
\r
249 return String.Empty;
\r
251 StringWriter sw = new StringWriter ();
\r
253 sw.Write ("DataViewSettingCollectionString>");
\r
254 foreach (DataViewSetting s in DataViewSettings) {
\r
256 sw.Write (XmlConvert.EncodeName (
\r
257 s.Table.TableName));
\r
258 sw.Write (" Sort=\"");
\r
259 sw.Write (Escape (s.Sort));
\r
261 // LAMESPEC: MS.NET does not seem to handle this property as expected.
\r
262 if (s.ApplyDefaultSort)
\r
263 sw.Write (" ApplyDefaultSort=\"true\"");
\r
264 sw.Write (" RowFilter=\"");
\r
265 sw.Write (Escape (s.RowFilter));
\r
266 sw.Write ("\" RowStateFilter=\"");
\r
267 sw.Write (s.RowStateFilter.ToString ());
\r
270 sw.Write ("</DataViewSettingCollectionString>");
\r
271 return sw.ToString ();
\r
274 private string Escape (string s)
\r
276 return s.Replace ("&", "&")
\r
277 .Replace ("\"", """)
\r
278 .Replace ("\'", "'")
\r
279 .Replace ("<", "<")
\r
280 .Replace (">", ">");
\r
283 public DataView CreateDataView (DataTable table)
\r
285 if (settings [table] != null) {
\r
286 DataViewSetting s = settings [table];
\r
287 return new DataView (table, this, s.Sort, s.RowFilter, s.RowStateFilter);
\r
289 return new DataView (table);
\r
294 void IBindingList.AddIndex (PropertyDescriptor property)
\r
296 throw new NotImplementedException ();
\r
300 object IBindingList.AddNew ()
\r
302 throw new NotImplementedException ();
\r
306 void IBindingList.ApplySort (PropertyDescriptor property, ListSortDirection direction)
\r
308 throw new NotImplementedException ();
\r
312 int IBindingList.Find (PropertyDescriptor property, object key)
\r
314 throw new NotImplementedException ();
\r
318 void IBindingList.RemoveIndex (PropertyDescriptor property)
\r
320 throw new NotImplementedException ();
\r
324 void IBindingList.RemoveSort ()
\r
326 throw new NotImplementedException ();
\r
330 void ICollection.CopyTo (Array array, int index)
\r
332 throw new NotImplementedException ();
\r
336 IEnumerator IEnumerable.GetEnumerator ()
\r
338 throw new NotImplementedException ();
\r
342 int IList.Add (object value)
\r
344 throw new NotImplementedException ();
\r
348 void IList.Clear ()
\r
350 throw new NotImplementedException ();
\r
354 bool IList.Contains (object value)
\r
356 throw new NotImplementedException ();
\r
360 int IList.IndexOf (object value)
\r
362 throw new NotImplementedException ();
\r
366 void IList.Insert (int index, object value)
\r
368 throw new NotImplementedException ();
\r
372 void IList.Remove (object value)
\r
374 throw new NotImplementedException ();
\r
378 void IList.RemoveAt (int index)
\r
380 throw new NotImplementedException ();
\r
383 PropertyDescriptorCollection ITypedList.GetItemProperties (PropertyDescriptor[] listAccessors)
\r
385 if (dataSet == null)
\r
386 throw new DataException ("dataset is null");
\r
388 if (listAccessors == null || listAccessors.Length == 0) {
\r
389 ICustomTypeDescriptor desc = new DataViewManagerListItemTypeDescriptor (this);
\r
390 return desc.GetProperties ();
\r
393 throw new NotImplementedException ();
\r
396 string ITypedList.GetListName (PropertyDescriptor[] listAccessors)
\r
398 if (dataSet != null) {
\r
399 if (listAccessors == null || listAccessors.Length == 0) {
\r
400 return dataSet.DataSetName;
\r
404 return string.Empty;
\r
407 protected virtual void OnListChanged (ListChangedEventArgs e)
\r
409 if (ListChanged != null)
\r
410 ListChanged (this, e);
\r
413 protected virtual void RelationCollectionChanged (object sender, CollectionChangeEventArgs e)
\r
415 ListChangedEventArgs args;
\r
417 if (e.Action == CollectionChangeAction.Remove) {
\r
420 else if (e.Action == CollectionChangeAction.Refresh) {
\r
421 args = new ListChangedEventArgs(ListChangedType.PropertyDescriptorChanged, null);
\r
423 else if (e.Action == CollectionChangeAction.Add) {
\r
424 args = new ListChangedEventArgs(ListChangedType.PropertyDescriptorAdded, new DataRelationPropertyDescriptor(((DataRelation) e.Element)));
\r
427 args = new ListChangedEventArgs(ListChangedType.PropertyDescriptorDeleted, new DataRelationPropertyDescriptor(((DataRelation) e.Element)));
\r
430 this.OnListChanged(args);
\r
433 protected virtual void TableCollectionChanged (object sender, CollectionChangeEventArgs e)
\r
437 #endregion // Methods
\r
441 public event ListChangedEventHandler ListChanged;
\r
443 #endregion // Events
\r