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