[Ejtools-cvs] CVS: libraries/adwt/src/main/net/sourceforge/ejtools/awt/action Command.java,NONE,1.1
Brought to you by:
letiemble
From: Laurent E. <let...@us...> - 2002-04-18 21:09:52
|
Update of /cvsroot/ejtools/libraries/adwt/src/main/net/sourceforge/ejtools/awt/action In directory usw-pr-cvs1:/tmp/cvs-serv9585 Added Files: Command.java CommandAction.java Log Message: Initial Import --- NEW FILE: Command.java --- /* * EJTools, the Enterprise Java Tools * * Distributable under LGPL license. * See terms of license at www.gnu.org. */ package net.sourceforge.ejtools.awt.action; /** * Interface needed to implement to Command Pattern. * * @author laurent * @created 29 décembre 2001 */ public interface Command { /** Method to implement with the action. */ public void execute(); } --- NEW FILE: CommandAction.java --- /* * EJTools, the Enterprise Java Tools * * Distributable under LGPL license. * See terms of license at www.gnu.org. */ package net.sourceforge.ejtools.awt.action; import java.awt.event.ActionEvent; import java.beans.PropertyChangeListener; import java.beans.beancontext.BeanContextChildSupport; import java.beans.beancontext.BeanContextServiceAvailableEvent; import java.beans.beancontext.BeanContextServiceRevokedEvent; import java.beans.beancontext.BeanContextServices; import java.util.ResourceBundle; import javax.swing.AbstractAction; import javax.swing.Action; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JMenuItem; import javax.swing.KeyStroke; import net.sourceforge.ejtools.awt.services.MenuBarService; import net.sourceforge.ejtools.awt.services.ToolBarService; import org.apache.log4j.Category; /** * Description of the Class * * @author letiembl * @created 2 novembre 2001 * @todo Javadoc to complete */ public class CommandAction extends BeanContextChildSupport implements Action { private static Category cat = Category.getInstance(CommandAction.class.getName()); /** Description of the Field */ protected boolean inToolBar = false; /** Description of the Field */ protected String menu = null; /** Description of the Field */ protected int menuMnemonic = 0; /** Description of the Field */ protected String menuAccelerator = null; /** Description of the Field */ protected int menuLayout = MenuBarService.NO_LAYOUT; /** Description of the Field */ protected String icon = null; /** Description of the Field */ protected AbstractAction action = null; /** Description of the Field */ protected Command command; /** Description of the Field */ protected JMenuItem menuitem; /** Description of the Field */ protected ResourceBundle res; /** * Constructor for the CommandAction object * * @param command Description of Parameter * @param name Description of Parameter * @param res Description of Parameter */ public CommandAction(Command command, ResourceBundle res, String name) { this.setCommand(command); this.setResourceBundle(res); this.action = new AbstractAction(res.getString(name)) { public void actionPerformed(ActionEvent e) { getCommand().execute(); } }; } /** * Sets the enabled attribute of the CommandAction object * * @param b The new enabled value */ public void setEnabled(boolean b) { this.action.setEnabled(b); } /** * Constructor for the setIcon object * * @param icon Description of Parameter */ public final void setIcon(String icon) { if ((icon != null) && (!"".equals(icon))) { try { Icon image = new ImageIcon(getClass().getResource(icon)); this.putValue(Action.SMALL_ICON, image); } catch (Exception e) { } } } /** * Constructor for the setMenu object * * @param menu Description of Parameter */ public final void setMenu(String menu) { if ((menu != null) && (!"".equals(menu))) { this.menu = menu; } } /** * Setter for the menuMnemonic attribute * * @param menuMnemonic The new value for menuMnemonic attribute */ public final void setMenuMnemonic(String menuMnemonic) { if ((menu != menuMnemonic) && (!"".equals(menuMnemonic))) { this.menuMnemonic = Integer.parseInt(res.getString(menuMnemonic)); } } /** * Setter for the menuAccelerator attribute * * @param menuAccelerator The new value for menuAccelerator attribute */ public final void setMenuAccelerator(String menuAccelerator) { if ((menu != menuAccelerator) && (!"".equals(menuAccelerator))) { this.menuAccelerator = res.getString(menuAccelerator); } } /** * Sets the menuLayout attribute of the CommandAction object * * @param menuLayout The new menuLayout value */ public final void setMenuLayout(int menuLayout) { this.menuLayout = menuLayout; } /** * Sets the toolBar attribute of the CommandAction object * * @param inToolBar The new toolBar value */ public final void setToolBar(boolean inToolBar) { this.inToolBar = inToolBar; } /** * Gets the value attribute of the CommandAction object * * @param key Description of Parameter * @return The value value */ public Object getValue(String key) { return this.action.getValue(key); } /** * Getter for the resourceBundle attribute * * @return The value of resourceBundle attribute */ public ResourceBundle getResourceBundle() { return this.res; } /** * Gets the enabled attribute of the CommandAction object * * @return The enabled value */ public boolean isEnabled() { return this.action.isEnabled(); } /** * Gets the menu attribute of the CommandAction object * * @return The menu value */ public final String getMenu() { return this.menu; } /** * Gets the menuLayout attribute of the CommandAction object * * @return The menuLayout value */ public final int getMenuLayout() { return this.menuLayout; } /** * Gets the toolbar attribute of the CommandAction object * * @return The toolbar value */ public final boolean getToolBar() { return this.inToolBar; } /** * Getter for the menuItem attribute * * @return The value */ public JMenuItem getMenuItem() { if (this.menuitem == null) { this.menuitem = new JMenuItem(this); if (this.menuMnemonic != 0) { this.menuitem.setMnemonic(this.menuMnemonic); } if (this.menuAccelerator != null) { this.menuitem.setAccelerator(KeyStroke.getKeyStroke(menuAccelerator)); } } return this.menuitem; } /** * Description of the Method * * @param key Description of Parameter * @param value Description of Parameter */ public void putValue(String key, Object value) { this.action.putValue(key, value); } /** * Adds a feature to the PropertyChangeListener attribute of the * CommandAction object * * @param listener The feature to be added to the PropertyChangeListener * attribute */ public void addPropertyChangeListener(PropertyChangeListener listener) { this.action.addPropertyChangeListener(listener); } /** * Description of the Method * * @param listener Description of Parameter */ public void removePropertyChangeListener(PropertyChangeListener listener) { this.action.removePropertyChangeListener(listener); } /** * Description of the Method * * @param bcsre Description of Parameter */ public void serviceRevoked(BeanContextServiceRevokedEvent bcsre) { } /** * actionPerformed is what executed the command. actionPerformed is called * whenever the action is acted upon. * * @param e the action event */ public void actionPerformed(ActionEvent e) { this.action.actionPerformed(e); } /** * Description of the Method * * @param bcsae Description of Parameter */ public void serviceAvailable(BeanContextServiceAvailableEvent bcsae) { } /** * This method sets the action's command object. * * @param newValue the command for this action to act upon */ protected final void setCommand(Command newValue) { this.command = newValue; } /** * Setter for the resourceBundle attribute * * @param newValue The new value for resourceBundle attribute */ protected final void setResourceBundle(ResourceBundle newValue) { this.res = newValue; } /** * This method retrieves the encapsulated command. * * @return Command */ protected final Command getCommand() { return this.command; } /** Description of the Method */ protected void initializeBeanContextResources() { BeanContextServices context = (BeanContextServices) getBeanContext(); this.register(context); } /** Description of the Method */ protected void releaseBeanContextResources() { BeanContextServices context = (BeanContextServices) getBeanContext(); this.unregister(context); } /** * Description of the Method * * @param context Description of Parameter */ protected void register(BeanContextServices context) { // Set up MenuBar if ((context.hasService(MenuBarService.class)) && (this.menu != null) && (!"".equals(this.menu))) { cat.debug("Using service MenuBarService..."); try { MenuBarService service = (MenuBarService) context.getService(this, this, MenuBarService.class, this, this); service.register(this); } catch (Exception e) { cat.error("Error during utilisation of service MenuBarService (" + e.getMessage() + ")"); e.printStackTrace(); } } // Set up ToolBar if ((context.hasService(ToolBarService.class)) && this.inToolBar) { cat.debug("Using service ToolBarService..."); try { ToolBarService service = (ToolBarService) context.getService(this, this, ToolBarService.class, this, this); service.register(this); } catch (Exception e) { cat.error("Error during utilisation of service ToolBarService (" + e.getMessage() + ")"); e.printStackTrace(); } } } /** * Description of the Method * * @param context Description of Parameter */ protected void unregister(BeanContextServices context) { // Set up MenuBar if ((context.hasService(MenuBarService.class)) && (this.menu != null) && (!"".equals(this.menu))) { cat.debug("Using service MenuBarService..."); try { MenuBarService service = (MenuBarService) context.getService(this, this, MenuBarService.class, this, this); service.unregister(this); } catch (Exception e) { cat.error("Error during utilisation of service MenuBarService (" + e.getMessage() + ")"); e.printStackTrace(); } } // Set up ToolBar if ((context.hasService(ToolBarService.class)) && inToolBar) { cat.debug("Using service ToolBarService..."); try { ToolBarService service = (ToolBarService) context.getService(this, this, ToolBarService.class, this, this); service.unregister(this); } catch (Exception e) { cat.error("Error during utilisation of service ToolBarService (" + e.getMessage() + ")"); e.printStackTrace(); } } } } |