package com.ericsson.tic.vi; import java.awt.event.KeyListener; import java.awt.event.KeyEvent; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.Container; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.Frame; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JCheckBoxMenuItem; import javax.swing.JPanel; import javax.swing.JSplitPane; import java.util.ListIterator; import java.io.IOException; import java.io.File; /** * The top level object which defines the GUI. * * @author Sami Matilainen * @version 1.0 (2008-12-04) */ public class GUI extends WindowAdapter implements ActionListener { /** The parent window of this window. (null if this is the root window) */ public GUI parent; /** A JFrame which acts as the main window. */ public JFrame frame; /** A file chooser dialogue. */ //private JFileChooser fileChooser; /** The content pane. */ private Container pane; /** Main Menu Bar. */ private JMenuBar menuBar; /** Menu. */ private JMenu fileMenu, settingsMenu, helpMenu; /** Menu Item. */ private JMenuItem menuItem; /** Menu item. */ private JCheckBoxMenuItem cbMenuItem; /** Used for layout of the main window. */ private JSplitPane split1, split2, split3; /** A gui component representing the console. */ public ConsolePanel console; /** A gui component representing the toolbar. */ private ToolbarPanel toolbar; /** A gui component representing the data list. */ public CardPanel list; /** A gui component representing the main view. */ private SplitViewPanel view; /** A window ID. */ private int wid; /** If true, this one is the main Window which hosts the main console. */ private boolean mainWindow; /** * Creates the main GUI window. Multiple GUI objects (windows) can be * present at the same time. */ public GUI() { this.wid = VI.requestWindowID(); frame = new JFrame(VI.windowTitle); pane = frame.getContentPane(); pane.setLayout(new BorderLayout()); // create the 4 main GUI components // ConsolePanel // ToolbarPanel // CardPanel (settings) // SplitViewPanel (a hierarchy of ViewPanels in split panes) console = new ConsolePanel(wid); if (VI.consoleList.size() == 0) { this.mainWindow = true; VI.d = console; } else { this.mainWindow = false; } VI.consoleList.add(console); toolbar = new ToolbarPanel(); list = new CardPanel(); view = new SplitViewPanel(null, null); split3 = new JSplitPane(JSplitPane.VERTICAL_SPLIT, toolbar, console); split2 = new JSplitPane(JSplitPane.VERTICAL_SPLIT, view, split3); split1 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, split2, list); split2.setPreferredSize(new Dimension(3000, 3000)); split2.setMaximumSize(new Dimension(3000, 3000)); split2.setMinimumSize(new Dimension(400, 400)); split1.setDividerLocation(3000); split1.setOneTouchExpandable(true); split2.setPreferredSize(new Dimension(3000, 3000)); split2.setMaximumSize(new Dimension(3000, 3000)); split2.setMinimumSize(new Dimension(400, 400)); split2.setDividerLocation(3000); split2.setOneTouchExpandable(true); split3.setPreferredSize(new Dimension(50, 50)); split3.setMaximumSize(new Dimension(120, 120)); split3.setMinimumSize(new Dimension(50, 50)); split3.setDividerLocation(3000); split3.setOneTouchExpandable(true); pane.add(split1, BorderLayout.CENTER); menuBar = new JMenuBar(); fileMenu = new JMenu("File"); settingsMenu = new JMenu("Settings"); helpMenu = new JMenu("Help"); menuBar.add(fileMenu); menuBar.add(settingsMenu); menuBar.add(helpMenu); menuItem = new JMenuItem("New Window"); menuItem.addActionListener(this); menuItem.setActionCommand("new window"); fileMenu.add(menuItem); menuItem = new JMenuItem("Close Window"); menuItem.addActionListener(this); menuItem.setActionCommand("close window"); fileMenu.add(menuItem); fileMenu.addSeparator(); /* menuItem = new JMenuItem("Default Configuration (not implemented)"); menuItem.addActionListener(this); menuItem.setActionCommand("default configuration"); menuItem.setEnabled(false); fileMenu.add(menuItem); fileMenu.addSeparator(); */ menuItem = new JMenuItem("Exit"); menuItem.addActionListener(this); menuItem.setActionCommand("exit"); fileMenu.add(menuItem); cbMenuItem = new JCheckBoxMenuItem("Advanced lighting"); cbMenuItem.setState(VI.betterLightingOn); cbMenuItem.addActionListener(this); cbMenuItem.setActionCommand("lighting"); settingsMenu.add(cbMenuItem); cbMenuItem = new JCheckBoxMenuItem("Fog (globe view)"); cbMenuItem.setState(VI.fogOn); cbMenuItem.addActionListener(this); cbMenuItem.setActionCommand("fog"); settingsMenu.add(cbMenuItem); cbMenuItem = new JCheckBoxMenuItem("Skybox (globe view)"); cbMenuItem.setState(VI.skyboxOn); cbMenuItem.addActionListener(this); cbMenuItem.setActionCommand("skybox"); settingsMenu.add(cbMenuItem); cbMenuItem = new JCheckBoxMenuItem("Shading (bars)"); cbMenuItem.setState(VI.barShadingOn); cbMenuItem.addActionListener(this); cbMenuItem.setActionCommand("shading bars"); settingsMenu.add(cbMenuItem); /* cbMenuItem = new JCheckBoxMenuItem("Shading (nodes)"); cbMenuItem.setState(VI.nodeShadingOn); cbMenuItem.addActionListener(this); cbMenuItem.setActionCommand("shading nodes"); settingsMenu.add(cbMenuItem); */ cbMenuItem = new JCheckBoxMenuItem("Anti-aliasing (links)"); cbMenuItem.setState(VI.lineAAOn); cbMenuItem.addActionListener(this); cbMenuItem.setActionCommand("aa links"); settingsMenu.add(cbMenuItem); /* settingsMenu.addSeparator(); cbMenuItem = new JCheckBoxMenuItem("Enable slaving (not implemented)"); cbMenuItem.setState(false); cbMenuItem.setEnabled(false); settingsMenu.add(cbMenuItem); cbMenuItem = new JCheckBoxMenuItem("Enable change alerts (not implemented)"); cbMenuItem.setState(false); cbMenuItem.setEnabled(false); settingsMenu.add(cbMenuItem); settingsMenu.addSeparator(); cbMenuItem = new JCheckBoxMenuItem("Enable graceful scenario change (not implemented)"); cbMenuItem.setState(true); cbMenuItem.setEnabled(false); settingsMenu.add(cbMenuItem); cbMenuItem = new JCheckBoxMenuItem("Force default values for scenarios (not implemented)"); cbMenuItem.setState(false); cbMenuItem.setEnabled(false); settingsMenu.add(cbMenuItem); settingsMenu.addSeparator(); menuItem = new JMenuItem("Settings..."); menuItem.addActionListener(this); menuItem.setActionCommand("settings..."); settingsMenu.add(menuItem); */ menuItem = new JMenuItem("Read Me"); menuItem.addActionListener(this); menuItem.setEnabled(true); menuItem.setActionCommand("read me"); helpMenu.add(menuItem); menuItem = new JMenuItem("User Manual"); menuItem.addActionListener(this); menuItem.setEnabled(true); menuItem.setActionCommand("user manual"); helpMenu.add(menuItem); menuItem = new JMenuItem("Shortcuts"); menuItem.addActionListener(this); menuItem.setEnabled(true); menuItem.setActionCommand("shortcuts"); helpMenu.add(menuItem); menuItem = new JMenuItem("API"); menuItem.addActionListener(this); menuItem.setEnabled(true); menuItem.setActionCommand("api"); helpMenu.add(menuItem); menuItem = new JMenuItem("Change Log"); menuItem.addActionListener(this); menuItem.setEnabled(true); menuItem.setActionCommand("change log"); helpMenu.add(menuItem); frame.setJMenuBar(menuBar); frame.setSize(800, 600); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); frame.addWindowListener(this); VI.focusInView = true; frame.setExtendedState(Frame.MAXIMIZED_BOTH); split1.setDividerLocation(3000); split2.setDividerLocation(3000); split3.setDividerLocation(3000); frame.setVisible(true); } /** * Called when an ActionEvent is triggered. * Handles a multitude of different events in the GUI. * * @param e The ActionEvent which triggered the event. */ public void actionPerformed(ActionEvent e) { if ("new window".equals(e.getActionCommand())) { GUI window = new GUI(); VI.windowList.add(window); } else if ("close window".equals(e.getActionCommand())) { ListIterator iter = VI.windowList.listIterator(); GUI win = null; while (iter.hasNext()) { win = (GUI)iter.next(); if (win.wid == this.wid) { iter.remove(); } else { if (mainWindow) { VI.d = win.console; this.mainWindow = false; win.mainWindow = true; } } } iter = VI.consoleList.listIterator(); ConsolePanel con = null; while (iter.hasNext()) { con = (ConsolePanel)iter.next(); if (con.wid == this.wid) { iter.remove(); break; } } this.frame.dispose(); if (VI.windowList.size() == 0) { System.exit(0); } } else if ("default configuration".equals(e.getActionCommand())) { // does nothing... } else if ("exit".equals(e.getActionCommand())) { System.exit(0); } else if ("settings...".equals(e.getActionCommand())) { //GlobalSettingsDialog settingsDialog = new GlobalSettingsDialog(); } else if ("read me".equals(e.getActionCommand())) { // only works if the OS supports the Desktop class... try { java.awt.Desktop.getDesktop().open(new File("index.html")); } catch (IOException ex) { ex.printStackTrace(); } } else if ("user manual".equals(e.getActionCommand())) { // only works if the OS supports the Desktop class... try { java.awt.Desktop.getDesktop().open(new File("end_user_manual.html")); } catch (IOException ex) { ex.printStackTrace(); } } else if ("shortcuts".equals(e.getActionCommand())) { // only works if the OS supports the Desktop class... try { java.awt.Desktop.getDesktop().open(new File("keybindings.html")); } catch (IOException ex) { ex.printStackTrace(); } } else if ("api".equals(e.getActionCommand())) { // only works if the OS supports the Desktop class... try { java.awt.Desktop.getDesktop().open(new File("api.html")); } catch (IOException ex) { ex.printStackTrace(); } } else if ("change log".equals(e.getActionCommand())) { // only works if the OS supports the Desktop class... try { java.awt.Desktop.getDesktop().open(new File("changelog.html")); } catch (IOException ex) { ex.printStackTrace(); } } else if ("lighting".equals(e.getActionCommand())) { VI.betterLightingOn = !VI.betterLightingOn; } else if ("fog".equals(e.getActionCommand())) { VI.fogOn = !VI.fogOn; } else if ("skybox".equals(e.getActionCommand())) { VI.skyboxOn = !VI.skyboxOn; } else if ("shading bars".equals(e.getActionCommand())) { VI.barShadingOn = !VI.barShadingOn; } else if ("shading nodes".equals(e.getActionCommand())) { VI.nodeShadingOn = !VI.nodeShadingOn; } else if ("aa links".equals(e.getActionCommand())) { VI.lineAAOn = !VI.lineAAOn; } } /** * Called by the window state changed event. * * @param e the WindowEvent */ public void windowStateChanged(WindowEvent e) { split1.setDividerLocation(3000); split2.setDividerLocation(3000); split3.setDividerLocation(3000); } /** * Called by the window closing event. * * @param e the WindowEvent */ public void windowClosing(WindowEvent e) { ListIterator iter = VI.windowList.listIterator(); GUI win = null; while (iter.hasNext()) { win = (GUI)iter.next(); if (win.wid == this.wid) { iter.remove(); } else { if (mainWindow) { VI.d = win.console; this.mainWindow = false; win.mainWindow = true; } } } iter = VI.consoleList.listIterator(); ConsolePanel con = null; while (iter.hasNext()) { con = (ConsolePanel)iter.next(); if (con.wid == this.wid) { iter.remove(); break; } } this.frame.dispose(); if (VI.windowList.size() == 0) { System.exit(0); } } }