ToolboxItem.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. OnComponentsCreating(new ToolboxComponentsCreatingEventArgs(host));
  144. IComponent[] components;
  145. Type type = GetType(host, AssemblyName, TypeName, true);
  146. if (type == null)
  147. components = new IComponent[] { };
  148. else
  149. components = new IComponent[] { host.CreateComponent(type) };
  150. OnComponentsCreated(new ToolboxComponentsCreatedEventArgs(components));
  151. return components;
  152. }
  153. #if NET_2_0
  154. [MonoTODO]
  155. protected virtual IComponent[] CreateComponentsCore (IDesignerHost host, IDictionary defaultValues)
  156. {
  157. throw new NotImplementedException ();
  158. }
  159. [MonoTODO]
  160. public IComponent[] CreateComponents (IDesignerHost host, IDictionary defaultValues)
  161. {
  162. throw new NotImplementedException ();
  163. }
  164. [MonoTODO]
  165. public Type GetType (IDesignerHost host)
  166. {
  167. if (host == null)
  168. return null;
  169. throw new NotImplementedException ();
  170. }
  171. protected virtual object FilterPropertyValue (string propertyName, object value)
  172. {
  173. switch (propertyName) {
  174. case "AssemblyName":
  175. return (value == null) ? null : (value as ICloneable).Clone ();
  176. case "DisplayName":
  177. case "TypeName":
  178. return (value == null) ? String.Empty : value;
  179. case "Filter":
  180. return (value == null) ? new ToolboxItemFilterAttribute [0] : value;
  181. default:
  182. return value;
  183. }
  184. }
  185. #endif
  186. protected virtual void Deserialize (SerializationInfo info, StreamingContext context)
  187. {
  188. AssemblyName = (AssemblyName)info.GetValue ("AssemblyName", typeof (AssemblyName));
  189. Bitmap = (Bitmap)info.GetValue ("Bitmap", typeof (Bitmap));
  190. Filter = (ICollection)info.GetValue ("Filter", typeof (ICollection));
  191. DisplayName = info.GetString ("DisplayName");
  192. locked = info.GetBoolean ("Locked");
  193. TypeName = info.GetString ("TypeName");
  194. }
  195. // FIXME: too harsh??
  196. public override bool Equals (object obj)
  197. {
  198. ToolboxItem ti = (obj as ToolboxItem);
  199. if (ti == null)
  200. return false;
  201. if (obj == this)
  202. return true;
  203. return (ti.AssemblyName.Equals (AssemblyName) &&
  204. ti.Locked.Equals (locked) &&
  205. ti.TypeName.Equals (TypeName) &&
  206. ti.DisplayName.Equals (DisplayName) &&
  207. ti.Bitmap.Equals (Bitmap));
  208. }
  209. public override int GetHashCode ()
  210. {
  211. // FIXME: other algorithm?
  212. return string.Concat (TypeName, DisplayName).GetHashCode ();
  213. }
  214. protected virtual Type GetType (IDesignerHost host, AssemblyName assemblyName, string typeName, bool reference)
  215. {
  216. if (typeName == null)
  217. throw new ArgumentNullException ("typeName");
  218. if (host == null)
  219. return null;
  220. //get ITypeResolutionService from host, as we have no other IServiceProvider here
  221. ITypeResolutionService typeRes = host.GetService (typeof (ITypeResolutionService)) as ITypeResolutionService;
  222. Type type = null;
  223. if (typeRes != null) {
  224. //TODO: Using Assembly loader to throw errors. Silent fail and return null?
  225. typeRes.GetAssembly (assemblyName, true);
  226. if (reference)
  227. typeRes.ReferenceAssembly (assemblyName);
  228. type = typeRes.GetType (typeName, true);
  229. } else {
  230. Assembly assembly = Assembly.Load (assemblyName);
  231. if (assembly != null)
  232. type = assembly.GetType (typeName);
  233. }
  234. return type;
  235. }
  236. // FIXME - Should we be returning empty bitmap, or null?
  237. public virtual void Initialize (Type type)
  238. {
  239. CheckUnlocked ();
  240. if (type == null)
  241. return;
  242. AssemblyName = type.Assembly.GetName();
  243. DisplayName = type.Name;
  244. TypeName = type.FullName;
  245. // seems to be a right place to create the bitmap
  246. System.Drawing.Image image = null;
  247. foreach (object attribute in type.GetCustomAttributes(true)) {
  248. ToolboxBitmapAttribute tba = attribute as ToolboxBitmapAttribute;
  249. if (tba != null) {
  250. image = tba.GetImage (type);
  251. break;
  252. }
  253. }
  254. //fallback: check for image even if not attribute
  255. if (image == null)
  256. image = ToolboxBitmapAttribute.GetImageFromResource (type, null, false);
  257. if (image != null) {
  258. if (image is Bitmap)
  259. Bitmap = (Bitmap) image;
  260. else
  261. Bitmap = new Bitmap (image);
  262. }
  263. Filter = type.GetCustomAttributes (typeof (ToolboxItemFilterAttribute), true);
  264. }
  265. void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
  266. {
  267. Serialize (info, context);
  268. }
  269. #if NET_2_0
  270. public virtual void Lock ()
  271. #else
  272. public void Lock ()
  273. #endif
  274. {
  275. locked = true;
  276. }
  277. protected virtual void OnComponentsCreated (ToolboxComponentsCreatedEventArgs args)
  278. {
  279. if (ComponentsCreated != null)
  280. this.ComponentsCreated (this, args);
  281. }
  282. protected virtual void OnComponentsCreating (ToolboxComponentsCreatingEventArgs args)
  283. {
  284. if (ComponentsCreated != null)
  285. this.ComponentsCreating (this, args);
  286. }
  287. protected virtual void Serialize (SerializationInfo info, StreamingContext context)
  288. {
  289. info.AddValue ("AssemblyName", AssemblyName);
  290. info.AddValue ("Bitmap", Bitmap);
  291. info.AddValue ("Filter", Filter);
  292. info.AddValue ("DisplayName", DisplayName);
  293. info.AddValue ("Locked", locked);
  294. info.AddValue ("TypeName", TypeName);
  295. }
  296. public override string ToString()
  297. {
  298. return DisplayName;
  299. }
  300. #if NET_2_0
  301. protected void ValidatePropertyType (string propertyName, object value, Type expectedType, bool allowNull)
  302. {
  303. if (!allowNull && (value == null))
  304. throw new ArgumentNullException ("value");
  305. if ((value != null) && !expectedType.Equals (value.GetType ())) {
  306. string msg = Locale.GetText ("Type mismatch between value ({0}) and expected type ({1}).",
  307. value.GetType (), expectedType);
  308. throw new ArgumentException (msg, "value");
  309. }
  310. }
  311. protected virtual object ValidatePropertyValue (string propertyName, object value)
  312. {
  313. switch (propertyName) {
  314. case "AssemblyName":
  315. ValidatePropertyType (propertyName, value, typeof (AssemblyName), true);
  316. break;
  317. case "Bitmap":
  318. ValidatePropertyType (propertyName, value, typeof (Bitmap), true);
  319. break;
  320. case "Company":
  321. case "Description":
  322. case "DisplayName":
  323. case "TypeName":
  324. ValidatePropertyType (propertyName, value, typeof (string), true);
  325. if (value == null)
  326. value = String.Empty;
  327. break;
  328. case "IsTransient":
  329. ValidatePropertyType (propertyName, value, typeof (bool), false);
  330. break;
  331. case "Filter":
  332. ValidatePropertyType (propertyName, value, typeof (ToolboxItemFilterAttribute[]), true);
  333. if (value == null)
  334. value = new ToolboxItemFilterAttribute [0];
  335. break;
  336. case "DependentAssemblies":
  337. ValidatePropertyType (propertyName, value, typeof (AssemblyName[]), true);
  338. break;
  339. default:
  340. break;
  341. }
  342. return value;
  343. }
  344. private void SetValue (string propertyName, object value)
  345. {
  346. CheckUnlocked ();
  347. properties [propertyName] = ValidatePropertyValue (propertyName, value);
  348. }
  349. #else
  350. private void SetValue (string propertyName, object value)
  351. {
  352. CheckUnlocked ();
  353. properties [propertyName] = value;
  354. }
  355. #endif
  356. private string GetValue (string propertyName)
  357. {
  358. string s = (string) properties [propertyName];
  359. return (s == null) ? String.Empty : s;
  360. }
  361. public event ToolboxComponentsCreatedEventHandler ComponentsCreated;
  362. public event ToolboxComponentsCreatingEventHandler ComponentsCreating;
  363. }
  364. }