KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > MenuItem


1 /*
2  * MenuItem.java
3  *
4  * Copyright (C) 1998-2003 Peter Graves
5  * $Id: MenuItem.java,v 1.4 2003/10/15 12:07:24 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * 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 JavaDoc;
25 import java.awt.Dimension JavaDoc;
26 import java.awt.Font JavaDoc;
27 import java.awt.FontMetrics JavaDoc;
28 import java.awt.Graphics JavaDoc;
29 import java.awt.Insets JavaDoc;
30 import java.awt.event.KeyEvent JavaDoc;
31 import javax.swing.JMenuItem JavaDoc;
32 import javax.swing.MenuElement JavaDoc;
33 import javax.swing.MenuSelectionManager JavaDoc;
34 import javax.swing.UIManager JavaDoc;
35
36 public final class MenuItem extends JMenuItem JavaDoc
37 {
38     private static final Font JavaDoc acceleratorFont =
39         UIManager.getFont("MenuItem.acceleratorFont");
40     private static final Color JavaDoc acceleratorForeground =
41         UIManager.getColor("MenuItem.acceleratorForeground");
42     private static final Color JavaDoc acceleratorSelectionForeground =
43         UIManager.getColor("MenuItem.acceleratorSelectionForeground");
44     private static final Color JavaDoc disabledForeground =
45         UIManager.getColor("MenuItem.disabledForeground");
46
47     private final String JavaDoc acceleratorText;
48
49     public MenuItem(String JavaDoc label, String JavaDoc acceleratorText)
50     {
51         super(label);
52         this.acceleratorText = acceleratorText;
53     }
54
55     public Dimension JavaDoc getPreferredSize()
56     {
57         Dimension JavaDoc d = super.getPreferredSize();
58         if (acceleratorText != null)
59             d.width += getToolkit().getFontMetrics(acceleratorFont).stringWidth(acceleratorText) + 30;
60         return d;
61     }
62
63     // We paint our own menu items so the accelerator text will be consistent
64
// with our key map format.
65
public void paint(Graphics JavaDoc g)
66     {
67         Display.setRenderingHints(g);
68         super.paint(g);
69         if (acceleratorText != null) {
70             g.setFont(acceleratorFont);
71             Color JavaDoc c;
72             if (isEnabled())
73                 c = getModel().isArmed() ? acceleratorSelectionForeground : acceleratorForeground;
74             else
75                 c = disabledForeground;
76             g.setColor(c);
77             FontMetrics JavaDoc fm = g.getFontMetrics();
78             Insets JavaDoc insets = getInsets();
79             g.drawString(acceleratorText,
80                          getWidth() - (fm.stringWidth(acceleratorText) + insets.right + insets.left),
81                          getFont().getSize() + (insets.top - 1));
82         }
83     }
84
85     private static final boolean consumeKeyEvent =
86         Platform.isJava13() || Platform.isJava140();
87
88     public void processKeyEvent(KeyEvent JavaDoc e, MenuElement JavaDoc path[], MenuSelectionManager JavaDoc manager)
89     {
90         super.processKeyEvent(e, path, manager);
91         if (consumeKeyEvent)
92             if (Character.toUpperCase(e.getKeyChar()) == getMnemonic())
93                 e.consume();
94     }
95 }
96
Popular Tags