2 // System.Drawing.Design.ToolboxItem.cs
5 // Alejandro Sánchez Acosta
6 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
8 // (C) Alejandro Sánchez Acosta
9 // (C) 2003 Andreas Nahr
13 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
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.
35 using System.Collections;
36 using System.ComponentModel;
37 using System.ComponentModel.Design;
39 using System.Reflection;
40 using System.Runtime.Serialization;
42 namespace System.Drawing.Design
45 public class ToolboxItem : ISerializable
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;
55 public ToolboxItem() {
58 public ToolboxItem (Type toolType) {
59 Initialize (toolType);
62 public AssemblyName AssemblyName {
73 public Bitmap Bitmap {
84 public string DisplayName {
95 public ICollection Filter {
106 protected bool Locked {
112 public string TypeName {
123 protected void CheckUnlocked ()
126 throw new InvalidOperationException ("The ToolboxItem is locked");
129 public IComponent[] CreateComponents ()
131 return CreateComponents (null);
134 public IComponent[] CreateComponents (IDesignerHost host)
136 OnComponentsCreating (new ToolboxComponentsCreatingEventArgs (host));
137 IComponent[] Comp = CreateComponentsCore (host);
138 OnComponentsCreated ( new ToolboxComponentsCreatedEventArgs (Comp));
142 [MonoTODO ("get error handling logic correct")]
143 protected virtual IComponent[] CreateComponentsCore (IDesignerHost host)
146 throw new ArgumentNullException("host");
148 OnComponentsCreating(new ToolboxComponentsCreatingEventArgs(host));
150 IComponent[] components;
\r
151 Type type = GetType(host, assembly, name, true);
153 components = new IComponent[] { };
155 components = new IComponent[] { host.CreateComponent(type) };
157 OnComponentsCreated(new ToolboxComponentsCreatedEventArgs(components));
\r
161 protected virtual void Deserialize (SerializationInfo info, StreamingContext context)
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");
171 public override bool Equals (object obj)
173 // FIXME: too harsh??
174 if (!(obj is ToolboxItem))
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);
185 public override int GetHashCode ()
187 // FIXME: other algorithm?
188 return string.Concat (name, displayname).GetHashCode ();
192 protected virtual Type GetType (IDesignerHost host, AssemblyName assemblyName, string typeName, bool reference)
195 throw new ArgumentNullException("host");
197 //get ITypeResolutionService from host, as we have no other IServiceProvider here
198 ITypeResolutionService typeRes = host.GetService(typeof(ITypeResolutionService)) as ITypeResolutionService;
200 throw new Exception("Host does not provide an ITypeResolutionService");
202 //TODO: Using Assembly loader to throw errors. Silent fail and return null?
203 Assembly assembly = typeRes.GetAssembly(assemblyName, true);
\r
205 typeRes.ReferenceAssembly(assemblyName);
\r
206 return typeRes.GetType(typeName, true);
209 [MonoTODO ("Should we be returning empty bitmap, or null?")]
210 public virtual void Initialize (Type type)
212 assembly = type.Assembly.GetName();
213 displayname = type.Name;
214 name = type.FullName;
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;
221 image = tba.GetImage (type);
225 //fallback: check for image even if not attribute
227 image = ToolboxBitmapAttribute.GetImageFromResource (type, null, false);
231 bitmap = (Bitmap) image;
233 bitmap = new Bitmap (image);
236 filter = type.GetCustomAttributes (typeof (ToolboxItemFilterAttribute), true);
239 void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
241 Serialize (info, context);
249 protected virtual void OnComponentsCreated (ToolboxComponentsCreatedEventArgs args)
251 if (ComponentsCreated != null)
252 this.ComponentsCreated (this, args);
255 protected virtual void OnComponentsCreating (ToolboxComponentsCreatingEventArgs args)
257 if (ComponentsCreated != null)
258 this.ComponentsCreating (this, args);
261 protected virtual void Serialize (SerializationInfo info, StreamingContext context)
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);
271 public override string ToString()
276 public event ToolboxComponentsCreatedEventHandler ComponentsCreated;
278 public event ToolboxComponentsCreatingEventHandler ComponentsCreating;