2 // System.ComponentModel.Design.DesignerOptionService
5 // Ivan N. Zlatev (contact i-nZ.net)
7 // (C) 2006-2007 Ivan N. Zlatev
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
32 using System.Collections;
33 using System.Globalization;
34 using System.ComponentModel;
36 namespace System.ComponentModel.Design
39 public abstract class DesignerOptionService : IDesignerOptionService
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
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.
51 public sealed class WrappedPropertyDescriptor : PropertyDescriptor
53 private PropertyDescriptor _property;
54 private object _component;
56 public WrappedPropertyDescriptor (PropertyDescriptor property, object component) : base (property.Name, new Attribute[0])
59 _component = component;
62 public override object GetValue (object ignored)
64 return _property.GetValue (_component);
67 public override void SetValue (object ignored, object value)
69 _property.SetValue (_component, value);
72 public override bool CanResetValue (object ignored)
74 return _property.CanResetValue (_component);
77 public override void ResetValue (object ignored)
79 _property.ResetValue (_component);
82 public override bool ShouldSerializeValue (object ignored)
84 return _property.ShouldSerializeValue (_component);
87 public override AttributeCollection Attributes {
88 get { return _property.Attributes; }
91 public override bool IsReadOnly {
92 get { return _property.IsReadOnly; }
95 public override Type ComponentType {
96 get { return _property.ComponentType; }
99 public override Type PropertyType {
100 get { return _property.PropertyType; }
102 } // WrappedPropertyDescriptor
104 private string _name;
105 private object _propertiesProvider;
106 private DesignerOptionCollection _parent;
107 private ArrayList _children;
108 private DesignerOptionService _optionService;
110 internal DesignerOptionCollection (DesignerOptionCollection parent, string name, object propertiesProvider, DesignerOptionService service)
113 _propertiesProvider = propertiesProvider;
116 if (parent != null) {
117 if (parent._children == null)
118 parent._children = new ArrayList ();
119 parent._children.Add (this);
122 _children = new ArrayList ();
123 _optionService = service;
124 service.PopulateOptionCollection (this);
127 public bool ShowDialog ()
129 return _optionService.ShowDialog (this, _propertiesProvider);
132 public DesignerOptionCollection this[int index] {
133 get { return (DesignerOptionCollection) _children[index]; }
136 public DesignerOptionCollection this[string index] {
138 foreach (DesignerOptionCollection dc in _children) {
139 if (String.Compare (dc.Name, index, true, CultureInfo.InvariantCulture) == 0)
147 get { return _name; }
152 if (_children != null)
153 return _children.Count;
158 public DesignerOptionCollection Parent {
159 get { return _parent; }
162 public PropertyDescriptorCollection Properties {
164 // TypeDescriptor.GetProperties gets only the public properties.
166 PropertyDescriptorCollection properties = TypeDescriptor.GetProperties (_propertiesProvider);
167 ArrayList wrappedProperties = new ArrayList (properties.Count);
169 foreach (PropertyDescriptor pd in properties)
170 wrappedProperties.Add (new WrappedPropertyDescriptor (pd, _propertiesProvider));
172 PropertyDescriptor[] propertyArray = (PropertyDescriptor[]) wrappedProperties.ToArray (typeof (PropertyDescriptor));
173 return new PropertyDescriptorCollection (propertyArray);
177 public IEnumerator GetEnumerator ()
179 return _children.GetEnumerator ();
182 public int IndexOf (DesignerOptionCollection item)
184 return _children.IndexOf (item);
187 public void CopyTo (Array array, int index)
189 _children.CopyTo (array, index);
192 bool IList.IsFixedSize {
196 bool IList.IsReadOnly {
200 object IList.this[int index] {
201 get { return this[index]; }
202 set { throw new NotSupportedException (); }
205 bool ICollection.IsSynchronized {
206 get { return false; }
209 object ICollection.SyncRoot {
213 bool IList.Contains (object item)
215 return _children.Contains (item);
218 int IList.IndexOf (object item)
220 return _children.IndexOf (item);
223 int IList.Add (object item)
225 throw new NotSupportedException ();
228 void IList.Remove (object item)
230 throw new NotSupportedException ();
233 void IList.RemoveAt (int index)
235 throw new NotSupportedException ();
238 void IList.Insert (int index, object item)
240 throw new NotSupportedException ();
245 throw new NotSupportedException ();
248 } // DesignerOptionCollection
251 private DesignerOptionCollection _options;
253 protected internal DesignerOptionService ()
257 protected DesignerOptionCollection CreateOptionCollection (DesignerOptionCollection parent, string name, Object value)
260 throw new ArgumentNullException ("name");
262 throw new ArgumentNullException ("parent");
263 if (name == String.Empty)
264 throw new ArgumentException ("name.Length == 0");
266 return new DesignerOptionCollection (parent, name, value, this);
269 protected virtual bool ShowDialog (DesignerOptionCollection options, object optionObject)
274 protected virtual void PopulateOptionCollection (DesignerOptionCollection options)
278 public DesignerOptionCollection Options {
280 if (_options == null)
281 _options = new DesignerOptionCollection (null, String.Empty, null, this);
288 object IDesignerOptionService.GetOptionValue (string pageName, string valueName)
290 if (pageName == null)
291 throw new ArgumentNullException ("pageName");
292 if (valueName == null)
293 throw new ArgumentNullException ("valueName");
295 PropertyDescriptor property = GetOptionProperty (pageName, valueName);
296 if (property != null)
297 return property.GetValue (null);
302 void IDesignerOptionService.SetOptionValue (string pageName, string valueName, object value)
304 if (pageName == null)
305 throw new ArgumentNullException ("pageName");
306 if (valueName == null)
307 throw new ArgumentNullException ("valueName");
309 PropertyDescriptor property = GetOptionProperty (pageName, valueName);
310 if (property != null)
311 property.SetValue (null, value);
314 // Go to the page and get the property associated with the option name.
316 private PropertyDescriptor GetOptionProperty (string pageName, string valueName)
318 string[] pages = pageName.Split (new char[] { '\\' });
320 DesignerOptionCollection options = this.Options;
321 foreach (string page in pages) {
322 options = options[page];
327 return options.Properties[valueName];