copying the latest Sys.Web.Services from trunk.
[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 //
8 // (C) Alejandro Sánchez Acosta
9 // (C) 2003 Andreas Nahr
10 //
11
12 //
13 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 //
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
22 // 
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
25 // 
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 //
34
35 using System.Collections;
36 using System.ComponentModel;
37 using System.ComponentModel.Design;
38 using System.Drawing;
39 using System.Reflection;
40 using System.Runtime.Serialization;
41
42 namespace System.Drawing.Design 
43 {
44         [Serializable]
45         public class ToolboxItem : ISerializable
46         {
47
48                 private AssemblyName assembly;
49                 private Bitmap bitmap;
50                 private ICollection filter = new ToolboxItemFilterAttribute[0];
51                 private string displayname = string.Empty;
52                 private bool locked = false;
53                 private string name = string.Empty;
54                 
55                 public ToolboxItem() {
56                 }
57
58                 public ToolboxItem (Type toolType) {
59                         Initialize (toolType);
60                 }
61
62                 public AssemblyName AssemblyName {
63                         get {
64                                 return assembly;
65                         }
66
67                         set {
68                                 CheckUnlocked ();
69                                 assembly = value;
70                         }
71                 }
72
73                 public Bitmap Bitmap {
74                         get {
75                                 return bitmap;
76                         }
77                         
78                         set {
79                                 CheckUnlocked ();
80                                 bitmap = value;
81                         }
82                 }
83
84                 public string DisplayName {
85                         get {
86                                 return displayname;
87                         }
88                         
89                         set {
90                                 CheckUnlocked ();
91                                 displayname = value;
92                         }
93                 }
94
95                 public ICollection Filter {
96                         get {
97                                 return filter;
98                         }
99                         
100                         set {
101                                 CheckUnlocked ();
102                                 filter = value;
103                         }
104                 }
105                 
106                 protected bool Locked {
107                         get {
108                                 return locked;
109                         }
110                 }
111
112                 public string TypeName {
113                         get {
114                                 return name;
115                         }
116
117                         set {
118                                 CheckUnlocked ();
119                                 name = value;
120                         }
121                 }
122                 
123                 protected void CheckUnlocked ()
124                 {
125                         throw new InvalidOperationException ("The ToolboxItem is locked");
126                 }
127
128                 public IComponent[] CreateComponents () 
129                 {
130                         return CreateComponents (null);
131                 }
132
133                 public IComponent[] CreateComponents (IDesignerHost host)
134                 {
135                         OnComponentsCreating (new ToolboxComponentsCreatingEventArgs (host));
136                         IComponent[] Comp = CreateComponentsCore (host);
137                         OnComponentsCreated ( new ToolboxComponentsCreatedEventArgs (Comp));
138                         return Comp;
139                 }
140
141                 [MonoTODO]
142                 protected virtual IComponent[] CreateComponentsCore (IDesignerHost host)
143                 {
144                         throw new NotImplementedException ();
145                 }
146
147                 protected virtual void Deserialize (SerializationInfo info, StreamingContext context)
148                 {
149                         assembly = (AssemblyName)info.GetValue ("AssemblyName", typeof (AssemblyName));
150                         bitmap = (Bitmap)info.GetValue ("Bitmap", typeof (Bitmap));
151                         filter = (ICollection)info.GetValue ("Filter", typeof (ICollection));
152                         displayname = info.GetString ("DisplayName");
153                         locked = info.GetBoolean ("Locked");
154                         name = info.GetString ("TypeName");
155                 }
156
157                 public override bool Equals (object obj)
158                 {
159                         // FIXME: too harsh??
160                         if (!(obj is ToolboxItem))
161                                 return false;
162                         if (obj == this)
163                                 return true;
164                         return ((ToolboxItem) obj).AssemblyName.Equals (assembly) &&
165                                 ((ToolboxItem) obj).Locked.Equals (locked) &&
166                                 ((ToolboxItem) obj).TypeName.Equals (name) &&
167                                 ((ToolboxItem) obj).DisplayName.Equals (displayname) &&
168                                 ((ToolboxItem) obj).Bitmap.Equals (bitmap);
169                 }
170                 
171                 public override int GetHashCode ()
172                 {
173                         // FIXME: other algorithm?
174                         return string.Concat (name, displayname).GetHashCode ();
175                 }
176
177                 [MonoTODO]
178                 protected virtual Type GetType (IDesignerHost host, AssemblyName assemblyName, string typeName, bool reference)
179                 {
180                         throw new NotImplementedException ();
181                 }
182
183                 [MonoTODO]
184                 public virtual void Initialize (Type type) 
185                 {
186                         // assembly = // FIXME we need to get the AssemblyName data from somewhere or create a new one
187                         displayname = type.Name;
188                         name = type.FullName;
189                         // seems to be a right place to create the bitmap
190                         bitmap = new Bitmap (16, 16); // FIXME set some default bitmap !?
191
192                         filter = type.GetCustomAttributes (typeof (ToolboxItemFilterAttribute), true);
193
194                         throw new NotImplementedException ();
195                 }
196                         
197                 void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
198                 {
199                         Serialize (info, context);
200                 }
201
202                 public void Lock ()
203                 {
204                         locked = true;
205                 }
206
207                 protected virtual void OnComponentsCreated (ToolboxComponentsCreatedEventArgs args)
208                 {
209                         if (ComponentsCreated != null)
210                                 this.ComponentsCreated (this, args);
211                 }
212
213                 protected virtual void OnComponentsCreating (ToolboxComponentsCreatingEventArgs args)
214                 {
215                         if (ComponentsCreated != null)
216                                 this.ComponentsCreating (this, args);
217                 }
218
219                 protected virtual void Serialize (SerializationInfo info, StreamingContext context)
220                 {
221                         info.AddValue ("AssemblyName", assembly);
222                         info.AddValue ("Bitmap", bitmap);
223                         info.AddValue ("Filter", filter);
224                         info.AddValue ("DisplayName", displayname);
225                         info.AddValue ("Locked", locked);
226                         info.AddValue ("TypeName", name);
227                 }
228
229                 public override string ToString()
230                 {
231                         return displayname;
232                 }
233
234                 public event ToolboxComponentsCreatedEventHandler ComponentsCreated;
235
236                 public event ToolboxComponentsCreatingEventHandler ComponentsCreating;
237         }
238 }