01: /*
02: * Menu.java
03: *
04: * Copyright (C) 1998-2003 Peter Graves
05: * $Id: Menu.java,v 1.6 2003/06/13 00:56:55 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 javax.swing.JMenu;
25: import javax.swing.KeyStroke;
26:
27: public final class Menu extends JMenu implements Constants {
28: public Menu(String s) {
29: super (s);
30: }
31:
32: public Menu(String s, char mnemonic) {
33: super (s);
34: if (Editor.preferences().getBooleanProperty(
35: Property.USE_MENU_MNEMONICS))
36: setMnemonic(mnemonic);
37: addMenuListener(MenuBar.getListener());
38: }
39:
40: public void setPopupMenuVisible(boolean b) {
41: if (b) {
42: final Editor editor = Editor.currentEditor();
43: editor.getMode().populateMenu(editor, this );
44: }
45: super .setPopupMenuVisible(b);
46: if (!b)
47: removeAll();
48: }
49:
50: public MenuItem add(Editor editor, String label, char mnemonic,
51: String command, boolean enabled) {
52: Object[] values = editor.getKeyMapping(command);
53: Debug.assertTrue(values != null);
54: Debug.assertTrue(values.length == 2);
55: KeyMapping mapping = (KeyMapping) values[0];
56: String keyText = mapping != null ? mapping.getKeyText() : "";
57: if (keyText.length() == 3) {
58: if (keyText.charAt(0) == '\'' && keyText.charAt(2) == '\'')
59: keyText = keyText.substring(1, 2).toUpperCase(); // 'a' => A
60: }
61: MenuItem menuItem = new MenuItem(label, keyText);
62: if (mnemonic != '\0')
63: menuItem.setMnemonic(mnemonic);
64: menuItem.setActionCommand(command);
65: menuItem.addActionListener(editor.getDispatcher());
66: if (!enabled)
67: menuItem.setEnabled(false);
68: add(menuItem);
69: return menuItem;
70: }
71:
72: public MenuItem add(Editor editor, String label, char mnemonic,
73: String command) {
74: return add(editor, label, mnemonic, command, true);
75: }
76: }
|