2 // System.Drawing.Design.ToolboxItem.cs
5 // Alejandro Sánchez Acosta
6 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
7 // Jordi Mas i Hernandez, jordimash@gmail.com
9 // (C) Alejandro Sánchez Acosta
10 // (C) 2003 Andreas Nahr
14 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
16 // Permission is hereby granted, free of charge, to any person obtaining
17 // a copy of this software and associated documentation files (the
18 // "Software"), to deal in the Software without restriction, including
19 // without limitation the rights to use, copy, modify, merge, publish,
20 // distribute, sublicense, and/or sell copies of the Software, and to
21 // permit persons to whom the Software is furnished to do so, subject to
22 // the following conditions:
24 // The above copyright notice and this permission notice shall be
25 // included in all copies or substantial portions of the Software.
27 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
31 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
32 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
33 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 using System.Collections;
37 using System.ComponentModel;
38 using System.ComponentModel.Design;
39 using System.Reflection;
40 using System.Runtime.Serialization;
41 using System.Security.Permissions;
43 namespace System.Drawing.Design
46 [PermissionSet (SecurityAction.LinkDemand, Unrestricted = true)]
47 [PermissionSet (SecurityAction.InheritanceDemand, Unrestricted = true)]
48 public class ToolboxItem : ISerializable
50 private bool locked = false;
51 private ICollection filter = new ToolboxItemFilterAttribute[0];
52 private Hashtable properties = new Hashtable ();
54 public ToolboxItem() {
57 public ToolboxItem (Type toolType) {
58 Initialize (toolType);
61 public AssemblyName AssemblyName {
63 return (AssemblyName) properties["AssemblyName"];
68 properties["AssemblyName"] = value;
72 public Bitmap Bitmap {
74 return (Bitmap) properties["Bitmap"];
79 properties["Bitmap"] = value;
83 public string DisplayName {
85 return (string) properties["DisplayName"];
90 properties["DisplayName"] = value;
94 public ICollection Filter {
105 public virtual bool Locked {
107 protected bool Locked {
114 public string TypeName {
116 return (string) properties["TypeName"];
121 properties["TypeName"] = value;
125 public string Company {
126 get { return (string) properties["Company"]; }
127 set { properties["Company"] = value; }
130 public virtual string ComponentType {
131 get { return "DotNET_ComponentType"; }
134 public AssemblyName[] DependentAssemblies {
135 get { return (AssemblyName[]) properties["DependentAssemblies"]; }
136 set { properties["DependentAssemblies"] = value; }
139 public string Description {
140 get { return (string) properties["Description"]; }
141 set { properties["Description"] = value; }
144 public bool IsTransient {
145 get { return (bool) properties["IsTransient"]; }
146 set { properties["IsTransient"] = value; }
149 public IDictionary Properties {
150 get { return properties; }
153 public virtual string Version {
154 get { return string.Empty; }
158 protected void CheckUnlocked ()
161 throw new InvalidOperationException ("The ToolboxItem is locked");
164 public IComponent[] CreateComponents ()
166 return CreateComponents (null);
169 public IComponent[] CreateComponents (IDesignerHost host)
171 OnComponentsCreating (new ToolboxComponentsCreatingEventArgs (host));
172 IComponent[] Comp = CreateComponentsCore (host);
173 OnComponentsCreated ( new ToolboxComponentsCreatedEventArgs (Comp));
177 [MonoTODO ("get error handling logic correct")]
178 protected virtual IComponent[] CreateComponentsCore (IDesignerHost host)
181 throw new ArgumentNullException("host");
183 OnComponentsCreating(new ToolboxComponentsCreatingEventArgs(host));
185 IComponent[] components;
\r
186 Type type = GetType(host, AssemblyName, TypeName, true);
188 components = new IComponent[] { };
190 components = new IComponent[] { host.CreateComponent(type) };
192 OnComponentsCreated(new ToolboxComponentsCreatedEventArgs(components));
\r
198 public IComponent[] CreateComponents (IDesignerHost host, IDictionary defaultValues)
\r
200 throw new NotImplementedException ();
204 public Type GetType (IDesignerHost host)
\r
206 throw new NotImplementedException ();
\r
210 protected virtual object FilterPropertyValue(string propertyName, object value)
\r
212 throw new NotImplementedException ();
216 protected virtual void Deserialize (SerializationInfo info, StreamingContext context)
218 AssemblyName = (AssemblyName)info.GetValue ("AssemblyName", typeof (AssemblyName));
219 Bitmap = (Bitmap)info.GetValue ("Bitmap", typeof (Bitmap));
220 filter = (ICollection)info.GetValue ("Filter", typeof (ICollection));
221 DisplayName = info.GetString ("DisplayName");
222 locked = info.GetBoolean ("Locked");
223 TypeName = info.GetString ("TypeName");
226 public override bool Equals (object obj)
228 // FIXME: too harsh??
229 if (!(obj is ToolboxItem))
233 return ((ToolboxItem) obj).AssemblyName.Equals (AssemblyName) &&
234 ((ToolboxItem) obj).Locked.Equals (locked) &&
235 ((ToolboxItem) obj).TypeName.Equals (TypeName) &&
236 ((ToolboxItem) obj).DisplayName.Equals (DisplayName) &&
237 ((ToolboxItem) obj).Bitmap.Equals (Bitmap);
240 public override int GetHashCode ()
242 // FIXME: other algorithm?
243 return string.Concat (TypeName, DisplayName).GetHashCode ();
247 protected virtual Type GetType (IDesignerHost host, AssemblyName assemblyName, string typeName, bool reference)
250 throw new ArgumentNullException("host");
252 //get ITypeResolutionService from host, as we have no other IServiceProvider here
253 ITypeResolutionService typeRes = host.GetService(typeof(ITypeResolutionService)) as ITypeResolutionService;
255 throw new Exception("Host does not provide an ITypeResolutionService");
257 //TODO: Using Assembly loader to throw errors. Silent fail and return null?
258 Assembly assembly = typeRes.GetAssembly(assemblyName, true);
\r
260 typeRes.ReferenceAssembly(assemblyName);
\r
261 return typeRes.GetType(typeName, true);
264 [MonoTODO ("Should we be returning empty bitmap, or null?")]
265 public virtual void Initialize (Type type)
267 AssemblyName = type.Assembly.GetName();
268 DisplayName = type.Name;
269 TypeName = type.FullName;
271 // seems to be a right place to create the bitmap
272 System.Drawing.Image image = null;
273 foreach (object attribute in type.GetCustomAttributes(true)) {
274 ToolboxBitmapAttribute tba = attribute as ToolboxBitmapAttribute;
276 image = tba.GetImage (type);
280 //fallback: check for image even if not attribute
282 image = ToolboxBitmapAttribute.GetImageFromResource (type, null, false);
286 Bitmap = (Bitmap) image;
288 Bitmap = new Bitmap (image);
291 filter = type.GetCustomAttributes (typeof (ToolboxItemFilterAttribute), true);
294 void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
296 Serialize (info, context);
300 public virtual void Lock ()
308 protected virtual void OnComponentsCreated (ToolboxComponentsCreatedEventArgs args)
310 if (ComponentsCreated != null)
311 this.ComponentsCreated (this, args);
314 protected virtual void OnComponentsCreating (ToolboxComponentsCreatingEventArgs args)
316 if (ComponentsCreated != null)
317 this.ComponentsCreating (this, args);
320 protected virtual void Serialize (SerializationInfo info, StreamingContext context)
322 info.AddValue ("AssemblyName", AssemblyName);
323 info.AddValue ("Bitmap", Bitmap);
324 info.AddValue ("Filter", filter);
325 info.AddValue ("DisplayName", DisplayName);
326 info.AddValue ("Locked", locked);
327 info.AddValue ("TypeName", TypeName);
330 public override string ToString()
336 protected void ValidatePropertyType (string propertyName, object value, Type expectedType, bool allowNull)
\r
338 throw new NotImplementedException ();
341 protected virtual object ValidatePropertyValue (string propertyName, object value)
\r
343 throw new NotImplementedException ();
347 public event ToolboxComponentsCreatedEventHandler ComponentsCreated;
349 public event ToolboxComponentsCreatingEventHandler ComponentsCreating;