Merge branch 'master' of http://github.com/mono/mono
[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                 readonly ConfigurationElement owner;
43                 readonly ConfigurationProperty property;
44                 
45                 internal PropertyInformation (ConfigurationElement owner, ConfigurationProperty property)
46                 {
47                         if (owner == null)
48                                 throw new ArgumentNullException ("owner");
49                         if (property == null)
50                                 throw new ArgumentNullException ("property");
51                         this.owner = owner;
52                         this.property = property;
53                 }
54                 
55                 public TypeConverter Converter {
56                         get { return property.Converter; }
57                 }
58                 
59                 public object DefaultValue {
60                         get { return property.DefaultValue; }
61                 }
62                 
63                 public string Description {
64                         get { return property.Description; }
65                 }
66                 
67                 public bool IsKey {
68                         get { return property.IsKey; }
69                 }
70                 
71                 [MonoTODO]
72                 public bool IsLocked {
73                         get { return isLocked; }
74                         internal set { isLocked = value; }
75                 }
76                 
77                 public bool IsModified {
78                         get { return isModified; }
79                         internal set { isModified = value; }
80                 }
81                 
82                 public bool IsRequired {
83                         get { return property.IsRequired; }
84                 }
85                 
86                 [MonoTODO]
87                 public int LineNumber {
88                         get { return lineNumber; }
89                         internal set { lineNumber = value; }
90                 }
91                 
92                 public string Name {
93                         get { return property.Name; }
94                 }
95                 
96                 [MonoTODO]
97                 public string Source {
98                         get { return source; }
99                         internal set { source = value; }
100                 }
101                 
102                 public Type Type {
103                         get { return property.Type; }
104                 }
105                 
106                 public ConfigurationValidatorBase Validator {
107                         get { return property.Validator; }
108                 }
109                 
110                 public object Value {
111                         get {
112                                 if (origin == PropertyValueOrigin.Default) {
113                                         if (property.IsElement) {
114                                                 ConfigurationElement elem = (ConfigurationElement) Activator.CreateInstance (Type, true);
115                                                 elem.InitFromProperty (this);
116                                                 if (owner != null && owner.IsReadOnly ())
117                                                         elem.SetReadOnly ();
118                                                 val = elem;
119                                                 origin = PropertyValueOrigin.Inherited;
120                                         }
121                                         else {
122                                                 return DefaultValue;
123                                         }
124                                 }
125                                 return val;
126                         }
127                         set {
128                                 val = value;
129                                 isModified = true;
130                                 origin = PropertyValueOrigin.SetHere;
131                         }
132                 }
133                 
134                 internal void Reset (PropertyInformation parentProperty)
135                 {
136                         if (parentProperty != null) {
137                                 if (property.IsElement) {
138                                         ((ConfigurationElement)Value).Reset ((ConfigurationElement) parentProperty.Value);
139                                 }
140                                 else {
141                                         val = parentProperty.Value;
142                                         origin = PropertyValueOrigin.Inherited;
143                                 }
144                         } else {
145                                 origin = PropertyValueOrigin.Default;
146                         }
147                 }
148                 
149                 internal bool IsElement {
150                         get { return property.IsElement; }
151                 }
152                 
153                 public PropertyValueOrigin ValueOrigin {
154                         get { return origin; }
155                 }
156                 
157                 internal string GetStringValue ()
158                 {
159                         return property.ConvertToString (Value);
160                 }
161                 
162                 internal void SetStringValue (string value)
163                 {
164                         val = property.ConvertFromString (value);
165                         if (!object.Equals (val, DefaultValue))
166                                 origin = PropertyValueOrigin.SetHere;
167                 }
168                 
169                 internal ConfigurationProperty Property {
170                         get { return property; }
171                 }
172         }
173 }
174 #endif