KickJava   Java API By Example, From Geeks To Geeks.

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


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

19
20 package org.objectweb.jac.aspects.gui;
21
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.Enumeration;
25 import java.util.LinkedList;
26 import org.apache.log4j.Level;
27 import org.apache.log4j.LogManager;
28 import org.apache.log4j.Logger;
29 import org.objectweb.jac.aspects.authentication.AuthenticationAC;
30 import org.objectweb.jac.aspects.session.SessionAC;
31 import org.objectweb.jac.core.ACManager;
32 import org.objectweb.jac.core.Collaboration;
33 import org.objectweb.jac.core.Display;
34 import org.objectweb.jac.core.NameRepository;
35 import org.objectweb.jac.core.rtti.AbstractMethodItem;
36 import org.objectweb.jac.core.rtti.ClassItem;
37 import org.objectweb.jac.core.rtti.ClassRepository;
38 import org.objectweb.jac.core.rtti.MetaItem;
39 import org.objectweb.jac.core.rtti.MethodItem;
40 import org.objectweb.jac.util.MimeTypes;
41 import java.io.File;
42
43 /**
44  * This class is a container of commonly used GUI actions static
45  * methods.
46  *
47  * <p>Usually, a GUI action takes in parameter the display
48  * context. Using the context, is can then know the customized window
49  * to use to interact with the users. When an action does not take any
50  * display context, it means that it does not need any interaction
51  * with the GUI or that the display context is retrieved throught the
52  * collaboration. */

