2005-10-07 Chris Toshok <toshok@ximian.com>
[mono.git] / mcs / class / System.Configuration / System.Configuration / PropertyInformation.cs
1 //
2 // System.Configuration.PropertyInformation.cs
3 //
4 // Authors:
5 //  Lluis Sanchez Gual (lluis@novell.com)
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining
8 // a copy of this software and associated documentation files (the
9 // "Software"), to deal in the Software without restriction, including
10 // without limitation the rights to use, copy, modify, merge, publish,
11 // distribute, sublicense, and/or sell copies of the Software, and to
12 // permit persons to whom the Software is furnished to do so, subject to
13 // the following conditions:
14 // 
15 // The above copyright notice and this permission notice shall be
16 // included in all copies or substantial portions of the Software.
17 // 
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 //
26 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
27 //
28
29 #if NET_2_0
30 using System.ComponentModel;
31
32 namespace System.Configuration
33 {
34         public sealed class PropertyInformation
35         {
36                 bool isLocked;
37                 bool isModified;
38                 int lineNumber;
39                 string source;
40                 object val;
41                 PropertyValueOrigin origin;
42                 ConfigurationElement owner;
43                 
44                 ConfigurationProperty property;
45                 
46                 internal PropertyInformation (ConfigurationElement owner, ConfigurationProperty property)
47                 {
48                         this.owner = owner;
49                         this.property = property;
50                 }
51                 
52                 public TypeConverter Converter {
53                         get { return property.Converter; }
54                 }
55                 
56                 public object DefaultValue {
57                         get { return property.DefaultValue; }
58                 }
59                 
60                 public string Description {
61                         get { return property.Description; }
62                 }
63                 
64                 public bool IsKey {
65                         get { return property.IsKey; }
66                 }
67                 
68                 [MonoTODO]
69                 public bool IsLocked {
70                         get { return isLocked; }
71                         internal set { isLocked = value; }
72                 }
73                 
74                 public bool IsModified {
75                         get { return isModified; }
76                         internal set { isModified = value; }
77                 }
78                 
79                 public bool IsRequired {
80                         get { return property.IsRequired; }
81                 }
82                 
83                 [MonoTODO]
84                 public int LineNumber {
85                         get { return lineNumber; }
86                         internal set { lineNumber = value; }
87                 }
88                 
89                 public string Name {
90                         get { return property.Name; }
91                 }
92                 
93                 [MonoTODO]
94                 public string Source {
95                         get { return source; }
96                         internal set { source = value; }
97                 }
98                 
99                 public Type Type {
100                         get { return property.Type; }
101                 }
102                 
103                 public ConfigurationValidatorBase Validator {
104                         get { return property.Validator; }
105                 }
106                 
107                 public object Value {
108                         get {
109                                 if (origin == PropertyValueOrigin.Default) {
110                                         if (property.IsElement) {
111                                                 ConfigurationElement elem = (ConfigurationElement) Activator.CreateInstance (Type);
112                                                 elem.InitFromProperty (this);
113                                                 if (owner != null && owner.IsReadOnly ())
114                                                         elem.SetReadOnly ();
115                                                 val = elem;
116                                                 origin = PropertyValueOrigin.Inherited;
117                                         } else
118                                                 return DefaultValue;
119                                 }
120                                 return val;
121                         }
122                         set {
123                                 val = value;
124                                 isModified = true;
125                                 origin = PropertyValueOrigin.SetHere;
126                         }
127                 }
128                 
129                 internal void Reset (PropertyInformation parentProperty)
130                 {
131                         if (parentProperty != null) {
132                                 if (property.IsElement) {
133                                         ((ConfigurationElement)Value).Reset ((ConfigurationElement) parentProperty.Value);
134                                 }
135                                 else {
136                                         val = parentProperty.Value;
137                                         origin = PropertyValueOrigin.Inherited;
138                                 }
139                         } else {
140                                 origin = PropertyValueOrigin.Default;
141                         }
142                 }
143                 
144                 internal bool IsElement {
145                         get { return property.IsElement; }
146                 }
147                 
148                 public PropertyValueOrigin ValueOrigin {
149                         get { return origin; }
150                 }
151                 
152                 internal string GetStringValue ()
153                 {
154                         return property.ConvertToString (Value);
155                 }
156                 
157                 internal void SetStringValue (string value)
158                 {
159                         val = property.ConvertFromString (value);
160                         if (!object.Equals (val, DefaultValue))
161                                 origin = PropertyValueOrigin.SetHere;
162                 }
163                 
164                 internal ConfigurationProperty Property {
165                         get { return property; }
166                 }
167         }
168 }
169 #endif