001: /*
002: * EnhancedMenuItem.java - Menu item with user-specified accelerator string
003: * :tabSize=8:indentSize=8:noTabs=false:
004: * :folding=explicit:collapseFolds=1:
005: *
006: * Copyright (C) 1999, 2003 Slava Pestov
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: */
022:
023: package org.gjt.sp.jedit.menu;
024:
025: //{{{ Imports
026: import javax.swing.*;
027: import java.awt.event.*;
028: import java.awt.*;
029: import org.gjt.sp.jedit.*;
030:
031: //}}}
032:
033: /**
034: * jEdit's custom menu item. It adds support for multi-key shortcuts.
035: */
036: public class EnhancedMenuItem extends JMenuItem {
037: //{{{ EnhancedMenuItem constructor
038: /**
039: * Creates a new menu item. Most plugins should call
040: * GUIUtilities.loadMenuItem() instead.
041: * @param label The menu item label
042: * @param action The edit action
043: * @param context An action context
044: * @since jEdit 4.2pre1
045: */
046: public EnhancedMenuItem(String label, String action,
047: ActionContext context) {
048: this .action = action;
049: this .shortcut = GUIUtilities.getShortcutLabel(action);
050: if (OperatingSystem.hasScreenMenuBar() && shortcut != null) {
051: setText(label + " (" + shortcut + ")");
052: shortcut = null;
053: } else
054: setText(label);
055:
056: if (action != null) {
057: setEnabled(true);
058: addActionListener(new EditAction.Wrapper(context, action));
059: addMouseListener(new MouseHandler());
060: } else
061: setEnabled(false);
062: } //}}}
063:
064: //{{{ getPreferredSize() method
065: public Dimension getPreferredSize() {
066: Dimension d = super .getPreferredSize();
067:
068: if (shortcut != null) {
069: d.width += (getFontMetrics(acceleratorFont).stringWidth(
070: shortcut) + 15);
071: }
072: return d;
073: } //}}}
074:
075: //{{{ paint() method
076: public void paint(Graphics g) {
077: super .paint(g);
078:
079: if (shortcut != null) {
080: g.setFont(acceleratorFont);
081: g
082: .setColor(getModel().isArmed() ? acceleratorSelectionForeground
083: : acceleratorForeground);
084: FontMetrics fm = g.getFontMetrics();
085: Insets insets = getInsets();
086: g.drawString(shortcut, getWidth()
087: - (fm.stringWidth(shortcut) + insets.right
088: + insets.left + 5), getFont().getSize()
089: + (insets.top - (OperatingSystem.isMacOSLF() ? 0
090: : 1))
091: /* XXX magic number */);
092: }
093: } //}}}
094:
095: //{{{ Package-private members
096: static Font acceleratorFont;
097: static Color acceleratorForeground;
098: static Color acceleratorSelectionForeground;
099: //}}}
100:
101: //{{{ Private members
102:
103: //{{{ Instance variables
104: private String shortcut;
105: private String action;
106: //}}}
107:
108: //{{{ Class initializer
109: static {
110: String shortcutFont;
111: if (OperatingSystem.isMacOSLF()) {
112: shortcutFont = "Lucida Grande";
113: } else {
114: shortcutFont = "Monospaced";
115: }
116:
117: acceleratorFont = UIManager.getFont("MenuItem.acceleratorFont");
118: if (acceleratorFont == null) {
119: acceleratorFont = new Font(shortcutFont, Font.PLAIN, 12);
120: }
121: acceleratorForeground = UIManager
122: .getColor("MenuItem.acceleratorForeground");
123: if (acceleratorForeground == null) {
124: acceleratorForeground = Color.black;
125: }
126:
127: acceleratorSelectionForeground = UIManager
128: .getColor("MenuItem.acceleratorSelectionForeground");
129: if (acceleratorSelectionForeground == null) {
130: acceleratorSelectionForeground = Color.black;
131: }
132: } //}}}
133:
134: //}}}
135:
136: //{{{ MouseHandler class
137: class MouseHandler extends MouseAdapter {
138: boolean msgSet = false;
139:
140: public void mouseReleased(MouseEvent evt) {
141: if (msgSet) {
142: GUIUtilities.getView((Component) evt.getSource())
143: .getStatus().setMessage(null);
144: msgSet = false;
145: }
146: }
147:
148: public void mouseEntered(MouseEvent evt) {
149: String msg = jEdit.getProperty(action + ".mouse-over");
150: if (msg != null) {
151: GUIUtilities.getView((Component) evt.getSource())
152: .getStatus().setMessage(msg);
153: msgSet = true;
154: }
155: }
156:
157: public void mouseExited(MouseEvent evt) {
158: if (msgSet) {
159: GUIUtilities.getView((Component) evt.getSource())
160: .getStatus().setMessage(null);
161: msgSet = false;
162: }
163: }
164: } //}}}
165: }
|