ToolboxItem.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. //
  2. // System.Drawing.Design.ToolboxItem.cs
  3. //
  4. // Authors:
  5. // Alejandro Sánchez Acosta
  6. // Andreas Nahr ([email protected])
  7. //
  8. // (C) Alejandro Sánchez Acosta
  9. // (C) 2003 Andreas Nahr
  10. //
  11. //
  12. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  13. //
  14. // Permission is hereby granted, free of charge, to any person obtaining
  15. // a copy of this software and associated documentation files (the
  16. // "Software"), to deal in the Software without restriction, including
  17. // without limitation the rights to use, copy, modify, merge, publish,
  18. // distribute, sublicense, and/or sell copies of the Software, and to
  19. // permit persons to whom the Software is furnished to do so, subject to
  20. // the following conditions:
  21. //
  22. // The above copyright notice and this permission notice shall be
  23. // included in all copies or substantial portions of the Software.
  24. //
  25. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  29. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  30. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  31. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  32. //
  33. using System.Collections;
  34. using System.ComponentModel;
  35. using System.ComponentModel.Design;
  36. using System.Drawing;
  37. using System.Reflection;
  38. using System.Runtime.Serialization;
  39. namespace System.Drawing.Design
  40. {
  41. [Serializable]
  42. public class ToolboxItem : ISerializable
  43. {
  44. private AssemblyName assembly;
  45. private Bitmap bitmap = null;
  46. private ICollection filter = new ToolboxItemFilterAttribute[0];
  47. private string displayname = string.Empty;
  48. private bool locked = false;
  49. private string name = string.Empty;
  50. public ToolboxItem() {
  51. }
  52. public ToolboxItem (Type toolType) {
  53. Initialize (toolType);
  54. }
  55. public AssemblyName AssemblyName {
  56. get {
  57. return assembly;
  58. }
  59. set {
  60. CheckUnlocked ();
  61. assembly = value;
  62. }
  63. }
  64. public Bitmap Bitmap {
  65. get {
  66. return bitmap;
  67. }
  68. set {
  69. CheckUnlocked ();
  70. bitmap = value;
  71. }
  72. }
  73. public string DisplayName {
  74. get {
  75. return displayname;
  76. }
  77. set {
  78. CheckUnlocked ();
  79. displayname = value;
  80. }
  81. }
  82. public ICollection Filter {
  83. get {
  84. return filter;
  85. }
  86. set {
  87. CheckUnlocked ();
  88. filter = value;
  89. }
  90. }
  91. protected bool Locked {
  92. get {
  93. return locked;
  94. }
  95. }
  96. public string TypeName {
  97. get {
  98. return name;
  99. }
  100. set {
  101. CheckUnlocked ();
  102. name = value;
  103. }
  104. }
  105. protected void CheckUnlocked ()
  106. {
  107. if (locked)
  108. throw new InvalidOperationException ("The ToolboxItem is locked");
  109. }
  110. public IComponent[] CreateComponents ()
  111. {
  112. return CreateComponents (null);
  113. }
  114. public IComponent[] CreateComponents (IDesignerHost host)
  115. {
  116. OnComponentsCreating (new ToolboxComponentsCreatingEventArgs (host));
  117. IComponent[] Comp = CreateComponentsCore (host);
  118. OnComponentsCreated ( new ToolboxComponentsCreatedEventArgs (Comp));
  119. return Comp;
  120. }
  121. [MonoTODO ("get error handling logic correct")]
  122. protected virtual IComponent[] CreateComponentsCore (IDesignerHost host)
  123. {
  124. if (host == null)
  125. throw new ArgumentNullException("host");
  126. OnComponentsCreating(new ToolboxComponentsCreatingEventArgs(host));
  127. IComponent[] components;
  128. Type type = GetType(host, assembly, name, true);
  129. if (type == null)
  130. components = new IComponent[] { };
  131. else
  132. components = new IComponent[] { host.CreateComponent(type) };
  133. OnComponentsCreated(new ToolboxComponentsCreatedEventArgs(components));
  134. return components;
  135. }
  136. protected virtual void Deserialize (SerializationInfo info, StreamingContext context)
  137. {
  138. assembly = (AssemblyName)info.GetValue ("AssemblyName", typeof (AssemblyName));
  139. bitmap = (Bitmap)info.GetValue ("Bitmap", typeof (Bitmap));
  140. filter = (ICollection)info.GetValue ("Filter", typeof (ICollection));
  141. displayname = info.GetString ("DisplayName");
  142. locked = info.GetBoolean ("Locked");
  143. name = info.GetString ("TypeName");
  144. }
  145. public override bool Equals (object obj)
  146. {
  147. // FIXME: too harsh??
  148. if (!(obj is ToolboxItem))
  149. return false;
  150. if (obj == this)
  151. return true;
  152. return ((ToolboxItem) obj).AssemblyName.Equals (assembly) &&
  153. ((ToolboxItem) obj).Locked.Equals (locked) &&
  154. ((ToolboxItem) obj).TypeName.Equals (name) &&
  155. ((ToolboxItem) obj).DisplayName.Equals (displayname) &&
  156. ((ToolboxItem) obj).Bitmap.Equals (bitmap);
  157. }
  158. public override int GetHashCode ()
  159. {
  160. // FIXME: other algorithm?
  161. return string.Concat (name, displayname).GetHashCode ();
  162. }
  163. [MonoTODO]
  164. protected virtual Type GetType (IDesignerHost host, AssemblyName assemblyName, string typeName, bool reference)
  165. {
  166. if (host == null)
  167. throw new ArgumentNullException("host");
  168. //get ITypeResolutionService from host, as we have no other IServiceProvider here
  169. ITypeResolutionService typeRes = host.GetService(typeof(ITypeResolutionService)) as ITypeResolutionService;
  170. if (typeRes == null)
  171. throw new Exception("Host does not provide an ITypeResolutionService");
  172. //TODO: Using Assembly loader to throw errors. Silent fail and return null?
  173. Assembly assembly = typeRes.GetAssembly(assemblyName, true);
  174. if (reference)
  175. typeRes.ReferenceAssembly(assemblyName);
  176. return typeRes.GetType(typeName, true);
  177. }
  178. [MonoTODO ("Should we be returning empty bitmap, or null?")]
  179. public virtual void Initialize (Type type)
  180. {
  181. assembly = type.Assembly.GetName();
  182. displayname = type.Name;
  183. name = type.FullName;
  184. // seems to be a right place to create the bitmap
  185. System.Drawing.Image image = null;
  186. foreach (object attribute in type.GetCustomAttributes(true)) {
  187. ToolboxBitmapAttribute tba = attribute as ToolboxBitmapAttribute;
  188. if (tba != null) {
  189. image = tba.GetImage (type);
  190. break;
  191. }
  192. }
  193. //fallback: check for image even if not attribute
  194. if (image == null)
  195. image = ToolboxBitmapAttribute.GetImageFromResource (type, null, false);
  196. if (image != null) {
  197. if (image is Bitmap)
  198. bitmap = (Bitmap) image;
  199. else
  200. bitmap = new Bitmap (image);
  201. }
  202. filter = type.GetCustomAttributes (typeof (ToolboxItemFilterAttribute), true);
  203. }
  204. void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
  205. {
  206. Serialize (info, context);
  207. }
  208. public void Lock ()
  209. {
  210. locked = true;
  211. }
  212. protected virtual void OnComponentsCreated (ToolboxComponentsCreatedEventArgs args)
  213. {
  214. if (ComponentsCreated != null)
  215. this.ComponentsCreated (this, args);
  216. }
  217. protected virtual void OnComponentsCreating (ToolboxComponentsCreatingEventArgs args)
  218. {
  219. if (ComponentsCreated != null)
  220. this.ComponentsCreating (this, args);
  221. }
  222. protected virtual void Serialize (SerializationInfo info, StreamingContext context)
  223. {
  224. info.AddValue ("AssemblyName", assembly);
  225. info.AddValue ("Bitmap", bitmap);
  226. info.AddValue ("Filter", filter);
  227. info.AddValue ("DisplayName", displayname);
  228. info.AddValue ("Locked", locked);
  229. info.AddValue ("TypeName", name);
  230. }
  231. public override string ToString()
  232. {
  233. return displayname;
  234. }
  235. public event ToolboxComponentsCreatedEventHandler ComponentsCreated;
  236. public event ToolboxComponentsCreatingEventHandler ComponentsCreating;
  237. }
  238. }