merge -r 58060:58217
[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 if (property.IsDefaultCollection) {
119                                                 ConfigurationElementCollection col = (ConfigurationElementCollection) Activator.CreateInstance (Type);
120                                                 col.InitFromProperty (this);
121                                                 if (owner != null && owner.IsReadOnly ())
122                                                         col.SetReadOnly ();
123                                                 val = col;
124                                                 origin = PropertyValueOrigin.Inherited;
125                                         }
126                                         else {
127                                                 return DefaultValue;
128                                         }
129                                 }
130                                 return val;
131                         }
132                         set {
133                                 val = value;
134                                 isModified = true;
135                                 origin = PropertyValueOrigin.SetHere;
136                         }
137                 }
138                 
139                 internal void Reset (PropertyInformation parentProperty)
140                 {
141                         if (parentProperty != null) {
142                                 if (property.IsElement) {
143                                         ((ConfigurationElement)Value).Reset ((ConfigurationElement) parentProperty.Value);
144                                 }
145                                 else {
146                                         val = parentProperty.Value;
147                                         origin = PropertyValueOrigin.Inherited;
148                                 }
149                         } else {
150                                 origin = PropertyValueOrigin.Default;
151                         }
152                 }
153                 
154                 internal bool IsElement {
155                         get { return property.IsElement; }
156                 }
157                 
158                 public PropertyValueOrigin ValueOrigin {
159                         get { return origin; }
160                 }
161                 
162                 internal string GetStringValue ()
163                 {
164                         return property.ConvertToString (Value);
165                 }
166                 
167                 internal void SetStringValue (string value)
168                 {
169                         val = property.ConvertFromString (value);
170                         if (!object.Equals (val, DefaultValue))
171                                 origin = PropertyValueOrigin.SetHere;
172                 }
173                 
174                 internal ConfigurationProperty Property {
175                         get { return property; }
176                 }
177         }
178 }
179 #endif