1 7 package org.ejtools.adwt.action; 8 9 import java.awt.event.ActionEvent ; 10 import java.beans.PropertyChangeListener ; 11 import java.beans.beancontext.BeanContextServiceAvailableEvent ; 12 import java.beans.beancontext.BeanContextServiceRevokedEvent ; 13 import java.beans.beancontext.BeanContextServices ; 14 import java.util.ResourceBundle ; 15 16 import javax.swing.AbstractAction ; 17 import javax.swing.Action ; 18 import javax.swing.Icon ; 19 import javax.swing.ImageIcon ; 20 import javax.swing.JMenuItem ; 21 import javax.swing.KeyStroke ; 22 23 import org.apache.log4j.Logger; 24 import org.ejtools.adwt.service.MenuBarService; 25 import org.ejtools.adwt.service.ToolBarService; 26 import org.ejtools.beans.beancontext.CustomBeanContextServicesSupport; 27 28 54 public class CommandAction extends CustomBeanContextServicesSupport implements Action 55 { 56 57 protected AbstractAction action = null; 58 59 protected Command command; 60 61 protected ResourceBundle resource; 62 63 private static Logger logger = Logger.getLogger(CommandAction.class); 64 65 66 public final static String ICON = "Icon"; 67 68 public final static String MENU = "Menu"; 69 70 public final static String MENU_ITEM = "MenuItem"; 71 72 public final static String MENU_LAYOUT = "MenuLayout"; 73 74 public final static String TOOLBAR = "Toolbar"; 75 76 public final static String TOOLTIP = "ToolTip"; 77 78 79 80 87 protected CommandAction(Command command, ResourceBundle res, String name) 88 { 89 this.setCommand(command); 90 this.setResourceBundle(res); 91 92 this.action = 93 new AbstractAction (res.getString(name)) 94 { 95 public void actionPerformed(ActionEvent e) 96 { 97 getCommand().execute(); 98 } 99 }; 100 101 this.putValue(TOOLBAR, Boolean.FALSE); 102 this.putValue(MENU_LAYOUT, new Integer (MenuBarService.NO_LAYOUT)); 103 104 try 105 { 106 this.putValue(Action.MNEMONIC_KEY, new Integer (resource.getString(name + ".mnemonic"))); 107 } 108 catch (Exception e) 109 { 110 } 112 try 113 { 114 this.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(resource.getString(name + ".accelerator"))); 115 } 116 catch (Exception e) 117 { 118 } 120 try 121 { 122 this.putValue(TOOLTIP, resource.getString(name + ".tooltip")); 123 } 124 catch (Exception e) 125 { 126 } 128 } 129 130 131 136 public void actionPerformed(ActionEvent e) 137 { 138 this.action.actionPerformed(e); 139 } 140 141 142 147 public void addPropertyChangeListener(PropertyChangeListener listener) 148 { 149 this.action.addPropertyChangeListener(listener); 150 } 151 152 153 158 public JMenuItem getMenuItem() 159 { 160 JMenuItem menuitem = (JMenuItem ) this.getValue(MENU_ITEM); 161 162 if (menuitem == null) 163 { 164 logger.debug("Building MenuItem"); 165 166 menuitem = new JMenuItem (this); 167 168 if (this.getValue(Action.MNEMONIC_KEY) != null) 169 { 170 menuitem.setMnemonic(((Integer ) this.getValue(Action.MNEMONIC_KEY)).intValue()); 171 } 172 if (this.getValue(Action.ACCELERATOR_KEY) != null) 173 { 174 menuitem.setAccelerator((KeyStroke ) this.getValue(Action.ACCELERATOR_KEY)); 175 } 176 this.putValue(MENU_ITEM, menuitem); 177 } 178 return menuitem; 179 } 180 181 182 188 public Object getValue(String key) 189 { 190 return this.action.getValue(key); 191 } 192 193 194 199 public boolean isEnabled() 200 { 201 return this.action.isEnabled(); 202 } 203 204 205 211 public void putValue(String key, Object value) 212 { 213 this.action.putValue(key, value); 214 } 215 216 217 222 public void removePropertyChangeListener(PropertyChangeListener listener) 223 { 224 this.action.removePropertyChangeListener(listener); 225 } 226 227 228 233 public void serviceAvailable(BeanContextServiceAvailableEvent bcsae) { } 234 235 236 241 public void serviceRevoked(BeanContextServiceRevokedEvent bcsre) { } 242 243 244 249 public void setEnabled(boolean b) 250 { 251 this.action.setEnabled(b); 252 } 253 254 255 256 protected void initializeBeanContextResources() 257 { 258 super.initializeBeanContextResources(); 259 BeanContextServices context = (BeanContextServices ) getBeanContext(); 260 this.register(context); 261 } 262 263 264 269 protected void register(BeanContextServices context) 270 { 271 if ((context.hasService(MenuBarService.class)) && (this.getValue(MENU) != null) && (!"".equals(this.getValue(MENU)))) 273 { 274 logger.debug("Using service MenuBarService"); 275 try 276 { 277 MenuBarService service = (MenuBarService) context.getService(this, this, MenuBarService.class, this, this); 278 service.register(this); 279 } 280 catch (Exception e) 281 { 282 logger.error("Error during utilisation of service MenuBarService (" + e.getMessage() + ")"); 283 e.printStackTrace(); 284 } 285 } 286 287 if ((context.hasService(ToolBarService.class)) && (((Boolean ) this.getValue(TOOLBAR)).booleanValue())) 289 { 290 logger.debug("Using service ToolBarService"); 291 try 292 { 293 ToolBarService service = (ToolBarService) context.getService(this, this, ToolBarService.class, this, this); 294 service.register(this); 295 } 296 catch (Exception e) 297 { 298 logger.error("Error during utilisation of service ToolBarService (" + e.getMessage() + ")"); 299 e.printStackTrace(); 300 } 301 } 302 } 303 304 305 306 protected void releaseBeanContextResources() 307 { 308 BeanContextServices context = (BeanContextServices ) getBeanContext(); 309 this.unregister(context); 310 super.releaseBeanContextResources(); 311 } 312 313 314 319 protected void unregister(BeanContextServices context) 320 { 321 if ((context.hasService(MenuBarService.class)) && (this.getValue(MENU) != null) && (!"".equals(this.getValue(MENU)))) 323 { 324 logger.debug("Using service MenuBarService"); 325 try 326 { 327 MenuBarService service = (MenuBarService) context.getService(this, this, MenuBarService.class, this, this); 328 service.unregister(this); 329 } 330 catch (Exception e) 331 { 332 logger.error("Error during utilisation of service MenuBarService (" + e.getMessage() + ")"); 333 e.printStackTrace(); 334 } 335 } 336 337 if ((context.hasService(ToolBarService.class)) && (((Boolean ) this.getValue(TOOLBAR)).booleanValue())) 339 { 340 logger.debug("Using service ToolBarService"); 341 try 342 { 343 ToolBarService service = (ToolBarService) context.getService(this, this, ToolBarService.class, this, this); 344 service.unregister(this); 345 } 346 catch (Exception e) 347 { 348 logger.error("Error during utilisation of service ToolBarService (" + e.getMessage() + ")"); 349 e.printStackTrace(); 350 } 351 } 352 } 353 354 355 360 public final String getMenu() 361 { 362 return (String ) this.getValue(MENU); 363 } 364 365 366 371 public final int getMenuLayout() 372 { 373 return ((Integer ) this.getValue(MENU_LAYOUT)).intValue(); 374 } 375 376 377 382 public final ResourceBundle getResourceBundle() 383 { 384 return this.resource; 385 } 386 387 388 393 public final boolean getToolBar() 394 { 395 return ((Boolean ) this.getValue(TOOLBAR)).booleanValue(); 396 } 397 398 399 404 public final void setIcon(String icon) 405 { 406 if ((icon != null) && (!"".equals(icon))) 407 { 408 try 409 { 410 Icon image = new ImageIcon (getClass().getResource(icon)); 411 this.putValue(ICON, image); 412 } 413 catch (Exception e) 414 { 415 } 416 } 417 } 418 419 420 425 public final void setMenu(String menu) 426 { 427 if ((menu != null) && (!"".equals(menu))) 428 { 429 this.putValue(MENU, menu); 430 } 431 } 432 433 434 439 public final void setMenuLayout(int menuLayout) 440 { 441 this.putValue(MENU_LAYOUT, new Integer (menuLayout)); 442 } 443 444 445 450 public final void setSmallIcon(String icon) 451 { 452 if ((icon != null) && (!"".equals(icon))) 453 { 454 try 455 { 456 Icon image = new ImageIcon (getClass().getResource(icon)); 457 this.putValue(Action.SMALL_ICON, image); 458 } 459 catch (Exception e) 460 { 461 } 462 } 463 } 464 465 466 471 public final void setToolBar(boolean inToolBar) 472 { 473 this.putValue(TOOLBAR, new Boolean (inToolBar)); 474 } 475 476 477 482 protected final Command getCommand() 483 { 484 return this.command; 485 } 486 487 488 493 protected final void setCommand(Command newValue) 494 { 495 this.command = newValue; 496 } 497 498 499 504 protected final void setResourceBundle(ResourceBundle newValue) 505 { 506 this.resource = newValue; 507 } 508 } 509 510 | Popular Tags |