-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMenuParser.java
More file actions
73 lines (65 loc) · 3.24 KB
/
MenuParser.java
File metadata and controls
73 lines (65 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import je3.reflect.*;
import java.awt.event.*;
import java.util.MissingResourceException;
import javax.swing.*;
import java.util.StringTokenizer;
/**
* This class parses a JMenu or JPopupMenu from textual descriptions found in
* a GUIResourceBundle. The grammar is straightforward: the menu label
* followed by a colon and a list of menu items. Menu items that begin with
* a '>' character are submenus. Menu items that begin with a '-' character
* are separators. All other items are action names.
**/
public class MenuParser extends ResourceParser {
static final Class[ ] supportedTypes = new Class[ ] {
JMenu.class, JPopupMenu.class // This class handles two resource types
};
public Class[ ] getResourceTypes( ) { return supportedTypes; }
public Object parse(GUIResourceBundle bundle, String key, Class type) //throws MissingResourceException, MalformedResourceException
//throws MissingResourceException, MalformedResourceException
{
// Get the string value of the key
String menudef = bundle.getString(key);
// Break it up into words, ignoring whitespace, colons, and commas
StringTokenizer st = new StringTokenizer(menudef, " \t:,");
// The first word is the label of the menu
String menuLabel = st.nextToken( );
// Create either a JMenu or JPopupMenu
JMenu menu = null;
JPopupMenu popup = null;
if (type == JMenu.class) menu = new JMenu(menuLabel);
else popup = new JPopupMenu(menuLabel);
// Then loop through the rest of the words, creating a JMenuItem
// for each one. Accumulate these items in a list
while(st.hasMoreTokens( )) {
String item = st.nextToken( ); // the next word
char firstchar = item.charAt(0); // determines type of menu item
switch(firstchar) {
case '-': // words beginning with - add a separator to the menu
if (menu != null) menu.addSeparator( );
else popup.addSeparator( );
break;
case '>': // words beginning with > are submenu names
// strip off the > character, and recurse to parse the submenu
item = item.substring(1);
// Parse a submenu and add it to the list of items
JMenu submenu = (JMenu)parse(bundle, item, JMenu.class);
if (menu != null) menu.add(submenu);
else popup.add(submenu);
break;
case '!': // words beginning with ! are action names
item = item.substring(1); // strip off the ! character
/* falls through */ // fall through to the next case
default: // By default all other words are taken as action names
// Look up the named action and add it to the menu
Action action = (Action)bundle.getResource(item, Action.class);
if (menu != null) menu.add(action);
else popup.add(action);
break;
}
}
// Finally, return the menu or the popup menu
if (menu != null) return menu;
else return popup;
}
}