DesignerOptionService.cs 8.9 KB

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