KickJava   Java API By Example, From Geeks To Geeks.

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


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