Test mailing list
[mono.git] / mcs / class / System.Data / System.Data / DataRowView.cs
1 //
2 // System.Data.DataRowView.cs
3 //
4 // Author:
5 //    Rodrigo Moya <rodrigo@ximian.com>
6 //    Miguel de Icaza <miguel@ximian.com>
7 //    Daniel Morgan <danmorg@sc.rr.com>
8 //
9 // (C) Ximian, Inc 2002
10 // (C) Daniel Morgan 2002
11 //
12
13 //
14 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
15 //
16 // Permission is hereby granted, free of charge, to any person obtaining
17 // a copy of this software and associated documentation files (the
18 // "Software"), to deal in the Software without restriction, including
19 // without limitation the rights to use, copy, modify, merge, publish,
20 // distribute, sublicense, and/or sell copies of the Software, and to
21 // permit persons to whom the Software is furnished to do so, subject to
22 // the following conditions:
23 // 
24 // The above copyright notice and this permission notice shall be
25 // included in all copies or substantial portions of the Software.
26 // 
27 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
31 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
32 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
33 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 //
35
36 using System;
37 using System.Collections;
38 using System.ComponentModel;
39 using System.Reflection;
40
41 namespace System.Data
42 {
43         /// <summary>
44         /// Represents a customized view of a DataRow exposed as a fully featured Windows Forms control.
45         /// </summary>
46         public class DataRowView : ICustomTypeDescriptor, IEditableObject, IDataErrorInfo
47         {
48                 #region Fields
49
50                 DataView dataView;
51                 DataRow dataRow;
52                 DataRowVersion rowVersion = DataRowVersion.Current;
53
54                 bool isNew;
55
56                 #endregion // Fields
57
58                 #region Constructors
59
60                 internal DataRowView (DataView dataView, DataRow row) : this(dataView, row, false){
61                 }
62
63                 internal DataRowView (DataView dataView, DataRow row, bool isNew) {
64                         this.dataView = dataView;
65                         this.dataRow = row;
66                         this.isNew = isNew;
67                 }
68
69                 #endregion // Constructors
70
71                 #region Methods
72
73                 public override bool Equals(object other)
74                 {
75                         return (other != null &&
76                                         other is DataRowView && 
77                                         ((DataRowView)other).dataRow != null && 
78                                         ((DataRowView)other).dataRow.Equals(this.dataRow));
79                 }
80
81                 public void BeginEdit ()
82                 {
83                         dataRow.BeginEdit ();
84                 }
85
86                 public void CancelEdit ()
87                 {
88                         dataView.CancelEditRowView (this);
89                         isNew = false;
90                 }
91
92                 public DataView CreateChildView (DataRelation relation)
93                 {
94                         if (relation == null)
95                                 throw new ArgumentException ("The relation is not parented to the table.");
96                         return new DataView (relation.ChildTable,
97                                 dataRow.GetChildRows (relation));
98                 }
99
100                 public DataView CreateChildView (string name)
101                 {
102                         return CreateChildView (
103                                 dataRow.Table.ChildRelations [name]);
104                 }
105
106                 public void Delete ()
107                 {
108                         dataView.DeleteRowView (this);
109                         isNew = false;
110                 }
111
112                 public void EndEdit ()
113                 {
114                         dataView.EndEditRowView (this);
115                         isNew = false;
116                 }
117
118                 #endregion // Methods
119
120                 #region Properties
121                 
122                 public DataView DataView {
123                         get { return dataView; }
124                 }
125
126                 public bool IsEdit {
127                         get { return dataRow.IsEditing; }
128                 }
129
130                 // It becomes true when this instance is created by
131                 // DataView.AddNew(). If it is true, then the DataRow is
132                 // "Detached", and when this.EndEdit() is invoked, the row
133                 // will be added to the table.
134                 public bool IsNew {
135                         get { return isNew; }
136                 }
137                 
138                 [System.Runtime.CompilerServices.IndexerName("Item")]
139                 public object this[string column] {
140                         get {
141                                 DataColumn dc = dataView.Table.Columns[column];
142                                 return dataRow[dc, GetActualRowVersion ()];
143                         }
144                         set {
145                                 DataColumn dc = dataView.Table.Columns[column];
146                                 dataRow[dc] = value;
147                         }
148                 }
149
150                 // the compiler creates a DefaultMemeberAttribute from
151                 // this IndexerNameAttribute
152                 public object this[int column] {
153                         get {
154                                 DataColumn dc = dataView.Table.Columns[column];
155                                 return dataRow[dc, GetActualRowVersion ()];
156                         }
157                         set {
158                                 DataColumn dc = dataView.Table.Columns[column];
159                                 dataRow[dc] = value;
160
161                         }
162                 }
163
164                 private DataRowVersion GetActualRowVersion ()
165                 {
166                         switch (dataView.RowStateFilter) {
167                         case DataViewRowState.Added:
168                                 return DataRowVersion.Proposed;
169                         case DataViewRowState.ModifiedOriginal:
170                         case DataViewRowState.Deleted:
171                         case DataViewRowState.Unchanged:
172                         case DataViewRowState.OriginalRows:
173                                 return DataRowVersion.Original;
174                         case DataViewRowState.ModifiedCurrent:
175                                 return DataRowVersion.Current;
176                         }
177                         return DataRowVersion.Default;
178                 }
179
180                 public DataRow Row {
181                         get { return dataRow; }
182                 }
183
184                 public DataRowVersion RowVersion {
185                         [MonoTODO]
186                         get {
187                                 return rowVersion;
188                         }
189                 }
190
191                 // It returns the hash code of the DataRow object.
192                 public override int GetHashCode ()
193                 {
194                         return dataRow.GetHashCode ();
195                 }
196
197                 #endregion // Properties
198                 
199                 #region ICustomTypeDescriptor implementations
200                 
201                 [MonoTODO]
202                 AttributeCollection ICustomTypeDescriptor.GetAttributes  ()
203                 {
204                         System.ComponentModel.AttributeCollection attributes;
205                         attributes = AttributeCollection.Empty;
206                         return attributes;
207                 }
208
209                 [MonoTODO]
210                 string ICustomTypeDescriptor.GetClassName ()
211                 {
212                         return "";
213                 }
214                 
215                 [MonoTODO]
216                 string ICustomTypeDescriptor.GetComponentName ()
217                 {
218                         return null;
219                 }
220
221                 [MonoTODO]
222                 TypeConverter ICustomTypeDescriptor.GetConverter ()
223                 {
224                         return null;
225                 }
226
227                 [MonoTODO]
228                 EventDescriptor ICustomTypeDescriptor.GetDefaultEvent ()
229                 {
230                         return null;
231                 }
232                 
233                 [MonoTODO]
234                 PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty ()
235                 {
236                         return null;
237                 }
238                 
239                 [MonoTODO]
240                 object ICustomTypeDescriptor.GetEditor (Type editorBaseType)
241                 {
242                         return null;
243                 }
244                 
245                 [MonoTODO]
246                 EventDescriptorCollection ICustomTypeDescriptor.GetEvents ()
247                 {
248                         return new EventDescriptorCollection(null);
249                 }
250
251                 [MonoTODO]
252                 EventDescriptorCollection ICustomTypeDescriptor.GetEvents (Attribute [] attributes)
253                 {
254                         return new EventDescriptorCollection(null);
255                 }
256
257                 [MonoTODO]
258                 PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties ()
259                 {
260                         ITypedList typedList = (ITypedList) dataView;
261                         return typedList.GetItemProperties(new PropertyDescriptor[0]);
262                 }
263
264                 [MonoTODO]
265                 PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties (Attribute [] attributes)
266                 {
267                         PropertyDescriptorCollection descriptors;
268                         descriptors = ((ICustomTypeDescriptor) this).GetProperties ();
269                         // TODO: filter out descriptors which do not contain
270                         //       any of those attributes
271                         //       except, those descriptors 
272                         //       that contain DefaultMemeberAttribute
273                         return descriptors;
274                 }
275                 
276                 [MonoTODO]
277                 object ICustomTypeDescriptor.GetPropertyOwner (PropertyDescriptor pd)
278                 {
279                         return this;
280                 }
281
282                 #endregion // ICustomTypeDescriptor implementations
283
284                 #region IDataErrorInfo implementation
285
286                 string IDataErrorInfo.Error {
287                         [MonoTODO]
288                         get {
289                                 return ""; // FIXME
290                         }
291                 }
292
293                 string IDataErrorInfo.this[string columnName] {
294                         [MonoTODO]
295                         get {
296                                 return ""; // FIXME
297                         }
298                 }
299
300                 #endregion // IDataErrorInfo implementation
301         }
302 }