package com.c2kernel.gui; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Point; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.net.URL; import java.util.Properties; import java.util.StringTokenizer; import javax.swing.BorderFactory; import javax.swing.ImageIcon; import javax.swing.JComboBox; import javax.swing.JFileChooser; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JSplitPane; import javax.swing.SwingUtilities; import javax.swing.UIManager; import com.c2kernel.entity.proxy.AgentProxy; import com.c2kernel.gui.data.Node; import com.c2kernel.gui.data.NodeContext; import com.c2kernel.gui.tabs.execution.DefaultExecutor; import com.c2kernel.gui.tabs.execution.Executor; import com.c2kernel.lookup.DomainPath; import com.c2kernel.process.Gateway; import com.c2kernel.utils.Language; import com.c2kernel.utils.Logger; import com.c2kernel.utils.Resource; /** * @version $Revision: 1.83 $ $Date: 2005/09/12 14:56:19 $ * @author $Author: abranson $ */ public class MainFrame extends javax.swing.JFrame { public static TreeBrowser treeBrowser; public static EntityTabManager myDesktopManager; public static EntityFinder itemFinder; protected static Node userNode = null; protected MenuBuilder menuBuilder; protected org.omg.CORBA.ORB orb; public static Properties prefs = new Properties(); public static JLabel status = new JLabel(); public String logoURL; public static AgentProxy userAgent; protected JSplitPane splitPane; public static boolean isAdmin; int splitPanePos; public static final JFileChooser xmlChooser; static { xmlChooser = new JFileChooser(); xmlChooser.addChoosableFileFilter( new javax.swing.filechooser.FileFilter() { @Override public String getDescription() { return "XML Files"; } @Override public boolean accept(File f) { if (f.isDirectory() || (f.isFile() && f.getName().endsWith(".xml"))) { return true; } return false; } }); } /** Creates new gui client for Cristal2 */ public MainFrame() { // Load gui preferences try { FileInputStream prefsfile = new FileInputStream("cristal.preferences"); prefs.load(prefsfile); prefsfile.close(); } catch (IOException e) { Logger.msg(2, "Creating new preference file"); } // set look & feel from pref try { String lf = getPref("Style", null); if (lf == null) lf = UIManager.getCrossPlatformLookAndFeelClassName(); UIManager.setLookAndFeel(lf); SwingUtilities.updateComponentTreeUI(this); } catch (Exception e) { e.printStackTrace(); } } public void showLogin() { // Log in logoURL = Gateway.getProperty("Logo"); URL pictureUrl; String bottomMessage = Language.translate("Please enter username & password"); ImageIcon imageHolder = new ImageIcon(""); try { pictureUrl = new URL(logoURL); imageHolder = new ImageIcon(pictureUrl); } catch (java.net.MalformedURLException m) { imageHolder = Resource.getImageResource(logoURL); } LoginBox login = new LoginBox( 5, Gateway.getProperty("Name"), getPref("lastUser."+Gateway.getCentreId(), null), bottomMessage, imageHolder, this); login.setVisible(true); } public void mainFrameShow() { prefs.setProperty("lastUser."+Gateway.getCentreId(), userAgent.getName()); isAdmin = userAgent.getPath().hasRole("Admin"); GridBagLayout gridbag = new GridBagLayout(); getContentPane().setLayout(gridbag); this.setTitle( userAgent.getName()+"@"+Gateway.getProperty("Name") + " - " + Language.translate("Cristal 2")); String iconFile = Gateway.getProperty("AppIcon"); if (iconFile != null) this.setIconImage(Resource.getImageResource(iconFile).getImage()); //preload loading image Resource.getImageResource("loading.gif"); // close listener addWindowListener(new java.awt.event.WindowAdapter() { @Override public void windowClosing(java.awt.event.WindowEvent evt) { exitForm(); } }); // initialise the desktop manager myDesktopManager = new EntityTabManager(); //get the menu bar and add it to the frame menuBuilder = new MenuBuilder(this); setJMenuBar(menuBuilder); // set the menu builder in the window manager myDesktopManager.setMenuBuilder(menuBuilder); userNode = new NodeContext(new DomainPath(""), myDesktopManager); treeBrowser = new TreeBrowser(myDesktopManager, userNode); treeBrowser.setVisible(getPref("ShowTree", "true").equals("true")); // add search box itemFinder = new EntityFinder(); GridBagConstraints c = new GridBagConstraints(); c.gridx = 0; c.gridy = 0; c.weightx = 1.0; c.weighty = 0.0; c.fill = GridBagConstraints.HORIZONTAL; gridbag.setConstraints(itemFinder, c); getContentPane().add(itemFinder); // register the browser as the key consumer itemFinder.setDefaultConsumer(treeBrowser); c.gridy++; c.weightx = 1.0; c.weighty = 1.0; c.fill = GridBagConstraints.BOTH; gridbag.setConstraints(getSplitPanel(), c); getContentPane().add(getSplitPanel()); // setup status bar status.setText("Cristal 2"); status.setFont( new Font("SansSerif", Font.PLAIN, status.getFont().getSize())); JPanel statusPanel = new JPanel(); statusPanel.setLayout(new BorderLayout()); statusPanel.setBorder(BorderFactory.createLoweredBevelBorder()); status.setForeground(Color.black); statusPanel.add(status); c.gridy++; c.weighty = 0.0; gridbag.setConstraints(statusPanel, c); getContentPane().add(statusPanel); pack(); String paneSize = getPref("WindowSize", null); if (paneSize != null) { StringTokenizer tok = new StringTokenizer(paneSize, ","); Dimension window = new Dimension(); window.setSize( Integer.parseInt(tok.nextToken()), Integer.parseInt(tok.nextToken())); this.setSize(window); } String panePos = getPref("WindowPosition", null); if (panePos != null) { StringTokenizer tok = new StringTokenizer(panePos, ","); Point window = new Point( Integer.parseInt(tok.nextToken()), Integer.parseInt(tok.nextToken())); this.setLocation(window); } super.toFront(); this.validate(); this.setVisible(true); } public static String getPref(String name, String defaultValue) { return prefs.getProperty(name, defaultValue); } public static void setPref(String name, String value) { prefs.setProperty(name, value); } // things to do on exit public void exitForm() { // close connections Gateway.close(); // save window sizing setPref( "WindowSize", (int) (this.getSize().getWidth()) + "," + (int) (this.getSize().getHeight())); setPref( "WindowPosition", (int) (this.getLocation().getX()) + "," + (int) (this.getLocation().getY())); setPref( "Style", UIManager.getLookAndFeel().getClass().getName()); setPref( "SplitPanePosition", String.valueOf(splitPane.getDividerLocation())); // save preferences file try { FileOutputStream prefsfile = new FileOutputStream("cristal.preferences", false); prefs.store(prefsfile, "Cristal 2"); prefsfile.close(); } catch (Exception e) { Logger.warning( "Could not write to preferences file. Preferences have not been updated."); } this.setVisible(false); // allow waiting threads time to quit try { Thread.sleep(2000); } catch (InterruptedException e) { } System.exit(0); } public void toggleTree() { boolean showTree = getPref("ShowTree", "true").equals("false"); setPref("ShowTree", String.valueOf(showTree)); if (!showTree) splitPanePos = splitPane.getDividerLocation(); getSplitPanel().getLeftComponent().setVisible(showTree); if (showTree) getSplitPanel().setDividerLocation(splitPanePos); getSplitPanel().validate(); } public static JComboBox getExecutionPlugins() { JComboBox plugins = new JComboBox(); // create execution selector Executor defaultExecutor = new DefaultExecutor(); plugins.addItem(defaultExecutor); plugins.setSelectedIndex(0); // load execution plugins String pluginList = Gateway.getProperty("Executors"); if (pluginList != null) { StringTokenizer tok = new StringTokenizer(pluginList, ","); while (tok.hasMoreTokens()) { String pluginName = tok.nextToken(); try { Class pluginClass = Class.forName(pluginName); Executor domainExecutor = (Executor)pluginClass.newInstance(); plugins.addItem(domainExecutor); } catch (Exception ex) { Logger.error("Could not load the executor plugin "+pluginName); } } } return plugins; } protected JSplitPane getSplitPanel() { // create the split pane, and add the Tree to it. if (splitPane == null) { splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true, treeBrowser, myDesktopManager); splitPane.setDividerSize(5); splitPanePos = Integer.parseInt(getPref("SplitPanePosition", "200")); getSplitPanel().setDividerLocation(splitPanePos); } return splitPane; } }