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.Color;
016: import java.awt.Component;
017: import java.awt.Dimension;
018: import java.awt.event.ItemEvent;
019: import java.awt.event.ItemListener;
020: import java.beans.PropertyChangeEvent;
021: import java.beans.PropertyChangeListener;
022:
023: import javax.swing.AbstractListModel;
024: import javax.swing.ComboBoxModel;
025: import javax.swing.DefaultListCellRenderer;
026: import javax.swing.JButton;
027: import javax.swing.JComboBox;
028: import javax.swing.JComponent;
029: import javax.swing.JLabel;
030: import javax.swing.JList;
031: import javax.swing.JPanel;
032: import javax.swing.JScrollPane;
033: import javax.swing.ScrollPaneConstants;
034:
035: import com.eviware.soapui.impl.wsdl.actions.support.ShowOnlineHelpAction;
036: import com.eviware.soapui.impl.wsdl.loadtest.WsdlLoadTest;
037: import com.eviware.soapui.impl.wsdl.loadtest.data.actions.ExportStatisticsHistoryAction;
038: import com.eviware.soapui.impl.wsdl.support.HelpUrls;
039: import com.eviware.soapui.model.support.TestSuiteListenerAdapter;
040: import com.eviware.soapui.model.testsuite.TestStep;
041: import com.eviware.soapui.support.UISupport;
042: import com.eviware.soapui.support.components.JXToolBar;
043: import com.eviware.soapui.ui.support.DefaultDesktopPanel;
044:
045: /**
046: * DesktopPanel for Statistics Graphs
047: *
048: * @author Ole.Matzura
049: */
050:
051: public class StatisticsDesktopPanel extends DefaultDesktopPanel {
052: private JPanel panel;
053: private final WsdlLoadTest loadTest;
054: private JStatisticsGraph statisticsGraph;
055: private JButton exportButton;
056: private SelectStepComboBoxModel selectStepComboBoxModel;
057: private InternalPropertyChangeListener propertyChangeListener = new InternalPropertyChangeListener();
058: private JComboBox resolutionCombo;
059:
060: public StatisticsDesktopPanel(WsdlLoadTest loadTest) {
061: super ("Statistics for [" + loadTest.getName() + "]", null, null);
062: this .loadTest = loadTest;
063:
064: loadTest.addPropertyChangeListener(propertyChangeListener);
065:
066: buildUI();
067: }
068:
069: private void buildUI() {
070: statisticsGraph = new JStatisticsGraph(loadTest);
071:
072: JScrollPane scrollPane = new JScrollPane(statisticsGraph);
073: scrollPane.getViewport().setBackground(Color.WHITE);
074: scrollPane
075: .setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
076:
077: panel = UISupport
078: .buildPanelWithToolbarAndStatusBar(buildToolbar(),
079: scrollPane, statisticsGraph.getLegend());
080: panel.setPreferredSize(new Dimension(600, 400));
081: }
082:
083: private JComponent buildToolbar() {
084: exportButton = UISupport
085: .createToolbarButton(new ExportStatisticsHistoryAction(
086: statisticsGraph));
087:
088: JXToolBar toolbar = UISupport.createToolbar();
089:
090: toolbar.addSpace(5);
091: toolbar.addLabeledFixed("Select Step:", buildSelectStepCombo());
092: toolbar.addUnrelatedGap();
093: toolbar.addLabeledFixed("Resolution:", buildResolutionCombo());
094: toolbar.addGlue();
095: toolbar.addFixed(exportButton);
096: toolbar.addFixed(UISupport
097: .createToolbarButton(new ShowOnlineHelpAction(
098: HelpUrls.STATISTICSGRAPH_HELP_URL)));
099:
100: return toolbar;
101: }
102:
103: private JComponent buildResolutionCombo() {
104: resolutionCombo = new JComboBox(new String[] { "data", "250",
105: "500", "1000" });
106: resolutionCombo.setEditable(true);
107: resolutionCombo
108: .setToolTipText("Sets update interval of graph in milliseconds");
109: long resolution = statisticsGraph.getResolution();
110: resolutionCombo.setSelectedItem(resolution == 0 ? "data"
111: : String.valueOf(resolution));
112: resolutionCombo.addItemListener(new ItemListener() {
113:
114: public void itemStateChanged(ItemEvent e) {
115: try {
116: String value = resolutionCombo.getSelectedItem()
117: .toString();
118: long resolution = value.equals("data") ? 0 : Long
119: .parseLong(value);
120: if (resolution != statisticsGraph.getResolution()) {
121: statisticsGraph.setResolution(resolution);
122: }
123: } catch (Exception ex) {
124: long resolution = statisticsGraph.getResolution();
125: resolutionCombo
126: .setSelectedItem(resolution == 0 ? "data"
127: : String.valueOf(resolution));
128: }
129: }
130: });
131: return resolutionCombo;
132: }
133:
134: private JComponent buildSelectStepCombo() {
135: selectStepComboBoxModel = new SelectStepComboBoxModel();
136: JComboBox selectStepCombo = new JComboBox(
137: selectStepComboBoxModel);
138: selectStepCombo.setRenderer(new TestStepCellRenderer());
139: return selectStepCombo;
140: }
141:
142: public JComponent getComponent() {
143: return panel;
144: }
145:
146: private final class InternalPropertyChangeListener implements
147: PropertyChangeListener {
148: public void propertyChange(PropertyChangeEvent evt) {
149: if (evt.getPropertyName()
150: .equals(WsdlLoadTest.NAME_PROPERTY)) {
151: setTitle("Statistics for [" + loadTest.getName() + "]");
152: }
153: }
154: }
155:
156: private class SelectStepComboBoxModel extends AbstractListModel
157: implements ComboBoxModel {
158: private TestStep selectedStep;
159: private InternalTestSuiteListener testSuiteListener = new InternalTestSuiteListener();
160:
161: public SelectStepComboBoxModel() {
162: loadTest.getTestCase().getTestSuite().addTestSuiteListener(
163: testSuiteListener);
164: }
165:
166: public void setSelectedItem(Object anItem) {
167: if (anItem == selectedStep)
168: return;
169:
170: if (anItem == null || anItem.equals("Total"))
171: selectedStep = null;
172:
173: if (anItem instanceof TestStep) {
174: selectedStep = (TestStep) anItem;
175: }
176:
177: statisticsGraph.setTestStep(selectedStep);
178: }
179:
180: public Object getSelectedItem() {
181: return selectedStep == null ? "Total" : selectedStep;
182: }
183:
184: public int getSize() {
185: return loadTest.getTestCase().getTestStepCount() + 1;
186: }
187:
188: public Object getElementAt(int index) {
189: return index == getSize() - 1 ? "Total" : loadTest
190: .getTestCase().getTestStepAt(index);
191: }
192:
193: private final class InternalTestSuiteListener extends
194: TestSuiteListenerAdapter {
195: public void testStepAdded(TestStep testStep, int index) {
196: if (testStep.getTestCase() == loadTest.getTestCase()) {
197: fireIntervalAdded(SelectStepComboBoxModel.this ,
198: index, index);
199: }
200: }
201:
202: public void testStepRemoved(TestStep testStep, int index) {
203: if (testStep.getTestCase() == loadTest.getTestCase()) {
204: if (selectedStep == testStep) {
205: setSelectedItem(null);
206: fireContentsChanged(
207: SelectStepComboBoxModel.this , -1, -1);
208: }
209:
210: fireIntervalRemoved(SelectStepComboBoxModel.this ,
211: index, index);
212: }
213: }
214: }
215:
216: public void release() {
217: loadTest.getTestCase().getTestSuite()
218: .removeTestSuiteListener(testSuiteListener);
219: }
220: }
221:
222: private final static class TestStepCellRenderer extends
223: DefaultListCellRenderer {
224: public Component getListCellRendererComponent(JList list,
225: Object value, int index, boolean isSelected,
226: boolean cellHasFocus) {
227: JLabel label = (JLabel) super .getListCellRendererComponent(
228: list, value, index, isSelected, cellHasFocus);
229:
230: if (value instanceof TestStep)
231: label.setText(((TestStep) value).getName());
232:
233: return label;
234: }
235: }
236:
237: public boolean onClose(boolean canCancel) {
238: selectStepComboBoxModel.release();
239: loadTest.removePropertyChangeListener(propertyChangeListener);
240: statisticsGraph.release();
241:
242: return super.onClose(canCancel);
243: }
244: }
|