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 dataSet)
\r
70 // Null argument is allowed here.
\r
71 SetDataSet (dataSet);
\r
74 #endregion // Constructors
\r
79 [DataSysDescription ("Indicates the source of data for this DataViewManager.")]
\r
81 [DefaultValue (null)]
\r
82 public DataSet DataSet {
\r
83 get { return dataSet; }
\r
86 throw new DataException ("Cannot set null DataSet.");
\r
91 public string DataViewSettingCollectionString {
\r
95 ParseSettingString (value);
\r
96 xml = BuildSettingString ();
\r
97 } catch (XmlException ex) {
\r
98 throw new DataException ("Cannot set DataViewSettingCollectionString.", ex);
\r
104 [DataSysDescription ("Indicates the sorting/filtering/state settings for any table in the corresponding DataSet.")]
\r
106 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
\r
107 public DataViewSettingCollection DataViewSettings {
\r
108 get { return settings; }
\r
111 int ICollection.Count {
\r
115 bool ICollection.IsSynchronized {
\r
117 get { throw new NotImplementedException (); }
\r
120 object ICollection.SyncRoot {
\r
122 get { throw new NotImplementedException (); }
\r
125 bool IList.IsFixedSize {
\r
126 get { return true; }
\r
129 bool IList.IsReadOnly {
\r
130 get { return true; }
\r
133 object IList.this [int index] {
\r
135 if (descriptor == null)
\r
136 descriptor = new DataViewManagerListItemTypeDescriptor (this);
\r
141 set { throw new ArgumentException ("Not modifiable"); }
\r
144 bool IBindingList.AllowEdit {
\r
146 get { throw new NotImplementedException (); }
\r
149 bool IBindingList.AllowNew {
\r
151 get { throw new NotImplementedException (); }
\r
154 bool IBindingList.AllowRemove {
\r
156 get { throw new NotImplementedException (); }
\r
159 bool IBindingList.IsSorted {
\r
161 get { throw new NotImplementedException (); }
\r
164 ListSortDirection IBindingList.SortDirection {
\r
166 get { throw new NotImplementedException (); }
\r
169 PropertyDescriptor IBindingList.SortProperty {
\r
171 get { throw new NotImplementedException (); }
\r
174 bool IBindingList.SupportsChangeNotification {
\r
176 get { throw new NotImplementedException (); }
\r
179 bool IBindingList.SupportsSearching {
\r
181 get { throw new NotImplementedException (); }
\r
184 bool IBindingList.SupportsSorting {
\r
186 get { throw new NotImplementedException (); }
\r
189 #endregion // Properties
\r
193 private void SetDataSet (DataSet ds)
\r
196 settings = new DataViewSettingCollection (this);
\r
197 xml = BuildSettingString ();
\r
200 private void ParseSettingString (string source)
\r
202 XmlTextReader xtr = new XmlTextReader (source,
\r
203 XmlNodeType.Element, null);
\r
206 if (xtr.Name != "DataViewSettingCollectionString")
\r
207 // easy way to throw the expected exception ;-)
\r
208 xtr.ReadStartElement ("DataViewSettingCollectionString");
\r
209 if (xtr.IsEmptyElement)
\r
210 return; // MS does not change the value.
\r
214 xtr.MoveToContent ();
\r
215 if (xtr.NodeType == XmlNodeType.EndElement)
\r
217 if (xtr.NodeType == XmlNodeType.Element)
\r
218 ReadTableSetting (xtr);
\r
221 } while (!xtr.EOF);
\r
222 if (xtr.NodeType == XmlNodeType.EndElement)
\r
223 xtr.ReadEndElement ();
\r
226 private void ReadTableSetting (XmlReader reader)
\r
228 // Namespace is ignored BTW.
\r
229 DataTable dt = DataSet.Tables [XmlConvert.DecodeName (
\r
230 reader.LocalName)];
\r
231 // The code below might result in NullReference error.
\r
232 DataViewSetting s = settings [dt];
\r
233 string sort = reader.GetAttribute ("Sort");
\r
235 s.Sort = sort.Trim ();
\r
236 string ads = reader.GetAttribute ("ApplyDefaultSort");
\r
237 if (ads != null && ads.Trim () == "true")
\r
238 s.ApplyDefaultSort = true;
\r
239 string rowFilter = reader.GetAttribute ("RowFilter");
\r
240 if (rowFilter != null)
\r
241 s.RowFilter = rowFilter.Trim ();
\r
242 string rsf = reader.GetAttribute ("RowStateFilter");
\r
244 s.RowStateFilter = (DataViewRowState)
\r
245 Enum.Parse (typeof (DataViewRowState),
\r
250 private string BuildSettingString ()
\r
252 if (dataSet == null)
\r
253 return String.Empty;
\r
255 StringWriter sw = new StringWriter ();
\r
257 sw.Write ("DataViewSettingCollectionString>");
\r
258 foreach (DataViewSetting s in DataViewSettings) {
\r
260 sw.Write (XmlConvert.EncodeName (
\r
261 s.Table.TableName));
\r
262 sw.Write (" Sort=\"");
\r
263 sw.Write (Escape (s.Sort));
\r
265 // LAMESPEC: MS.NET does not seem to handle this property as expected.
\r
266 if (s.ApplyDefaultSort)
\r
267 sw.Write (" ApplyDefaultSort=\"true\"");
\r
268 sw.Write (" RowFilter=\"");
\r
269 sw.Write (Escape (s.RowFilter));
\r
270 sw.Write ("\" RowStateFilter=\"");
\r
271 sw.Write (s.RowStateFilter.ToString ());
\r
274 sw.Write ("</DataViewSettingCollectionString>");
\r
275 return sw.ToString ();
\r
278 private string Escape (string s)
\r
280 return s.Replace ("&", "&")
\r
281 .Replace ("\"", """)
\r
282 .Replace ("\'", "'")
\r
283 .Replace ("<", "<")
\r
284 .Replace (">", ">");
\r
287 public DataView CreateDataView (DataTable table)
\r
289 if (settings [table] != null) {
\r
290 DataViewSetting s = settings [table];
\r
291 return new DataView (table, this, s.Sort, s.RowFilter, s.RowStateFilter);
\r
293 return new DataView (table);
\r
298 void IBindingList.AddIndex (PropertyDescriptor property)
\r
300 throw new NotImplementedException ();
\r
304 object IBindingList.AddNew ()
\r
306 throw new NotImplementedException ();
\r
310 void IBindingList.ApplySort (PropertyDescriptor property, ListSortDirection direction)
\r
312 throw new NotImplementedException ();
\r
316 int IBindingList.Find (PropertyDescriptor property, object key)
\r
318 throw new NotImplementedException ();
\r
322 void IBindingList.RemoveIndex (PropertyDescriptor property)
\r
324 throw new NotImplementedException ();
\r
328 void IBindingList.RemoveSort ()
\r
330 throw new NotImplementedException ();
\r
334 void ICollection.CopyTo (Array array, int index)
\r
336 throw new NotImplementedException ();
\r
340 IEnumerator IEnumerable.GetEnumerator ()
\r
342 throw new NotImplementedException ();
\r
346 int IList.Add (object value)
\r
348 throw new NotImplementedException ();
\r
352 void IList.Clear ()
\r
354 throw new NotImplementedException ();
\r
358 bool IList.Contains (object value)
\r
360 throw new NotImplementedException ();
\r
364 int IList.IndexOf (object value)
\r
366 throw new NotImplementedException ();
\r
370 void IList.Insert (int index, object value)
\r
372 throw new NotImplementedException ();
\r
376 void IList.Remove (object value)
\r
378 throw new NotImplementedException ();
\r
382 void IList.RemoveAt (int index)
\r
384 throw new NotImplementedException ();
\r
387 [MonoLimitation("Supported only empty list of listAccessors")]
\r
388 PropertyDescriptorCollection ITypedList.GetItemProperties (PropertyDescriptor[] listAccessors)
\r
390 if (dataSet == null)
\r
391 throw new DataException ("dataset is null");
\r
393 if (listAccessors == null || listAccessors.Length == 0) {
\r
394 ICustomTypeDescriptor desc = new DataViewManagerListItemTypeDescriptor (this);
\r
395 return desc.GetProperties ();
\r
398 throw new NotImplementedException ();
\r
401 string ITypedList.GetListName (PropertyDescriptor[] listAccessors)
\r
403 if (dataSet != null) {
\r
404 if (listAccessors == null || listAccessors.Length == 0)
\r
405 return dataSet.DataSetName;
\r
408 return string.Empty;
\r
411 protected virtual void OnListChanged (ListChangedEventArgs e)
\r
413 if (ListChanged != null)
\r
414 ListChanged (this, e);
\r
417 protected virtual void RelationCollectionChanged (object sender, CollectionChangeEventArgs e)
\r
419 ListChangedEventArgs args;
\r
421 if (e.Action == CollectionChangeAction.Remove) {
\r
423 } else if (e.Action == CollectionChangeAction.Refresh) {
\r
424 args = new ListChangedEventArgs(ListChangedType.PropertyDescriptorChanged, null);
\r
425 } else if (e.Action == CollectionChangeAction.Add) {
\r
426 args = new ListChangedEventArgs(ListChangedType.PropertyDescriptorAdded, new DataRelationPropertyDescriptor(((DataRelation) e.Element)));
\r
428 args = new ListChangedEventArgs(ListChangedType.PropertyDescriptorDeleted, new DataRelationPropertyDescriptor(((DataRelation) e.Element)));
\r
431 this.OnListChanged(args);
\r
434 protected virtual void TableCollectionChanged (object sender, CollectionChangeEventArgs e)
\r
438 #endregion // Methods
\r
442 public event ListChangedEventHandler ListChanged;
\r
444 #endregion // Events
\r