ToolboxItem.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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. // Sebastien Pouliot <[email protected]>
  9. //
  10. // (C) Alejandro Sánchez Acosta
  11. // (C) 2003 Andreas Nahr
  12. // Copyright (C) 2004-2006 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.Reflection;
  37. using System.Runtime.Serialization;
  38. using System.Security.Permissions;
  39. namespace System.Drawing.Design
  40. {
  41. [Serializable]
  42. [PermissionSet (SecurityAction.LinkDemand, Unrestricted = true)]
  43. [PermissionSet (SecurityAction.InheritanceDemand, Unrestricted = true)]
  44. [MonoTODO ("Implementation is incomplete.")]
  45. public class ToolboxItem : ISerializable {
  46. private bool locked = false;
  47. private Hashtable properties = new Hashtable ();
  48. public ToolboxItem ()
  49. {
  50. }
  51. public ToolboxItem (Type toolType)
  52. {
  53. Initialize (toolType);
  54. }
  55. public AssemblyName AssemblyName {
  56. get { return (AssemblyName) properties["AssemblyName"]; }
  57. set { SetValue ("AssemblyName", value); }
  58. }
  59. public Bitmap Bitmap {
  60. get { return (Bitmap) properties["Bitmap"]; }
  61. set { SetValue ("Bitmap", value); }
  62. }
  63. public string DisplayName {
  64. get { return GetValue ("DisplayName"); }
  65. set { SetValue ("DisplayName", value); }
  66. }
  67. public ICollection Filter {
  68. get {
  69. ICollection filter = (ICollection) properties["Filter"];
  70. if (filter == null)
  71. filter = new ToolboxItemFilterAttribute[0];
  72. return filter;
  73. }
  74. set { SetValue ("Filter", value); }
  75. }
  76. #if NET_2_0
  77. public virtual bool Locked {
  78. #else
  79. protected bool Locked {
  80. #endif
  81. get { return locked; }
  82. }
  83. public string TypeName {
  84. get { return GetValue ("TypeName"); }
  85. set { SetValue ("TypeName", value); }
  86. }
  87. #if NET_2_0
  88. public string Company {
  89. get { return (string) properties["Company"]; }
  90. set { SetValue ("Company", value); }
  91. }
  92. public virtual string ComponentType {
  93. get { return ".NET Component"; }
  94. }
  95. public AssemblyName[] DependentAssemblies {
  96. get { return (AssemblyName[]) properties["DependentAssemblies"]; }
  97. set {
  98. AssemblyName[] names = new AssemblyName [value.Length];
  99. for (int i=0; i < names.Length; i++)
  100. names [i] = value [i];
  101. SetValue ("DependentAssemblies", names);
  102. }
  103. }
  104. public string Description {
  105. get { return (string) properties["Description"]; }
  106. set { SetValue ("Description", value); }
  107. }
  108. public bool IsTransient {
  109. get {
  110. object o = properties ["IsTransient"];
  111. return (o == null) ? false : (bool) o;
  112. }
  113. set { SetValue ("IsTransient", value); }
  114. }
  115. public IDictionary Properties {
  116. get { return properties; }
  117. }
  118. public virtual string Version {
  119. get { return string.Empty; }
  120. }
  121. #endif
  122. protected void CheckUnlocked ()
  123. {
  124. if (locked)
  125. throw new InvalidOperationException ("The ToolboxItem is locked");
  126. }
  127. public IComponent[] CreateComponents ()
  128. {
  129. return CreateComponents (null);
  130. }
  131. public IComponent[] CreateComponents (IDesignerHost host)
  132. {
  133. OnComponentsCreating (new ToolboxComponentsCreatingEventArgs (host));
  134. IComponent[] Comp = CreateComponentsCore (host);
  135. OnComponentsCreated (new ToolboxComponentsCreatedEventArgs (Comp));
  136. return Comp;
  137. }
  138. // FIXME - get error handling logic correct
  139. protected virtual IComponent[] CreateComponentsCore (IDesignerHost host)
  140. {
  141. if (host == null)
  142. throw new ArgumentNullException("host");
  143. IComponent[] components;
  144. Type type = GetType(host, AssemblyName, TypeName, true);
  145. if (type == null)
  146. components = new IComponent[] { };
  147. else
  148. components = new IComponent[] { host.CreateComponent(type) };
  149. return components;
  150. }
  151. #if NET_2_0
  152. protected virtual IComponent[] CreateComponentsCore (IDesignerHost host, IDictionary defaultValues)
  153. {
  154. IComponent[] components = CreateComponentsCore (host);
  155. foreach (Component c in components) {
  156. IComponentInitializer initializer = host.GetDesigner (c) as IComponentInitializer;
  157. initializer.InitializeNewComponent (defaultValues);
  158. }
  159. return components;
  160. }
  161. public IComponent[] CreateComponents (IDesignerHost host, IDictionary defaultValues)
  162. {
  163. OnComponentsCreating (new ToolboxComponentsCreatingEventArgs (host));
  164. IComponent[] components = CreateComponentsCore (host, defaultValues);
  165. OnComponentsCreated (new ToolboxComponentsCreatedEventArgs (components));
  166. return components;
  167. }
  168. protected virtual object FilterPropertyValue (string propertyName, object value)
  169. {
  170. switch (propertyName) {
  171. case "AssemblyName":
  172. return (value == null) ? null : (value as ICloneable).Clone ();
  173. case "DisplayName":
  174. case "TypeName":
  175. return (value == null) ? String.Empty : value;
  176. case "Filter":
  177. return (value == null) ? new ToolboxItemFilterAttribute [0] : value;
  178. default:
  179. return value;
  180. }
  181. }
  182. #endif
  183. protected virtual void Deserialize (SerializationInfo info, StreamingContext context)
  184. {
  185. AssemblyName = (AssemblyName)info.GetValue ("AssemblyName", typeof (AssemblyName));
  186. Bitmap = (Bitmap)info.GetValue ("Bitmap", typeof (Bitmap));
  187. Filter = (ICollection)info.GetValue ("Filter", typeof (ICollection));
  188. DisplayName = info.GetString ("DisplayName");
  189. locked = info.GetBoolean ("Locked");
  190. TypeName = info.GetString ("TypeName");
  191. }
  192. // FIXME: too harsh??
  193. public override bool Equals (object obj)
  194. {
  195. ToolboxItem ti = (obj as ToolboxItem);
  196. if (ti == null)
  197. return false;
  198. if (obj == this)
  199. return true;
  200. return (ti.AssemblyName.Equals (AssemblyName) &&
  201. ti.Locked.Equals (locked) &&
  202. ti.TypeName.Equals (TypeName) &&
  203. ti.DisplayName.Equals (DisplayName) &&
  204. ti.Bitmap.Equals (Bitmap));
  205. }
  206. public override int GetHashCode ()
  207. {
  208. // FIXME: other algorithm?
  209. return string.Concat (TypeName, DisplayName).GetHashCode ();
  210. }
  211. #if NET_2_0
  212. public Type GetType (IDesignerHost host)
  213. {
  214. return GetType (host, this.AssemblyName, this.TypeName, false);
  215. }
  216. #endif
  217. protected virtual Type GetType (IDesignerHost host, AssemblyName assemblyName, string typeName, bool reference)
  218. {
  219. if (typeName == null)
  220. throw new ArgumentNullException ("typeName");
  221. if (host == null)
  222. return null;
  223. //get ITypeResolutionService from host, as we have no other IServiceProvider here
  224. ITypeResolutionService typeRes = host.GetService (typeof (ITypeResolutionService)) as ITypeResolutionService;
  225. Type type = null;
  226. if (typeRes != null) {
  227. //TODO: Using Assembly loader to throw errors. Silent fail and return null?
  228. typeRes.GetAssembly (assemblyName, true);
  229. if (reference)
  230. typeRes.ReferenceAssembly (assemblyName);
  231. type = typeRes.GetType (typeName, true);
  232. } else {
  233. Assembly assembly = Assembly.Load (assemblyName);
  234. if (assembly != null)
  235. type = assembly.GetType (typeName);
  236. }
  237. return type;
  238. }
  239. // FIXME - Should we be returning empty bitmap, or null?
  240. public virtual void Initialize (Type type)
  241. {
  242. CheckUnlocked ();
  243. if (type == null)
  244. return;
  245. AssemblyName = type.Assembly.GetName();
  246. DisplayName = type.Name;
  247. TypeName = type.FullName;
  248. // seems to be a right place to create the bitmap
  249. System.Drawing.Image image = null;
  250. foreach (object attribute in type.GetCustomAttributes(true)) {
  251. ToolboxBitmapAttribute tba = attribute as ToolboxBitmapAttribute;
  252. if (tba != null) {
  253. image = tba.GetImage (type);
  254. break;
  255. }
  256. }
  257. //fallback: check for image even if not attribute
  258. if (image == null)
  259. image = ToolboxBitmapAttribute.GetImageFromResource (type, null, false);
  260. if (image != null) {
  261. if (image is Bitmap)
  262. Bitmap = (Bitmap) image;
  263. else
  264. Bitmap = new Bitmap (image);
  265. }
  266. Filter = type.GetCustomAttributes (typeof (ToolboxItemFilterAttribute), true);
  267. }
  268. void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
  269. {
  270. Serialize (info, context);
  271. }
  272. #if NET_2_0
  273. public virtual void Lock ()
  274. #else
  275. public void Lock ()
  276. #endif
  277. {
  278. locked = true;
  279. }
  280. protected virtual void OnComponentsCreated (ToolboxComponentsCreatedEventArgs args)
  281. {
  282. if (ComponentsCreated != null)
  283. this.ComponentsCreated (this, args);
  284. }
  285. protected virtual void OnComponentsCreating (ToolboxComponentsCreatingEventArgs args)
  286. {
  287. if (ComponentsCreating != null)
  288. this.ComponentsCreating (this, args);
  289. }
  290. protected virtual void Serialize (SerializationInfo info, StreamingContext context)
  291. {
  292. info.AddValue ("AssemblyName", AssemblyName);
  293. info.AddValue ("Bitmap", Bitmap);
  294. info.AddValue ("Filter", Filter);
  295. info.AddValue ("DisplayName", DisplayName);
  296. info.AddValue ("Locked", locked);
  297. info.AddValue ("TypeName", TypeName);
  298. }
  299. public override string ToString()
  300. {
  301. return DisplayName;
  302. }
  303. #if NET_2_0
  304. protected void ValidatePropertyType (string propertyName, object value, Type expectedType, bool allowNull)
  305. {
  306. if (!allowNull && (value == null))
  307. throw new ArgumentNullException ("value");
  308. if ((value != null) && !expectedType.Equals (value.GetType ())) {
  309. string msg = Locale.GetText ("Type mismatch between value ({0}) and expected type ({1}).",
  310. value.GetType (), expectedType);
  311. throw new ArgumentException (msg, "value");
  312. }
  313. }
  314. protected virtual object ValidatePropertyValue (string propertyName, object value)
  315. {
  316. switch (propertyName) {
  317. case "AssemblyName":
  318. ValidatePropertyType (propertyName, value, typeof (AssemblyName), true);
  319. break;
  320. case "Bitmap":
  321. ValidatePropertyType (propertyName, value, typeof (Bitmap), true);
  322. break;
  323. case "Company":
  324. case "Description":
  325. case "DisplayName":
  326. case "TypeName":
  327. ValidatePropertyType (propertyName, value, typeof (string), true);
  328. if (value == null)
  329. value = String.Empty;
  330. break;
  331. case "IsTransient":
  332. ValidatePropertyType (propertyName, value, typeof (bool), false);
  333. break;
  334. case "Filter":
  335. ValidatePropertyType (propertyName, value, typeof (ToolboxItemFilterAttribute[]), true);
  336. if (value == null)
  337. value = new ToolboxItemFilterAttribute [0];
  338. break;
  339. case "DependentAssemblies":
  340. ValidatePropertyType (propertyName, value, typeof (AssemblyName[]), true);
  341. break;
  342. default:
  343. break;
  344. }
  345. return value;
  346. }
  347. private void SetValue (string propertyName, object value)
  348. {
  349. CheckUnlocked ();
  350. properties [propertyName] = ValidatePropertyValue (propertyName, value);
  351. }
  352. #else
  353. private void SetValue (string propertyName, object value)
  354. {
  355. CheckUnlocked ();
  356. properties [propertyName] = value;
  357. }
  358. #endif
  359. private string GetValue (string propertyName)
  360. {
  361. string s = (string) properties [propertyName];
  362. return (s == null) ? String.Empty : s;
  363. }
  364. public event ToolboxComponentsCreatedEventHandler ComponentsCreated;
  365. public event ToolboxComponentsCreatingEventHandler ComponentsCreating;
  366. }
  367. }