ToolboxItem.cs 9.3 KB

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