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