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
11 // Copyright (C) 2004-2006 Novell, Inc (http://www.novell.com)
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
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;
40 namespace System.Drawing.Design
43 [PermissionSet (SecurityAction.LinkDemand, Unrestricted = true)]
44 [PermissionSet (SecurityAction.InheritanceDemand, Unrestricted = true)]
45 public class ToolboxItem : ISerializable
47 private bool locked = false;
48 private ICollection filter = new ToolboxItemFilterAttribute[0];
49 private Hashtable properties = new Hashtable ();
51 public ToolboxItem() {
54 public ToolboxItem (Type toolType) {
55 Initialize (toolType);
58 public AssemblyName AssemblyName {
60 return (AssemblyName) properties["AssemblyName"];
65 properties["AssemblyName"] = value;
69 public Bitmap Bitmap {
71 return (Bitmap) properties["Bitmap"];
76 properties["Bitmap"] = value;
80 public string DisplayName {
82 return (string) properties["DisplayName"];
87 properties["DisplayName"] = value;
91 public ICollection Filter {
102 public virtual bool Locked {
104 protected bool Locked {
111 public string TypeName {
113 return (string) properties["TypeName"];
118 properties["TypeName"] = value;
122 public string Company {
123 get { return (string) properties["Company"]; }
124 set { properties["Company"] = value; }
127 public virtual string ComponentType {
128 get { return ".NET Component"; }
131 public AssemblyName[] DependentAssemblies {
132 get { return (AssemblyName[]) properties["DependentAssemblies"]; }
133 set { properties["DependentAssemblies"] = value; }
136 public string Description {
137 get { return (string) properties["Description"]; }
138 set { properties["Description"] = value; }
141 public bool IsTransient {
142 get { return (bool) properties["IsTransient"]; }
143 set { properties["IsTransient"] = value; }
146 public IDictionary Properties {
147 get { return properties; }
150 public virtual string Version {
151 get { return string.Empty; }
155 protected void CheckUnlocked ()
158 throw new InvalidOperationException ("The ToolboxItem is locked");
161 public IComponent[] CreateComponents ()
163 return CreateComponents (null);
166 public IComponent[] CreateComponents (IDesignerHost host)
168 OnComponentsCreating (new ToolboxComponentsCreatingEventArgs (host));
169 IComponent[] Comp = CreateComponentsCore (host);
170 OnComponentsCreated ( new ToolboxComponentsCreatedEventArgs (Comp));
174 [MonoTODO ("get error handling logic correct")]
175 protected virtual IComponent[] CreateComponentsCore (IDesignerHost host)
178 throw new ArgumentNullException("host");
180 OnComponentsCreating(new ToolboxComponentsCreatingEventArgs(host));
182 IComponent[] components;
\r
183 Type type = GetType(host, AssemblyName, TypeName, true);
185 components = new IComponent[] { };
187 components = new IComponent[] { host.CreateComponent(type) };
189 OnComponentsCreated(new ToolboxComponentsCreatedEventArgs(components));
\r
195 protected virtual IComponent[] CreateComponentsCore (IDesignerHost host, IDictionary defaultValues)
197 throw new NotImplementedException ();
201 public IComponent[] CreateComponents (IDesignerHost host, IDictionary defaultValues)
\r
203 throw new NotImplementedException ();
207 public Type GetType (IDesignerHost host)
\r
209 throw new NotImplementedException ();
\r
213 protected virtual object FilterPropertyValue(string propertyName, object value)
\r
215 throw new NotImplementedException ();
219 protected virtual void Deserialize (SerializationInfo info, StreamingContext context)
221 AssemblyName = (AssemblyName)info.GetValue ("AssemblyName", typeof (AssemblyName));
222 Bitmap = (Bitmap)info.GetValue ("Bitmap", typeof (Bitmap));
223 filter = (ICollection)info.GetValue ("Filter", typeof (ICollection));
224 DisplayName = info.GetString ("DisplayName");
225 locked = info.GetBoolean ("Locked");
226 TypeName = info.GetString ("TypeName");
229 public override bool Equals (object obj)
231 // FIXME: too harsh??
232 if (!(obj is ToolboxItem))
236 return ((ToolboxItem) obj).AssemblyName.Equals (AssemblyName) &&
237 ((ToolboxItem) obj).Locked.Equals (locked) &&
238 ((ToolboxItem) obj).TypeName.Equals (TypeName) &&
239 ((ToolboxItem) obj).DisplayName.Equals (DisplayName) &&
240 ((ToolboxItem) obj).Bitmap.Equals (Bitmap);
243 public override int GetHashCode ()
245 // FIXME: other algorithm?
246 return string.Concat (TypeName, DisplayName).GetHashCode ();
250 protected virtual Type GetType (IDesignerHost host, AssemblyName assemblyName, string typeName, bool reference)
253 throw new ArgumentNullException("host");
255 //get ITypeResolutionService from host, as we have no other IServiceProvider here
256 ITypeResolutionService typeRes = host.GetService(typeof(ITypeResolutionService)) as ITypeResolutionService;
258 throw new Exception("Host does not provide an ITypeResolutionService");
260 //TODO: Using Assembly loader to throw errors. Silent fail and return null?
261 Assembly assembly = typeRes.GetAssembly(assemblyName, true);
\r
263 typeRes.ReferenceAssembly(assemblyName);
\r
264 return typeRes.GetType(typeName, true);
267 [MonoTODO ("Should we be returning empty bitmap, or null?")]
268 public virtual void Initialize (Type type)
270 AssemblyName = type.Assembly.GetName();
271 DisplayName = type.Name;
272 TypeName = type.FullName;
274 // seems to be a right place to create the bitmap
275 System.Drawing.Image image = null;
276 foreach (object attribute in type.GetCustomAttributes(true)) {
277 ToolboxBitmapAttribute tba = attribute as ToolboxBitmapAttribute;
279 image = tba.GetImage (type);
283 //fallback: check for image even if not attribute
285 image = ToolboxBitmapAttribute.GetImageFromResource (type, null, false);
289 Bitmap = (Bitmap) image;
291 Bitmap = new Bitmap (image);
294 filter = type.GetCustomAttributes (typeof (ToolboxItemFilterAttribute), true);
297 void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
299 Serialize (info, context);
303 public virtual void Lock ()
311 protected virtual void OnComponentsCreated (ToolboxComponentsCreatedEventArgs args)
313 if (ComponentsCreated != null)
314 this.ComponentsCreated (this, args);
317 protected virtual void OnComponentsCreating (ToolboxComponentsCreatingEventArgs args)
319 if (ComponentsCreated != null)
320 this.ComponentsCreating (this, args);
323 protected virtual void Serialize (SerializationInfo info, StreamingContext context)
325 info.AddValue ("AssemblyName", AssemblyName);
326 info.AddValue ("Bitmap", Bitmap);
327 info.AddValue ("Filter", filter);
328 info.AddValue ("DisplayName", DisplayName);
329 info.AddValue ("Locked", locked);
330 info.AddValue ("TypeName", TypeName);
333 public override string ToString()
339 protected void ValidatePropertyType (string propertyName, object value, Type expectedType, bool allowNull)
\r
341 throw new NotImplementedException ();
344 protected virtual object ValidatePropertyValue (string propertyName, object value)
\r
346 throw new NotImplementedException ();
350 public event ToolboxComponentsCreatedEventHandler ComponentsCreated;
352 public event ToolboxComponentsCreatingEventHandler ComponentsCreating;