KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > gui > Utils


1 /*
2   Copyright (C) 2001-2003 Laurent Martelli <laurent@aopsys.com>
3   
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

17
18 package org.objectweb.jac.aspects.gui;
19
20 import java.net.MalformedURLException JavaDoc;
21 import java.net.URL JavaDoc;
22 import org.apache.log4j.Logger;
23 import org.objectweb.jac.core.Wrappee;
24 import org.objectweb.jac.core.Wrapping;
25 import org.objectweb.jac.core.rtti.ClassRepository;
26 import org.objectweb.jac.core.rtti.CollectionItem;
27 import org.objectweb.jac.core.rtti.FieldItem;
28 import org.objectweb.jac.core.rtti.MethodItem;
29
30
31 /**
32  * A class with static methods to factorize code between display types
33  * (swing and web)
34  */

35
36 public class Utils {
37     static Logger logger = Logger.getLogger("gui");
38
39     /**
40      * Converts a string into an URL
41      */

42     public static URL JavaDoc stringToURL(String JavaDoc string) {
43         if (string.equals("")) {
44             return null;
45         }
46         if (string.indexOf(":")==-1) {
47             string = "file:/"+string;
48         }
49       
50         try {
51             return new URL JavaDoc(string);
52         } catch (MalformedURLException JavaDoc e) {
53             logger.warn("Malfromed URL: "+string);
54             return null;
55         }
56
57     }
58
59
60     /**
61      * Register object events for a single object.
62      *
63      * @param object the object that is listened to
64      * @param client the object that is notified for the events
65      */

66     public static void registerObject(Object JavaDoc object, ObjectUpdate client) {
67         if (object instanceof Wrappee) {
68             Wrapping.invokeRoleMethod((Wrappee)object,
69                                       ViewControlWrapper.class,"registerObject",
70                                       new Object JavaDoc[] {client,null});
71         }
72     }
73
74     /**
75      * Register object events for a single object with extra parameters.
76      *
77      * @param object the object that is listened to
78      * @param client the object that is notified for the events
79      * @param param extra info that can be passed to the event
80      * listeners */

81     public static void registerObject(Object JavaDoc object,
82                                       ObjectUpdate client,
83                                       Object JavaDoc param) {
84         if (object instanceof Wrappee) {
85             Wrapping.invokeRoleMethod((Wrappee)object,
86                                       ViewControlWrapper.class,"registerObject",
87                                       new Object JavaDoc[] {client,param});
88         }
89     }
90
91     /**
92      * Register field events for the field of an object.
93      *
94      * @param object the object that owns the field
95      * @param field the field that is listened to
96      * @param client the object that is notified for the events
97      */

98     public static void registerField(Object JavaDoc object, FieldItem field,
99                                      FieldUpdate client) {
100         if (object instanceof Wrappee && field!=null) {
101             Wrapping.invokeRoleMethod((Wrappee)object,
102                                       ViewControlWrapper.class,"registerField",
103                                       new Object JavaDoc[] {field,client,null});
104         }
105     }
106
107     /**
108      * Register field events for the field of an object (with parameters).
109      *
110      * @param object the object that owns the field
111      * @param field the field that is listened to
112      * @param client the object that is notified for the events
113      * @param param extra info that can be passed to the event
114      * listeners */

115     public static void registerField(Object JavaDoc object, FieldItem field,
116                                      FieldUpdate client,
117                                      Object JavaDoc param) {
118         if (object instanceof Wrappee && field!=null) {
119             Wrapping.invokeRoleMethod((Wrappee)object,
120                                       ViewControlWrapper.class,"registerField",
121                                       new Object JavaDoc[] {field,client,param});
122         }
123     }
124
125     /**
126      * Register for the collection events of an object.
127      *
128      * @param object the object that owns the collection
129      * @param collection the collection that is listened to
130      * @param client the object that is notified for the events
131      */

132     public static void registerCollection(Object JavaDoc object,
133                                           CollectionItem collection,
134                                           CollectionUpdate client) {
135         if (object instanceof Wrappee && collection!=null) {
136             Wrapping.invokeRoleMethod((Wrappee)object,
137                                       ViewControlWrapper.class,"registerCollection",
138                                       new Object JavaDoc[] {collection,client,null});
139         }
140     }
141
142
143     /**
144      * Register for the collection events of an object.
145      *
146      * @param object the object that owns the collection
147      * @param collectionName name the collection that is listened to
148      * @param client the object that is notified for the events
149      */

150     public static void registerCollection(Object JavaDoc object,
151                                           String JavaDoc collectionName,
152                                           CollectionUpdate client) {
153         CollectionItem collection =
154             ClassRepository.get().getClass(object).getCollection(collectionName);
155         registerCollection(object,collection,client);
156     }
157
158     /**
159      * Register for method events. The client will be notified if the
160      * value returned by the method changes.
161      *
162      * @param object the object that owns the field
163      * @param method the method that is listened to
164      * @param client the object that is notified for the events */

165     public static void registerMethod(Object JavaDoc object, MethodItem method,
166                                       MethodUpdate client) {
167         if ((object instanceof Wrappee || object==null) && method!=null) {
168             Wrapping.invokeRoleMethod((Wrappee)object,
169                                       ViewControlWrapper.class,"registerMethod",
170                                       new Object JavaDoc[] {method,client,null});
171         }
172     }
173
174     /**
175      * Register for method events. The client will be notified if the
176      * value returned by the method changes.
177      *
178      * @param object the object that owns the field
179      * @param method the method that is listened to
180      * @param client the object that is notified for the events
181      * @param param extra info that can be passed to the event listeners
182      */

183     public static void registerMethod(Object JavaDoc object, MethodItem method,
184                                       MethodUpdate client, Object JavaDoc param) {
185         if ((object instanceof Wrappee || object==null )&& method!=null) {
186             Wrapping.invokeRoleMethod((Wrappee)object,method.getClassItem(),
187                                       ViewControlWrapper.class,"registerMethod",
188                                       new Object JavaDoc[] {method,client,param});
189         }
190     }
191
192     /**
193      * Register for the collection events of an object.
194      *
195      * @param object the object that owns the collection
196      * @param collection the collection that is listened to
197      * @param client the object that is notified for the events
198      * @param param extra info that can be passed to the event
199      * listeners */

200     public static void registerCollection(Object JavaDoc object,
201                                           CollectionItem collection,
202                                           CollectionUpdate client,
203                                           Object JavaDoc param) {
204         if (object instanceof Wrappee && collection!=null) {
205             Wrapping.invokeRoleMethod((Wrappee)object,
206                                       ViewControlWrapper.class,"registerCollection",
207                                       new Object JavaDoc[] {collection,client,param});
208         }
209     }
210
211     /**
212      * Unregister from a single object.
213      */

214     public static void unregisterObject(Object JavaDoc object, ObjectUpdate client) {
215         if (object instanceof Wrappee) {
216             Wrapping.invokeRoleMethod((Wrappee)object,
217                                       ViewControlWrapper.class,"unregisterObject",
218                                       new Object JavaDoc[] {client});
219         }
220     }
221
222     /**
223      * Unregister from a single object's field.
224      *
225      * @param object the object whose collection to unregister from
226      * @param field the field to unregister from
227      * @param client the client object unregister
228      */

229     public static void unregisterField(Object JavaDoc object, FieldItem field,
230                                        FieldUpdate client) {
231         if (object instanceof Wrappee && field!=null) {
232             Wrapping.invokeRoleMethod((Wrappee)object,
233                                       ViewControlWrapper.class,"unregisterField",
234                                       new Object JavaDoc[] {field,client});
235         }
236     }
237
238     /**
239      * Unregister from a single object's collection.
240      *
241      * @param object the object whose collection to unregister from
242      * @param collection the collection to unregister from
243      * @param client the client object unregister
244      */

245     public static void unregisterCollection(Object JavaDoc object,
246                                             CollectionItem collection,
247                                             CollectionUpdate client) {
248         if (object instanceof Wrappee && collection!=null) {
249             Wrapping.invokeRoleMethod((Wrappee)object,
250                                       ViewControlWrapper.class,"unregisterCollection",
251                                       new Object JavaDoc[] {collection,client});
252         }
253     }
254
255     /**
256      * Unregister from a single object's collection.
257      *
258      * @param object the object whose collection to unregister from
259      * @param collectionName the name of the collection to unregister from
260      * @param client the client object to unregister
261      */

262     public static void unregisterCollection(Object JavaDoc object,
263                                             String JavaDoc collectionName,
264                                             CollectionUpdate client) {
265         CollectionItem collection =
266             ClassRepository.get().getClass(object).getCollection(collectionName);
267         unregisterCollection(object,collection,client);
268     }
269
270     /**
271      * Unregister from a single object's method.
272      *
273      * @param object the object whose method to unregister from
274      * @param method the method to unregister from
275      * @param client the client object unregister
276      */

277     public static void unregisterMethod(Object JavaDoc object, MethodItem method,
278                                         MethodUpdate client) {
279         if (object instanceof Wrappee && method!=null) {
280             Wrapping.invokeRoleMethod((Wrappee)object,method.getClassItem(),
281                                       ViewControlWrapper.class,"unregisterMethod",
282                                       new Object JavaDoc[] {method,client});
283         }
284     }
285
286     /**
287      * Unregister from an object's events.
288      *
289      * @param object the object to unregister from
290      * @param client the client object to unregister
291      */

292     public static void unregister(Object JavaDoc object, CollectionUpdate client) {
293         if (object instanceof Wrappee) {
294             Wrapping.invokeRoleMethod((Wrappee)object,
295                                       ViewControlWrapper.class,"unregister",
296                                       new Object JavaDoc[] {client});
297         }
298     }
299
300 }
301
302
Popular Tags