2006-05-24 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / System.Drawing / System.Drawing.Design / ToolboxItem.cs
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 //
9 // (C) Alejandro Sánchez Acosta
10 // (C) 2003 Andreas Nahr
11 // Copyright (C) 2004-2006 Novell, Inc (http://www.novell.com)
12 //
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:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
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.
31 //
32
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;
39
40 namespace System.Drawing.Design 
41 {
42         [Serializable]
43         [PermissionSet (SecurityAction.LinkDemand, Unrestricted = true)]
44         [PermissionSet (SecurityAction.InheritanceDemand, Unrestricted = true)]
45         public class ToolboxItem : ISerializable
46         {               
47                 private bool locked = false;
48                 private ICollection filter = new ToolboxItemFilterAttribute[0];
49                 private Hashtable properties = new Hashtable ();
50                 
51                 public ToolboxItem() {
52                 }
53
54                 public ToolboxItem (Type toolType) {
55                         Initialize (toolType);
56                 }
57
58                 public AssemblyName AssemblyName {
59                         get {
60                                 return (AssemblyName) properties["AssemblyName"];
61                         }
62
63                         set {
64                                 CheckUnlocked ();
65                                 properties["AssemblyName"] = value;
66                         }
67                 }
68
69                 public Bitmap Bitmap {
70                         get {
71                                 return (Bitmap) properties["Bitmap"];
72                         }
73                         
74                         set {
75                                 CheckUnlocked ();
76                                 properties["Bitmap"] = value;
77                         }
78                 }
79
80                 public string DisplayName {
81                         get {
82                                 return (string) properties["DisplayName"];
83                         }
84                         
85                         set {
86                                 CheckUnlocked ();
87                                 properties["DisplayName"] = value;
88                         }
89                 }
90
91                 public ICollection Filter {
92                         get {
93                                 return filter;
94                         }
95                         
96                         set {
97                                 CheckUnlocked ();
98                                 filter = value;
99                         }
100                 }
101 #if NET_2_0
102                 public virtual bool Locked {
103 #else           
104                 protected bool Locked {
105 #endif          
106                         get {
107                                 return locked;
108                         }
109                 }
110
111                 public string TypeName {
112                         get {
113                                 return (string) properties["TypeName"];
114                         }
115
116                         set {
117                                 CheckUnlocked ();
118                                 properties["TypeName"] = value;
119                         }
120                 }
121 #if NET_2_0
122                 public string Company {
123                         get { return (string) properties["Company"]; }
124                         set { properties["Company"] = value; }
125                 }
126 \r
127                 public virtual string ComponentType {
128                         get { return ".NET Component"; }
129                 }
130 \r
131                 public AssemblyName[] DependentAssemblies {
132                         get { return (AssemblyName[]) properties["DependentAssemblies"]; }
133                         set { properties["DependentAssemblies"] = value; }
134                 }
135 \r
136                 public string Description {
137                         get { return (string) properties["Description"]; }
138                         set { properties["Description"] = value; }
139                 }\r
140 \r
141                 public bool IsTransient {                       
142                         get { return (bool) properties["IsTransient"]; }
143                         set { properties["IsTransient"] = value; }
144                 }
145 \r
146                 public IDictionary Properties {
147                          get { return properties; }
148                 }
149
150                 public virtual string Version { 
151                         get { return string.Empty; }
152                 }                               
153
154 #endif          
155                 protected void CheckUnlocked ()
156                 {
157                         if (locked)
158                                 throw new InvalidOperationException ("The ToolboxItem is locked");
159                 }
160
161                 public IComponent[] CreateComponents () 
162                 {
163                         return CreateComponents (null);
164                 }
165
166                 public IComponent[] CreateComponents (IDesignerHost host)
167                 {
168                         OnComponentsCreating (new ToolboxComponentsCreatingEventArgs (host));
169                         IComponent[] Comp = CreateComponentsCore (host);
170                         OnComponentsCreated ( new ToolboxComponentsCreatedEventArgs (Comp));
171                         return Comp;
172                 }
173
174                 [MonoTODO ("get error handling logic correct")] 
175                 protected virtual IComponent[] CreateComponentsCore (IDesignerHost host)
176                 {\r
177                         if (host == null)\r
178                                 throw new ArgumentNullException("host");
179
180                         OnComponentsCreating(new ToolboxComponentsCreatingEventArgs(host));
181                         \r
182                         IComponent[] components;\r
183                         Type type = GetType(host, AssemblyName, TypeName, true);
184                         if (type == null)\r
185                                 components = new IComponent[] { };
186                         else\r
187                                 components = new IComponent[] { host.CreateComponent(type) };
188
189                         OnComponentsCreated(new ToolboxComponentsCreatedEventArgs(components));\r
190                         return components;
191                 }
192
193 #if NET_2_0
194                 [MonoTODO] 
195                 protected virtual IComponent[] CreateComponentsCore (IDesignerHost host, IDictionary defaultValues)
196                 {
197                         throw new NotImplementedException ();
198                 } 
199
200                 [MonoTODO] 
201                 public IComponent[] CreateComponents (IDesignerHost host, IDictionary defaultValues)\r
202                 {
203                         throw new NotImplementedException ();
204                 } 
205
206                 [MonoTODO] 
207                 public Type GetType (IDesignerHost host)\r
208                 {\r
209                         throw new NotImplementedException ();\r
210                 }
211
212                 [MonoTODO] 
213                 protected virtual object FilterPropertyValue(string propertyName, object value)\r
214                 {
215                         throw new NotImplementedException ();
216                 }\r
217 #endif
218
219                 protected virtual void Deserialize (SerializationInfo info, StreamingContext context)
220                 {                       
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");
227                 }
228
229                 public override bool Equals (object obj)
230                 {
231                         // FIXME: too harsh??
232                         if (!(obj is ToolboxItem))
233                                 return false;
234                         if (obj == this)
235                                 return true;
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);
241                 }
242                 
243                 public override int GetHashCode ()
244                 {
245                         // FIXME: other algorithm?
246                         return string.Concat (TypeName, DisplayName).GetHashCode ();
247                 }
248
249                 [MonoTODO]
250                 protected virtual Type GetType (IDesignerHost host, AssemblyName assemblyName, string typeName, bool reference)
251                 {\r
252                         if (host == null)\r
253                                 throw new ArgumentNullException("host");
254
255                         //get ITypeResolutionService from host, as we have no other IServiceProvider here
256                         ITypeResolutionService typeRes = host.GetService(typeof(ITypeResolutionService)) as ITypeResolutionService;
257                         if (typeRes == null)
258                                 throw new Exception("Host does not provide an ITypeResolutionService");
259
260                         //TODO: Using Assembly loader to throw errors. Silent fail and return null?
261                         Assembly assembly = typeRes.GetAssembly(assemblyName, true);\r
262                         if (reference)\r
263                                 typeRes.ReferenceAssembly(assemblyName);\r
264                         return typeRes.GetType(typeName, true);
265                 }
266
267                 [MonoTODO ("Should we be returning empty bitmap, or null?")]
268                 public virtual void Initialize (Type type) 
269                 {
270                         AssemblyName = type.Assembly.GetName();
271                         DisplayName = type.Name;
272                         TypeName = type.FullName;
273                         
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;
278                                 if (tba != null) {
279                                         image = tba.GetImage (type);
280                                         break;
281                                 }
282                         }
283                         //fallback: check for image even if not attribute
284                         if (image == null)
285                                 image = ToolboxBitmapAttribute.GetImageFromResource (type, null, false);
286                         
287                         if (image != null) {
288                                 if (image is Bitmap)
289                                         Bitmap = (Bitmap) image;
290                                 else
291                                         Bitmap = new Bitmap (image);
292                         }
293
294                         filter = type.GetCustomAttributes (typeof (ToolboxItemFilterAttribute), true);
295                 }
296                         
297                 void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
298                 {
299                         Serialize (info, context);
300                 }
301
302 #if NET_2_0
303                 public virtual void Lock () 
304 #else           
305                 public void Lock ()
306 #endif                          
307                 {
308                         locked = true;
309                 }
310
311                 protected virtual void OnComponentsCreated (ToolboxComponentsCreatedEventArgs args)
312                 {
313                         if (ComponentsCreated != null)
314                                 this.ComponentsCreated (this, args);
315                 }
316
317                 protected virtual void OnComponentsCreating (ToolboxComponentsCreatingEventArgs args)
318                 {
319                         if (ComponentsCreated != null)
320                                 this.ComponentsCreating (this, args);
321                 }
322
323                 protected virtual void Serialize (SerializationInfo info, StreamingContext context)
324                 {
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);
331                 }
332
333                 public override string ToString()
334                 {
335                         return DisplayName;
336                 }
337
338 #if NET_2_0
339                 protected void ValidatePropertyType (string propertyName, object value, Type expectedType, bool allowNull)\r
340                 {
341                         throw new NotImplementedException ();
342                 }
343
344                 protected virtual object ValidatePropertyValue (string propertyName, object value)\r
345                 {
346                         throw new NotImplementedException ();
347                 } 
348 #endif
349
350                 public event ToolboxComponentsCreatedEventHandler ComponentsCreated;
351
352                 public event ToolboxComponentsCreatingEventHandler ComponentsCreating;
353         }
354 }