In System.Drawing:
[mono.git] / mcs / class / System.Drawing / System.Drawing.Design / ToolboxItem.cs
1 //
2 // System.Drawing.Design.ToolboxItem.cs
3 //
4 // Authors:
5 //   Alejandro Sánchez Acosta
6 //   Andreas Nahr (ClassDevelopment@A-SoftTech.com)
7 //
8 // (C) Alejandro Sánchez Acosta
9 // (C) 2003 Andreas Nahr
10 //
11
12 //
13 // Copyright (C) 2004 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
35 using System.Collections;
36 using System.ComponentModel;
37 using System.ComponentModel.Design;
38 using System.Drawing;
39 using System.Reflection;
40 using System.Runtime.Serialization;
41
42 namespace System.Drawing.Design 
43 {
44         [Serializable]
45         public class ToolboxItem : ISerializable
46         {
47
48                 private AssemblyName assembly;
49                 private Bitmap bitmap = null;
50                 private ICollection filter = new ToolboxItemFilterAttribute[0];
51                 private string displayname = string.Empty;
52                 private bool locked = false;
53                 private string name = string.Empty;
54                 
55                 public ToolboxItem() {
56                 }
57
58                 public ToolboxItem (Type toolType) {
59                         Initialize (toolType);
60                 }
61
62                 public AssemblyName AssemblyName {
63                         get {
64                                 return assembly;
65                         }
66
67                         set {
68                                 CheckUnlocked ();
69                                 assembly = value;
70                         }
71                 }
72
73                 public Bitmap Bitmap {
74                         get {
75                                 return bitmap;
76                         }
77                         
78                         set {
79                                 CheckUnlocked ();
80                                 bitmap = value;
81                         }
82                 }
83
84                 public string DisplayName {
85                         get {
86                                 return displayname;
87                         }
88                         
89                         set {
90                                 CheckUnlocked ();
91                                 displayname = value;
92                         }
93                 }
94
95                 public ICollection Filter {
96                         get {
97                                 return filter;
98                         }
99                         
100                         set {
101                                 CheckUnlocked ();
102                                 filter = value;
103                         }
104                 }
105                 
106                 protected bool Locked {
107                         get {
108                                 return locked;
109                         }
110                 }
111
112                 public string TypeName {
113                         get {
114                                 return name;
115                         }
116
117                         set {
118                                 CheckUnlocked ();
119                                 name = value;
120                         }
121                 }
122                 
123                 protected void CheckUnlocked ()
124                 {
125                         if (locked)
126                                 throw new InvalidOperationException ("The ToolboxItem is locked");
127                 }
128
129                 public IComponent[] CreateComponents () 
130                 {
131                         return CreateComponents (null);
132                 }
133
134                 public IComponent[] CreateComponents (IDesignerHost host)
135                 {
136                         OnComponentsCreating (new ToolboxComponentsCreatingEventArgs (host));
137                         IComponent[] Comp = CreateComponentsCore (host);
138                         OnComponentsCreated ( new ToolboxComponentsCreatedEventArgs (Comp));
139                         return Comp;
140                 }
141
142                 [MonoTODO ("get error handling logic correct")] 
143                 protected virtual IComponent[] CreateComponentsCore (IDesignerHost host)
144                 {\r
145                         if (host == null)\r
146                                 throw new ArgumentNullException("host");
147
148                         OnComponentsCreating(new ToolboxComponentsCreatingEventArgs(host));
149                         \r
150                         IComponent[] components;\r
151                         Type type = GetType(host, assembly, name, true);
152                         if (type == null)\r
153                                 components = new IComponent[] { };
154                         else\r
155                                 components = new IComponent[] { host.CreateComponent(type) };
156
157                         OnComponentsCreated(new ToolboxComponentsCreatedEventArgs(components));\r
158                         return components;
159                 }
160
161                 protected virtual void Deserialize (SerializationInfo info, StreamingContext context)
162                 {
163                         assembly = (AssemblyName)info.GetValue ("AssemblyName", typeof (AssemblyName));
164                         bitmap = (Bitmap)info.GetValue ("Bitmap", typeof (Bitmap));
165                         filter = (ICollection)info.GetValue ("Filter", typeof (ICollection));
166                         displayname = info.GetString ("DisplayName");
167                         locked = info.GetBoolean ("Locked");
168                         name = info.GetString ("TypeName");
169                 }
170
171                 public override bool Equals (object obj)
172                 {
173                         // FIXME: too harsh??
174                         if (!(obj is ToolboxItem))
175                                 return false;
176                         if (obj == this)
177                                 return true;
178                         return ((ToolboxItem) obj).AssemblyName.Equals (assembly) &&
179                                 ((ToolboxItem) obj).Locked.Equals (locked) &&
180                                 ((ToolboxItem) obj).TypeName.Equals (name) &&
181                                 ((ToolboxItem) obj).DisplayName.Equals (displayname) &&
182                                 ((ToolboxItem) obj).Bitmap.Equals (bitmap);
183                 }
184                 
185                 public override int GetHashCode ()
186                 {
187                         // FIXME: other algorithm?
188                         return string.Concat (name, displayname).GetHashCode ();
189                 }
190
191                 [MonoTODO]
192                 protected virtual Type GetType (IDesignerHost host, AssemblyName assemblyName, string typeName, bool reference)
193                 {\r
194                         if (host == null)\r
195                                 throw new ArgumentNullException("host");
196
197                         //get ITypeResolutionService from host, as we have no other IServiceProvider here
198                         ITypeResolutionService typeRes = host.GetService(typeof(ITypeResolutionService)) as ITypeResolutionService;
199                         if (typeRes == null)
200                                 throw new Exception("Host does not provide an ITypeResolutionService");
201
202                         //TODO: Using Assembly loader to throw errors. Silent fail and return null?
203                         Assembly assembly = typeRes.GetAssembly(assemblyName, true);\r
204                         if (reference)\r
205                                 typeRes.ReferenceAssembly(assemblyName);\r
206                         return typeRes.GetType(typeName, true);
207                 }
208
209                 [MonoTODO ("Should we be returning empty bitmap, or null?")]
210                 public virtual void Initialize (Type type) 
211                 {
212                         assembly = type.Assembly.GetName();
213                         displayname = type.Name;
214                         name = type.FullName;
215                         
216                         // seems to be a right place to create the bitmap
217                         System.Drawing.Image image = null;
218                         foreach (object attribute in type.GetCustomAttributes(true)) {
219                                 ToolboxBitmapAttribute tba = attribute as ToolboxBitmapAttribute;
220                                 if (tba != null) {
221                                         image = tba.GetImage (type);
222                                         break;
223                                 }
224                         }
225                         //fallback: check for image even if not attribute
226                         if (image == null)
227                                 image = ToolboxBitmapAttribute.GetImageFromResource (type, null, false);
228                         
229                         if (image != null) {
230                                 if (image is Bitmap)
231                                         bitmap = (Bitmap) image;
232                                 else
233                                         bitmap = new Bitmap (image);
234                         }
235
236                         filter = type.GetCustomAttributes (typeof (ToolboxItemFilterAttribute), true);
237                 }
238                         
239                 void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
240                 {
241                         Serialize (info, context);
242                 }
243
244                 public void Lock ()
245                 {
246                         locked = true;
247                 }
248
249                 protected virtual void OnComponentsCreated (ToolboxComponentsCreatedEventArgs args)
250                 {
251                         if (ComponentsCreated != null)
252                                 this.ComponentsCreated (this, args);
253                 }
254
255                 protected virtual void OnComponentsCreating (ToolboxComponentsCreatingEventArgs args)
256                 {
257                         if (ComponentsCreated != null)
258                                 this.ComponentsCreating (this, args);
259                 }
260
261                 protected virtual void Serialize (SerializationInfo info, StreamingContext context)
262                 {
263                         info.AddValue ("AssemblyName", assembly);
264                         info.AddValue ("Bitmap", bitmap);
265                         info.AddValue ("Filter", filter);
266                         info.AddValue ("DisplayName", displayname);
267                         info.AddValue ("Locked", locked);
268                         info.AddValue ("TypeName", name);
269                 }
270
271                 public override string ToString()
272                 {
273                         return displayname;
274                 }
275
276                 public event ToolboxComponentsCreatedEventHandler ComponentsCreated;
277
278                 public event ToolboxComponentsCreatingEventHandler ComponentsCreating;
279         }
280 }