MenuProperties.java
2.76 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package com.sitech.ismp.ice.util;
import java.awt.event.ActionListener;
import javax.swing.JComponent;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
public class MenuProperties extends Object {
static public JPopupMenu loadPopupMenu(String menuPropertyName,
ActionListener listener) {
JPopupMenu popup = new JPopupMenu();
MenuProperties.addMenuItems(popup, menuPropertyName, listener);
return popup;
}
static public void addGenericItem(JComponent menu, JComponent item) {
if (menu instanceof JMenu) {
JMenu jm = (JMenu) menu;
if (item == null)
jm.addSeparator();
else
jm.add(item);
} else {
JPopupMenu jp = (JPopupMenu) menu;
if (item == null)
jp.addSeparator();
else
jp.add(item);
}
}
static public void addMenuItems(JComponent menu, String menuPropertyName,
ActionListener listener) {
String[] itemList;
String itemString;
String menuString;
String itemNameStr;
JMenuItem mItem;
menuString = UserProperties.getProperty("menu." + menuPropertyName,
null);
if (menuString == null) {
ICETracer.traceWithStack("Menu definition property '"
+ menuPropertyName + "' is not defined.");
return;
}
itemList = StringUtilities.splitString(menuString, ":");
if (itemList != null) {
for (int iIdx = 0; iIdx < itemList.length; ++iIdx) {
itemNameStr = "item." + menuPropertyName + "." + itemList[iIdx];
itemString = UserProperties.getProperty(itemNameStr, null);
if (itemString == null) {
ICETracer.traceWithStack("Menu definition '"
+ menuPropertyName
+ "' is missing item definition property '"
+ itemNameStr + "'.");
} else {
int colonIdx = itemString.indexOf(':');
if (itemString.equals("-")) {
MenuProperties.addGenericItem(menu, null);
} else if (colonIdx < 0) {
ICETracer.traceWithStack("Menu '" + menuPropertyName
+ "' Item '" + itemNameStr
+ "' has invalid definition.");
} else {
String title = itemString.substring(0, colonIdx);
String command = itemString.substring(colonIdx + 1);
if (command.equals("@")) {
JMenu subMenu = new JMenu(title);
String subMenuName = menuPropertyName + "."
+ itemList[iIdx];
MenuProperties.addMenuItems(subMenu, subMenuName,
listener);
MenuProperties.addGenericItem(menu, subMenu);
} else if (title.equals("-")) {
MenuProperties.addGenericItem(menu, null);
} else {
mItem = new JMenuItem(title);
if (listener != null) {
mItem.addActionListener(listener);
mItem.setActionCommand(command);
}
MenuProperties.addGenericItem(menu, mItem);
}
}
} // itemString != null
} // foreach item
} // itemList != null
}
}