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
8 // Sebastien Pouliot <sebastien@ximian.com>
10 // (C) Alejandro Sánchez Acosta
11 // (C) 2003 Andreas Nahr
12 // Copyright (C) 2004-2006 Novell, Inc (http://www.novell.com)
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System
.Collections
;
35 using System
.ComponentModel
;
36 using System
.ComponentModel
.Design
;
37 using System
.Reflection
;
38 using System
.Runtime
.Serialization
;
39 using System
.Security
.Permissions
;
41 namespace System
.Drawing
.Design
44 [PermissionSet (SecurityAction
.LinkDemand
, Unrestricted
= true)]
45 [PermissionSet (SecurityAction
.InheritanceDemand
, Unrestricted
= true)]
46 [MonoTODO ("Implementation is incomplete.")]
47 public class ToolboxItem
: ISerializable
{
49 private bool locked
= false;
50 private Hashtable properties
= new Hashtable ();
56 public ToolboxItem (Type toolType
)
58 Initialize (toolType
);
61 public AssemblyName AssemblyName
{
62 get { return (AssemblyName) properties["AssemblyName"]; }
63 set { SetValue ("AssemblyName", value); }
66 public Bitmap Bitmap
{
67 get { return (Bitmap) properties["Bitmap"]; }
68 set { SetValue ("Bitmap", value); }
71 public string DisplayName
{
72 get { return GetValue ("DisplayName"); }
73 set { SetValue ("DisplayName", value); }
76 public ICollection Filter
{
78 ICollection filter
= (ICollection
) properties
["Filter"];
80 filter
= new ToolboxItemFilterAttribute
[0];
83 set { SetValue ("Filter", value); }
86 public virtual bool Locked
{
88 protected bool Locked
{
90 get { return locked; }
93 public string TypeName
{
94 get { return GetValue ("TypeName"); }
95 set { SetValue ("TypeName", value); }
98 public string Company
{
99 get { return (string) properties["Company"]; }
100 set { SetValue ("Company", value); }
103 public virtual string ComponentType
{
104 get { return ".NET Component"; }
107 public AssemblyName
[] DependentAssemblies
{
108 get { return (AssemblyName[]) properties["DependentAssemblies"]; }
110 AssemblyName
[] names
= new AssemblyName
[value.Length
];
111 for (int i
=0; i
< names
.Length
; i
++)
112 names
[i
] = value [i
];
113 SetValue ("DependentAssemblies", names
);
117 public string Description
{
118 get { return (string) properties["Description"]; }
119 set { SetValue ("Description", value); }
122 public bool IsTransient
{
124 object o
= properties
["IsTransient"];
125 return (o
== null) ? false : (bool) o
;
127 set { SetValue ("IsTransient", value); }
130 public IDictionary Properties
{
131 get { return properties; }
134 public virtual string Version
{
135 get { return string.Empty; }
139 protected void CheckUnlocked ()
142 throw new InvalidOperationException ("The ToolboxItem is locked");
145 public IComponent
[] CreateComponents ()
147 return CreateComponents (null);
150 public IComponent
[] CreateComponents (IDesignerHost host
)
152 OnComponentsCreating (new ToolboxComponentsCreatingEventArgs (host
));
153 IComponent
[] Comp
= CreateComponentsCore (host
);
154 OnComponentsCreated (new ToolboxComponentsCreatedEventArgs (Comp
));
158 // FIXME - get error handling logic correct
159 protected virtual IComponent
[] CreateComponentsCore (IDesignerHost host
)
162 throw new ArgumentNullException("host");
164 IComponent
[] components
;
165 Type type
= GetType(host
, AssemblyName
, TypeName
, true);
167 components
= new IComponent
[] { }
;
169 components
= new IComponent
[] { host.CreateComponent(type) }
;
175 protected virtual IComponent
[] CreateComponentsCore (IDesignerHost host
, IDictionary defaultValues
)
177 IComponent
[] components
= CreateComponentsCore (host
);
178 foreach (Component c
in components
) {
179 IComponentInitializer initializer
= host
.GetDesigner (c
) as IComponentInitializer
;
180 initializer
.InitializeNewComponent (defaultValues
);
185 public IComponent
[] CreateComponents (IDesignerHost host
, IDictionary defaultValues
)
187 OnComponentsCreating (new ToolboxComponentsCreatingEventArgs (host
));
188 IComponent
[] components
= CreateComponentsCore (host
, defaultValues
);
189 OnComponentsCreated (new ToolboxComponentsCreatedEventArgs (components
));
194 protected virtual object FilterPropertyValue (string propertyName
, object value)
196 switch (propertyName
) {
198 return (value == null) ? null : (value as ICloneable
).Clone ();
201 return (value == null) ? String
.Empty
: value;
203 return (value == null) ? new ToolboxItemFilterAttribute
[0] : value;
210 protected virtual void Deserialize (SerializationInfo info
, StreamingContext context
)
212 AssemblyName
= (AssemblyName
)info
.GetValue ("AssemblyName", typeof (AssemblyName
));
213 Bitmap
= (Bitmap
)info
.GetValue ("Bitmap", typeof (Bitmap
));
214 Filter
= (ICollection
)info
.GetValue ("Filter", typeof (ICollection
));
215 DisplayName
= info
.GetString ("DisplayName");
216 locked
= info
.GetBoolean ("Locked");
217 TypeName
= info
.GetString ("TypeName");
220 // FIXME: too harsh??
221 public override bool Equals (object obj
)
223 ToolboxItem ti
= (obj
as ToolboxItem
);
228 return (ti
.AssemblyName
.Equals (AssemblyName
) &&
229 ti
.Locked
.Equals (locked
) &&
230 ti
.TypeName
.Equals (TypeName
) &&
231 ti
.DisplayName
.Equals (DisplayName
) &&
232 ti
.Bitmap
.Equals (Bitmap
));
235 public override int GetHashCode ()
237 // FIXME: other algorithm?
238 return string.Concat (TypeName
, DisplayName
).GetHashCode ();
242 public Type
GetType (IDesignerHost host
)
244 return GetType (host
, this.AssemblyName
, this.TypeName
, false);
248 protected virtual Type
GetType (IDesignerHost host
, AssemblyName assemblyName
, string typeName
, bool reference
)
250 if (typeName
== null)
251 throw new ArgumentNullException ("typeName");
256 //get ITypeResolutionService from host, as we have no other IServiceProvider here
257 ITypeResolutionService typeRes
= host
.GetService (typeof (ITypeResolutionService
)) as ITypeResolutionService
;
259 if (typeRes
!= null) {
260 //TODO: Using Assembly loader to throw errors. Silent fail and return null?
261 typeRes
.GetAssembly (assemblyName
, true);
263 typeRes
.ReferenceAssembly (assemblyName
);
264 type
= typeRes
.GetType (typeName
, true);
266 Assembly assembly
= Assembly
.Load (assemblyName
);
267 if (assembly
!= null)
268 type
= assembly
.GetType (typeName
);
273 // FIXME - Should we be returning empty bitmap, or null?
274 public virtual void Initialize (Type type
)
280 AssemblyName
= type
.Assembly
.GetName();
281 DisplayName
= type
.Name
;
282 TypeName
= type
.FullName
;
284 // seems to be a right place to create the bitmap
285 System
.Drawing
.Image image
= null;
286 foreach (object attribute
in type
.GetCustomAttributes(true)) {
287 ToolboxBitmapAttribute tba
= attribute
as ToolboxBitmapAttribute
;
289 image
= tba
.GetImage (type
);
293 //fallback: check for image even if not attribute
295 image
= ToolboxBitmapAttribute
.GetImageFromResource (type
, null, false);
298 Bitmap
= (image
as Bitmap
);
300 Bitmap
= new Bitmap (image
);
303 Filter
= type
.GetCustomAttributes (typeof (ToolboxItemFilterAttribute
), true);
306 void ISerializable
.GetObjectData (SerializationInfo info
, StreamingContext context
)
308 Serialize (info
, context
);
312 public virtual void Lock ()
320 protected virtual void OnComponentsCreated (ToolboxComponentsCreatedEventArgs args
)
322 if (ComponentsCreated
!= null)
323 this.ComponentsCreated (this, args
);
326 protected virtual void OnComponentsCreating (ToolboxComponentsCreatingEventArgs args
)
328 if (ComponentsCreating
!= null)
329 this.ComponentsCreating (this, args
);
332 protected virtual void Serialize (SerializationInfo info
, StreamingContext context
)
334 info
.AddValue ("AssemblyName", AssemblyName
);
335 info
.AddValue ("Bitmap", Bitmap
);
336 info
.AddValue ("Filter", Filter
);
337 info
.AddValue ("DisplayName", DisplayName
);
338 info
.AddValue ("Locked", locked
);
339 info
.AddValue ("TypeName", TypeName
);
342 public override string ToString()
348 protected void ValidatePropertyType (string propertyName
, object value, Type expectedType
, bool allowNull
)
350 if (!allowNull
&& (value == null))
351 throw new ArgumentNullException ("value");
353 if ((value != null) && !expectedType
.Equals (value.GetType ())) {
354 string msg
= Locale
.GetText ("Type mismatch between value ({0}) and expected type ({1}).",
355 value.GetType (), expectedType
);
356 throw new ArgumentException (msg
, "value");
360 protected virtual object ValidatePropertyValue (string propertyName
, object value)
362 switch (propertyName
) {
364 ValidatePropertyType (propertyName
, value, typeof (AssemblyName
), true);
367 ValidatePropertyType (propertyName
, value, typeof (Bitmap
), true);
373 ValidatePropertyType (propertyName
, value, typeof (string), true);
375 value = String
.Empty
;
378 ValidatePropertyType (propertyName
, value, typeof (bool), false);
381 ValidatePropertyType (propertyName
, value, typeof (ToolboxItemFilterAttribute
[]), true);
383 value = new ToolboxItemFilterAttribute
[0];
385 case "DependentAssemblies":
386 ValidatePropertyType (propertyName
, value, typeof (AssemblyName
[]), true);
394 private void SetValue (string propertyName
, object value)
397 properties
[propertyName
] = ValidatePropertyValue (propertyName
, value);
400 private void SetValue (string propertyName
, object value)
403 properties
[propertyName
] = value;
406 private string GetValue (string propertyName
)
408 string s
= (string) properties
[propertyName
];
409 return (s
== null) ? String
.Empty
: s
;
412 public event ToolboxComponentsCreatedEventHandler ComponentsCreated
;
414 public event ToolboxComponentsCreatingEventHandler ComponentsCreating
;