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