|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| CommandAction.java | 50% | 65,9% | 85,7% | 67,7% |
|
||||||||||||||
| 1 |
/*
|
|
| 2 |
* EJTools, the Enterprise Java Tools
|
|
| 3 |
*
|
|
| 4 |
* Distributable under LGPL license.
|
|
| 5 |
* See terms of license at www.gnu.org.
|
|
| 6 |
*/
|
|
| 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 |
/**
|
|
| 29 |
* BeanContext that wraps an Action and delegates to a Command object.
|
|
| 30 |
* <p>
|
|
| 31 |
* It supports the Command pattern by delegating the <code>ActionEvent</code> event to the <code>execute</code> method of the Command object.
|
|
| 32 |
* </p>
|
|
| 33 |
* <p>
|
|
| 34 |
* It automatically handles the negotiation in ToolBar and MenuBar if these services are present.
|
|
| 35 |
* </p>
|
|
| 36 |
* <p>
|
|
| 37 |
* It supports the following properties :<ul>
|
|
| 38 |
* <li>Custom Icon</li>
|
|
| 39 |
* <li>Tooltip</li>
|
|
| 40 |
* <li>Mnemonic key</li>
|
|
| 41 |
* <li>Accelerator key</li>
|
|
| 42 |
* <li>MenuBar presence</li>
|
|
| 43 |
* <li>Toolbar presence</li>
|
|
| 44 |
* <li>Menu name</li>
|
|
| 45 |
* <li>Menu item</li>
|
|
| 46 |
* <li>Menu position</li>
|
|
| 47 |
* </ul>
|
|
| 48 |
* </p>
|
|
| 49 |
*
|
|
| 50 |
* @author Laurent Etiemble
|
|
| 51 |
* @version $Revision: 1.8 $
|
|
| 52 |
* @see org.ejtools.adwt.action.Command
|
|
| 53 |
*/
|
|
| 54 |
public class CommandAction extends CustomBeanContextServicesSupport implements Action |
|
| 55 |
{
|
|
| 56 |
/** The wrapped action */
|
|
| 57 |
protected AbstractAction action = null; |
|
| 58 |
/** The command to execute */
|
|
| 59 |
protected Command command;
|
|
| 60 |
/** Resource Bundle */
|
|
| 61 |
protected ResourceBundle resource;
|
|
| 62 |
/** Default logger */
|
|
| 63 |
private static Logger logger = Logger.getLogger(CommandAction.class); |
|
| 64 |
|
|
| 65 |
/** Constant for ICON property */
|
|
| 66 |
public final static String ICON = "Icon"; |
|
| 67 |
/** Constant for MENU property */
|
|
| 68 |
public final static String MENU = "Menu"; |
|
| 69 |
/** Constant for MENU_ITEM property */
|
|
| 70 |
public final static String MENU_ITEM = "MenuItem"; |
|
| 71 |
/** Constant for MENU_LAYOUT property */
|
|
| 72 |
public final static String MENU_LAYOUT = "MenuLayout"; |
|
| 73 |
/** Constant for TOOLBAR property */
|
|
| 74 |
public final static String TOOLBAR = "Toolbar"; |
|
| 75 |
/** Constant for TOOLTIP property */
|
|
| 76 |
public final static String TOOLTIP = "ToolTip"; |
|
| 77 |
|
|
| 78 |
|
|
| 79 |
|
|
| 80 |
/**
|
|
| 81 |
* Constructor used to fully define the CommandAction.
|
|
| 82 |
*
|
|
| 83 |
* @param command The command object to delegate the ActionEvent to
|
|
| 84 |
* @param res The Resource Bundle used for resolving the name and other properties
|
|
| 85 |
* @param name The name of the CommandAction. Must be a key found in the Resource Bundle
|
|
| 86 |
*/
|
|
| 87 | 512 |
protected CommandAction(Command command, ResourceBundle res, String name)
|
| 88 |
{
|
|
| 89 | 512 |
this.setCommand(command);
|
| 90 | 512 |
this.setResourceBundle(res);
|
| 91 |
|
|
| 92 | 512 |
this.action =
|
| 93 |
new AbstractAction(res.getString(name))
|
|
| 94 |
{
|
|
| 95 | 24 |
public void actionPerformed(ActionEvent e) |
| 96 |
{
|
|
| 97 | 24 |
getCommand().execute(); |
| 98 |
} |
|
| 99 |
}; |
|
| 100 |
|
|
| 101 | 512 |
this.putValue(TOOLBAR, Boolean.FALSE);
|
| 102 | 512 |
this.putValue(MENU_LAYOUT, new Integer(MenuBarService.NO_LAYOUT)); |
| 103 |
|
|
| 104 | 512 |
try
|
| 105 |
{
|
|
| 106 | 512 |
this.putValue(Action.MNEMONIC_KEY, new Integer(resource.getString(name + ".mnemonic"))); |
| 107 |
} |
|
| 108 |
catch (Exception e)
|
|
| 109 |
{
|
|
| 110 |
// Do nothing
|
|
| 111 |
} |
|
| 112 | 512 |
try
|
| 113 |
{
|
|
| 114 | 512 |
this.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(resource.getString(name + ".accelerator"))); |
| 115 |
} |
|
| 116 |
catch (Exception e)
|
|
| 117 |
{
|
|
| 118 |
// Do nothing
|
|
| 119 |
} |
|
| 120 | 512 |
try
|
| 121 |
{
|
|
| 122 | 512 |
this.putValue(TOOLTIP, resource.getString(name + ".tooltip")); |
| 123 |
} |
|
| 124 |
catch (Exception e)
|
|
| 125 |
{
|
|
| 126 |
// Do nothing
|
|
| 127 |
} |
|
| 128 |
} |
|
| 129 |
|
|
| 130 |
|
|
| 131 |
/**
|
|
| 132 |
* This method is called whenever the action is acted upon. The call will be delegated to the wrapped action.
|
|
| 133 |
*
|
|
| 134 |
* @param e The action event
|
|
| 135 |
*/
|
|
| 136 | 24 |
public void actionPerformed(ActionEvent e) |
| 137 |
{
|
|
| 138 | 24 |
this.action.actionPerformed(e);
|
| 139 |
} |
|
| 140 |
|
|
| 141 |
|
|
| 142 |
/**
|
|
| 143 |
* Adds a PropertyChangeListener. The call will be delegated to the wrapped action.
|
|
| 144 |
*
|
|
| 145 |
* @param listener The listener to be added
|
|
| 146 |
*/
|
|
| 147 | 40 |
public void addPropertyChangeListener(PropertyChangeListener listener) |
| 148 |
{
|
|
| 149 | 40 |
this.action.addPropertyChangeListener(listener);
|
| 150 |
} |
|
| 151 |
|
|
| 152 |
|
|
| 153 |
/**
|
|
| 154 |
* Getter for the menuItem attribute
|
|
| 155 |
*
|
|
| 156 |
* @return The value
|
|
| 157 |
*/
|
|
| 158 | 24 |
public JMenuItem getMenuItem()
|
| 159 |
{
|
|
| 160 | 24 |
JMenuItem menuitem = (JMenuItem) this.getValue(MENU_ITEM);
|
| 161 |
|
|
| 162 | 24 |
if (menuitem == null) |
| 163 |
{
|
|
| 164 | 24 |
logger.debug("Building MenuItem");
|
| 165 |
|
|
| 166 | 24 |
menuitem = new JMenuItem(this); |
| 167 |
|
|
| 168 | 24 |
if (this.getValue(Action.MNEMONIC_KEY) != null) |
| 169 |
{
|
|
| 170 | 24 |
menuitem.setMnemonic(((Integer) this.getValue(Action.MNEMONIC_KEY)).intValue());
|
| 171 |
} |
|
| 172 | 24 |
if (this.getValue(Action.ACCELERATOR_KEY) != null) |
| 173 |
{
|
|
| 174 | 24 |
menuitem.setAccelerator((KeyStroke) this.getValue(Action.ACCELERATOR_KEY));
|
| 175 |
} |
|
| 176 | 24 |
this.putValue(MENU_ITEM, menuitem);
|
| 177 |
} |
|
| 178 | 24 |
return menuitem;
|
| 179 |
} |
|
| 180 |
|
|
| 181 |
|
|
| 182 |
/**
|
|
| 183 |
* Gets the value attribute of the CommandAction object
|
|
| 184 |
*
|
|
| 185 |
* @param key Description of Parameter
|
|
| 186 |
* @return The value value
|
|
| 187 |
*/
|
|
| 188 | 464 |
public Object getValue(String key)
|
| 189 |
{
|
|
| 190 | 464 |
return this.action.getValue(key); |
| 191 |
} |
|
| 192 |
|
|
| 193 |
|
|
| 194 |
/**
|
|
| 195 |
* Gets the enabled attribute of the CommandAction object
|
|
| 196 |
*
|
|
| 197 |
* @return The enabled value
|
|
| 198 |
*/
|
|
| 199 | 40 |
public boolean isEnabled() |
| 200 |
{
|
|
| 201 | 40 |
return this.action.isEnabled(); |
| 202 |
} |
|
| 203 |
|
|
| 204 |
|
|
| 205 |
/**
|
|
| 206 |
* Description of the Method
|
|
| 207 |
*
|
|
| 208 |
* @param key Description of Parameter
|
|
| 209 |
* @param value Description of Parameter
|
|
| 210 |
*/
|
|
| 211 | 2648 |
public void putValue(String key, Object value) |
| 212 |
{
|
|
| 213 | 2648 |
this.action.putValue(key, value);
|
| 214 |
} |
|
| 215 |
|
|
| 216 |
|
|
| 217 |
/**
|
|
| 218 |
* Removes a PropertyChangeListener. The call will be delegated to the wrapped action.
|
|
| 219 |
*
|
|
| 220 |
* @param listener The listener to remove
|
|
| 221 |
*/
|
|
| 222 | 0 |
public void removePropertyChangeListener(PropertyChangeListener listener) |
| 223 |
{
|
|
| 224 | 0 |
this.action.removePropertyChangeListener(listener);
|
| 225 |
} |
|
| 226 |
|
|
| 227 |
|
|
| 228 |
/**
|
|
| 229 |
* Triggered when a service is made available.
|
|
| 230 |
*
|
|
| 231 |
* @param bcsae The availability event
|
|
| 232 |
*/
|
|
| 233 | 0 |
public void serviceAvailable(BeanContextServiceAvailableEvent bcsae) { } |
| 234 |
|
|
| 235 |
|
|
| 236 |
/**
|
|
| 237 |
* Triggered when a service is revoked.
|
|
| 238 |
*
|
|
| 239 |
* @param bcsre The revocation event
|
|
| 240 |
*/
|
|
| 241 | 0 |
public void serviceRevoked(BeanContextServiceRevokedEvent bcsre) { } |
| 242 |
|
|
| 243 |
|
|
| 244 |
/**
|
|
| 245 |
* Enables the Action. The call will be delegated to the wrapped action.
|
|
| 246 |
*
|
|
| 247 |
* @param b If true, the action will be enable
|
|
| 248 |
*/
|
|
| 249 | 0 |
public void setEnabled(boolean b) |
| 250 |
{
|
|
| 251 | 0 |
this.action.setEnabled(b);
|
| 252 |
} |
|
| 253 |
|
|
| 254 |
|
|
| 255 |
/** Called when the CommandAction is put in a BeanContext */
|
|
| 256 | 24 |
protected void initializeBeanContextResources() |
| 257 |
{
|
|
| 258 | 24 |
super.initializeBeanContextResources();
|
| 259 | 24 |
BeanContextServices context = (BeanContextServices) getBeanContext(); |
| 260 | 24 |
this.register(context);
|
| 261 |
} |
|
| 262 |
|
|
| 263 |
|
|
| 264 |
/**
|
|
| 265 |
* Description of the Method
|
|
| 266 |
*
|
|
| 267 |
* @param context Description of Parameter
|
|
| 268 |
*/
|
|
| 269 | 24 |
protected void register(BeanContextServices context) |
| 270 |
{
|
|
| 271 |
// Set up MenuBar
|
|
| 272 | 24 |
if ((context.hasService(MenuBarService.class)) && (this.getValue(MENU) != null) && (!"".equals(this.getValue(MENU)))) |
| 273 |
{
|
|
| 274 | 0 |
logger.debug("Using service MenuBarService");
|
| 275 | 0 |
try
|
| 276 |
{
|
|
| 277 | 0 |
MenuBarService service = (MenuBarService) context.getService(this, this, MenuBarService.class, this, this); |
| 278 | 0 |
service.register(this);
|
| 279 |
} |
|
| 280 |
catch (Exception e)
|
|
| 281 |
{
|
|
| 282 | 0 |
logger.error("Error during utilisation of service MenuBarService (" + e.getMessage() + ")"); |
| 283 | 0 |
e.printStackTrace(); |
| 284 |
} |
|
| 285 |
} |
|
| 286 |
|
|
| 287 |
// Set up ToolBar
|
|
| 288 | 24 |
if ((context.hasService(ToolBarService.class)) && (((Boolean) this.getValue(TOOLBAR)).booleanValue())) |
| 289 |
{
|
|
| 290 | 0 |
logger.debug("Using service ToolBarService");
|
| 291 | 0 |
try
|
| 292 |
{
|
|
| 293 | 0 |
ToolBarService service = (ToolBarService) context.getService(this, this, ToolBarService.class, this, this); |
| 294 | 0 |
service.register(this);
|
| 295 |
} |
|
| 296 |
catch (Exception e)
|
|
| 297 |
{
|
|
| 298 | 0 |
logger.error("Error during utilisation of service ToolBarService (" + e.getMessage() + ")"); |
| 299 | 0 |
e.printStackTrace(); |
| 300 |
} |
|
| 301 |
} |
|
| 302 |
} |
|
| 303 |
|
|
| 304 |
|
|
| 305 |
/** Called when the CommandAction is removed from a BeanContext */
|
|
| 306 | 24 |
protected void releaseBeanContextResources() |
| 307 |
{
|
|
| 308 | 24 |
BeanContextServices context = (BeanContextServices) getBeanContext(); |
| 309 | 24 |
this.unregister(context);
|
| 310 | 24 |
super.releaseBeanContextResources();
|
| 311 |
} |
|
| 312 |
|
|
| 313 |
|
|
| 314 |
/**
|
|
| 315 |
* Description of the Method
|
|
| 316 |
*
|
|
| 317 |
* @param context Description of Parameter
|
|
| 318 |
*/
|
|
| 319 | 24 |
protected void unregister(BeanContextServices context) |
| 320 |
{
|
|
| 321 |
// Set up MenuBar
|
|
| 322 | 24 |
if ((context.hasService(MenuBarService.class)) && (this.getValue(MENU) != null) && (!"".equals(this.getValue(MENU)))) |
| 323 |
{
|
|
| 324 | 0 |
logger.debug("Using service MenuBarService");
|
| 325 | 0 |
try
|
| 326 |
{
|
|
| 327 | 0 |
MenuBarService service = (MenuBarService) context.getService(this, this, MenuBarService.class, this, this); |
| 328 | 0 |
service.unregister(this);
|
| 329 |
} |
|
| 330 |
catch (Exception e)
|
|
| 331 |
{
|
|
| 332 | 0 |
logger.error("Error during utilisation of service MenuBarService (" + e.getMessage() + ")"); |
| 333 | 0 |
e.printStackTrace(); |
| 334 |
} |
|
| 335 |
} |
|
| 336 |
|
|
| 337 |
// Set up ToolBar
|
|
| 338 | 24 |
if ((context.hasService(ToolBarService.class)) && (((Boolean) this.getValue(TOOLBAR)).booleanValue())) |
| 339 |
{
|
|
| 340 | 0 |
logger.debug("Using service ToolBarService");
|
| 341 | 0 |
try
|
| 342 |
{
|
|
| 343 | 0 |
ToolBarService service = (ToolBarService) context.getService(this, this, ToolBarService.class, this, this); |
| 344 | 0 |
service.unregister(this);
|
| 345 |
} |
|
| 346 |
catch (Exception e)
|
|
| 347 |
{
|
|
| 348 | 0 |
logger.error("Error during utilisation of service ToolBarService (" + e.getMessage() + ")"); |
| 349 | 0 |
e.printStackTrace(); |
| 350 |
} |
|
| 351 |
} |
|
| 352 |
} |
|
| 353 |
|
|
| 354 |
|
|
| 355 |
/**
|
|
| 356 |
* Returns the menu property
|
|
| 357 |
*
|
|
| 358 |
* @return The menu property
|
|
| 359 |
*/
|
|
| 360 | 24 |
public final String getMenu()
|
| 361 |
{
|
|
| 362 | 24 |
return (String) this.getValue(MENU); |
| 363 |
} |
|
| 364 |
|
|
| 365 |
|
|
| 366 |
/**
|
|
| 367 |
* Returns the menu layout property
|
|
| 368 |
*
|
|
| 369 |
* @return The menu layout property
|
|
| 370 |
*/
|
|
| 371 | 24 |
public final int getMenuLayout() |
| 372 |
{
|
|
| 373 | 24 |
return ((Integer) this.getValue(MENU_LAYOUT)).intValue(); |
| 374 |
} |
|
| 375 |
|
|
| 376 |
|
|
| 377 |
/**
|
|
| 378 |
* Returns the Resource Bundle used by this object
|
|
| 379 |
*
|
|
| 380 |
* @return The Resource Bundle
|
|
| 381 |
*/
|
|
| 382 | 464 |
public final ResourceBundle getResourceBundle()
|
| 383 |
{
|
|
| 384 | 464 |
return this.resource; |
| 385 |
} |
|
| 386 |
|
|
| 387 |
|
|
| 388 |
/**
|
|
| 389 |
* Returns the toolbar presence property
|
|
| 390 |
*
|
|
| 391 |
* @return The toolbar presence property
|
|
| 392 |
*/
|
|
| 393 | 24 |
public final boolean getToolBar() |
| 394 |
{
|
|
| 395 | 24 |
return ((Boolean) this.getValue(TOOLBAR)).booleanValue(); |
| 396 |
} |
|
| 397 |
|
|
| 398 |
|
|
| 399 |
/**
|
|
| 400 |
* Sets the icon attribute of the CommandAction object
|
|
| 401 |
*
|
|
| 402 |
* @param icon The new icon value
|
|
| 403 |
*/
|
|
| 404 | 304 |
public final void setIcon(String icon) |
| 405 |
{
|
|
| 406 | 304 |
if ((icon != null) && (!"".equals(icon))) |
| 407 |
{
|
|
| 408 | 304 |
try
|
| 409 |
{
|
|
| 410 | 304 |
Icon image = new ImageIcon(getClass().getResource(icon));
|
| 411 | 0 |
this.putValue(ICON, image);
|
| 412 |
} |
|
| 413 |
catch (Exception e)
|
|
| 414 |
{
|
|
| 415 |
} |
|
| 416 |
} |
|
| 417 |
} |
|
| 418 |
|
|
| 419 |
|
|
| 420 |
/**
|
|
| 421 |
* Constructor for the setMenu object
|
|
| 422 |
*
|
|
| 423 |
* @param menu Description of Parameter
|
|
| 424 |
*/
|
|
| 425 | 512 |
public final void setMenu(String menu) |
| 426 |
{
|
|
| 427 | 512 |
if ((menu != null) && (!"".equals(menu))) |
| 428 |
{
|
|
| 429 | 512 |
this.putValue(MENU, menu);
|
| 430 |
} |
|
| 431 |
} |
|
| 432 |
|
|
| 433 |
|
|
| 434 |
/**
|
|
| 435 |
* Sets the menuLayout attribute of the CommandAction object
|
|
| 436 |
*
|
|
| 437 |
* @param menuLayout The new menuLayout value
|
|
| 438 |
*/
|
|
| 439 | 48 |
public final void setMenuLayout(int menuLayout) |
| 440 |
{
|
|
| 441 | 48 |
this.putValue(MENU_LAYOUT, new Integer(menuLayout)); |
| 442 |
} |
|
| 443 |
|
|
| 444 |
|
|
| 445 |
/**
|
|
| 446 |
* Constructor for the setIcon object
|
|
| 447 |
*
|
|
| 448 |
* @param icon Description of Parameter
|
|
| 449 |
*/
|
|
| 450 | 304 |
public final void setSmallIcon(String icon) |
| 451 |
{
|
|
| 452 | 304 |
if ((icon != null) && (!"".equals(icon))) |
| 453 |
{
|
|
| 454 | 304 |
try
|
| 455 |
{
|
|
| 456 | 304 |
Icon image = new ImageIcon(getClass().getResource(icon));
|
| 457 | 0 |
this.putValue(Action.SMALL_ICON, image);
|
| 458 |
} |
|
| 459 |
catch (Exception e)
|
|
| 460 |
{
|
|
| 461 |
} |
|
| 462 |
} |
|
| 463 |
} |
|
| 464 |
|
|
| 465 |
|
|
| 466 |
/**
|
|
| 467 |
* Sets the toolBar attribute of the CommandAction object
|
|
| 468 |
*
|
|
| 469 |
* @param inToolBar The new toolBar value
|
|
| 470 |
*/
|
|
| 471 | 272 |
public final void setToolBar(boolean inToolBar) |
| 472 |
{
|
|
| 473 | 272 |
this.putValue(TOOLBAR, new Boolean(inToolBar)); |
| 474 |
} |
|
| 475 |
|
|
| 476 |
|
|
| 477 |
/**
|
|
| 478 |
* Returns the wrapped command object
|
|
| 479 |
*
|
|
| 480 |
* @return The command object
|
|
| 481 |
*/
|
|
| 482 | 24 |
protected final Command getCommand()
|
| 483 |
{
|
|
| 484 | 24 |
return this.command; |
| 485 |
} |
|
| 486 |
|
|
| 487 |
|
|
| 488 |
/**
|
|
| 489 |
* This method sets the action's command object.
|
|
| 490 |
*
|
|
| 491 |
* @param newValue the command for this action to act upon
|
|
| 492 |
*/
|
|
| 493 | 512 |
protected final void setCommand(Command newValue) |
| 494 |
{
|
|
| 495 | 512 |
this.command = newValue;
|
| 496 |
} |
|
| 497 |
|
|
| 498 |
|
|
| 499 |
/**
|
|
| 500 |
* Setter for the resourceBundle attribute
|
|
| 501 |
*
|
|
| 502 |
* @param newValue The new value for resourceBundle attribute
|
|
| 503 |
*/
|
|
| 504 | 512 |
protected final void setResourceBundle(ResourceBundle newValue) |
| 505 |
{
|
|
| 506 | 512 |
this.resource = newValue;
|
| 507 |
} |
|
| 508 |
} |
|
| 509 |
|
|
| 510 |
|
|
||||||||||