001    /*
002      Copyright (C) 2001-2003 Laurent Martelli <laurent@aopsys.com>
003      
004      This program is free software; you can redistribute it and/or modify
005      it under the terms of the GNU Lesser General Public License as
006      published by the Free Software Foundation; either version 2 of the
007      License, or (at your option) any later version.
008    
009      This program is distributed in the hope that it will be useful,
010      but WITHOUT ANY WARRANTY; without even the implied warranty of
011      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012      GNU Lesser General Public License for more details.
013    
014      You should have received a copy of the GNU Lesser General Public License
015      along with this program; if not, write to the Free Software
016      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
017    
018    package org.objectweb.jac.aspects.gui;
019    
020    import java.net.MalformedURLException;
021    import java.net.URL;
022    import org.apache.log4j.Logger;
023    import org.objectweb.jac.core.Wrappee;
024    import org.objectweb.jac.core.Wrapping;
025    import org.objectweb.jac.core.rtti.ClassRepository;
026    import org.objectweb.jac.core.rtti.CollectionItem;
027    import org.objectweb.jac.core.rtti.FieldItem;
028    import org.objectweb.jac.core.rtti.MethodItem;
029    
030    
031    /**
032     * A class with static methods to factorize code between display types
033     * (swing and web)
034     */
035    
036    public class Utils {
037        static Logger logger = Logger.getLogger("gui");
038    
039        /**
040         * Converts a string into an URL
041         */
042        public static URL stringToURL(String string) {
043            if (string.equals("")) {
044                return null;
045            }
046            if (string.indexOf(":")==-1) {
047                string = "file:/"+string;
048            }
049          
050            try {
051                return new URL(string);
052            } catch (MalformedURLException e) {
053                logger.warn("Malfromed URL: "+string);
054                return null;
055            }
056    
057        }
058    
059    
060        /**
061         * Register object events for a single object.
062         *
063         * @param object the object that is listened to
064         * @param client the object that is notified for the events
065         */
066        public static void registerObject(Object object, ObjectUpdate client) {
067            if (object instanceof Wrappee) {
068                Wrapping.invokeRoleMethod((Wrappee)object,
069                                          ViewControlWrapper.class,"registerObject",
070                                          new Object[] {client,null});
071            }
072        }
073    
074        /**
075         * Register object events for a single object with extra parameters.
076         *
077         * @param object the object that is listened to
078         * @param client the object that is notified for the events
079         * @param param extra info that can be passed to the event
080         * listeners */
081        public static void registerObject(Object object, 
082                                          ObjectUpdate client, 
083                                          Object param) {
084            if (object instanceof Wrappee) {
085                Wrapping.invokeRoleMethod((Wrappee)object,
086                                          ViewControlWrapper.class,"registerObject",
087                                          new Object[] {client,param});
088            }
089        }
090    
091        /**
092         * Register field events for the field of an object.
093         *
094         * @param object the object that owns the field
095         * @param field the field that is listened to
096         * @param client the object that is notified for the events
097         */
098        public static void registerField(Object object, FieldItem field, 
099                                         FieldUpdate client) {
100            if (object instanceof Wrappee && field!=null) {
101                Wrapping.invokeRoleMethod((Wrappee)object,
102                                          ViewControlWrapper.class,"registerField",
103                                          new Object[] {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 object, FieldItem field, 
116                                         FieldUpdate client, 
117                                         Object param) {
118            if (object instanceof Wrappee && field!=null) {
119                Wrapping.invokeRoleMethod((Wrappee)object,
120                                          ViewControlWrapper.class,"registerField",
121                                          new Object[] {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 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[] {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 object, 
151                                              String 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 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[] {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 object, MethodItem method, 
184                                          MethodUpdate client, Object param) {
185            if ((object instanceof Wrappee || object==null )&& method!=null) {
186                Wrapping.invokeRoleMethod((Wrappee)object,method.getClassItem(),
187                                          ViewControlWrapper.class,"registerMethod",
188                                          new Object[] {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 object,
201                                              CollectionItem collection, 
202                                              CollectionUpdate client,
203                                              Object param) {
204            if (object instanceof Wrappee && collection!=null) {
205                Wrapping.invokeRoleMethod((Wrappee)object,
206                                          ViewControlWrapper.class,"registerCollection",
207                                          new Object[] {collection,client,param});
208            }
209        }
210    
211        /**
212         * Unregister from a single object.
213         */
214        public static void unregisterObject(Object object, ObjectUpdate client) {
215            if (object instanceof Wrappee) {
216                Wrapping.invokeRoleMethod((Wrappee)object,
217                                          ViewControlWrapper.class,"unregisterObject",
218                                          new Object[] {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 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[] {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 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[] {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 object,
263                                                String 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 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[] {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 object, CollectionUpdate client) {
293            if (object instanceof Wrappee) {
294                Wrapping.invokeRoleMethod((Wrappee)object,
295                                          ViewControlWrapper.class,"unregister",
296                                          new Object[] {client});
297            }
298        }
299    
300    }
301