001 /* 002 Copyright (C) 2002-2003 Laurent Martelli <laurent@aopsys.com> 003 Renaud Pawlak <renaud@aopsys.com> 004 005 This program is free software; you can redistribute it and/or modify 006 it under the terms of the GNU Lesser General Public License as 007 published by the Free Software Foundation; either version 2 of the 008 License, or (at your option) any later version. 009 010 This program is distributed in the hope that it will be useful, but 011 WITHOUT ANY WARRANTY; without even the implied warranty of 012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 Lesser General Public License for more details. 014 015 You should have received a copy of the GNU Lesser General Public 016 License along with this program; if not, write to the Free Software 017 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 018 USA */ 019 020 package org.objectweb.jac.aspects.gui; 021 022 import java.util.ArrayList; 023 import java.util.Collection; 024 import java.util.Enumeration; 025 import java.util.LinkedList; 026 import org.apache.log4j.Level; 027 import org.apache.log4j.LogManager; 028 import org.apache.log4j.Logger; 029 import org.objectweb.jac.aspects.authentication.AuthenticationAC; 030 import org.objectweb.jac.aspects.session.SessionAC; 031 import org.objectweb.jac.core.ACManager; 032 import org.objectweb.jac.core.Collaboration; 033 import org.objectweb.jac.core.Display; 034 import org.objectweb.jac.core.NameRepository; 035 import org.objectweb.jac.core.rtti.AbstractMethodItem; 036 import org.objectweb.jac.core.rtti.ClassItem; 037 import org.objectweb.jac.core.rtti.ClassRepository; 038 import org.objectweb.jac.core.rtti.MetaItem; 039 import org.objectweb.jac.core.rtti.MethodItem; 040 import org.objectweb.jac.util.MimeTypes; 041 import java.io.File; 042 043 /** 044 * This class is a container of commonly used GUI actions static 045 * methods. 046 * 047 * <p>Usually, a GUI action takes in parameter the display 048 * context. Using the context, is can then know the customized window 049 * to use to interact with the users. When an action does not take any 050 * display context, it means that it does not need any interaction 051 * with the GUI or that the display context is retrieved throught the 052 * collaboration. */ 053 054 public class Actions { 055 static Logger logger = Logger.getLogger("gui"); 056 057 /** 058 * Quit the application by shutting down the server. 059 * 060 * @param context the display context for this action 061 */ 062 public static void exit(DisplayContext context) { 063 Display display = context.getDisplay(); 064 if (display!=null) 065 display.close(); 066 System.exit(0); 067 } 068 069 /** 070 * Logout from an application by clearing the current user. If an 071 * authentication aspect is woven, the user is asked again. 072 * 073 * @param context the display context for this action */ 074 075 public static void logout(DisplayContext context) { 076 SessionAC sac = ((SessionAC)ACManager.getACM().getAC("session")); 077 if (sac==null) { 078 logger.error("No session aspect found: logout cannot work"); 079 return; 080 } 081 sac.clearCurrentSessionAttribute(AuthenticationAC.USER); 082 Collaboration.get().removeAttribute(SessionAC.INITIALIZED); 083 context.getDisplay().fullRefresh(); 084 } 085 086 /** 087 * Shows a message on the status bar of the current customized. If 088 * no display context available, then <code>println</code> is 089 * used. 090 * 091 * @param message the message to show */ 092 093 public static void showStatus(String message) { 094 CustomizedView cview = ((DisplayContext)Collaboration.get() 095 .getAttribute(GuiAC.DISPLAY_CONTEXT)).getCustomizedView(); 096 if(cview!=null) { 097 cview.showStatus(message); 098 } 099 } 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 }