New test.
[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                                         }
118                                         else {
119                                                 return DefaultValue;
120                                         }
121                                 }
122                                 return val;
123                         }
124                         set {
125                                 val = value;
126                                 isModified = true;
127                                 origin = PropertyValueOrigin.SetHere;
128                         }
129                 }
130                 
131                 internal void Reset (PropertyInformation parentProperty)
132                 {
133                         if (parentProperty != null) {
134                                 if (property.IsElement) {
135                                         ((ConfigurationElement)Value).Reset ((ConfigurationElement) parentProperty.Value);
136                                 }
137                                 else {
138                                         val = parentProperty.Value;
139                                         origin = PropertyValueOrigin.Inherited;
140                                 }
141                         } else {
142                                 origin = PropertyValueOrigin.Default;
143                         }
144                 }
145                 
146                 internal bool IsElement {
147                         get { return property.IsElement; }
148                 }
149                 
150                 public PropertyValueOrigin ValueOrigin {
151                         get { return origin; }
152                 }
153                 
154                 internal string GetStringValue ()
155                 {
156                         return property.ConvertToString (Value);
157                 }
158                 
159                 internal void SetStringValue (string value)
160                 {
161                         val = property.ConvertFromString (value);
162                         if (!object.Equals (val, DefaultValue))
163                                 origin = PropertyValueOrigin.SetHere;
164                 }
165                 
166                 internal ConfigurationProperty Property {
167                         get { return property; }
168                 }
169         }
170 }
171 #endif