GenericParameter.cs: override Module properly
[mcs.git] / class / System.Drawing / System.Drawing.Design / ToolboxItem.cs
blob1762737ee9ef54315e815f4b4ef35da8cfbff600
1 //
2 // System.Drawing.Design.ToolboxItem.cs
3 //
4 // Authors:
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>
9 //
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:
21 //
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 //
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
43 [Serializable]
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 ();
52 public ToolboxItem ()
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 {
77 get {
78 ICollection filter = (ICollection) properties["Filter"];
79 if (filter == null)
80 filter = new ToolboxItemFilterAttribute[0];
81 return filter;
83 set { SetValue ("Filter", value); }
85 #if NET_2_0
86 public virtual bool Locked {
87 #else
88 protected bool Locked {
89 #endif
90 get { return locked; }
93 public string TypeName {
94 get { return GetValue ("TypeName"); }
95 set { SetValue ("TypeName", value); }
97 #if NET_2_0
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"]; }
109 set {
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 {
123 get {
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; }
138 #endif
139 protected void CheckUnlocked ()
141 if (locked)
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));
155 return Comp;
158 // FIXME - get error handling logic correct
159 protected virtual IComponent[] CreateComponentsCore (IDesignerHost host)
161 if (host == null)
162 throw new ArgumentNullException("host");
164 IComponent[] components;
165 Type type = GetType(host, AssemblyName, TypeName, true);
166 if (type == null)
167 components = new IComponent[] { };
168 else
169 components = new IComponent[] { host.CreateComponent(type) };
171 return components;
174 #if NET_2_0
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);
182 return components;
185 public IComponent[] CreateComponents (IDesignerHost host, IDictionary defaultValues)
187 OnComponentsCreating (new ToolboxComponentsCreatingEventArgs (host));
188 IComponent[] components = CreateComponentsCore (host, defaultValues);
189 OnComponentsCreated (new ToolboxComponentsCreatedEventArgs (components));
191 return components;
194 protected virtual object FilterPropertyValue (string propertyName, object value)
196 switch (propertyName) {
197 case "AssemblyName":
198 return (value == null) ? null : (value as ICloneable).Clone ();
199 case "DisplayName":
200 case "TypeName":
201 return (value == null) ? String.Empty : value;
202 case "Filter":
203 return (value == null) ? new ToolboxItemFilterAttribute [0] : value;
204 default:
205 return value;
208 #endif
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);
224 if (ti == null)
225 return false;
226 if (obj == this)
227 return true;
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 ();
241 #if NET_2_0
242 public Type GetType (IDesignerHost host)
244 return GetType (host, this.AssemblyName, this.TypeName, false);
246 #endif
248 protected virtual Type GetType (IDesignerHost host, AssemblyName assemblyName, string typeName, bool reference)
250 if (typeName == null)
251 throw new ArgumentNullException ("typeName");
253 if (host == null)
254 return null;
256 //get ITypeResolutionService from host, as we have no other IServiceProvider here
257 ITypeResolutionService typeRes = host.GetService (typeof (ITypeResolutionService)) as ITypeResolutionService;
258 Type type = null;
259 if (typeRes != null) {
260 //TODO: Using Assembly loader to throw errors. Silent fail and return null?
261 typeRes.GetAssembly (assemblyName, true);
262 if (reference)
263 typeRes.ReferenceAssembly (assemblyName);
264 type = typeRes.GetType (typeName, true);
265 } else {
266 Assembly assembly = Assembly.Load (assemblyName);
267 if (assembly != null)
268 type = assembly.GetType (typeName);
270 return type;
273 // FIXME - Should we be returning empty bitmap, or null?
274 public virtual void Initialize (Type type)
276 CheckUnlocked ();
277 if (type == null)
278 return;
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;
288 if (tba != null) {
289 image = tba.GetImage (type);
290 break;
293 //fallback: check for image even if not attribute
294 if (image == null)
295 image = ToolboxBitmapAttribute.GetImageFromResource (type, null, false);
297 if (image != null) {
298 Bitmap = (image as Bitmap);
299 if (Bitmap == null)
300 Bitmap = new Bitmap (image);
303 Filter = type.GetCustomAttributes (typeof (ToolboxItemFilterAttribute), true);
306 void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
308 Serialize (info, context);
311 #if NET_2_0
312 public virtual void Lock ()
313 #else
314 public void Lock ()
315 #endif
317 locked = true;
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()
344 return DisplayName;
347 #if NET_2_0
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) {
363 case "AssemblyName":
364 ValidatePropertyType (propertyName, value, typeof (AssemblyName), true);
365 break;
366 case "Bitmap":
367 ValidatePropertyType (propertyName, value, typeof (Bitmap), true);
368 break;
369 case "Company":
370 case "Description":
371 case "DisplayName":
372 case "TypeName":
373 ValidatePropertyType (propertyName, value, typeof (string), true);
374 if (value == null)
375 value = String.Empty;
376 break;
377 case "IsTransient":
378 ValidatePropertyType (propertyName, value, typeof (bool), false);
379 break;
380 case "Filter":
381 ValidatePropertyType (propertyName, value, typeof (ToolboxItemFilterAttribute[]), true);
382 if (value == null)
383 value = new ToolboxItemFilterAttribute [0];
384 break;
385 case "DependentAssemblies":
386 ValidatePropertyType (propertyName, value, typeof (AssemblyName[]), true);
387 break;
388 default:
389 break;
391 return value;
394 private void SetValue (string propertyName, object value)
396 CheckUnlocked ();
397 properties [propertyName] = ValidatePropertyValue (propertyName, value);
399 #else
400 private void SetValue (string propertyName, object value)
402 CheckUnlocked ();
403 properties [propertyName] = value;
405 #endif
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;