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.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 availableMenus = new HashMap(); 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")); while(servers.hasNext()) { Path thisServerPath = (Path)servers.next(); try { int syskey = thisServerPath.getSysKey(); String serverName = ((Property)Gateway.getStorage().get(syskey, ClusterStorage.PROPERTY+"/Name", null)).getValue(); String portStr = ((Property)Gateway.getStorage().get(syskey, 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().getProperty("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().getProperty("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) { } }