01: /*
02: * MenuItem.java
03: *
04: * Copyright (C) 1998-2003 Peter Graves
05: * $Id: MenuItem.java,v 1.4 2003/10/15 12:07:24 piso Exp $
06: *
07: * This program is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU General Public License
09: * as published by the Free Software Foundation; either version 2
10: * of the License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public License
18: * along with this program; if not, write to the Free Software
19: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20: */
21:
22: package org.armedbear.j;
23:
24: import java.awt.Color;
25: import java.awt.Dimension;
26: import java.awt.Font;
27: import java.awt.FontMetrics;
28: import java.awt.Graphics;
29: import java.awt.Insets;
30: import java.awt.event.KeyEvent;
31: import javax.swing.JMenuItem;
32: import javax.swing.MenuElement;
33: import javax.swing.MenuSelectionManager;
34: import javax.swing.UIManager;
35:
36: public final class MenuItem extends JMenuItem {
37: private static final Font acceleratorFont = UIManager
38: .getFont("MenuItem.acceleratorFont");
39: private static final Color acceleratorForeground = UIManager
40: .getColor("MenuItem.acceleratorForeground");
41: private static final Color acceleratorSelectionForeground = UIManager
42: .getColor("MenuItem.acceleratorSelectionForeground");
43: private static final Color disabledForeground = UIManager
44: .getColor("MenuItem.disabledForeground");
45:
46: private final String acceleratorText;
47:
48: public MenuItem(String label, String acceleratorText) {
49: super (label);
50: this .acceleratorText = acceleratorText;
51: }
52:
53: public Dimension getPreferredSize() {
54: Dimension d = super .getPreferredSize();
55: if (acceleratorText != null)
56: d.width += getToolkit().getFontMetrics(acceleratorFont)
57: .stringWidth(acceleratorText) + 30;
58: return d;
59: }
60:
61: // We paint our own menu items so the accelerator text will be consistent
62: // with our key map format.
63: public void paint(Graphics g) {
64: Display.setRenderingHints(g);
65: super .paint(g);
66: if (acceleratorText != null) {
67: g.setFont(acceleratorFont);
68: Color c;
69: if (isEnabled())
70: c = getModel().isArmed() ? acceleratorSelectionForeground
71: : acceleratorForeground;
72: else
73: c = disabledForeground;
74: g.setColor(c);
75: FontMetrics fm = g.getFontMetrics();
76: Insets insets = getInsets();
77: g.drawString(acceleratorText,
78: getWidth()
79: - (fm.stringWidth(acceleratorText)
80: + insets.right + insets.left),
81: getFont().getSize() + (insets.top - 1));
82: }
83: }
84:
85: private static final boolean consumeKeyEvent = Platform.isJava13()
86: || Platform.isJava140();
87:
88: public void processKeyEvent(KeyEvent e, MenuElement path[],
89: MenuSelectionManager manager) {
90: super.processKeyEvent(e, path, manager);
91: if (consumeKeyEvent)
92: if (Character.toUpperCase(e.getKeyChar()) == getMnemonic())
93: e.consume();
94: }
95: }
|