001: /*
002: * Jacareto Copyright (c) 2002-2005
003: * Applied Computer Science Research Group, Darmstadt University of
004: * Technology, Institute of Mathematics & Computer Science,
005: * Ludwigsburg University of Education, and Computer Based
006: * Learning Research Group, Aachen University. All rights reserved.
007: *
008: * Jacareto is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * Jacareto is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public
019: * License along with Jacareto; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: *
022: */
023:
024: package jacareto.cleverphl.gui;
025:
026: import jacareto.cleverphl.CleverPHL;
027: import jacareto.cleverphl.session.Session;
028: import jacareto.cleverphl.session.SessionEvent;
029: import jacareto.cleverphl.session.SessionList;
030: import jacareto.cleverphl.session.SessionListEvent;
031: import jacareto.cleverphl.session.SessionListListener;
032: import jacareto.cleverphl.session.SessionListener;
033: import jacareto.system.Language;
034: import jacareto.test.TestReport;
035: import jacareto.test.TestReportElement;
036: import jacareto.test.TestReportEvent;
037: import jacareto.test.TestReportListener;
038:
039: import java.awt.BorderLayout;
040: import java.awt.Color;
041: import java.awt.FlowLayout;
042: import java.awt.GridBagConstraints;
043: import java.awt.GridBagLayout;
044: import java.awt.Insets;
045: import java.awt.event.WindowEvent;
046: import java.awt.event.WindowListener;
047:
048: import java.util.Iterator;
049:
050: import javax.swing.BorderFactory;
051: import javax.swing.JCheckBoxMenuItem;
052: import javax.swing.JLabel;
053: import javax.swing.JPanel;
054: import javax.swing.JScrollPane;
055: import javax.swing.JTabbedPane;
056: import javax.swing.JTable;
057: import javax.swing.JTextArea;
058: import javax.swing.ListSelectionModel;
059: import javax.swing.border.EmptyBorder;
060: import javax.swing.border.LineBorder;
061: import javax.swing.border.TitledBorder;
062: import javax.swing.event.ListSelectionEvent;
063: import javax.swing.event.ListSelectionListener;
064: import javax.swing.table.DefaultTableModel;
065:
066: /**
067: * A frame displaying the results of the performed tests in a table.
068: *
069: * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
070: * @version 1.01
071: */
072: public class TestReportFrame extends CleverPHLFrame implements
073: SessionListListener, SessionListener, WindowListener,
074: TestReportListener, ListSelectionListener {
075: /** The JTable showing the test results. */
076: private JTable table;
077:
078: /** The model of the table. */
079: private DefaultTableModel tableModel;
080:
081: /** The JTextArea displaying the message. */
082: private JTextArea msgArea;
083:
084: /** The label showing the number of successes. */
085: private JLabel successesLabel;
086:
087: /** The label showing the number of failures. */
088: private JLabel failuresLabel;
089:
090: /** The label showing the number of corrections. */
091: private JLabel correctionsLabel;
092:
093: /** The label showing the number of ignored failures. */
094: private JLabel ignoredLabel;
095:
096: /** The main pane. */
097: JTabbedPane tabPane;
098:
099: /** Some strings. */
100: private String okString;
101: private String yesString;
102: private String noString;
103:
104: /**
105: * Creates a new test report frame.
106: *
107: * @param cleverPHL the CleverPHL instance
108: */
109: public TestReportFrame(CleverPHL cleverPHL) {
110: super (cleverPHL, "CleverPHL.TestReportFrame",
111: "CleverPHL.Menu.ViewTestReportWindow");
112:
113: Language language = cleverPHL.getLanguage();
114: setTitle(language.getString("CleverPHL.TestReportFrame.Title"));
115: addWindowListener(this );
116:
117: createMenu("CleverPHL.Menu.TestReportFrame.MenuBar");
118:
119: cleverPHL.getSessionList().addSessionListListener(this );
120:
121: okString = language.getString("General.ok");
122: yesString = language.getString("General.yes");
123: noString = language.getString("General.no");
124:
125: // The table
126: String[] colNames = new String[5];
127: colNames[0] = language
128: .getString("Tests.TestReport.ComponentName");
129: colNames[1] = language.getString("Tests.TestReport.Result");
130: colNames[2] = language.getString("Tests.TestReport.Corrected");
131: colNames[3] = language.getString("Tests.TestReport.Ignored");
132: colNames[4] = language.getString("Tests.TestReport.Message");
133: tableModel = new DefaultTableModel(colNames, 0);
134: table = new JTable(tableModel) {
135: public boolean isCellEditable(int row, int column) {
136: return false;
137: }
138: };
139: table.setRowSelectionAllowed(true);
140: table.setColumnSelectionAllowed(false);
141:
142: JScrollPane scrollPane = new JScrollPane(table,
143: JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
144: JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
145: scrollPane.setBorder(BorderFactory.createCompoundBorder(
146: new EmptyBorder(10, 10, 10, 10), new TitledBorder(
147: new LineBorder(Color.black), language
148: .getString("Tests.TestReport.Tests"))));
149:
150: ListSelectionModel selectionModel = table.getSelectionModel();
151: selectionModel
152: .setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
153: selectionModel.addListSelectionListener(this );
154:
155: // the message area
156: msgArea = new JTextArea(4, 50);
157: msgArea.setLineWrap(true);
158: msgArea.setWrapStyleWord(true);
159: msgArea.setEditable(false);
160:
161: JScrollPane msgAreaPane = new JScrollPane(msgArea,
162: JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
163: JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
164: msgAreaPane
165: .setBorder(BorderFactory
166: .createCompoundBorder(
167: new EmptyBorder(10, 10, 10, 10),
168: new TitledBorder(
169: new LineBorder(Color.black),
170: language
171: .getString("Tests.TestReport.Details"))));
172:
173: // The test pane
174: JPanel testPane = new JPanel(new BorderLayout());
175: testPane.add(scrollPane, BorderLayout.CENTER);
176: testPane.add(msgAreaPane, BorderLayout.SOUTH);
177:
178: // The statistics pane
179: JPanel statPane = new JPanel(new GridBagLayout());
180: GridBagConstraints c = new GridBagConstraints();
181: c.gridx = 0;
182: c.gridy = 0;
183: c.weightx = 80;
184: c.anchor = GridBagConstraints.WEST;
185: c.fill = GridBagConstraints.NONE;
186: c.insets = new Insets(5, 5, 5, 5);
187: statPane.add(new JLabel("Successes:"), c);
188: c.gridy = 1;
189: statPane.add(new JLabel("Failures:"), c);
190: c.gridy = 2;
191: statPane.add(new JLabel("Corrections:"), c);
192: c.gridy = 3;
193: statPane.add(new JLabel("Ignored Failures:"), c);
194: c.weightx = 20;
195: c.gridx = 1;
196: c.gridy = 0;
197: successesLabel = new JLabel("0");
198: statPane.add(successesLabel, c);
199: c.gridy = 1;
200: failuresLabel = new JLabel("0");
201: statPane.add(failuresLabel, c);
202: c.gridy = 2;
203: correctionsLabel = new JLabel("0");
204: statPane.add(correctionsLabel, c);
205: c.gridy = 3;
206: ignoredLabel = new JLabel("0");
207: statPane.add(ignoredLabel, c);
208:
209: JPanel statPaneWrapper = new JPanel(new FlowLayout(
210: FlowLayout.LEFT));
211: statPaneWrapper.add(statPane);
212:
213: tabPane = new JTabbedPane();
214: tabPane.add(testPane, language
215: .getString("CleverPHL.TestReportFrame.TestOverview"));
216: tabPane.add(statPaneWrapper, language
217: .getString("CleverPHL.TestReportFrame.Statistics"));
218: tabPane.setBorder(new EmptyBorder(5, 5, 5, 5));
219:
220: getContentPane().setLayout(new BorderLayout());
221:
222: //setLocation (300, 150);
223: //setToPreferredSize (480, 400);
224: setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
225: getCleverPHLMenuBar().setMenuItemEnabled(
226: "CleverPHL.Menu.TestReport", false);
227: }
228:
229: /**
230: * Called when the state of the session list has changed.
231: *
232: * @param event DOCUMENT ME!
233: */
234: /**
235: * Invoked when the session list has changed.
236: *
237: * @param event the session list event
238: */
239: public void sessionListChanged(SessionListEvent event) {
240: Session session = event.getSession();
241: SessionList sessionList = cleverPHL.getSessionList();
242: TestReport testReport = session.getTestReport();
243:
244: int ID = event.getID();
245:
246: if (ID == SessionListEvent.NEW_ACTUAL_SESSION) {
247: clearTable();
248:
249: Iterator it = testReport.iterator();
250:
251: while (it.hasNext()) {
252: addTableModelRow((TestReportElement) it.next());
253: }
254:
255: updateStatistics(testReport);
256: getCleverPHLMenuBar().setMenuItemEnabled(
257: "CleverPHL.Menu.TestReport", true);
258: } else if (ID == SessionListEvent.SESSION_ADDED) {
259: session.addSessionListener(this );
260:
261: if (sessionList.size() == 1) {
262: getContentPane().add(tabPane, BorderLayout.CENTER);
263: validate();
264: repaint();
265: }
266:
267: session.getTestReport().addTestReportListener(this );
268: getCleverPHLMenuBar().setMenuItemEnabled(
269: "CleverPHL.Menu.TestReport", true);
270: } else if (ID == SessionListEvent.SESSION_REMOVED) {
271: session.getTestReport().removeTestReportListener(this );
272: session.removeSessionListener(this );
273: getCleverPHLMenuBar()
274: .setMenuItemEnabled("CleverPHL.Menu.TestReport",
275: sessionList.size() > 0);
276:
277: if (sessionList.size() == 0) {
278: getContentPane().removeAll();
279: validate();
280: repaint();
281: }
282: }
283: }
284:
285: /**
286: * Invoked when a session event has occured.
287: *
288: * @param event the session event
289: */
290: public void sessionStateChanged(SessionEvent event) {
291: int id = event.getID();
292:
293: switch (id) {
294: case SessionEvent.REPLAY_START:
295: case SessionEvent.REPLAY_RESET:
296: clearTable();
297: msgArea.setText("");
298:
299: break;
300: }
301: }
302:
303: /**
304: * Called when an element has been added to a test report.
305: *
306: * @param event DOCUMENT ME!
307: */
308: public void testReportElementAdded(TestReportEvent event) {
309: addTableModelRow(event.getElement());
310: updateStatistics((TestReport) event.getSource());
311: }
312:
313: /**
314: * Called when the test report has been cleared.
315: *
316: * @param event DOCUMENT ME!
317: */
318: public void testReportCleared(TestReportEvent event) {
319: clearTable();
320: msgArea.setText("");
321: }
322:
323: /**
324: * Updates the statistics components.
325: *
326: * @param report DOCUMENT ME!
327: */
328: private void updateStatistics(TestReport report) {
329: successesLabel.setText("" + report.getNumberOfSuccesses());
330: failuresLabel.setText("" + report.getNumberOfFailures());
331: correctionsLabel.setText("" + report.getNumberOfCorrections());
332: ignoredLabel.setText("" + report.getNumberOfIgnoredFailures());
333: }
334:
335: /**
336: * Adds a test report element as table row.
337: *
338: * @param element DOCUMENT ME!
339: */
340: private void addTableModelRow(TestReportElement element) {
341: String[] data = new String[5];
342: data[0] = element.getComponentName();
343:
344: boolean result = element.getResult();
345:
346: if (!result) {
347: data[1] = "x";
348:
349: if (element.getCorrected()) {
350: data[2] = yesString;
351: } else {
352: data[2] = noString;
353: }
354:
355: if (element.getIgnored()) {
356: data[3] = yesString;
357: } else {
358: data[3] = noString;
359: }
360: } else {
361: data[1] = okString;
362: data[2] = "";
363: data[3] = "";
364: }
365:
366: String evaluationMessage = element.getEvaluationMessage();
367:
368: if (evaluationMessage != null) {
369: data[4] = evaluationMessage;
370: } else {
371: data[4] = "";
372: }
373:
374: tableModel.addRow(data);
375: }
376:
377: /**
378: * Clears the table.
379: */
380: private void clearTable() {
381: for (int i = tableModel.getRowCount() - 1; i >= 0; i--) {
382: tableModel.removeRow(i);
383: }
384:
385: successesLabel.setText("0");
386: failuresLabel.setText("0");
387: correctionsLabel.setText("0");
388: ignoredLabel.setText("0");
389: }
390:
391: /**
392: * Called when the list selection has changed.
393: *
394: * @param event DOCUMENT ME!
395: */
396: public void valueChanged(ListSelectionEvent event) {
397: try {
398: int index = table.getSelectedRow();
399: String detailMessage = tableModel.getValueAt(index, 0)
400: .toString();
401: String evaluationMessage = tableModel.getValueAt(index, 4)
402: .toString();
403:
404: if ((evaluationMessage != null)
405: && !evaluationMessage.equals("")) {
406: detailMessage += (":\n" + evaluationMessage);
407: }
408:
409: msgArea.setText(detailMessage);
410: } catch (ArrayIndexOutOfBoundsException ignored) {
411: ;
412: }
413: }
414:
415: /**
416: * Does nothing.
417: *
418: * @param w DOCUMENT ME!
419: */
420: public void windowActivated(WindowEvent w) {
421: }
422:
423: /**
424: * Does nothing.
425: *
426: * @param w DOCUMENT ME!
427: */
428: public void windowClosed(WindowEvent w) {
429: }
430:
431: /**
432: * Does nothing.
433: *
434: * @param w DOCUMENT ME!
435: */
436: public void windowDeactivated(WindowEvent w) {
437: }
438:
439: /**
440: * Does nothing.
441: *
442: * @param w DOCUMENT ME!
443: */
444: public void windowDeiconified(WindowEvent w) {
445: }
446:
447: /**
448: * Does nothing.
449: *
450: * @param w DOCUMENT ME!
451: */
452: public void windowIconified(WindowEvent w) {
453: }
454:
455: /**
456: * Does nothing.
457: *
458: * @param w DOCUMENT ME!
459: */
460: public void windowOpened(WindowEvent w) {
461: }
462:
463: /**
464: * Exits the system.
465: *
466: * @param w DOCUMENT ME!
467: */
468: public void windowClosing(WindowEvent w) {
469: close();
470: }
471:
472: /**
473: * Closes the frame.
474: */
475: public void close() {
476: setVisible(false);
477: ((JCheckBoxMenuItem) cleverPHL.getMainFrame()
478: .getCleverPHLMenuBar().getMenuItem(
479: "CleverPHL.Menu.ViewTestReportWindow"))
480: .setSelected(false);
481: }
482: }
|