ToolboxItem.cs 9.5 KB

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