Implement MachineKey.Protect and MachineKey.Unprotect
[mono.git] / mcs / class / System / System.ComponentModel.Design / DesignerOptionService.cs
1 //
2 // System.ComponentModel.Design.DesignerOptionService
3 //
4 // Authors: 
5 //  Ivan N. Zlatev (contact i-nZ.net)
6 //
7 // (C) 2006-2007 Ivan N. Zlatev
8
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30
31 using System;
32 using System.Collections;
33 using System.Globalization;
34 using System.ComponentModel;
35
36 namespace System.ComponentModel.Design
37 {
38
39         public abstract class DesignerOptionService : IDesignerOptionService
40         {
41                 [MonoTODO ("implement own TypeConverter")]
42                 [TypeConverter (typeof (TypeConverter))]
43                 [Editor ("", "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
44                 public sealed class DesignerOptionCollection : IList, ICollection, IEnumerable
45                 {
46                         // PropertyDescriptor objects are taken directly from the value passed to the CreateOptionCollection method
47                         // and are wrapped in an additional property descriptor that hides the value object from the user. This means
48                         // that any value may be passed into the component parameter of the various PropertyDescriptor methods. The
49                         // value is not recognized and is replaced with the correct value internally.
50                         //
51                         public sealed class WrappedPropertyDescriptor : PropertyDescriptor
52                         {
53                                 private PropertyDescriptor _property;
54                                 private object _component;
55                                 
56                                 public WrappedPropertyDescriptor (PropertyDescriptor property, object component) : base (property.Name, new Attribute[0])
57                                 {
58                                         _property = property;
59                                         _component = component;
60                                 }
61
62                                 public override object GetValue (object ignored)
63                                 {
64                                         return _property.GetValue (_component);
65                                 }
66
67                                 public override void SetValue (object ignored, object value) 
68                                 {
69                                         _property.SetValue (_component, value);
70                                 }
71
72                                 public override bool CanResetValue (object ignored)
73                                 {
74                                         return _property.CanResetValue (_component);
75                                 }
76
77                                 public override void ResetValue (object ignored)
78                                 {
79                                         _property.ResetValue (_component);
80                                 }
81
82                                 public override bool ShouldSerializeValue (object ignored)
83                                 {
84                                         return _property.ShouldSerializeValue (_component);
85                                 }
86
87                                 public override AttributeCollection Attributes {
88                                         get { return _property.Attributes; }
89                                 }
90
91                                 public override bool IsReadOnly {
92                                         get { return _property.IsReadOnly; }
93                                 }
94
95                                 public override Type ComponentType {
96                                         get { return _property.ComponentType; }
97                                 }
98
99                                 public override Type PropertyType {
100                                         get { return _property.PropertyType; }
101                                 }
102                         } // WrappedPropertyDescriptor
103                         
104                         private string _name;
105                         private object _propertiesProvider;
106                         private DesignerOptionCollection _parent;
107                         private ArrayList _children;
108                         private DesignerOptionService _optionService;
109                         
110                         internal DesignerOptionCollection (DesignerOptionCollection parent, string name, object propertiesProvider, DesignerOptionService service)
111                         {
112                                 _name = name;
113                                 _propertiesProvider = propertiesProvider;
114                                 _parent = parent;
115
116                                 if (parent != null) {
117                                         if (parent._children == null)
118                                                 parent._children = new ArrayList ();
119                                         parent._children.Add (this);
120                                 }
121                                 
122                                 _children = new ArrayList ();
123                                 _optionService = service;
124                                 service.PopulateOptionCollection (this);
125                         }
126
127                         public bool ShowDialog ()
128                         {
129                                 return _optionService.ShowDialog (this, _propertiesProvider);
130                         }
131
132                         public DesignerOptionCollection this[int index] {
133                                 get { return (DesignerOptionCollection) _children[index]; }
134                         }
135                         
136                         public DesignerOptionCollection this[string index] {
137                                 get {
138                                         foreach (DesignerOptionCollection dc in _children) {
139                                                 if (String.Compare (dc.Name, index, true, CultureInfo.InvariantCulture) == 0)
140                                                                 return dc;
141                                         }
142                                         return null;
143                                 }
144                         }
145                         
146                         public string Name {
147                                 get { return _name; }
148                         }
149
150                         public int Count {
151                                 get {
152                                         if (_children != null)                                   
153                                                 return _children.Count;
154                                         return 0;
155                                 }
156                         }
157
158                         public DesignerOptionCollection Parent {
159                                 get { return _parent; }
160                         }
161
162                         public PropertyDescriptorCollection Properties {
163                                 get {
164                                         // TypeDescriptor.GetProperties gets only the public properties.
165                                         //
166                                         PropertyDescriptorCollection properties = TypeDescriptor.GetProperties (_propertiesProvider);                              
167                                         ArrayList wrappedProperties = new ArrayList (properties.Count);
168                                         
169                                         foreach (PropertyDescriptor pd in properties)
170                                                 wrappedProperties.Add (new WrappedPropertyDescriptor (pd, _propertiesProvider));
171
172                                         PropertyDescriptor[] propertyArray = (PropertyDescriptor[]) wrappedProperties.ToArray (typeof (PropertyDescriptor));
173                                         return new PropertyDescriptorCollection (propertyArray);
174                                 }
175                         }
176                         
177                         public IEnumerator GetEnumerator ()
178                         {
179                                 return _children.GetEnumerator ();
180                         }
181
182                         public int IndexOf (DesignerOptionCollection item)
183                         {
184                                 return _children.IndexOf (item);
185                         }
186
187                         public void CopyTo (Array array, int index)
188                         {
189                                 _children.CopyTo (array, index);
190                         }
191                         
192                         bool IList.IsFixedSize {
193                                 get { return true; }
194                         }
195
196                         bool IList.IsReadOnly {
197                                 get { return true; }
198                         }
199
200                         object IList.this[int index] {
201                                 get { return this[index]; }
202                                 set { throw new NotSupportedException (); }
203                         }
204
205                         bool ICollection.IsSynchronized {
206                                 get { return false; }
207                         }
208
209                         object ICollection.SyncRoot {
210                                 get { return this; }
211                         }
212
213                         bool IList.Contains (object item)
214                         {
215                                 return _children.Contains (item);
216                         }
217
218                         int IList.IndexOf (object item)
219                         {
220                                 return _children.IndexOf (item);
221                         }
222                         
223                         int IList.Add (object item)
224                         {
225                                 throw new NotSupportedException ();
226                         }
227
228                         void IList.Remove (object item)
229                         {
230                                 throw new NotSupportedException ();
231                         }
232                         
233                         void IList.RemoveAt (int index)
234                         {
235                                 throw new NotSupportedException ();
236                         }
237                         
238                         void IList.Insert (int index, object item)
239                         {
240                                 throw new NotSupportedException ();
241                         }
242
243                         void IList.Clear ()
244                         {
245                                 throw new NotSupportedException ();
246                         }
247                         
248                 } // DesignerOptionCollection
249
250
251                 private DesignerOptionCollection _options;
252                 
253                 protected internal DesignerOptionService ()
254                 {
255                 }
256
257                 protected DesignerOptionCollection CreateOptionCollection (DesignerOptionCollection parent, string name, Object value)
258                 {
259                         if (name == null)
260                                 throw new ArgumentNullException ("name");
261                         if (parent == null)
262                                 throw new ArgumentNullException ("parent");
263                         if (name == String.Empty)
264                                 throw new ArgumentException ("name.Length == 0");
265                                 
266                         return new DesignerOptionCollection (parent, name, value, this);                        
267                 }
268
269                 protected virtual bool ShowDialog (DesignerOptionCollection options, object optionObject)
270                 {
271                         return false;
272                 }
273
274                 protected virtual void PopulateOptionCollection (DesignerOptionCollection options)
275                 {
276                 }
277                 
278                 public DesignerOptionCollection Options {
279                         get {
280                                 if (_options == null)
281                                         _options = new DesignerOptionCollection (null, String.Empty, null, this);
282
283                                 return _options;
284                         }
285                 }
286
287                 
288                 object IDesignerOptionService.GetOptionValue (string pageName, string valueName)
289                 {
290                         if (pageName == null)
291                                 throw new ArgumentNullException ("pageName");
292                         if (valueName == null)
293                                 throw new ArgumentNullException ("valueName");
294
295                         PropertyDescriptor property = GetOptionProperty (pageName, valueName);
296                         if (property != null)
297                                 return property.GetValue (null);
298                         
299                         return null;
300                 }
301
302                 void IDesignerOptionService.SetOptionValue (string pageName, string valueName, object value)
303                 {
304                         if (pageName == null)
305                                 throw new ArgumentNullException ("pageName");
306                         if (valueName == null)
307                                 throw new ArgumentNullException ("valueName");
308
309                         PropertyDescriptor property = GetOptionProperty (pageName, valueName);
310                         if (property != null)
311                                 property.SetValue (null, value);
312                 }
313
314                 // Go to the page and get the property associated with the option name.
315                 //
316                 private PropertyDescriptor GetOptionProperty (string pageName, string valueName)
317                 {
318                         string[] pages = pageName.Split (new char[] { '\\' });
319
320                         DesignerOptionCollection options = this.Options;
321                         foreach (string page in pages) {
322                                 options = options[page];
323                                 if (options == null)
324                                         return null;
325                         }
326
327                         return options.Properties[valueName];
328                 }
329                 
330         }
331 }