Update Reference Sources to .NET Framework 4.6
[mono.git] / mcs / class / referencesource / System.Data / System / Data / DataViewManager.cs
1 //------------------------------------------------------------------------------
2 // <copyright file="DataViewManager.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 // <owner current="true" primary="true">Microsoft</owner>
6 // <owner current="true" primary="false">Microsoft</owner>
7 // <owner current="false" primary="false">Microsoft</owner>
8 //------------------------------------------------------------------------------
9
10 namespace System.Data {
11     using System;
12     using System.ComponentModel;
13     using System.Collections;
14     using System.IO;
15     using System.Text;
16     using System.Xml;
17
18     [
19     Designer("Microsoft.VSDesigner.Data.VS.DataViewManagerDesigner, " + AssemblyRef.MicrosoftVSDesigner)
20     ]
21     public class DataViewManager : MarshalByValueComponent, IBindingList, System.ComponentModel.ITypedList {
22         private DataViewSettingCollection dataViewSettingsCollection;
23         private DataSet dataSet;
24         private DataViewManagerListItemTypeDescriptor item;
25         private bool locked;
26         internal int nViews = 0;
27
28         private System.ComponentModel.ListChangedEventHandler onListChanged;
29
30         private static NotSupportedException NotSupported = new NotSupportedException();
31
32         public DataViewManager() : this(null, false) {}
33
34         public DataViewManager(DataSet dataSet) : this(dataSet, false) {}
35
36         internal DataViewManager(DataSet dataSet, bool locked) {
37             GC.SuppressFinalize(this);
38             this.dataSet = dataSet;
39             if (this.dataSet != null) {
40                 this.dataSet.Tables.CollectionChanged += new CollectionChangeEventHandler(TableCollectionChanged);
41                 this.dataSet.Relations.CollectionChanged += new CollectionChangeEventHandler(RelationCollectionChanged);
42             }
43             this.locked = locked;
44             this.item = new DataViewManagerListItemTypeDescriptor(this);
45             this.dataViewSettingsCollection = new DataViewSettingCollection(this);
46         }
47
48         [
49         DefaultValue(null),
50         ResDescriptionAttribute(Res.DataViewManagerDataSetDescr)
51         ]
52         public DataSet DataSet {
53             get {
54                 return dataSet;
55             }
56             set {
57                 if (value == null)
58                     throw ExceptionBuilder.SetFailed("DataSet to null");
59
60                 if (locked)
61                     throw ExceptionBuilder.SetDataSetFailed();
62
63                 if (dataSet != null) {
64                     if (nViews > 0)
65                         throw ExceptionBuilder.CanNotSetDataSet();
66
67                     this.dataSet.Tables.CollectionChanged -= new CollectionChangeEventHandler(TableCollectionChanged);
68                     this.dataSet.Relations.CollectionChanged -= new CollectionChangeEventHandler(RelationCollectionChanged);
69                 }
70
71                 this.dataSet = value;
72                 this.dataSet.Tables.CollectionChanged += new CollectionChangeEventHandler(TableCollectionChanged);
73                 this.dataSet.Relations.CollectionChanged += new CollectionChangeEventHandler(RelationCollectionChanged);
74                 this.dataViewSettingsCollection = new DataViewSettingCollection(this);
75                 item.Reset();
76             }
77         }
78
79         [
80         DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
81         ResDescriptionAttribute(Res.DataViewManagerTableSettingsDescr)
82         ]
83         public DataViewSettingCollection DataViewSettings {
84             get {
85                 return dataViewSettingsCollection;
86             }
87         }
88
89         public string DataViewSettingCollectionString {
90             get {
91                 if (dataSet == null)
92                     return "";
93
94                 StringBuilder builder = new StringBuilder();
95                 builder.Append("<DataViewSettingCollectionString>");
96                 foreach (DataTable dt in dataSet.Tables) {
97                     DataViewSetting ds = dataViewSettingsCollection[dt];
98                     builder.AppendFormat(System.Globalization.CultureInfo.InvariantCulture, "<{0} Sort=\"{1}\" RowFilter=\"{2}\" RowStateFilter=\"{3}\"/>", dt.EncodedTableName, ds.Sort, ds.RowFilter, ds.RowStateFilter);
99                 }
100                 builder.Append("</DataViewSettingCollectionString>");
101                 return builder.ToString();
102             }
103             set {
104                 if (value == null || value.Length == 0)
105                     return;
106
107                 XmlTextReader r = new XmlTextReader(new StringReader(value));
108                 r.WhitespaceHandling = WhitespaceHandling.None;
109                 r.Read();
110                 if (r.Name != "DataViewSettingCollectionString")
111                     throw ExceptionBuilder.SetFailed("DataViewSettingCollectionString");
112                 while (r.Read()) {
113                     if (r.NodeType != XmlNodeType.Element)
114                         continue;
115
116                     string table = XmlConvert.DecodeName(r.LocalName);
117                     if (r.MoveToAttribute("Sort"))
118                         dataViewSettingsCollection[table].Sort = r.Value;
119
120                     if (r.MoveToAttribute("RowFilter"))
121                         dataViewSettingsCollection[table].RowFilter = r.Value;
122
123                     if (r.MoveToAttribute("RowStateFilter"))
124                         dataViewSettingsCollection[table].RowStateFilter = (DataViewRowState)Enum.Parse(typeof(DataViewRowState),r.Value);
125                 }
126             }
127         }
128
129         IEnumerator IEnumerable.GetEnumerator() {
130             DataViewManagerListItemTypeDescriptor[] items = new DataViewManagerListItemTypeDescriptor[1];
131             ((ICollection)this).CopyTo(items, 0);
132             return items.GetEnumerator();
133         }
134
135         int ICollection.Count {
136             get {
137                 return 1;
138             }
139         }
140
141         object ICollection.SyncRoot {
142             get {
143                 return this;
144             }
145         }
146
147         bool ICollection.IsSynchronized {
148             get {
149                 return false;
150             }
151         }
152
153         bool IList.IsReadOnly {
154             get {
155                 return true;
156             }
157         }
158
159         bool IList.IsFixedSize {
160             get {
161                 return true;
162             }
163         }
164
165         void ICollection.CopyTo(Array array, int index) {
166             array.SetValue((object)(new DataViewManagerListItemTypeDescriptor(this)), index);
167         }
168
169         object IList.this[int index] {
170             get {
171                 return item;
172             }
173             set {
174                 throw ExceptionBuilder.CannotModifyCollection();
175             }
176         }
177
178         int IList.Add(object value) {
179             throw ExceptionBuilder.CannotModifyCollection();
180         }
181
182         void IList.Clear() {
183             throw ExceptionBuilder.CannotModifyCollection();
184         }
185
186         bool IList.Contains(object value) {
187             return(value == item);
188         }
189
190         int IList.IndexOf(object value) {
191             return(value == item) ? 1 : -1;
192         }
193
194         void IList.Insert(int index, object value) {
195             throw ExceptionBuilder.CannotModifyCollection();
196         }
197
198         void IList.Remove(object value) {
199             throw ExceptionBuilder.CannotModifyCollection();
200         }
201
202         void IList.RemoveAt(int index) {
203             throw ExceptionBuilder.CannotModifyCollection();
204         }
205
206         // ------------- IBindingList: ---------------------------
207
208         bool IBindingList.AllowNew {
209             get {
210                 return false;
211             }
212         }
213         object IBindingList.AddNew() {
214             throw NotSupported;
215         }
216
217         bool IBindingList.AllowEdit {
218             get {
219                 return false;
220             }
221         }
222
223         bool IBindingList.AllowRemove {
224             get {
225                 return false;
226             }
227         }
228
229         bool IBindingList.SupportsChangeNotification {
230             get {
231                 return true;
232             }
233         }
234
235         bool IBindingList.SupportsSearching {
236             get {
237                 return false;
238             }
239         }
240
241         bool IBindingList.SupportsSorting {
242             get {
243                 return false;
244             }
245         }
246
247         bool IBindingList.IsSorted {
248             get {
249                 throw NotSupported;
250             }
251         }
252
253         PropertyDescriptor IBindingList.SortProperty {
254             get {
255                 throw NotSupported;
256             }
257         }
258
259         ListSortDirection IBindingList.SortDirection {
260             get {
261                 throw NotSupported;
262             }
263         }
264
265         public event System.ComponentModel.ListChangedEventHandler ListChanged {
266             add {
267                 onListChanged += value;
268             }
269             remove {
270                 onListChanged -= value;
271             }
272         }
273
274         void IBindingList.AddIndex(PropertyDescriptor property) {
275             // no operation
276         }
277
278         void IBindingList.ApplySort(PropertyDescriptor property, ListSortDirection direction) {
279             throw NotSupported;
280         }
281
282         int IBindingList.Find(PropertyDescriptor property, object key) {
283                     throw NotSupported;
284         }
285
286         void IBindingList.RemoveIndex(PropertyDescriptor property) {
287             // no operation
288         }
289
290         void IBindingList.RemoveSort() {
291             throw NotSupported;
292         }
293
294         /*
295         string IBindingList.GetListName() {
296             return ((System.Data.ITypedList)this).GetListName(null);
297         }
298         string IBindingList.GetListName(PropertyDescriptor[] listAccessors) {
299             return ((System.Data.ITypedList)this).GetListName(listAccessors);
300         }
301         */
302
303         // Microsoft: GetListName and GetItemProperties almost the same in DataView and DataViewManager
304         string System.ComponentModel.ITypedList.GetListName(PropertyDescriptor[] listAccessors) {
305             DataSet dataSet = DataSet;
306             if (dataSet == null)
307                 throw ExceptionBuilder.CanNotUseDataViewManager();
308
309             if (listAccessors == null || listAccessors.Length == 0) {
310                 return dataSet.DataSetName;
311             }
312             else {
313                 DataTable table = dataSet.FindTable(null, listAccessors, 0);
314                 if (table != null) {
315                     return table.TableName;
316                 }
317             }
318             return String.Empty;
319         }
320
321         PropertyDescriptorCollection System.ComponentModel.ITypedList.GetItemProperties(PropertyDescriptor[] listAccessors) {
322             DataSet dataSet = DataSet;
323             if (dataSet == null)
324                 throw ExceptionBuilder.CanNotUseDataViewManager();
325
326             if (listAccessors == null || listAccessors.Length == 0) {
327                 return((ICustomTypeDescriptor)(new DataViewManagerListItemTypeDescriptor(this))).GetProperties();
328             }
329             else {
330                 DataTable table = dataSet.FindTable(null, listAccessors, 0);
331                 if (table != null) {
332                     return table.GetPropertyDescriptorCollection(null);
333                 }
334             }
335             return new PropertyDescriptorCollection(null);
336         }
337
338         public DataView CreateDataView(DataTable table) {
339             if (dataSet == null)
340                 throw ExceptionBuilder.CanNotUseDataViewManager();
341
342             DataView dataView = new DataView(table);
343             dataView.SetDataViewManager(this);
344             return dataView;
345         }
346
347         protected virtual void OnListChanged(ListChangedEventArgs e) {
348             try {
349                 if (onListChanged != null) {
350                     onListChanged(this, e);
351                 }
352             }
353             catch (Exception f) {
354                 // 
355                 if (!Common.ADP.IsCatchableExceptionType(f)) {
356                     throw;
357                 }
358                 ExceptionBuilder.TraceExceptionWithoutRethrow(f);
359                 // ignore the exception
360             }
361         }
362
363         protected virtual void TableCollectionChanged(object sender, CollectionChangeEventArgs e) {
364              PropertyDescriptor NullProp = null;
365              OnListChanged(
366                  e.Action == CollectionChangeAction.Add ? new ListChangedEventArgs(ListChangedType.PropertyDescriptorAdded, new DataTablePropertyDescriptor((System.Data.DataTable)e.Element)) :
367                  e.Action == CollectionChangeAction.Refresh ? new ListChangedEventArgs(ListChangedType.PropertyDescriptorChanged, NullProp) :
368                  e.Action == CollectionChangeAction.Remove ? new ListChangedEventArgs(ListChangedType.PropertyDescriptorDeleted, new DataTablePropertyDescriptor((System.Data.DataTable)e.Element)) :
369                  /*default*/ null
370              );
371         }
372
373         protected virtual void RelationCollectionChanged(object sender, CollectionChangeEventArgs e) {
374             DataRelationPropertyDescriptor NullProp = null;
375             OnListChanged(
376                 e.Action == CollectionChangeAction.Add ? new ListChangedEventArgs(ListChangedType.PropertyDescriptorAdded, new DataRelationPropertyDescriptor((System.Data.DataRelation)e.Element)) :
377                 e.Action == CollectionChangeAction.Refresh ? new ListChangedEventArgs(ListChangedType.PropertyDescriptorChanged, NullProp):
378                 e.Action == CollectionChangeAction.Remove ? new ListChangedEventArgs(ListChangedType.PropertyDescriptorDeleted, new DataRelationPropertyDescriptor((System.Data.DataRelation)e.Element)) :
379             /*default*/ null
380             );
381         }
382     }
383 }