package edu.princeton.swing; import javax.swing.*; /** * AbstractAction2 extends action to provide convenient get/set methods as well as a method to * create a JMenuItem from this action. * * @author btsang * @version 7.1 */ public abstract class AbstractAction2 extends AbstractAction { /** * The key used for storing the name for the action, used for a menu or button. */ public static final String NAME_KEY = NAME; /** * The key used for storing a small icon for the action, used for toolbar buttons. */ public static final String SMALL_ICON_KEY = SMALL_ICON; /** * The key used for storing a short description for the action, used for tooltip text. */ public static final String SHORT_DESCRIPTION_KEY = SHORT_DESCRIPTION; /** * The key used for storing a longer description for the action, could be used for * context-sensitive help. */ public static final String LONG_DESCRIPTION_KEY = LONG_DESCRIPTION; /** * The key used for storing a random object containing extraneous information. */ public static final String EXTRA_INFO_KEY = "extraInfo"; /** * Creates a new AbstractAction2. */ public AbstractAction2() { super(); } /** * Creates a new AbstractAction2. */ public AbstractAction2(String name) { super(name); } /** * Creates a new AbstractAction2. */ public AbstractAction2(String name, Icon icon) { super(name, icon); } /** * Gets the name of this Action. A value of null indicates that this Action * does not have a name. * * @return The name for this Action. */ public String getName() { return (String)getValue(NAME_KEY); } /** * Sets the name of this Action. A value of null indicates that this Action * should not have a name. * * @param name The name of this Action. */ public void setName(String name) { putValue(NAME_KEY, name); } /** * Gets the small icon for this Action. A value of null indicates that this Action * does not have a small icon. * * @return The small icon for this Action. */ public Icon getSmallIcon() { return (Icon)getValue(SMALL_ICON_KEY); } /** * Sets the small icon for this Action. A value of null indicates that this Action * should not have a small icon. * * @param icon The small icon for this Action. */ public void setSmallIcon(Icon icon) { putValue(SMALL_ICON_KEY, icon); } /** * Gets the short description for this Action. A value of null indicates that this Action * does not have a short description. * * @return The short description for this Action. */ public String getShortDescription() { return (String)getValue(SHORT_DESCRIPTION_KEY); } /** * Sets the short description for this Action. A value of null indicates that this Action * should not have a short description. * * @param description The short description for this Action. */ public void setShortDescription(String description) { putValue(SHORT_DESCRIPTION_KEY, description); } /** * Gets the long description for this Action. A value of null indicates that this Action * does not have a long description. * * @return The long description for this Action. */ public String getLongDescription() { return (String)getValue(LONG_DESCRIPTION_KEY); } /** * Sets the long description for this Action. A value of null indicates that this Action * should not have a long description. * * @param description The long description for this Action. */ public void setLongDescription(String description) { putValue(LONG_DESCRIPTION_KEY, description); } /** * Gets the mnemonic key for this Action. A value of 0 indicates that this Action does not * have a mnemonic key. * * @return The mnemonic key for this Action. */ public int getMnemonic() { Object value = getValue(MNEMONIC_KEY); if (value == null) return 0; else return ((Integer)value).intValue(); } /** * Sets the mnemonic key for this Action. A value of 0 indicates that this Action should not * have a mnemonic key. * * @param mnemonic The mnemonic for this action (This should be one of the KeyEvent.VK_x * constants). */ public void setMnemonic(int mnemonic) { putValue( MNEMONIC_KEY, (mnemonic == 0)?null:new Integer(mnemonic) ); } /** * Gets the accelerator keystroke for this Action. A value of null indicates that this Action * does not have an accelerator keystroke. * * @return The accelerator keystroke for this Action. */ public KeyStroke getAccelerator() { return (KeyStroke)getValue(ACCELERATOR_KEY); } /** * Sets the accelerator keystroke for this Action. A value of null indicates that this Action * should not have an accelerator keystroke. * * @param accelerator The accelerator for this Action. */ public void setAccelerator(KeyStroke accelerator) { putValue(ACCELERATOR_KEY, accelerator); } /** * Gets the action command for this Action. A value of null indicates that this Action * does not have an action command. * * @return The action command for this Action. */ public String getActionCommand() { return (String)getValue(ACTION_COMMAND_KEY); } /** * Sets the action command for this Action. A value of null indicates that this Action * should not have an action command. * * @param command The action command for this Action. */ public void setActionCommand(String command) { putValue(ACTION_COMMAND_KEY, command); } /** * Gets the extra info for this Action. A value of null indicates that this Action * does not have any extra info. * * @return The extra info for this Action. */ public Object getExtraInfo() { return (Object)getValue(EXTRA_INFO_KEY); } /** * Sets the extra info for this Action. A value of null indicates that this Action * should not have any extra info. * * @param extraInfo The extra info for this Action. */ public void setExtraInfo(Object extraInfo) { putValue(EXTRA_INFO_KEY, extraInfo); } /** * Creates a new JMenuItem for this Action. This method ensures that the JMenuItem has the * correct accelerator (the JVM for OS X has problems with this). */ public JMenuItem createMenuItem() { JMenuItem menuItem = new JMenuItem(this); KeyStroke accelerator = getAccelerator(); if (accelerator != null) menuItem.setAccelerator(accelerator); return menuItem; } }