| 1 | package tmcsim.cadsimulator.viewer; |
|---|
| 2 | |
|---|
| 3 | import java.awt.AWTEvent; |
|---|
| 4 | import java.awt.Dimension; |
|---|
| 5 | import java.awt.event.ActionEvent; |
|---|
| 6 | import java.awt.event.KeyEvent; |
|---|
| 7 | import java.awt.event.WindowEvent; |
|---|
| 8 | import java.io.IOException; |
|---|
| 9 | import java.util.Observable; |
|---|
| 10 | import java.util.Properties; |
|---|
| 11 | import java.util.logging.Level; |
|---|
| 12 | import java.util.logging.Logger; |
|---|
| 13 | import javax.swing.JFrame; |
|---|
| 14 | import javax.swing.JMenu; |
|---|
| 15 | import javax.swing.JMenuBar; |
|---|
| 16 | import javax.swing.JMenuItem; |
|---|
| 17 | import javax.swing.JOptionPane; |
|---|
| 18 | import javax.swing.JTabbedPane; |
|---|
| 19 | import javax.swing.KeyStroke; |
|---|
| 20 | import tmcsim.cadsimulator.managers.TrafficModelManager; |
|---|
| 21 | import tmcsim.cadsimulator.viewer.actions.ExitAction; |
|---|
| 22 | import tmcsim.cadsimulator.viewer.model.CADMediaStatus; |
|---|
| 23 | import tmcsim.cadsimulator.viewer.model.CADSimulatorStatus; |
|---|
| 24 | import tmcsim.interfaces.CADViewer; |
|---|
| 25 | |
|---|
| 26 | /** |
|---|
| 27 | * This class provides a GUI to view current status information for the CAD |
|---|
| 28 | * Server. |
|---|
| 29 | * |
|---|
| 30 | * @see SimulationStatusPanel |
|---|
| 31 | * @see MediaStatusPanel |
|---|
| 32 | * @author Matthew Cechini |
|---|
| 33 | * @version $Revision: 1.3 $ $Date: 2006/06/06 20:46:41 $ |
|---|
| 34 | */ |
|---|
| 35 | @SuppressWarnings("serial") |
|---|
| 36 | public class CADServerViewer extends JFrame implements CADViewer |
|---|
| 37 | { |
|---|
| 38 | private JTabbedPane cadSimTabbedPane; |
|---|
| 39 | private JMenuBar menubar; |
|---|
| 40 | private JMenu fileMenu; |
|---|
| 41 | private JMenuItem exitMenuItem; |
|---|
| 42 | |
|---|
| 43 | /** |
|---|
| 44 | * Panel to display simulation information. |
|---|
| 45 | */ |
|---|
| 46 | private SimulationStatusPanel simulationPanel; |
|---|
| 47 | /** |
|---|
| 48 | * Panel to display media control information. |
|---|
| 49 | */ |
|---|
| 50 | private MediaStatusPanel mediaPanel; |
|---|
| 51 | /** |
|---|
| 52 | * Panel to display configuration files. |
|---|
| 53 | */ |
|---|
| 54 | private ConfigStatusPanel configPanel; |
|---|
| 55 | /* |
|---|
| 56 | * Panel to display Traffic Model Event Queue |
|---|
| 57 | */ |
|---|
| 58 | private TrafficModelViewPanel trafficPanel; |
|---|
| 59 | |
|---|
| 60 | /** |
|---|
| 61 | * Constructor. |
|---|
| 62 | */ |
|---|
| 63 | public CADServerViewer() |
|---|
| 64 | { |
|---|
| 65 | super(); |
|---|
| 66 | setTitle("CAD Server " + getAppVersion()); |
|---|
| 67 | |
|---|
| 68 | initComponents(); |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | /** |
|---|
| 72 | * Read the version number from the application properties. The file |
|---|
| 73 | * 'application.properties' is generated by build.xml. |
|---|
| 74 | * |
|---|
| 75 | * @return a version string obtained from application.properties file, or |
|---|
| 76 | * "Version: unknown" if an IOerror prevents us from reading the file. |
|---|
| 77 | */ |
|---|
| 78 | private String getAppVersion() |
|---|
| 79 | { |
|---|
| 80 | String propfilename = "/tmcsim/application.properties"; |
|---|
| 81 | String propKey = "Application.revision"; |
|---|
| 82 | String version = "unknown"; |
|---|
| 83 | try |
|---|
| 84 | { |
|---|
| 85 | Properties props = new Properties(); |
|---|
| 86 | props.load(this.getClass().getResourceAsStream(propfilename)); |
|---|
| 87 | version = (String) props.get(propKey); |
|---|
| 88 | } catch (IOException ex) |
|---|
| 89 | { |
|---|
| 90 | Logger.getLogger("tmcsim/cadsimulator").log(Level.SEVERE, |
|---|
| 91 | "CADSimulatorView.getAppVersion()." |
|---|
| 92 | + " IOError reading " + propfilename); |
|---|
| 93 | } catch (NullPointerException npe) |
|---|
| 94 | { |
|---|
| 95 | Logger.getLogger("tmcsim/cadsimulator").log(Level.SEVERE, |
|---|
| 96 | "CADSimulatorView.getAppVersion().load." |
|---|
| 97 | + " Missing file: " + propfilename); |
|---|
| 98 | } |
|---|
| 99 | return "revision: " + version; |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | /** |
|---|
| 103 | * Method calls the processEvent() method with a WINDOW_CLOSING WindowEvent |
|---|
| 104 | * to start the application closing process. |
|---|
| 105 | */ |
|---|
| 106 | public void closeViewer() |
|---|
| 107 | { |
|---|
| 108 | processEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING)); |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | /** |
|---|
| 112 | * Overloads the processEvent method. If the AWTEvent is a WINDOW_CLOSING |
|---|
| 113 | * event, prompt the user to confirm the action. If confirmed, close the |
|---|
| 114 | * application. |
|---|
| 115 | */ |
|---|
| 116 | protected void processEvent(AWTEvent evt) |
|---|
| 117 | { |
|---|
| 118 | |
|---|
| 119 | if (evt.getID() == WindowEvent.WINDOW_CLOSING) |
|---|
| 120 | { |
|---|
| 121 | // int option = JOptionPane.showConfirmDialog(null, |
|---|
| 122 | // "Closing the CAD Simulator will stop the current " |
|---|
| 123 | // + "simulation. Do you wish to continue exiting?", |
|---|
| 124 | // "Confirm Exit", |
|---|
| 125 | // JOptionPane.YES_NO_OPTION); |
|---|
| 126 | // |
|---|
| 127 | // if (option != JOptionPane.NO_OPTION) |
|---|
| 128 | // { |
|---|
| 129 | System.exit(0); |
|---|
| 130 | // } |
|---|
| 131 | } |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | /** |
|---|
| 135 | * Initialize GUI Components |
|---|
| 136 | */ |
|---|
| 137 | private void initComponents() |
|---|
| 138 | { |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | simulationPanel = new SimulationStatusPanel(); |
|---|
| 142 | mediaPanel = new MediaStatusPanel(); |
|---|
| 143 | configPanel = new ConfigStatusPanel(); |
|---|
| 144 | trafficPanel = new TrafficModelViewPanel(); |
|---|
| 145 | |
|---|
| 146 | cadSimTabbedPane = new JTabbedPane(); |
|---|
| 147 | cadSimTabbedPane.addTab("Status", simulationPanel); |
|---|
| 148 | cadSimTabbedPane.addTab("Media", mediaPanel); |
|---|
| 149 | cadSimTabbedPane.addTab("Config", configPanel); |
|---|
| 150 | cadSimTabbedPane.addTab("Traffic", trafficPanel); |
|---|
| 151 | |
|---|
| 152 | add(cadSimTabbedPane); |
|---|
| 153 | |
|---|
| 154 | menubar = new JMenuBar(); |
|---|
| 155 | |
|---|
| 156 | fileMenu = new JMenu("File"); |
|---|
| 157 | fileMenu.setMnemonic(KeyEvent.VK_F); |
|---|
| 158 | menubar.add(fileMenu); |
|---|
| 159 | |
|---|
| 160 | exitMenuItem = new JMenuItem(new ExitAction(this)); |
|---|
| 161 | exitMenuItem.setAccelerator(KeyStroke.getKeyStroke( |
|---|
| 162 | KeyEvent.VK_X, ActionEvent.ALT_MASK)); |
|---|
| 163 | fileMenu.add(exitMenuItem); |
|---|
| 164 | |
|---|
| 165 | javax.swing.JMenu helpMenu = new javax.swing.JMenu("Help"); |
|---|
| 166 | |
|---|
| 167 | menubar.add(helpMenu); |
|---|
| 168 | |
|---|
| 169 | setJMenuBar(menubar); |
|---|
| 170 | |
|---|
| 171 | setPreferredSize(new Dimension(500, 575)); |
|---|
| 172 | pack(); |
|---|
| 173 | setResizable(true); |
|---|
| 174 | setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | @Override |
|---|
| 178 | public void update(Observable obs, Object obj) |
|---|
| 179 | { |
|---|
| 180 | if (obs instanceof CADSimulatorStatus) |
|---|
| 181 | { |
|---|
| 182 | simulationPanel.refresh(obs); |
|---|
| 183 | } |
|---|
| 184 | if (obs instanceof CADMediaStatus) |
|---|
| 185 | { |
|---|
| 186 | mediaPanel.refresh(obs); |
|---|
| 187 | } |
|---|
| 188 | if (obs instanceof TrafficModelManager) |
|---|
| 189 | { |
|---|
| 190 | trafficPanel.update(obs, obj); |
|---|
| 191 | } |
|---|
| 192 | } |
|---|
| 193 | } |
|---|