2005-02-03 Atsushi Enomoto <atsushi@ximian.com>
[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                 }
98
99                 public DataView CreateChildView (string name)
100                 {
101                         return CreateChildView (
102                                 dataRow.Table.ChildRelations [name]);
103                 }
104
105                 public void Delete ()
106                 {
107                         dataView.DeleteRowView (this);
108                         isNew = false;
109                 }
110
111                 public void EndEdit ()
112                 {
113                         dataView.EndEditRowView (this);
114                         isNew = false;
115                 }
116
117                 #endregion // Methods
118
119                 #region Properties
120                 
121                 public DataView DataView {
122                         get { return dataView; }
123                 }
124
125                 public bool IsEdit {
126                         get { return dataRow.IsEditing; }
127                 }
128
129                 // It becomes true when this instance is created by
130                 // DataView.AddNew(). If it is true, then the DataRow is
131                 // "Detached", and when this.EndEdit() is invoked, the row
132                 // will be added to the table.
133                 public bool IsNew {
134                         get { return isNew; }
135                 }
136                 
137                 [System.Runtime.CompilerServices.IndexerName("Item")]
138                 public object this[string column] {
139                         get {
140                                 DataColumn dc = dataView.Table.Columns[column];
141                                 return dataRow[dc];
142                         }
143                         set {
144                                 DataColumn dc = dataView.Table.Columns[column];
145                                 dataRow[dc] = value;
146                         }
147                 }
148
149                 // the compiler creates a DefaultMemeberAttribute from
150                 // this IndexerNameAttribute
151                 public object this[int column] {
152                         get {
153                                 DataColumn dc = dataView.Table.Columns[column];
154                                 return dataRow[dc];
155                         }
156                         set {
157                                 DataColumn dc = dataView.Table.Columns[column];
158                                 dataRow[dc] = value;
159
160                         }
161                 }
162
163                 public DataRow Row {
164                         get { return dataRow; }
165                 }
166
167                 public DataRowVersion RowVersion {
168                         [MonoTODO]
169                         get {
170                                 return rowVersion;
171                         }
172                 }
173
174                 // It returns the hash code of the DataRow object.
175                 public override int GetHashCode ()
176                 {
177                         return dataRow.GetHashCode ();
178                 }
179
180                 #endregion // Properties
181                 
182                 #region ICustomTypeDescriptor implementations
183                 
184                 [MonoTODO]
185                 AttributeCollection ICustomTypeDescriptor.GetAttributes  ()
186                 {
187                         System.ComponentModel.AttributeCollection attributes;
188                         attributes = AttributeCollection.Empty;
189                         return attributes;
190                 }
191
192                 [MonoTODO]
193                 string ICustomTypeDescriptor.GetClassName ()
194                 {
195                         return "";
196                 }
197                 
198                 [MonoTODO]
199                 string ICustomTypeDescriptor.GetComponentName ()
200                 {
201                         return null;
202                 }
203
204                 [MonoTODO]
205                 TypeConverter ICustomTypeDescriptor.GetConverter ()
206                 {
207                         return null;
208                 }
209
210                 [MonoTODO]
211                 EventDescriptor ICustomTypeDescriptor.GetDefaultEvent ()
212                 {
213                         return null;
214                 }
215                 
216                 [MonoTODO]
217                 PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty ()
218                 {
219                         return null;
220                 }
221                 
222                 [MonoTODO]
223                 object ICustomTypeDescriptor.GetEditor (Type editorBaseType)
224                 {
225                         return null;
226                 }
227                 
228                 [MonoTODO]
229                 EventDescriptorCollection ICustomTypeDescriptor.GetEvents ()
230                 {
231                         return new EventDescriptorCollection(null);
232                 }
233
234                 [MonoTODO]
235                 EventDescriptorCollection ICustomTypeDescriptor.GetEvents (Attribute [] attributes)
236                 {
237                         return new EventDescriptorCollection(null);
238                 }
239
240                 [MonoTODO]
241                 PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties ()
242                 {
243                         ITypedList typedList = (ITypedList) dataView;
244                         return typedList.GetItemProperties(new PropertyDescriptor[0]);
245                 }
246
247                 [MonoTODO]
248                 PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties (Attribute [] attributes)
249                 {
250                         PropertyDescriptorCollection descriptors;
251                         descriptors = ((ICustomTypeDescriptor) this).GetProperties ();
252                         // TODO: filter out descriptors which do not contain
253                         //       any of those attributes
254                         //       except, those descriptors 
255                         //       that contain DefaultMemeberAttribute
256                         return descriptors;
257                 }
258                 
259                 [MonoTODO]
260                 object ICustomTypeDescriptor.GetPropertyOwner (PropertyDescriptor pd)
261                 {
262                         return this;
263                 }
264
265                 #endregion // ICustomTypeDescriptor implementations
266
267                 #region IDataErrorInfo implementation
268
269                 string IDataErrorInfo.Error {
270                         [MonoTODO]
271                         get {
272                                 return ""; // FIXME
273                         }
274                 }
275
276                 string IDataErrorInfo.this[string columnName] {
277                         [MonoTODO]
278                         get {
279                                 return ""; // FIXME
280                         }
281                 }
282
283                 #endregion // IDataErrorInfo implementation
284         }
285 }