2010-04-06 Jb Evain <jbevain@novell.com>
[mcs.git] / class / System / System.ComponentModel.Design / DesignerOptionService.cs
blobb7c8dec100065379f6eddc92cc3b99e34a0a5ab7
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
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.
30 #if NET_2_0
32 using System;
33 using System.Collections;
34 using System.Globalization;
35 using System.ComponentModel;
37 namespace System.ComponentModel.Design
40 public abstract class DesignerOptionService : IDesignerOptionService
42 [MonoTODO ("implement own TypeConverter")]
43 [TypeConverter (typeof (TypeConverter))]
44 [Editor ("", "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
45 public sealed class DesignerOptionCollection : IList, ICollection, IEnumerable
47 // PropertyDescriptor objects are taken directly from the value passed to the CreateOptionCollection method
48 // and are wrapped in an additional property descriptor that hides the value object from the user. This means
49 // that any value may be passed into the component parameter of the various PropertyDescriptor methods. The
50 // value is not recognized and is replaced with the correct value internally.
52 public sealed class WrappedPropertyDescriptor : PropertyDescriptor
54 private PropertyDescriptor _property;
55 private object _component;
57 public WrappedPropertyDescriptor (PropertyDescriptor property, object component) : base (property.Name, new Attribute[0])
59 _property = property;
60 _component = component;
63 public override object GetValue (object ignored)
65 return _property.GetValue (_component);
68 public override void SetValue (object ignored, object value)
70 _property.SetValue (_component, value);
73 public override bool CanResetValue (object ignored)
75 return _property.CanResetValue (_component);
78 public override void ResetValue (object ignored)
80 _property.ResetValue (_component);
83 public override bool ShouldSerializeValue (object ignored)
85 return _property.ShouldSerializeValue (_component);
88 public override AttributeCollection Attributes {
89 get { return _property.Attributes; }
92 public override bool IsReadOnly {
93 get { return _property.IsReadOnly; }
96 public override Type ComponentType {
97 get { return _property.ComponentType; }
100 public override Type PropertyType {
101 get { return _property.PropertyType; }
103 } // WrappedPropertyDescriptor
105 private string _name;
106 private object _propertiesProvider;
107 private DesignerOptionCollection _parent;
108 private ArrayList _children;
109 private DesignerOptionService _optionService;
111 internal DesignerOptionCollection (DesignerOptionCollection parent, string name, object propertiesProvider, DesignerOptionService service)
113 _name = name;
114 _propertiesProvider = propertiesProvider;
115 _parent = parent;
117 if (parent != null) {
118 if (parent._children == null)
119 parent._children = new ArrayList ();
120 parent._children.Add (this);
123 _children = new ArrayList ();
124 _optionService = service;
125 service.PopulateOptionCollection (this);
128 public bool ShowDialog ()
130 return _optionService.ShowDialog (this, _propertiesProvider);
133 public DesignerOptionCollection this[int index] {
134 get { return (DesignerOptionCollection) _children[index]; }
137 public DesignerOptionCollection this[string index] {
138 get {
139 foreach (DesignerOptionCollection dc in _children) {
140 if (String.Compare (dc.Name, index, true, CultureInfo.InvariantCulture) == 0)
141 return dc;
143 return null;
147 public string Name {
148 get { return _name; }
151 public int Count {
152 get {
153 if (_children != null)
154 return _children.Count;
155 return 0;
159 public DesignerOptionCollection Parent {
160 get { return _parent; }
163 public PropertyDescriptorCollection Properties {
164 get {
165 // TypeDescriptor.GetProperties gets only the public properties.
167 PropertyDescriptorCollection properties = TypeDescriptor.GetProperties (_propertiesProvider);
168 ArrayList wrappedProperties = new ArrayList (properties.Count);
170 foreach (PropertyDescriptor pd in properties)
171 wrappedProperties.Add (new WrappedPropertyDescriptor (pd, _propertiesProvider));
173 PropertyDescriptor[] propertyArray = (PropertyDescriptor[]) wrappedProperties.ToArray (typeof (PropertyDescriptor));
174 return new PropertyDescriptorCollection (propertyArray);
178 public IEnumerator GetEnumerator ()
180 return _children.GetEnumerator ();
183 public int IndexOf (DesignerOptionCollection item)
185 return _children.IndexOf (item);
188 public void CopyTo (Array array, int index)
190 _children.CopyTo (array, index);
193 bool IList.IsFixedSize {
194 get { return true; }
197 bool IList.IsReadOnly {
198 get { return true; }
201 object IList.this[int index] {
202 get { return this[index]; }
203 set { throw new NotSupportedException (); }
206 bool ICollection.IsSynchronized {
207 get { return false; }
210 object ICollection.SyncRoot {
211 get { return this; }
214 bool IList.Contains (object item)
216 return _children.Contains (item);
219 int IList.IndexOf (object item)
221 return _children.IndexOf (item);
224 int IList.Add (object item)
226 throw new NotSupportedException ();
229 void IList.Remove (object item)
231 throw new NotSupportedException ();
234 void IList.RemoveAt (int index)
236 throw new NotSupportedException ();
239 void IList.Insert (int index, object item)
241 throw new NotSupportedException ();
244 void IList.Clear ()
246 throw new NotSupportedException ();
249 } // DesignerOptionCollection
252 private DesignerOptionCollection _options;
254 protected internal DesignerOptionService ()
258 protected DesignerOptionCollection CreateOptionCollection (DesignerOptionCollection parent, string name, Object value)
260 if (name == null)
261 throw new ArgumentNullException ("name");
262 if (parent == null)
263 throw new ArgumentNullException ("parent");
264 if (name == String.Empty)
265 throw new ArgumentException ("name.Length == 0");
267 return new DesignerOptionCollection (parent, name, value, this);
270 protected virtual bool ShowDialog (DesignerOptionCollection options, object optionObject)
272 return false;
275 protected virtual void PopulateOptionCollection (DesignerOptionCollection options)
279 public DesignerOptionCollection Options {
280 get {
281 if (_options == null)
282 _options = new DesignerOptionCollection (null, String.Empty, null, this);
284 return _options;
289 object IDesignerOptionService.GetOptionValue (string pageName, string valueName)
291 if (pageName == null)
292 throw new ArgumentNullException ("pageName");
293 if (valueName == null)
294 throw new ArgumentNullException ("valueName");
296 PropertyDescriptor property = GetOptionProperty (pageName, valueName);
297 if (property != null)
298 return property.GetValue (null);
300 return null;
303 void IDesignerOptionService.SetOptionValue (string pageName, string valueName, object value)
305 if (pageName == null)
306 throw new ArgumentNullException ("pageName");
307 if (valueName == null)
308 throw new ArgumentNullException ("valueName");
310 PropertyDescriptor property = GetOptionProperty (pageName, valueName);
311 if (property != null)
312 property.SetValue (null, value);
315 // Go to the page and get the property associated with the option name.
317 private PropertyDescriptor GetOptionProperty (string pageName, string valueName)
319 string[] pages = pageName.Split (new char[] { '\\' });
321 DesignerOptionCollection options = this.Options;
322 foreach (string page in pages) {
323 options = options[page];
324 if (options == null)
325 return null;
328 return options.Properties[valueName];
333 #endif