ToolboxItem.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. using System.Collections;
  12. using System.ComponentModel;
  13. using System.ComponentModel.Design;
  14. using System.Drawing;
  15. using System.Reflection;
  16. using System.Runtime.Serialization;
  17. namespace System.Drawing.Design
  18. {
  19. [Serializable]
  20. public class ToolboxItem : ISerializable
  21. {
  22. private AssemblyName assembly;
  23. private Bitmap bitmap;
  24. private ICollection filter = new ToolboxItemFilterAttribute[0];
  25. private string displayname = string.Empty;
  26. private bool locked = false;
  27. private string name = string.Empty;
  28. public ToolboxItem() {
  29. }
  30. public ToolboxItem (Type toolType) {
  31. Initialize (toolType);
  32. }
  33. public AssemblyName AssemblyName {
  34. get {
  35. return assembly;
  36. }
  37. set {
  38. CheckUnlocked ();
  39. assembly = value;
  40. }
  41. }
  42. public Bitmap Bitmap {
  43. get {
  44. return bitmap;
  45. }
  46. set {
  47. CheckUnlocked ();
  48. bitmap = value;
  49. }
  50. }
  51. public string DisplayName {
  52. get {
  53. return displayname;
  54. }
  55. set {
  56. CheckUnlocked ();
  57. displayname = value;
  58. }
  59. }
  60. public ICollection Filter {
  61. get {
  62. return filter;
  63. }
  64. set {
  65. CheckUnlocked ();
  66. filter = value;
  67. }
  68. }
  69. protected bool Locked {
  70. get {
  71. return locked;
  72. }
  73. }
  74. public string TypeName {
  75. get {
  76. return name;
  77. }
  78. set {
  79. CheckUnlocked ();
  80. name = value;
  81. }
  82. }
  83. protected void CheckUnlocked ()
  84. {
  85. throw new InvalidOperationException ("The ToolboxItem is locked");
  86. }
  87. public IComponent[] CreateComponents ()
  88. {
  89. return CreateComponents (null);
  90. }
  91. public IComponent[] CreateComponents (IDesignerHost host)
  92. {
  93. OnComponentsCreating (new ToolboxComponentsCreatingEventArgs (host));
  94. IComponent[] Comp = CreateComponentsCore (host);
  95. OnComponentsCreated ( new ToolboxComponentsCreatedEventArgs (Comp));
  96. return Comp;
  97. }
  98. [MonoTODO]
  99. protected virtual IComponent[] CreateComponentsCore (IDesignerHost host)
  100. {
  101. throw new NotImplementedException ();
  102. }
  103. protected virtual void Deserialize (SerializationInfo info, StreamingContext context)
  104. {
  105. assembly = (AssemblyName)info.GetValue ("AssemblyName", typeof (AssemblyName));
  106. bitmap = (Bitmap)info.GetValue ("Bitmap", typeof (Bitmap));
  107. filter = (ICollection)info.GetValue ("Filter", typeof (ICollection));
  108. displayname = info.GetString ("DisplayName");
  109. locked = info.GetBoolean ("Locked");
  110. name = info.GetString ("TypeName");
  111. }
  112. public override bool Equals (object obj)
  113. {
  114. // FIXME: too harsh??
  115. if (!(obj is ToolboxItem))
  116. return false;
  117. if (obj == this)
  118. return true;
  119. return ((ToolboxItem) obj).AssemblyName.Equals (assembly) &&
  120. ((ToolboxItem) obj).Locked.Equals (locked) &&
  121. ((ToolboxItem) obj).TypeName.Equals (name) &&
  122. ((ToolboxItem) obj).DisplayName.Equals (displayname) &&
  123. ((ToolboxItem) obj).Bitmap.Equals (bitmap);
  124. }
  125. public override int GetHashCode ()
  126. {
  127. // FIXME: other algorithm?
  128. return string.Concat (name, displayname).GetHashCode ();
  129. }
  130. [MonoTODO]
  131. protected virtual Type GetType (IDesignerHost host, AssemblyName assemblyName, string typeName, bool reference)
  132. {
  133. throw new NotImplementedException ();
  134. }
  135. [MonoTODO]
  136. public virtual void Initialize (Type type)
  137. {
  138. // assembly = // FIXME we need to get the AssemblyName data from somewhere or create a new one
  139. displayname = type.Name;
  140. name = type.FullName;
  141. // seems to be a right place to create the bitmap
  142. bitmap = new Bitmap (16, 16); // FIXME set some default bitmap !?
  143. filter = type.GetCustomAttributes (typeof (ToolboxItemFilterAttribute), true);
  144. throw new NotImplementedException ();
  145. }
  146. void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
  147. {
  148. Serialize (info, context);
  149. }
  150. public void Lock ()
  151. {
  152. locked = true;
  153. }
  154. protected virtual void OnComponentsCreated (ToolboxComponentsCreatedEventArgs args)
  155. {
  156. if (ComponentsCreated != null)
  157. this.ComponentsCreated (this, args);
  158. }
  159. protected virtual void OnComponentsCreating (ToolboxComponentsCreatingEventArgs args)
  160. {
  161. if (ComponentsCreated != null)
  162. this.ComponentsCreating (this, args);
  163. }
  164. protected virtual void Serialize (SerializationInfo info, StreamingContext context)
  165. {
  166. info.AddValue ("AssemblyName", assembly);
  167. info.AddValue ("Bitmap", bitmap);
  168. info.AddValue ("Filter", filter);
  169. info.AddValue ("DisplayName", displayname);
  170. info.AddValue ("Locked", locked);
  171. info.AddValue ("TypeName", name);
  172. }
  173. public override string ToString()
  174. {
  175. return displayname;
  176. }
  177. public event ToolboxComponentsCreatedEventHandler ComponentsCreated;
  178. public event ToolboxComponentsCreatingEventHandler ComponentsCreating;
  179. }
  180. }