| 1 | package tmcsim.paramicscommunicator.gui; |
|---|
| 2 | |
|---|
| 3 | import java.awt.Dimension; |
|---|
| 4 | import java.text.DateFormat; |
|---|
| 5 | import java.util.Date; |
|---|
| 6 | import java.util.Observable; |
|---|
| 7 | import java.util.Observer; |
|---|
| 8 | import java.util.TreeMap; |
|---|
| 9 | import java.util.logging.Handler; |
|---|
| 10 | import java.util.logging.Level; |
|---|
| 11 | import java.util.logging.LogRecord; |
|---|
| 12 | import java.util.logging.Logger; |
|---|
| 13 | import javax.swing.BorderFactory; |
|---|
| 14 | import javax.swing.Box; |
|---|
| 15 | import javax.swing.BoxLayout; |
|---|
| 16 | import javax.swing.JFrame; |
|---|
| 17 | import javax.swing.JMenuBar; |
|---|
| 18 | import javax.swing.JOptionPane; |
|---|
| 19 | import javax.swing.JScrollPane; |
|---|
| 20 | import javax.swing.JTabbedPane; |
|---|
| 21 | import javax.swing.JTextArea; |
|---|
| 22 | import tmcsim.common.RevisionNumber; |
|---|
| 23 | import tmcsim.paramicscommunicator.FileIOUpdate; |
|---|
| 24 | import tmcsim.paramicscommunicator.FileRegUpdate; |
|---|
| 25 | |
|---|
| 26 | /** |
|---|
| 27 | * ParamicsCommunicatorGUI is the view class for the ParamicsCommunicator. The |
|---|
| 28 | * user interface shows a tab for each I/O reader or writer that has been |
|---|
| 29 | * registered with the ParamicsCommunicator. The tab shows the history of I/O |
|---|
| 30 | * reads and writes, and the unique information for the target file and I/O |
|---|
| 31 | * interval. |
|---|
| 32 | * |
|---|
| 33 | * @author Matthew Cechini |
|---|
| 34 | * @version |
|---|
| 35 | */ |
|---|
| 36 | @SuppressWarnings("serial") |
|---|
| 37 | public class ParamicsCommunicatorGUI extends JFrame implements Observer |
|---|
| 38 | { |
|---|
| 39 | |
|---|
| 40 | /** |
|---|
| 41 | * Logging handler that writes all received log records to the error text |
|---|
| 42 | * area. |
|---|
| 43 | * |
|---|
| 44 | * @author Matthew Cechini |
|---|
| 45 | */ |
|---|
| 46 | protected class ParamicsLoggerHandler extends Handler |
|---|
| 47 | { |
|---|
| 48 | |
|---|
| 49 | DateFormat timeFormat = DateFormat.getTimeInstance(); |
|---|
| 50 | |
|---|
| 51 | public void close() throws SecurityException |
|---|
| 52 | { |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | public void flush() |
|---|
| 56 | { |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | public void publish(LogRecord record) |
|---|
| 60 | { |
|---|
| 61 | StringBuffer errorBuf = new StringBuffer(); |
|---|
| 62 | |
|---|
| 63 | errorBuf.append(timeFormat.format(new Date(record.getMillis()))); |
|---|
| 64 | errorBuf.append(" - " + record.getMessage() + "\n"); |
|---|
| 65 | |
|---|
| 66 | errorTA.setText(errorTA.getText() + errorBuf.toString()); |
|---|
| 67 | } |
|---|
| 68 | } |
|---|
| 69 | /** |
|---|
| 70 | * Map of FileIOTableModel objects for each reader id. |
|---|
| 71 | */ |
|---|
| 72 | protected TreeMap<String, FileIOTableModel> readerTables; |
|---|
| 73 | /** |
|---|
| 74 | * Map of FileIOTableModel objects for each writer id. |
|---|
| 75 | */ |
|---|
| 76 | protected TreeMap<String, FileIOTableModel> writerTables; |
|---|
| 77 | |
|---|
| 78 | /** |
|---|
| 79 | * Constructor. Initialize local lists, set up the logging handler and |
|---|
| 80 | * initialize the GUI. |
|---|
| 81 | */ |
|---|
| 82 | public ParamicsCommunicatorGUI() |
|---|
| 83 | { |
|---|
| 84 | super("Paramics Communicator"); |
|---|
| 85 | |
|---|
| 86 | readerTables = new TreeMap<String, FileIOTableModel>(); |
|---|
| 87 | writerTables = new TreeMap<String, FileIOTableModel>(); |
|---|
| 88 | |
|---|
| 89 | Logger.getLogger("tmcsim.paramicscommunicator").addHandler(new ParamicsLoggerHandler()); |
|---|
| 90 | |
|---|
| 91 | initializeGUI(); |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | /** |
|---|
| 95 | * Observer update method. If the update object is a FileIOUpdate object, |
|---|
| 96 | * update the TableModel corresponding to the unique I/O id. If the update |
|---|
| 97 | * object is a FileRegUpdate object, add a new tab if the registration type |
|---|
| 98 | * is REGISTER, or update an existing tab if the registration type is |
|---|
| 99 | * UNREGISTER. |
|---|
| 100 | */ |
|---|
| 101 | public void update(Observable o, Object arg) |
|---|
| 102 | { |
|---|
| 103 | |
|---|
| 104 | if (arg instanceof FileIOUpdate) |
|---|
| 105 | { |
|---|
| 106 | |
|---|
| 107 | try |
|---|
| 108 | { |
|---|
| 109 | FileIOUpdate update = (FileIOUpdate) arg; |
|---|
| 110 | |
|---|
| 111 | switch (update.ioType) |
|---|
| 112 | { |
|---|
| 113 | case READ: |
|---|
| 114 | readerTables.get(update.ioID).addIOUpdate(update); |
|---|
| 115 | break; |
|---|
| 116 | case WRITE: |
|---|
| 117 | writerTables.get(update.ioID).addIOUpdate(update); |
|---|
| 118 | break; |
|---|
| 119 | } |
|---|
| 120 | } catch (Exception e) |
|---|
| 121 | { |
|---|
| 122 | Logger.getLogger("tmcsim.paramicscommunicator.gui").logp( |
|---|
| 123 | Level.SEVERE, "ParamicsCommunicatorGUI", "update", |
|---|
| 124 | "Exception in receiving FileIOUpdate object.", e); |
|---|
| 125 | } |
|---|
| 126 | } |
|---|
| 127 | else if (arg instanceof FileRegUpdate) |
|---|
| 128 | { |
|---|
| 129 | try |
|---|
| 130 | { |
|---|
| 131 | FileRegUpdate update = (FileRegUpdate) arg; |
|---|
| 132 | |
|---|
| 133 | switch (update.ioType) |
|---|
| 134 | { |
|---|
| 135 | case READ: |
|---|
| 136 | switch (update.regType) |
|---|
| 137 | { |
|---|
| 138 | case REGISTER: |
|---|
| 139 | FileIOTableModel model = new FileIOTableModel(); |
|---|
| 140 | readerTables.put(update.ioID, model); |
|---|
| 141 | |
|---|
| 142 | addTab(update, model); |
|---|
| 143 | break; |
|---|
| 144 | case UNREGISTER: |
|---|
| 145 | //unregister |
|---|
| 146 | break; |
|---|
| 147 | } |
|---|
| 148 | break; |
|---|
| 149 | case WRITE: |
|---|
| 150 | switch (update.regType) |
|---|
| 151 | { |
|---|
| 152 | case REGISTER: |
|---|
| 153 | FileIOTableModel model = new FileIOTableModel(); |
|---|
| 154 | writerTables.put(update.ioID, model); |
|---|
| 155 | |
|---|
| 156 | addTab(update, model); |
|---|
| 157 | break; |
|---|
| 158 | case UNREGISTER: |
|---|
| 159 | //unregister |
|---|
| 160 | break; |
|---|
| 161 | } |
|---|
| 162 | break; |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | } catch (Exception e) |
|---|
| 166 | { |
|---|
| 167 | Logger.getLogger("tmcsim.paramicscommunicator.gui").logp( |
|---|
| 168 | Level.SEVERE, "ParamicsCommunicatorGUI", "update", |
|---|
| 169 | "Exception in receiving FileRegUpdate object.", e); |
|---|
| 170 | } |
|---|
| 171 | } |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | private void initializeGUI() |
|---|
| 175 | { |
|---|
| 176 | |
|---|
| 177 | JMenuBar menuBar = new JMenuBar(); |
|---|
| 178 | |
|---|
| 179 | javax.swing.JMenu fileMenu = new javax.swing.JMenu("File"); |
|---|
| 180 | javax.swing.JMenuItem logItem = new javax.swing.JMenuItem("Show Log"); |
|---|
| 181 | |
|---|
| 182 | logItem.addActionListener(new java.awt.event.ActionListener() |
|---|
| 183 | { |
|---|
| 184 | public void actionPerformed(java.awt.event.ActionEvent evt) |
|---|
| 185 | { |
|---|
| 186 | tmcsim.paramicslog.gui.ParamicsLogGUI.getInstance().display(); |
|---|
| 187 | } |
|---|
| 188 | }); |
|---|
| 189 | |
|---|
| 190 | fileMenu.add(logItem); |
|---|
| 191 | menuBar.add(fileMenu); |
|---|
| 192 | |
|---|
| 193 | javax.swing.JMenu helpMenu = new javax.swing.JMenu("Help"); |
|---|
| 194 | javax.swing.JMenuItem aboutItem = new javax.swing.JMenuItem("About"); |
|---|
| 195 | |
|---|
| 196 | aboutItem.addActionListener(new java.awt.event.ActionListener() |
|---|
| 197 | { |
|---|
| 198 | public void actionPerformed(java.awt.event.ActionEvent evt) |
|---|
| 199 | { |
|---|
| 200 | String ver = RevisionNumber.getString(); |
|---|
| 201 | JOptionPane.showMessageDialog(rootPane, "Version: " + ver, "About", JOptionPane.INFORMATION_MESSAGE); |
|---|
| 202 | } |
|---|
| 203 | }); |
|---|
| 204 | |
|---|
| 205 | helpMenu.add(aboutItem); |
|---|
| 206 | menuBar.add(helpMenu); |
|---|
| 207 | |
|---|
| 208 | this.setJMenuBar(menuBar); |
|---|
| 209 | |
|---|
| 210 | fileReaderTabs = new JTabbedPane(); |
|---|
| 211 | fileReaderTabs.setAlignmentX(Box.CENTER_ALIGNMENT); |
|---|
| 212 | fileReaderTabs.setMinimumSize(new Dimension(420, 480)); |
|---|
| 213 | fileReaderTabs.setPreferredSize(new Dimension(420, 480)); |
|---|
| 214 | fileReaderTabs.setMaximumSize(new Dimension(420, 480)); |
|---|
| 215 | fileReaderTabs.setBorder(BorderFactory.createCompoundBorder( |
|---|
| 216 | BorderFactory.createTitledBorder( |
|---|
| 217 | BorderFactory.createRaisedBevelBorder(), "Registered Readers "), |
|---|
| 218 | BorderFactory.createEmptyBorder(5, 5, 5, 5))); |
|---|
| 219 | |
|---|
| 220 | fileWriterTabs = new JTabbedPane(); |
|---|
| 221 | fileWriterTabs.setAlignmentX(Box.CENTER_ALIGNMENT); |
|---|
| 222 | fileWriterTabs.setMinimumSize(new Dimension(420, 480)); |
|---|
| 223 | fileWriterTabs.setPreferredSize(new Dimension(420, 480)); |
|---|
| 224 | fileWriterTabs.setMaximumSize(new Dimension(420, 480)); |
|---|
| 225 | fileWriterTabs.setBorder(BorderFactory.createCompoundBorder( |
|---|
| 226 | BorderFactory.createTitledBorder( |
|---|
| 227 | BorderFactory.createRaisedBevelBorder(), "Registered Writers "), |
|---|
| 228 | BorderFactory.createEmptyBorder(5, 5, 5, 5))); |
|---|
| 229 | |
|---|
| 230 | errorTA = new JTextArea(); |
|---|
| 231 | errorTA.setLineWrap(true); |
|---|
| 232 | |
|---|
| 233 | errorPane = new JScrollPane(); |
|---|
| 234 | errorPane.setViewportView(errorTA); |
|---|
| 235 | errorPane.setAlignmentX(Box.CENTER_ALIGNMENT); |
|---|
| 236 | errorPane.setBorder(BorderFactory.createCompoundBorder( |
|---|
| 237 | BorderFactory.createTitledBorder( |
|---|
| 238 | BorderFactory.createRaisedBevelBorder(), "Errors"), |
|---|
| 239 | BorderFactory.createEmptyBorder(5, 5, 5, 5))); |
|---|
| 240 | |
|---|
| 241 | Box tabbedBox = new Box(BoxLayout.X_AXIS); |
|---|
| 242 | tabbedBox.add(fileReaderTabs); |
|---|
| 243 | tabbedBox.add(fileWriterTabs); |
|---|
| 244 | |
|---|
| 245 | Box guiBox = new Box(BoxLayout.Y_AXIS); |
|---|
| 246 | guiBox.add(tabbedBox); |
|---|
| 247 | guiBox.add(Box.createVerticalStrut(10)); |
|---|
| 248 | guiBox.add(errorPane); |
|---|
| 249 | |
|---|
| 250 | add(guiBox); |
|---|
| 251 | |
|---|
| 252 | setMinimumSize(new Dimension(420, 680)); |
|---|
| 253 | setPreferredSize(new Dimension(840, 680)); |
|---|
| 254 | setResizable(false); |
|---|
| 255 | pack(); |
|---|
| 256 | //setVisible(true); |
|---|
| 257 | } |
|---|
| 258 | |
|---|
| 259 | /** |
|---|
| 260 | * Method creates a new tab for the new I/O object. The tab is labeled |
|---|
| 261 | * "Reader #" or "Writer #", where '#' is the I/O object's ID. |
|---|
| 262 | * |
|---|
| 263 | * @param update Initial update object. |
|---|
| 264 | * @param model TableModel for reader/writer that will display I/O |
|---|
| 265 | * operations. |
|---|
| 266 | */ |
|---|
| 267 | private void addTab(FileRegUpdate update, FileIOTableModel model) |
|---|
| 268 | { |
|---|
| 269 | |
|---|
| 270 | String tabName = null; |
|---|
| 271 | |
|---|
| 272 | switch (update.ioType) |
|---|
| 273 | { |
|---|
| 274 | case READ: |
|---|
| 275 | tabName = "Reader " + update.ioID; |
|---|
| 276 | fileReaderTabs.add(tabName, new ParamicsIOInfoPanel(update, model)); |
|---|
| 277 | break; |
|---|
| 278 | case WRITE: |
|---|
| 279 | tabName = "Writer " + update.ioID; |
|---|
| 280 | fileWriterTabs.add(tabName, new ParamicsIOInfoPanel(update, model)); |
|---|
| 281 | break; |
|---|
| 282 | } |
|---|
| 283 | |
|---|
| 284 | } |
|---|
| 285 | private JTabbedPane fileReaderTabs; |
|---|
| 286 | private JTabbedPane fileWriterTabs; |
|---|
| 287 | private JScrollPane errorPane; |
|---|
| 288 | private JTextArea errorTA; |
|---|
| 289 | } |
|---|