PageRenderTime 27ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/System/System.ComponentModel.Design/DesignerOptionService.cs

https://github.com/iainlane/mono
C# | 331 lines | 232 code | 63 blank | 36 comment | 30 complexity | e8c5a61fb922d16d264472700ea26b29 MD5 | raw file
  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. using System;
  29. using System.Collections;
  30. using System.Globalization;
  31. using System.ComponentModel;
  32. namespace System.ComponentModel.Design
  33. {
  34. public abstract class DesignerOptionService : IDesignerOptionService
  35. {
  36. [MonoTODO ("implement own TypeConverter")]
  37. [TypeConverter (typeof (TypeConverter))]
  38. [Editor ("", "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  39. public sealed class DesignerOptionCollection : IList, ICollection, IEnumerable
  40. {
  41. // PropertyDescriptor objects are taken directly from the value passed to the CreateOptionCollection method
  42. // and are wrapped in an additional property descriptor that hides the value object from the user. This means
  43. // that any value may be passed into the component parameter of the various PropertyDescriptor methods. The
  44. // value is not recognized and is replaced with the correct value internally.
  45. //
  46. public sealed class WrappedPropertyDescriptor : PropertyDescriptor
  47. {
  48. private PropertyDescriptor _property;
  49. private object _component;
  50. public WrappedPropertyDescriptor (PropertyDescriptor property, object component) : base (property.Name, new Attribute[0])
  51. {
  52. _property = property;
  53. _component = component;
  54. }
  55. public override object GetValue (object ignored)
  56. {
  57. return _property.GetValue (_component);
  58. }
  59. public override void SetValue (object ignored, object value)
  60. {
  61. _property.SetValue (_component, value);
  62. }
  63. public override bool CanResetValue (object ignored)
  64. {
  65. return _property.CanResetValue (_component);
  66. }
  67. public override void ResetValue (object ignored)
  68. {
  69. _property.ResetValue (_component);
  70. }
  71. public override bool ShouldSerializeValue (object ignored)
  72. {
  73. return _property.ShouldSerializeValue (_component);
  74. }
  75. public override AttributeCollection Attributes {
  76. get { return _property.Attributes; }
  77. }
  78. public override bool IsReadOnly {
  79. get { return _property.IsReadOnly; }
  80. }
  81. public override Type ComponentType {
  82. get { return _property.ComponentType; }
  83. }
  84. public override Type PropertyType {
  85. get { return _property.PropertyType; }
  86. }
  87. } // WrappedPropertyDescriptor
  88. private string _name;
  89. private object _propertiesProvider;
  90. private DesignerOptionCollection _parent;
  91. private ArrayList _children;
  92. private DesignerOptionService _optionService;
  93. internal DesignerOptionCollection (DesignerOptionCollection parent, string name, object propertiesProvider, DesignerOptionService service)
  94. {
  95. _name = name;
  96. _propertiesProvider = propertiesProvider;
  97. _parent = parent;
  98. if (parent != null) {
  99. if (parent._children == null)
  100. parent._children = new ArrayList ();
  101. parent._children.Add (this);
  102. }
  103. _children = new ArrayList ();
  104. _optionService = service;
  105. service.PopulateOptionCollection (this);
  106. }
  107. public bool ShowDialog ()
  108. {
  109. return _optionService.ShowDialog (this, _propertiesProvider);
  110. }
  111. public DesignerOptionCollection this[int index] {
  112. get { return (DesignerOptionCollection) _children[index]; }
  113. }
  114. public DesignerOptionCollection this[string index] {
  115. get {
  116. foreach (DesignerOptionCollection dc in _children) {
  117. if (String.Compare (dc.Name, index, true, CultureInfo.InvariantCulture) == 0)
  118. return dc;
  119. }
  120. return null;
  121. }
  122. }
  123. public string Name {
  124. get { return _name; }
  125. }
  126. public int Count {
  127. get {
  128. if (_children != null)
  129. return _children.Count;
  130. return 0;
  131. }
  132. }
  133. public DesignerOptionCollection Parent {
  134. get { return _parent; }
  135. }
  136. public PropertyDescriptorCollection Properties {
  137. get {
  138. // TypeDescriptor.GetProperties gets only the public properties.
  139. //
  140. PropertyDescriptorCollection properties = TypeDescriptor.GetProperties (_propertiesProvider);
  141. ArrayList wrappedProperties = new ArrayList (properties.Count);
  142. foreach (PropertyDescriptor pd in properties)
  143. wrappedProperties.Add (new WrappedPropertyDescriptor (pd, _propertiesProvider));
  144. PropertyDescriptor[] propertyArray = (PropertyDescriptor[]) wrappedProperties.ToArray (typeof (PropertyDescriptor));
  145. return new PropertyDescriptorCollection (propertyArray);
  146. }
  147. }
  148. public IEnumerator GetEnumerator ()
  149. {
  150. return _children.GetEnumerator ();
  151. }
  152. public int IndexOf (DesignerOptionCollection item)
  153. {
  154. return _children.IndexOf (item);
  155. }
  156. public void CopyTo (Array array, int index)
  157. {
  158. _children.CopyTo (array, index);
  159. }
  160. bool IList.IsFixedSize {
  161. get { return true; }
  162. }
  163. bool IList.IsReadOnly {
  164. get { return true; }
  165. }
  166. object IList.this[int index] {
  167. get { return this[index]; }
  168. set { throw new NotSupportedException (); }
  169. }
  170. bool ICollection.IsSynchronized {
  171. get { return false; }
  172. }
  173. object ICollection.SyncRoot {
  174. get { return this; }
  175. }
  176. bool IList.Contains (object item)
  177. {
  178. return _children.Contains (item);
  179. }
  180. int IList.IndexOf (object item)
  181. {
  182. return _children.IndexOf (item);
  183. }
  184. int IList.Add (object item)
  185. {
  186. throw new NotSupportedException ();
  187. }
  188. void IList.Remove (object item)
  189. {
  190. throw new NotSupportedException ();
  191. }
  192. void IList.RemoveAt (int index)
  193. {
  194. throw new NotSupportedException ();
  195. }
  196. void IList.Insert (int index, object item)
  197. {
  198. throw new NotSupportedException ();
  199. }
  200. void IList.Clear ()
  201. {
  202. throw new NotSupportedException ();
  203. }
  204. } // DesignerOptionCollection
  205. private DesignerOptionCollection _options;
  206. protected internal DesignerOptionService ()
  207. {
  208. }
  209. protected DesignerOptionCollection CreateOptionCollection (DesignerOptionCollection parent, string name, Object value)
  210. {
  211. if (name == null)
  212. throw new ArgumentNullException ("name");
  213. if (parent == null)
  214. throw new ArgumentNullException ("parent");
  215. if (name == String.Empty)
  216. throw new ArgumentException ("name.Length == 0");
  217. return new DesignerOptionCollection (parent, name, value, this);
  218. }
  219. protected virtual bool ShowDialog (DesignerOptionCollection options, object optionObject)
  220. {
  221. return false;
  222. }
  223. protected virtual void PopulateOptionCollection (DesignerOptionCollection options)
  224. {
  225. }
  226. public DesignerOptionCollection Options {
  227. get {
  228. if (_options == null)
  229. _options = new DesignerOptionCollection (null, String.Empty, null, this);
  230. return _options;
  231. }
  232. }
  233. object IDesignerOptionService.GetOptionValue (string pageName, string valueName)
  234. {
  235. if (pageName == null)
  236. throw new ArgumentNullException ("pageName");
  237. if (valueName == null)
  238. throw new ArgumentNullException ("valueName");
  239. PropertyDescriptor property = GetOptionProperty (pageName, valueName);
  240. if (property != null)
  241. return property.GetValue (null);
  242. return null;
  243. }
  244. void IDesignerOptionService.SetOptionValue (string pageName, string valueName, object value)
  245. {
  246. if (pageName == null)
  247. throw new ArgumentNullException ("pageName");
  248. if (valueName == null)
  249. throw new ArgumentNullException ("valueName");
  250. PropertyDescriptor property = GetOptionProperty (pageName, valueName);
  251. if (property != null)
  252. property.SetValue (null, value);
  253. }
  254. // Go to the page and get the property associated with the option name.
  255. //
  256. private PropertyDescriptor GetOptionProperty (string pageName, string valueName)
  257. {
  258. string[] pages = pageName.Split (new char[] { '\\' });
  259. DesignerOptionCollection options = this.Options;
  260. foreach (string page in pages) {
  261. options = options[page];
  262. if (options == null)
  263. return null;
  264. }
  265. return options.Properties[valueName];
  266. }
  267. }
  268. }