Source code: org/gjt/sp/jedit/gui/EnhancedMenu.java
1 /*
2 * EnhancedMenu.java - jEdit menu
3 * :tabSize=8:indentSize=8:noTabs=false:
4 * :folding=explicit:collapseFolds=1:
5 *
6 * Copyright (C) 2001, 2003 Slava Pestov
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 */
22
23 package org.gjt.sp.jedit.gui;
24
25 //{{{ Imports
26 import javax.swing.event.*;
27 import javax.swing.*;
28 import java.util.StringTokenizer;
29 import org.gjt.sp.jedit.*;
30 //}}}
31
32 public class EnhancedMenu extends JMenu implements MenuListener
33 {
34 //{{{ EnhancedMenu constructor
35 public EnhancedMenu(String name)
36 {
37 this._name = name;
38 String label = jEdit.getProperty(name.concat(".label"));
39 if(label == null)
40 label = name;
41
42 char mnemonic;
43 int index = label.indexOf('$');
44 if(index != -1 && label.length() - index > 1)
45 {
46 mnemonic = Character.toLowerCase(label.charAt(index + 1));
47 label = label.substring(0,index).concat(label.substring(++index));
48 }
49 else
50 mnemonic = '\0';
51
52 setText(label);
53 if(!OperatingSystem.isMacOS())
54 setMnemonic(mnemonic);
55
56 addMenuListener(this);
57 //init();
58 } //}}}
59
60 //{{{ menuSelected() method
61 public void menuSelected(MenuEvent evt)
62 {
63 init();
64 } //}}}
65
66 public void menuDeselected(MenuEvent e) {}
67
68 public void menuCanceled(MenuEvent e) {}
69
70 //{{{ init() method
71 public void init()
72 {
73 if(initialized)
74 return;
75
76 initialized = true;
77
78 String menuItems = jEdit.getProperty(_name);
79 if(menuItems != null)
80 {
81 StringTokenizer st = new StringTokenizer(menuItems);
82 while(st.hasMoreTokens())
83 {
84 String menuItemName = st.nextToken();
85 if(menuItemName.equals("-"))
86 addSeparator();
87 else
88 {
89 if(menuItemName.startsWith("%"))
90 add(GUIUtilities.loadMenu(menuItemName.substring(1)));
91 else
92 add(GUIUtilities.loadMenuItem(menuItemName));
93 }
94 }
95 }
96 } //}}}
97
98 protected String _name;
99 protected boolean initialized;
100 }