001: /*
002: * soapUI, copyright (C) 2004-2007 eviware.com
003: *
004: * soapUI is free software; you can redistribute it and/or modify it under the
005: * terms of version 2.1 of the GNU Lesser General Public License as published by
006: * the Free Software Foundation.
007: *
008: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
010: * See the GNU Lesser General Public License for more details at gnu.org.
011: */
012:
013: package com.eviware.soapui.impl.wsdl.panels.loadtest;
014:
015: import java.awt.BorderLayout;
016: import java.awt.Color;
017: import java.awt.Component;
018: import java.awt.event.ActionEvent;
019: import java.awt.event.MouseAdapter;
020: import java.awt.event.MouseEvent;
021:
022: import javax.swing.AbstractAction;
023: import javax.swing.BorderFactory;
024: import javax.swing.JMenu;
025: import javax.swing.JPanel;
026: import javax.swing.JPopupMenu;
027: import javax.swing.JScrollPane;
028: import javax.swing.JTable;
029: import javax.swing.table.TableCellRenderer;
030: import javax.swing.table.TableColumnModel;
031:
032: import org.jdesktop.swingx.JXTable;
033:
034: import com.eviware.soapui.impl.wsdl.loadtest.LoadTestAssertion;
035: import com.eviware.soapui.impl.wsdl.loadtest.WsdlLoadTest;
036: import com.eviware.soapui.impl.wsdl.loadtest.assertions.LoadTestAssertionRegistry;
037: import com.eviware.soapui.model.testsuite.TestStep;
038: import com.eviware.soapui.support.action.swing.ActionList;
039: import com.eviware.soapui.support.action.swing.ActionListBuilder;
040: import com.eviware.soapui.support.action.swing.ActionSupport;
041:
042: /**
043: * Table for displaying real-time LoadTest Statistics
044: *
045: * @author Ole.Matzura
046: */
047:
048: public class JStatisticsTable extends JPanel {
049: private final WsdlLoadTest loadTest;
050: private JXTable statisticsTable;
051: private JPopupMenu popup;
052:
053: public JStatisticsTable(WsdlLoadTest loadTest) {
054: super (new BorderLayout());
055: this .loadTest = loadTest;
056:
057: statisticsTable = new JXTable(loadTest.getStatisticsModel());
058: statisticsTable.setColumnControlVisible(true);
059: statisticsTable.getTableHeader().setReorderingAllowed(false);
060:
061: statisticsTable
062: .addMouseListener(new StatisticsTableMouseListener());
063:
064: TableColumnModel columnModel = statisticsTable.getColumnModel();
065: columnModel.getColumn(0).setMaxWidth(5);
066: columnModel.getColumn(0).setCellRenderer(
067: new ColorLabelTableCellRenderer());
068: columnModel.getColumn(1).setPreferredWidth(150);
069: columnModel.getColumn(2).setPreferredWidth(20);
070: columnModel.getColumn(3).setPreferredWidth(20);
071: columnModel.getColumn(4).setPreferredWidth(20);
072: columnModel.getColumn(5).setPreferredWidth(20);
073: columnModel.getColumn(6).setPreferredWidth(20);
074: columnModel.getColumn(7).setPreferredWidth(20);
075: columnModel.getColumn(8).setPreferredWidth(20);
076: columnModel.getColumn(9).setPreferredWidth(20);
077: columnModel.getColumn(10).setPreferredWidth(20);
078:
079: JScrollPane scrollPane = new JScrollPane(statisticsTable);
080: scrollPane.setBorder(BorderFactory
081: .createEmptyBorder(3, 3, 3, 3));
082: add(scrollPane, BorderLayout.CENTER);
083:
084: JMenu assertionsMenu = new JMenu("Add Assertion");
085: for (String assertion : LoadTestAssertionRegistry
086: .getAvailableAssertions()) {
087: assertionsMenu.add(new AddAssertionAction(assertion));
088: }
089:
090: popup = new JPopupMenu();
091: popup.add(assertionsMenu);
092: popup.setInvoker(statisticsTable);
093: }
094:
095: public void release() {
096: loadTest.getStatisticsModel().removeTableModelListener(
097: statisticsTable);
098: }
099:
100: private final class StatisticsTableMouseListener extends
101: MouseAdapter {
102: public void mouseClicked(MouseEvent e) {
103: if (statisticsTable.getSelectedColumn() == 1
104: && e.getClickCount() > 1) {
105: int row = statisticsTable.getSelectedRow();
106: if (row < 0)
107: return;
108:
109: row = statisticsTable.convertRowIndexToModel(row);
110: if (row == statisticsTable.getRowCount() - 1)
111: return;
112:
113: TestStep testStep = loadTest.getStatisticsModel()
114: .getTestStepAtRow(row);
115: ActionList actions = ActionListBuilder
116: .buildActions(testStep);
117: if (actions != null)
118: actions.performDefaultAction(new ActionEvent(
119: statisticsTable, 0, null));
120: }
121: }
122:
123: public void mousePressed(MouseEvent e) {
124: if (e.isPopupTrigger()) {
125: showPopup(e);
126: }
127: }
128:
129: public void mouseReleased(MouseEvent e) {
130: if (e.isPopupTrigger()) {
131: showPopup(e);
132: }
133: }
134: }
135:
136: private static final class ColorLabelTableCellRenderer extends
137: JPanel implements TableCellRenderer {
138: private Color bgColor;
139:
140: public ColorLabelTableCellRenderer() {
141: super ();
142:
143: bgColor = getBackground();
144: }
145:
146: public Component getTableCellRendererComponent(JTable table,
147: Object value, boolean isSelected, boolean hasFocus,
148: int row, int column) {
149: if (value instanceof Color)
150: setBackground((Color) value);
151: else
152: setBackground(bgColor);
153:
154: return this ;
155: }
156: }
157:
158: public void showPopup(MouseEvent e) {
159: int row = statisticsTable.rowAtPoint(e.getPoint());
160: if (row == -1)
161: return;
162:
163: if (statisticsTable.getSelectedRow() != row) {
164: statisticsTable.getSelectionModel().setSelectionInterval(
165: row, row);
166: }
167:
168: row = statisticsTable.convertRowIndexToModel(row);
169:
170: while (popup.getComponentCount() > 1)
171: popup.remove(1);
172:
173: if (row < statisticsTable.getRowCount() - 1) {
174: TestStep testStep = loadTest.getStatisticsModel()
175: .getTestStepAtRow(row);
176: ActionSupport.addActions(ActionListBuilder
177: .buildActions(testStep), popup);
178: }
179:
180: popup.setLocation((int) (statisticsTable.getLocationOnScreen()
181: .getX() + e.getPoint().getX()), (int) (statisticsTable
182: .getLocationOnScreen().getY() + e.getPoint().getY()));
183: popup.setVisible(true);
184: }
185:
186: private class AddAssertionAction extends AbstractAction {
187: private final String type;
188:
189: public AddAssertionAction(String type) {
190: super (type);
191: this .type = type;
192: }
193:
194: public void actionPerformed(ActionEvent e) {
195: int row = statisticsTable.getSelectedRow();
196: if (row == -1)
197: return;
198:
199: String target = LoadTestAssertion.ANY_TEST_STEP;
200:
201: row = statisticsTable.convertRowIndexToModel(row);
202:
203: if (row == statisticsTable.getRowCount() - 1)
204: target = LoadTestAssertion.ALL_TEST_STEPS;
205: else if (row >= 0)
206: target = loadTest.getTestCase().getTestStepAt(row)
207: .getName();
208:
209: loadTest.addAssertion(type, target, true);
210: }
211: }
212: }
|