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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
|
package com.c2kernel.gui;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.event.ItemListener;
import java.util.HashMap;
import java.util.Iterator;
import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.Icon;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JDialog;
import javax.swing.JEditorPane;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.text.html.HTMLEditorKit;
import com.c2kernel.common.ObjectNotFoundException;
import com.c2kernel.lookup.DomainPath;
import com.c2kernel.lookup.ItemPath;
import com.c2kernel.lookup.Path;
import com.c2kernel.persistency.ClusterStorage;
import com.c2kernel.process.Gateway;
import com.c2kernel.property.Property;
import com.c2kernel.utils.FileStringUtility;
import com.c2kernel.utils.Language;
import com.c2kernel.utils.Logger;
/**
* @version $Revision: 1.47 $ $Date: 2006/03/03 13:52:21 $
* @author $Author: abranson $
*/
public class MenuBuilder extends JMenuBar implements ActionListener, ItemListener, HyperlinkListener
{
protected UIManager.LookAndFeelInfo[] availableViews = UIManager.getInstalledLookAndFeels();
protected MainFrame myParentFrame;
protected JMenu fileMenu;
protected JMenu consoleMenu;
protected JMenu styleMenu;
protected JMenu prefMenu;
protected JMenu helpMenu;
protected HashMap<String, JMenu> availableMenus = new HashMap<String, JMenu>();
public MenuBuilder()
{}
/** Creates new DynamicMenuBuilder */
public MenuBuilder(MainFrame parentFrame)
{
myParentFrame = parentFrame;
fileMenu = new JMenu(Language.translate("File"));
consoleMenu = new JMenu(Language.translate("Console"));
styleMenu = new JMenu(Language.translate("Style"));
prefMenu = new JMenu("Preferences");
helpMenu = new JMenu(Language.translate("Help"));
availableMenus.put("file", fileMenu);
availableMenus.put("console", consoleMenu);
availableMenus.put("preferences", prefMenu);
availableMenus.put("style", styleMenu);
availableMenus.put("help", helpMenu);
addMenuItem(Language.translate("Close All"), "file", null, 0);
addMenuItem(Language.translate("Close Others"), "file", null, 0);
fileMenu.insertSeparator(2);
addMenuItem(Language.translate("Exit"), "file", null, 0);
addMenuItem(Language.translate("Local console"), "console", null, 0);
consoleMenu.insertSeparator(5);
addServerConsoles();
ButtonGroup styleButtonGroup = new ButtonGroup();
for (LookAndFeelInfo availableView : availableViews)
addMenuItem(availableView.getName(), "style", styleButtonGroup, 0);
addMenuItem(Language.translate("Tree Browser"), "preferences", null, MainFrame.getPref("ShowTree", "true").equals("true")?2:1);
addMenuItem(Language.translate("Outcome Field Help"), "preferences", null, MainFrame.getPref("ShowHelp", "true").equals("true")?2:1);
addMenuItem(Language.translate("Graph Properties"), "preferences", null, MainFrame.getPref("ShowProps", "true").equals("true")?2:1);
addMenuItem(Language.translate("About"), "help", null, 0);
add(fileMenu);
add(consoleMenu);
add(styleMenu);
add(prefMenu);
add(helpMenu);
}
/**
*
*/
private void addServerConsoles() {
Iterator<?> servers = Gateway.getLookup().search(new DomainPath("/servers"), new Property("Type", "Server", false));
while(servers.hasNext()) {
Path thisServerPath = (Path)servers.next();
try {
ItemPath serverItemPath = thisServerPath.getItemPath();
String serverName = ((Property)Gateway.getStorage().get(serverItemPath, ClusterStorage.PROPERTY+"/Name", null)).getValue();
String portStr = ((Property)Gateway.getStorage().get(serverItemPath, ClusterStorage.PROPERTY+"/ConsolePort", null)).getValue();
addMenuItem(serverName+":"+portStr, "console", null, 0);
} catch (Exception ex) {
Logger.error("Exception retrieving proxy server connection data for "+thisServerPath);
Logger.error(ex);
}
}
}
/**
* Adds a menu item to a menu. Adds an action listener to the menu item.
*/
public void addMenuItem(String itemName, String menuName, ButtonGroup bg, int checkBox)
{
//checks to see if the menu to add the item to exists
if (availableMenus.containsKey(menuName))
{
JMenuItem myItem = new JMenuItem(itemName);
if (bg != null)
{
//if the menu item equals the current style, set it selected
myItem = new JRadioButtonMenuItem(itemName, UIManager.getLookAndFeel().getName().equals(itemName));
bg.add(myItem);
}
if (checkBox != 0)
{
myItem = new JCheckBoxMenuItem(itemName, checkBox == 2);
}
myItem.addActionListener(this);
JMenu myMenu = availableMenus.get(menuName);
myMenu.add(myItem);
}
}
//checks to see if the event dispatched is one of the
//styles that belong to the UIManager
public int isStyleChange(String style)
{
for (int i = 0; i < availableViews.length; i++)
{
if (style.equals(availableViews[i].getName()))
return i;
}
return -1;
}
//listens for events performed on the menu items
@Override
public void actionPerformed(java.awt.event.ActionEvent e)
{
String s = e.getActionCommand();
int i = isStyleChange(s);
if (s.equals("Close All") || s.equals("Close Others")) {
MainFrame.myDesktopManager.closeAll(s.equals("Close Others"));
}
else if (s.equals(Language.translate("Exit")))
myParentFrame.exitForm();
else if (s.equals(Language.translate("About")))
showAboutWindow();
else if (i >= 0)
{
try
{
UIManager.setLookAndFeel(availableViews[i].getClassName());
SwingUtilities.updateComponentTreeUI(myParentFrame);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
else if (s.equals(Language.translate("Tree Browser")))
{
myParentFrame.toggleTree();
}
else if (s.indexOf(":")>0) { // server console
try
{
String[] serverDetails = s.split(":");
new Console(serverDetails[0], Integer.parseInt(serverDetails[1])).setVisible(true);
}
catch (Exception ex)
{
Logger.error(ex);
}
}
else if (s.equals(Language.translate("Local console"))) {
try
{
new Console("localhost", Logger.getConsolePort()).setVisible(true);
}
catch (Exception ex)
{
Logger.error(ex);
}
}
else if (s.equals(Language.translate("Outcome Field Help"))) {
MainFrame.setPref("ShowHelp", String.valueOf(!MainFrame.getPref("ShowHelp", "true").equals("true")));
}
else if (s.equals(Language.translate("Graph Properties"))) {
MainFrame.setPref("ShowProps", String.valueOf(!MainFrame.getPref("ShowProps", "true").equals("true")));
}
else
Logger.msg(1, "MenuBuilder.actionPerformed() - No action associated with the event received");
}
//constructs an about dialog
public void showAboutWindow()
{
JOptionPane myPane = new JOptionPane();
Box about = Box.createVerticalBox();
String aboutInfo;
try
{
aboutInfo = FileStringUtility.file2String(Gateway.getProperties().getString("about"));
}
catch (Exception e)
{
aboutInfo = Language.translate("Cristal 2 Itembrowser");
}
JLabel title = new JLabel(aboutInfo);
about.add(title);
about.add(new JLabel("Kernel version: "+Gateway.getKernelVersion()));
about.add(new JLabel("Modules loaded: "+Gateway.getModuleManager().getModuleVersions()));
// get license info
StringBuffer lictxt = new StringBuffer();
try {
lictxt.append(Gateway.getResource().getTextResource(null, "textFiles/license.html"));
} catch (ObjectNotFoundException e) { } // no kernel license found
for (String ns : Gateway.getResource().getModuleBaseURLs().keySet()) {
String domlictxt;
try {
domlictxt = Gateway.getResource().getTextResource(ns, "license.html");
lictxt.append(domlictxt).append("\n");
} catch (ObjectNotFoundException e) { }
}
JEditorPane license = new JEditorPane();
license.setEditable(false);
license.setEditorKit(new HTMLEditorKit());
license.setContentType("text/html");
license.addHyperlinkListener(this);
license.setText(lictxt.toString());
JScrollPane scroll = new JScrollPane(license);
scroll.setPreferredSize(new Dimension(300,200));
license.setCaretPosition(0);
about.add(scroll);
myPane.setMessage(about);
myPane.setMessageType(JOptionPane.INFORMATION_MESSAGE);
JDialog dialog = myPane.createDialog(null, Language.translate("About"));
dialog.setResizable(false);
Icon icon = ImageLoader.findImage(Gateway.getProperties().getString("banner"));
myPane.setIcon(icon);
dialog.pack();
dialog.setVisible(true);
}
@Override
public void hyperlinkUpdate(HyperlinkEvent e) {
try {
if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler "+e.getURL().toString());
} catch (Exception ex) {
MainFrame.exceptionDialog(ex);
}
}
@Override
public void itemStateChanged(java.awt.event.ItemEvent e)
{
}
}
|