001: package abbot.editor.actions;
002:
003: import java.awt.event.*;
004: import java.net.URL;
005:
006: import javax.swing.*;
007:
008: import abbot.Log;
009: import abbot.Platform;
010: import abbot.editor.*;
011: import abbot.editor.widgets.*;
012: import abbot.i18n.Strings;
013:
014: /** Encapsulate GUI attributes for an editor action. */
015:
016: public abstract class EditorAction extends AbstractAction implements
017: EditorConstants {
018:
019: /** Key to refer to the action in ActionMaps; <strong>not</strong> the
020: same as the NAME property which is typically used for labels.
021: */
022: public static final String ACTION_KEY = "action-key";
023: public static final String LARGE_ICON = "large-icon";
024: public static final String MNEMONIC_INDEX = "mnemonic-index";
025:
026: public EditorAction(String actionKey) {
027: super (actionKey);
028: putValue(ACTION_KEY, actionKey);
029:
030: String key = ACTION_PREFIX + actionKey;
031:
032: Mnemonic mnemonic = Mnemonic.getMnemonic(Strings.get(key));
033: mnemonic.setMnemonic(this );
034: if (mnemonic.index != -1)
035: putValue(MNEMONIC_INDEX, new Integer(mnemonic.index));
036: // Check deprecated usage
037: if (mnemonic.keycode == KeyEvent.VK_UNDEFINED) {
038: int mn = getMnemonic(key);
039: if (mn != KeyEvent.VK_UNDEFINED)
040: putValue(MNEMONIC_KEY, new Integer(mn));
041: }
042:
043: String desc = Strings.get(key + ".desc", true);
044: if (!"".equals(desc) && desc != null) {
045: putValue(SHORT_DESCRIPTION, TextFormat.tooltip(desc));
046: }
047: String longDesc = Strings.get(key + ".ldesc", true);
048: if (!"".equals(longDesc) && longDesc != null) {
049: putValue(LONG_DESCRIPTION, TextFormat.tooltip(longDesc));
050: } else {
051: putValue(LONG_DESCRIPTION, getValue(SHORT_DESCRIPTION));
052: }
053:
054: String iconName = Strings.get(key + ".icon", true);
055: if (!"".equals(iconName) && iconName != null) {
056: putValue(SMALL_ICON, getIcon(iconName, 16));
057: putValue(LARGE_ICON, getIcon(iconName, 24));
058: }
059:
060: String accelerator = Strings.get(key + ".acc", true);
061: if (!"".equals(accelerator) && accelerator != null) {
062: accelerator = abbot.tester.AWTConstants.MENU_SHORTCUT_STRING
063: + accelerator;
064: try {
065: // In case the accelerator given is garbage...
066: putValue(ACCELERATOR_KEY, KeyStroke
067: .getKeyStroke(accelerator));
068: } catch (Exception e) {
069: Log.warn("Bad accelerator '" + accelerator + "': " + e);
070: }
071: }
072: }
073:
074: /** @deprecated Encode the mnemonic into the localized label instead. */
075: public static int getMnemonic(String key) {
076: int code = KeyEvent.VK_UNDEFINED;
077: // No (visible) mnemonics on OSX
078: if (Platform.isOSX())
079: return code;
080:
081: String mnemonic = Strings.get(key + ".mn", true);
082: if (!"".equals(mnemonic) && mnemonic != null) {
083: if (!mnemonic.startsWith("VK_")) {
084: mnemonic = "VK_" + mnemonic;
085: }
086: try {
087: code = abbot.util.AWT.getKeyCode(mnemonic);
088: } catch (IllegalArgumentException e) {
089: String msg = Strings.get("editor.bad_mnemonic",
090: new Object[] { mnemonic, key + ".mn",
091: java.util.Locale.getDefault(),
092: e.toString() });
093: // TODO: format this into an email message
094: Log.warn(msg);
095: }
096: }
097: return code;
098: }
099:
100: /**
101: * Returns the Icon associated with the given name from the available
102: * resources.
103: *
104: * @param name Base name of the icon file e.g., help for help16.gif
105: * @param size Size in pixels of the icon from the filename, or zero if
106: * none, e.g. 16 for help16.gif or 0 for help.gif.
107: * @return an ImageIcon or null if no corresponding icon resource is found.
108: */
109: private ImageIcon getIcon(String name, int size) {
110: String ABBOT_IMAGE_DIR = "/abbot/editor/icons/";
111: String base = ABBOT_IMAGE_DIR + name;
112: URL url = getClass().getResource(base + ".gif");
113: if (url == null) {
114: url = getClass().getResource(base + size + ".gif");
115: }
116: ImageIcon icon = url != null ? new ImageIcon(url) : null;
117: return icon;
118: }
119: }
|