source: tmcsimulator/trunk/src/tmcsim/paramicscommunicator/gui/ParamicsCommunicatorGUI.java @ 2

Revision 2, 8.4 KB checked in by jdalbey, 10 years ago (diff)

Initial Import of project files

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