53
54 public class Actions {
55     static Logger logger = Logger.getLogger("gui");
56
57     /**
58      * Quit the application by shutting down the server.
59      *
60      * @param context the display context for this action
61      */

62     public static void exit(DisplayContext context) {
63         Display display = context.getDisplay();
64         if (display!=null)
65             display.close();
66         System.exit(0);
67     }
68    
69     /**
70      * Logout from an application by clearing the current user. If an
71      * authentication aspect is woven, the user is asked again.
72      *
73      * @param context the display context for this action */

74
75     public static void logout(DisplayContext context) {
76         SessionAC sac = ((SessionAC)ACManager.getACM().getAC("session"));
77         if (sac==null) {
78             logger.error("No session aspect found: logout cannot work");
79             return;
80         }
81         sac.clearCurrentSessionAttribute(AuthenticationAC.USER);
82         Collaboration.get().removeAttribute(SessionAC.INITIALIZED);
83         context.getDisplay().fullRefresh();
84     }
85
86     /**
87      * Shows a message on the status bar of the current customized. If
88      * no display context available, then <code>println</code> is
89      * used.
90      *
91      * @param message the message to show */

92
93     public static void showStatus(String message) {
94         CustomizedView cview = ((DisplayContext)Collaboration.get()
95                                 .getAttribute(GuiAC.DISPLAY_CONTEXT)).getCustomizedView();
96         if(cview!=null) {
97             cview.showStatus(message);
98         }
99     }
100
101     /**
102      * Show a customized window that has been declared and configured
103      * within the GUI aspect.
104      *
105      * @param context the display context for this action
106      * @param id the customized ID */

107
108     public static void showWindow(DisplayContext context,String id) {
109         ((GuiAC)ACManager.getACM().getAC("gui"))
110             .createSwingDisplays(new String[] {id});
111     }
112   
113     /**
114      * Invokes a method on an object. */

115
116     public static void invoke(Object object, String methodName) {
117         AbstractMethodItem method = ClassRepository.get()
118             .getClass(object).getMethod(methodName);
119         EventHandler.get().onInvoke(
120             (DisplayContext)Collaboration.get().getAttribute(GuiAC.DISPLAY_CONTEXT),
121             new InvokeEvent(null,object,method));
122     }
123
124     /**
125      * Display an object in a panel.
126      *
127      * @param context the DisplayContext of the custmoized view
128      * @param objectName the name of the object to display
129      * @param panelID the panel ID where to display the object
130      *
131      * @see #viewObject(DisplayContext,String,String)
132      * @see #openView(DisplayContext,String)
133      */

134     public static void viewObject2(DisplayContext context,
135                                    String objectName, String viewName,
136                                    String panelID)
137     {
138         CustomizedView custom = context.getCustomizedView();
139         if (custom!=null) {
140             ViewFactory factory = context.getDisplay().getFactory();
141             Object object = NameRepository.get().getObject(objectName);
142             if (object!=null)
143                 custom.getPanelView().addView(
144                     factory.createView(
145                         objectName,"Object",
146                         new Object[] {viewName,object},
147                         context),
148                     panelID
149                 );
150             else
151                 custom.getPanelView().addView(
152                     factory.createView("No such object "+objectName,"Label",context),
153                     panelID
154                 );
155
156             EventHandler.get().maybeInvalidatePane(
157                 context.getDisplay().getFactory(),context,
158                 custom,panelID);
159         }
160     }
161
162     /**
163      * Display an object in a panel.
164      *
165      * @param context the DisplayContext of the custmoized view
166      * @param objectName the name of the object to display
167      * @param panelID the panel ID where to display the object
168      *
169      * @see #viewObject2(DisplayContext,String,String,String)
170      * @see #openView(DisplayContext,String)
171      */

172     public static void viewObject(DisplayContext context,
173                                   String objectName, String panelID)
174     {
175         viewObject2(context,objectName,GuiAC.DEFAULT_VIEW,panelID);
176     }
177
178     /**
179      * Display an object in a new window
180      *
181      * @param context the DisplayContext of the custmoized view
182      * @param objectName the name of the object to display
183      *
184      * @see #viewObject2(DisplayContext,String,String,String)
185      * @see #viewObject(DisplayContext,String,String)
186      */

187     public static void openView(DisplayContext context,
188                                 String objectName)
189     {
190         Object object = NameRepository.get().getObject(objectName);
191         if (object!=null)
192             context.getDisplay().show(object);
193         else
194             context.getDisplay().showError("Error","No such object "+objectName);
195     }
196
197     public static String getOpenViewIcon(MethodItem method,
198                                          Object object, Object[] parameters) {
199         Object target = NameRepository.get().getObject((String)parameters[0]);
200         if (target!=null)
201             return GuiAC.getIcon(ClassRepository.get().getClass(target),target);
202         else
203             return null;
204     }
205
206     public static String getFileIcon(File file) {
207         if (file.isDirectory())
208             return ResourceManager.getResource("open_icon");
209         else
210             return ResourceManager.getResource("doc_icon");
211     }
212
213     /**
214      * Changes a trace for the current application
215      * @param loggerName category of the trace
216      * @param level level of the trace
217      */

218     public static void setTrace(String loggerName, Level level) {
219         Logger.getLogger(loggerName).setLevel(level);
220     }
221
222     /**
223      * Returns all known logger names
224      */

225     public static Collection getLoggerNames(Object substance) {
226         Enumeration enum = LogManager.getCurrentLoggers();
227         LinkedList loggers = new LinkedList();
228         while (enum.hasMoreElements()) {
229             loggers.add(((Logger)enum.nextElement()).getName());
230         }
231         return loggers;
232     }
233
234     /**
235      * Returns all known loggers
236      */

237     public static Collection getLoggers(ClassItem cli) {
238         Enumeration enum = LogManager.getCurrentLoggers();
239         LinkedList loggers = new LinkedList();
240         while (enum.hasMoreElements()) {
241             loggers.add(enum.nextElement());
242         }
243         return loggers;
244     }
245
246     /**
247      * Returns all known log levels
248      */

249     public static Collection getLogLevels(ClassItem cli) {
250         ArrayList levels = new ArrayList(7);
251         levels.add(Level.ALL);
252         levels.add(Level.OFF);
253         levels.add(Level.DEBUG);
254         levels.add(Level.WARN);
255         levels.add(Level.ERROR);
256         levels.add(Level.INFO);
257         levels.add(Level.FATAL);
258         return levels;
259     }
260
261     static MimeTypes mimeTypes = new MimeTypes();
262     public static Collection getMimeTypes(MetaItem cli) {
263         return mimeTypes.getMimeTypes();
264     }
265     static {
266         mimeTypes.readDefaults();
267     }
268
269     /**
270      * Reloads an aspect for the current application
271      */

272     public static void reloadAspect(String aspect) throws Exception {
273         ACManager.getACM().reloadAspect(aspect);
274     }
275
276     /**
277      * Enable EJP profiling
278      */

279     /*
280       public static void enableProfiling() throws Throwable {
281       if (!ejp.tracer.TracerAPI.enableTracing()) {
282       throw ejp.tracer.TracerAPI.getInitializationError();
283       }
284       }
285
286       public static void disableProfiling() throws Throwable {
287       if (!ejp.tracer.TracerAPI.disableTracing()) {
288       throw ejp.tracer.TracerAPI.getInitializationError();
289       }
290       }
291     */

292 }
293
Popular Tags