* DataSet.cs :
[mono.git] / mcs / class / System.Data / System.Data / DataColumnPropertyDescriptor.cs
1 //
2 // System.Data.DataColumnPropertyDescriptor.cs
3 //
4 // Author:
5 //   Daniel Morgan <danmorg@sc.rr.com>
6 //
7 // (c) copyright 2002 Daniel Morgan
8 //
9
10 using System;
11 using System.Data.Common;
12 using System.ComponentModel;
13 using System.Reflection;
14
15 namespace System.Data 
16 {
17         public class DataColumnPropertyDescriptor : PropertyDescriptor 
18         {
19                 private bool readOnly = true;
20                 private Type componentType = null;
21                 private Type propertyType = null;
22                 private PropertyInfo prop = null;
23                 private int columnIndex = 0;
24
25                 public DataColumnPropertyDescriptor (string name, int columnIndex, Attribute [] attrs)
26                         : base (name, attrs) 
27                 {
28                         this.columnIndex = columnIndex;
29                 }
30
31                 public void SetReadOnly (bool value) 
32                 {
33                         readOnly = value;
34                 }
35                 
36                 public void SetComponentType (Type type) 
37                 {
38                         componentType = type;
39                 }
40
41                 public void SetPropertyType (Type type) 
42                 {
43                         propertyType = type;
44                 }
45
46                 private PropertyInfo GetPropertyInfo () 
47                 {
48                         string defaultMemberName = "";
49                         object[] attribs = componentType.GetCustomAttributes (true);\r
50                                                 \r
51                         for (int at = 0; at < attribs.Length; at++) {\r
52                                 if (attribs[at] is DefaultMemberAttribute) {\r
53                                         defaultMemberName = ((DefaultMemberAttribute) attribs[at]).MemberName;\r
54                                         break;\r
55                                 }\r
56                         }\r
57 \r
58                         // FIXME: what do I do if a DefaultMemeberAttribute is not found?\r
59                         //        should I try looking for DefaultPropertyAttribute?\r
60                         if (defaultMemberName.Equals(""))\r
61                                 throw new Exception("Default property not found.");\r
62 \r
63                         Type[] parmTypes = new Type[1];\r
64                         parmTypes[0] = propertyType;\r
65                         PropertyInfo propertyInfo = componentType.GetProperty (defaultMemberName, parmTypes);
66                         return propertyInfo;
67                 }
68
69                 public override object GetValue (object component) 
70                 {
71                         // FIXME: what is the correct way to Get a Value?
72                         if(componentType == typeof(DataRowView) && component is DataRowView) {
73                                 DataRowView drv = (DataRowView) component;
74                                 return drv[base.Name];
75                         }
76                         else if(componentType == typeof(DbDataRecord) && component is DbDataRecord) {
77                                 DbDataRecord dr = (DbDataRecord) component;
78                                 return dr[columnIndex];
79                         }
80                         throw new InvalidOperationException();
81
82                         /*
83                         if (prop == null)
84                                 prop = GetPropertyInfo ();              
85                                                         \r
86                         // FIXME: should I allow multiple parameters?                                                                                   \r
87                         object[] parms = new object[1];\r
88                         parms[0] = base.Name;\r
89                         return prop.GetValue (component, parms);
90                         */
91                 }
92
93                 public override void SetValue(object component, object value) 
94                 {
95                         DataRowView drv = (DataRowView) component;
96                         drv[base.Name] = value;
97                         /*
98                         if (prop == null)
99                                 prop = GetPropertyInfo ();              
100
101                         if (readOnly == true) {
102                                 // FIXME: what really happens if read only?
103                                 throw new Exception("Property is ReadOnly");
104                         }
105                         \r
106                         // FIXME: should I allow multiple parameters?\r
107                         object[] parms = new Object[1];\r
108                         parms[0] = base.Name;\r
109                         prop.SetValue (component, value, parms);
110                         */
111                 }
112
113                 [MonoTODO]
114                 public override void ResetValue(object component) 
115                 {
116                         // FIXME:
117                 }
118
119                 [MonoTODO]
120                 public override bool CanResetValue(object component) 
121                 {
122                         return false; // FIXEME
123                 }
124
125                 [MonoTODO]
126                 public override bool ShouldSerializeValue(object component) 
127                 {
128                         return false;
129                 }
130
131                 public override Type ComponentType {
132                         get {
133                                 return componentType;
134                         }
135                 }
136
137                 public override bool IsReadOnly {
138                         get {
139                                 return readOnly;        
140                         }
141                 }
142
143                 public override Type PropertyType {
144                         get {
145                                 return propertyType;
146                         }
147                 }
148         }\r
149 }