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.Component;
017: import java.awt.Toolkit;
018: import java.awt.event.ActionEvent;
019: import java.awt.event.MouseAdapter;
020: import java.awt.event.MouseEvent;
021: import java.beans.PropertyChangeEvent;
022: import java.beans.PropertyChangeListener;
023:
024: import javax.swing.AbstractAction;
025: import javax.swing.Action;
026: import javax.swing.BorderFactory;
027: import javax.swing.Icon;
028: import javax.swing.ImageIcon;
029: import javax.swing.JButton;
030: import javax.swing.JComponent;
031: import javax.swing.JPanel;
032: import javax.swing.JPopupMenu;
033: import javax.swing.JScrollPane;
034: import javax.swing.JTable;
035: import javax.swing.ListSelectionModel;
036: import javax.swing.event.ListSelectionEvent;
037: import javax.swing.event.ListSelectionListener;
038: import javax.swing.table.AbstractTableModel;
039: import javax.swing.table.DefaultTableCellRenderer;
040: import javax.swing.table.TableColumnModel;
041:
042: import org.jdesktop.swingx.JXTable;
043:
044: import com.eviware.soapui.impl.wsdl.loadtest.LoadTestAssertion;
045: import com.eviware.soapui.impl.wsdl.loadtest.LoadTestListener;
046: import com.eviware.soapui.impl.wsdl.loadtest.WsdlLoadTest;
047: import com.eviware.soapui.impl.wsdl.loadtest.assertions.LoadTestAssertionRegistry;
048: import com.eviware.soapui.impl.wsdl.support.Configurable;
049: import com.eviware.soapui.support.UISupport;
050: import com.eviware.soapui.support.components.JXToolBar;
051:
052: /**
053: * Table showing configured assertions for a WsdlLoadTest
054: *
055: * @todo add popup menu
056: *
057: * @author Ole.Matzura
058: */
059:
060: public class JLoadTestAssertionsTable extends JPanel {
061: private JXTable table;
062: private final WsdlLoadTest loadTest;
063: private ConfigureAssertionAction configureAssertionAction;
064: private RemoveAssertionAction removeAssertionAction;
065: private AddLoadTestAssertionAction addLoadTestAssertionAction;
066: private LoadTestAssertionsTableModel tableModel;
067: private JPopupMenu assertionPopup;
068: private InternalLoadTestListener internalLoadTestListener = new InternalLoadTestListener();
069:
070: public JLoadTestAssertionsTable(WsdlLoadTest wsdlLoadTest) {
071: super (new BorderLayout());
072: this .loadTest = wsdlLoadTest;
073:
074: loadTest.addLoadTestListener(internalLoadTestListener);
075:
076: tableModel = new LoadTestAssertionsTableModel();
077: table = new JXTable(tableModel);
078: table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
079:
080: TableColumnModel columnModel = table.getColumnModel();
081: columnModel.getColumn(0).setMaxWidth(16);
082: columnModel.getColumn(0).setCellRenderer(
083: new IconTableCellRenderer());
084: columnModel.getColumn(1).setPreferredWidth(100);
085: columnModel.getColumn(2).setPreferredWidth(100);
086: columnModel.getColumn(3).setPreferredWidth(200);
087:
088: JScrollPane scrollPane = new JScrollPane(table);
089: add(scrollPane, BorderLayout.CENTER);
090:
091: table.addMouseListener(new MouseAdapter() {
092:
093: public void mouseClicked(MouseEvent e) {
094: if (e.getClickCount() < 2)
095: return;
096:
097: int ix = table.getSelectedRow();
098: if (ix == -1)
099: return;
100: ix = table.convertRowIndexToModel(ix);
101:
102: Object obj = loadTest.getAssertionAt(ix);
103: if (obj instanceof Configurable) {
104: ((Configurable) obj).configure();
105: } else
106: Toolkit.getDefaultToolkit().beep();
107: }
108: });
109:
110: add(buildToolbar(), BorderLayout.NORTH);
111:
112: table.getSelectionModel().addListSelectionListener(
113: new ListSelectionListener() {
114:
115: public void valueChanged(ListSelectionEvent e) {
116: int ix = table.getSelectedRow();
117:
118: configureAssertionAction.setEnabled(ix >= 0);
119: removeAssertionAction.setEnabled(ix >= 0);
120:
121: if (ix == -1)
122: return;
123:
124: ix = table.convertRowIndexToModel(ix);
125: configureAssertionAction
126: .setEnabled(loadTest.getAssertionAt(ix) instanceof Configurable);
127: }
128: });
129:
130: assertionPopup = new JPopupMenu();
131: assertionPopup.add(configureAssertionAction);
132: assertionPopup.addSeparator();
133: assertionPopup.add(addLoadTestAssertionAction);
134: assertionPopup.add(removeAssertionAction);
135:
136: setComponentPopupMenu(assertionPopup);
137:
138: scrollPane.setInheritsPopupMenu(true);
139: table.setComponentPopupMenu(assertionPopup);
140: }
141:
142: public void addNotify() {
143: super .addNotify();
144: loadTest.removeLoadTestListener(internalLoadTestListener);
145: }
146:
147: public void removeNotify() {
148: super .removeNotify();
149: loadTest.removeLoadTestListener(internalLoadTestListener);
150: }
151:
152: public void release() {
153: tableModel.release();
154: }
155:
156: private JComponent buildToolbar() {
157: configureAssertionAction = new ConfigureAssertionAction();
158: removeAssertionAction = new RemoveAssertionAction();
159: addLoadTestAssertionAction = new AddLoadTestAssertionAction();
160:
161: JXToolBar toolbar = UISupport.createToolbar();
162:
163: JButton button = UISupport
164: .createToolbarButton(addLoadTestAssertionAction);
165: button.setText(null);
166: toolbar.addFixed(button);
167: button = UISupport.createToolbarButton(removeAssertionAction);
168: button.setText(null);
169: toolbar.addFixed(button);
170: button = UISupport
171: .createToolbarButton(configureAssertionAction);
172: button.setText(null);
173: toolbar.addFixed(button);
174: toolbar.setBorder(BorderFactory.createEmptyBorder(0, 0, 2, 0));
175:
176: return toolbar;
177: }
178:
179: private class LoadTestAssertionsTableModel extends
180: AbstractTableModel implements PropertyChangeListener {
181: public LoadTestAssertionsTableModel() {
182: for (int c = 0; c < loadTest.getAssertionCount(); c++) {
183: loadTest.getAssertionAt(c).addPropertyChangeListener(
184: LoadTestAssertion.CONFIGURATION_PROPERTY, this );
185: }
186: }
187:
188: public void release() {
189: for (int c = 0; c < loadTest.getAssertionCount(); c++) {
190: loadTest
191: .getAssertionAt(c)
192: .removePropertyChangeListener(
193: LoadTestAssertion.CONFIGURATION_PROPERTY,
194: this );
195: }
196: }
197:
198: public int getRowCount() {
199: return loadTest.getAssertionCount();
200: }
201:
202: public int getColumnCount() {
203: return 4;
204: }
205:
206: public Class<?> getColumnClass(int columnIndex) {
207: switch (columnIndex) {
208: case 0:
209: return ImageIcon.class;
210: default:
211: return String.class;
212: }
213: }
214:
215: public String getColumnName(int column) {
216: switch (column) {
217: case 0:
218: return " ";
219: case 1:
220: return "Name";
221: case 2:
222: return "Step";
223: case 3:
224: return "Details";
225: }
226:
227: return null;
228: }
229:
230: public Object getValueAt(int rowIndex, int columnIndex) {
231: LoadTestAssertion assertion = loadTest
232: .getAssertionAt(rowIndex);
233:
234: switch (columnIndex) {
235: case 0:
236: return assertion.getIcon();
237: case 1:
238: return assertion.getName();
239: case 2:
240: return assertion.getTargetStep();
241: case 3:
242: return assertion.getDescription();
243: }
244:
245: return null;
246: }
247:
248: public void assertionRemoved(LoadTestAssertion assertion) {
249: assertion.removePropertyChangeListener(
250: LoadTestAssertion.CONFIGURATION_PROPERTY, this );
251: fireTableDataChanged();
252: }
253:
254: public void assertionAdded(LoadTestAssertion assertion) {
255: assertion.addPropertyChangeListener(
256: LoadTestAssertion.CONFIGURATION_PROPERTY, this );
257: fireTableRowsInserted(getRowCount() - 1, getRowCount() - 1);
258: }
259:
260: public void propertyChange(PropertyChangeEvent evt) {
261: fireTableDataChanged();
262: }
263: }
264:
265: private static final class IconTableCellRenderer extends
266: DefaultTableCellRenderer {
267: public Component getTableCellRendererComponent(JTable table,
268: Object value, boolean isSelected, boolean hasFocus,
269: int row, int column) {
270: if (value != null)
271: setIcon((Icon) value);
272:
273: if (isSelected) {
274: setBackground(table.getSelectionBackground());
275: setForeground(table.getSelectionForeground());
276: } else {
277: setBackground(table.getBackground());
278: setForeground(table.getForeground());
279: }
280:
281: return this ;
282: }
283: }
284:
285: public class AddLoadTestAssertionAction extends AbstractAction {
286: public AddLoadTestAssertionAction() {
287: super ("Add Assertion");
288: putValue(Action.SHORT_DESCRIPTION,
289: "Adds an assertion to this LoadTest");
290: putValue(Action.SMALL_ICON, UISupport
291: .createImageIcon("/addAssertion.gif"));
292: }
293:
294: public void actionPerformed(ActionEvent e) {
295: String[] types = LoadTestAssertionRegistry
296: .getAvailableAssertions();
297: String type = (String) UISupport.prompt(
298: "Select assertion type to add", "Add Assertion",
299: types);
300: if (type != null) {
301: loadTest.addAssertion(type,
302: LoadTestAssertion.ANY_TEST_STEP, true);
303: }
304: }
305: }
306:
307: public class ConfigureAssertionAction extends AbstractAction {
308: ConfigureAssertionAction() {
309: super ("Configure");
310: putValue(Action.SHORT_DESCRIPTION,
311: "Configures the selection assertion");
312: putValue(Action.SMALL_ICON, UISupport
313: .createImageIcon("/options.gif"));
314: setEnabled(false);
315: }
316:
317: public void actionPerformed(ActionEvent e) {
318: int ix = table.getSelectedRow();
319: if (ix == -1)
320: return;
321: ix = table.convertRowIndexToModel(ix);
322:
323: Object obj = loadTest.getAssertionAt(ix);
324: if (obj instanceof Configurable) {
325: ((Configurable) obj).configure();
326: tableModel.fireTableRowsUpdated(ix, ix);
327: } else
328: Toolkit.getDefaultToolkit().beep();
329: }
330: }
331:
332: public class RemoveAssertionAction extends AbstractAction {
333: public RemoveAssertionAction() {
334: super ("Remove Assertion");
335: putValue(Action.SHORT_DESCRIPTION,
336: "Removes the selected assertion from this LoadTest");
337: putValue(Action.SMALL_ICON, UISupport
338: .createImageIcon("/remove_assertion.gif"));
339: setEnabled(false);
340: }
341:
342: public void actionPerformed(ActionEvent e) {
343: int ix = table.getSelectedRow();
344: if (ix == -1)
345: return;
346: ix = table.convertRowIndexToModel(ix);
347:
348: LoadTestAssertion assertion = loadTest.getAssertionAt(ix);
349: if (UISupport.confirm("Remove assertion ["
350: + assertion.getName() + "]", "Remove Assertion")) {
351: loadTest.removeAssertion(assertion);
352: }
353: }
354: }
355:
356: public class InternalLoadTestListener implements LoadTestListener {
357: public void assertionAdded(LoadTestAssertion assertion) {
358: tableModel.assertionAdded(assertion);
359: table.getSelectionModel().setSelectionInterval(
360: tableModel.getRowCount() - 1,
361: tableModel.getRowCount() - 1);
362: }
363:
364: public void assertionRemoved(LoadTestAssertion assertion) {
365: tableModel.assertionRemoved(assertion);
366: }
367: }
368: }
|