001: /******************************************************************************
002: * Copyright (C) Lars Ivar Almli. All rights reserved. *
003: * ---------------------------------------------------------------------------*
004: * This file is part of MActor. *
005: * *
006: * MActor is free software; you can redistribute it and/or modify *
007: * it under the terms of the GNU General Public License as published by *
008: * the Free Software Foundation; either version 2 of the License, or *
009: * (at your option) any later version. *
010: * *
011: * MActor is distributed in the hope that it will be useful, *
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
014: * GNU General Public License for more details. *
015: * *
016: * You should have received a copy of the GNU General Public License *
017: * along with MActor; if not, write to the Free Software *
018: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
019: ******************************************************************************/package org.mactor.ui;
020:
021: import java.awt.BorderLayout;
022: import java.awt.Component;
023: import java.io.IOException;
024: import javax.swing.Icon;
025: import javax.swing.JEditorPane;
026: import javax.swing.JPanel;
027: import javax.swing.JScrollPane;
028: import javax.swing.JSplitPane;
029: import javax.swing.JTable;
030: import javax.swing.JTextArea;
031: import javax.swing.ListSelectionModel;
032: import javax.swing.event.ListSelectionEvent;
033: import javax.swing.event.ListSelectionListener;
034: import javax.swing.table.AbstractTableModel;
035: import javax.swing.table.DefaultTableCellRenderer;
036: import org.mactor.framework.TestSummary;
037: import org.mactor.framework.TestSummary.MessageInfo;
038:
039: public class TestSummaryPanel extends JPanel {
040: private JTable summaryTable;
041: private JEditorPane messagePane = new JEditorPane();
042: private JTextArea summaryText = new JTextArea(20, 10);
043: MessageHistoryTableModel model = new MessageHistoryTableModel();
044:
045: public TestSummaryPanel() {
046: super (new BorderLayout());
047: summaryTable = new JTable(model);
048: summaryTable.getColumnModel().getColumn(0).setCellRenderer(
049: new MessageTableCellRendere());
050: summaryTable
051: .setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
052: // summaryTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
053: summaryTable.getColumnModel().getColumn(0).setMaxWidth(25);
054: JScrollPane tableSp = new JScrollPane(summaryTable);
055: JPanel topP = new JPanel(new BorderLayout());
056: topP.add(new JScrollPane(summaryText), BorderLayout.NORTH);
057: topP.add(tableSp, BorderLayout.CENTER);
058:
059: JSplitPane splitPane = new JSplitPane(
060: JSplitPane.VERTICAL_SPLIT, topP, new JScrollPane(
061: messagePane));
062: add(splitPane, BorderLayout.CENTER);
063: summaryTable.getSelectionModel().addListSelectionListener(
064: new ListSelectionListener() {
065: public void valueChanged(ListSelectionEvent e) {
066: if (e.getValueIsAdjusting())
067: return;
068: ListSelectionModel lsm = (ListSelectionModel) e
069: .getSource();
070: if (!lsm.isSelectionEmpty()) {
071: int selectedRow = lsm
072: .getMinSelectionIndex();
073: MessageInfo mi = model
074: .getMessageInfo(selectedRow);
075: if (mi != null) {
076: try {
077: messagePane.setPage("file:"
078: + mi.getArchivePath());
079: } catch (IOException ioe) {
080: messagePane
081: .setText("Unable show the archived file '"
082: + mi
083: .getArchivePath()
084: + "'. Error: "
085: + ioe.getMessage());
086: }
087: } else {
088: messagePane.setText("");
089: }
090: }
091: }
092: });
093: }
094:
095: public void setTestSummary(TestSummary summary) {
096: if (summary == null) {
097: model.setMessageHistory(null);
098: } else {
099: int row = summaryTable.getSelectedRow();
100: summaryText
101: .setText((summary.isSuccess() ? "Test completed with SUCCESS!"
102: : "Test FAILED. Details: "
103: + summary.getFailedMessage())
104: + "\nStart time:\t"
105: + GUIUtil
106: .format(summary.getTestStartTime())
107: + "\nEnd time:\t"
108: + GUIUtil.format(summary
109: .getTestCompleteTime())
110: + "\nVariables:\t "
111: + summary.getContextValues().toString());
112: MessageInfo[] h = summary.getMessageHistory();
113: model.setMessageHistory(h);
114: if (row >= 0 && h != null & row < h.length)
115: summaryTable.setRowSelectionInterval(row, row);
116: }
117: }
118:
119: private static class MessageHistoryTableModel extends
120: AbstractTableModel {
121: private static final Icon INCOMING_ICON = GUIUtil
122: .loadIcon("/incoming_16.PNG");
123: private static final Icon INCOMING_RESP_ICON = GUIUtil
124: .loadIcon("/incoming_resp_16.PNG");
125: private static final Icon OUTGOING_ICON = GUIUtil
126: .loadIcon("/outgoing_16.PNG");
127: private static final Icon OUTGOING_RESP_ICON = GUIUtil
128: .loadIcon("/outgoing_resp_16.PNG");
129: MessageInfo[] history;
130: String[] colums = new String[] { "", "Timestamp", "Channel",
131: "Node" };
132:
133: @Override
134: public String getColumnName(int col) {
135: return colums[col];
136: }
137:
138: public void setMessageHistory(MessageInfo[] history) {
139: this .history = history;
140: super .fireTableDataChanged();
141: }
142:
143: public int getColumnCount() {
144: return colums.length;
145: }
146:
147: public int getRowCount() {
148: if (history == null)
149: return 0;
150: return history.length;
151: }
152:
153: public MessageInfo getMessageInfo(int row) {
154: if (history == null || row < 0 || row >= history.length)
155: return null;
156: return history[row];
157: }
158:
159: public Object getValueAt(int row, int col) {
160: if (history == null)
161: return null;
162: MessageInfo mi = history[row];
163: if (col == 0)
164: return getCol_0(mi);
165: if (col == 1)
166: return getCol_1(mi);
167: if (col == 2)
168: return getCol_2(mi);
169: if (col == 3)
170: return getCol_3(mi);
171: throw new RuntimeException("Invalid column '" + col + "'");
172: }
173:
174: private Object getCol_0(MessageInfo mi) {
175: if (mi.isIncoming()) {
176: if (mi.isResponse())
177: return INCOMING_RESP_ICON;
178: return INCOMING_ICON;
179: }
180: if (mi.isResponse())
181: return OUTGOING_RESP_ICON;
182: return OUTGOING_ICON;
183: }
184:
185: private Object getCol_1(MessageInfo mi) {
186: return GUIUtil.format(mi.getCreatedTime());
187: }
188:
189: private Object getCol_2(MessageInfo mi) {
190: return mi.getChannel();
191: }
192:
193: private Object getCol_3(MessageInfo mi) {
194: if (mi.getNode() == null)
195: return "";
196: return mi.getNode().getName();
197: }
198: }
199:
200: public class MessageTableCellRendere extends
201: DefaultTableCellRenderer {
202: public Component getTableCellRendererComponent(JTable table,
203: Object value, boolean isSelected, boolean hasFocus,
204: int row, int column) {
205: // super.getTableCellRendererComponent(table, value, isSelected,
206: // hasFocus, row, column);
207: if (value instanceof Icon)
208: setIcon((Icon) value);
209: else
210: setIcon(null);
211: setText(null);
212: return this;
213: }
214: }
215: }
|