View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of version 2.1 of the GNU Lesser General Public License as published by 
6    *  the Free Software Foundation.
7    *
8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
10   *  See the GNU Lesser General Public License for more details at gnu.org.
11   */
12  
13  package com.eviware.soapui.impl.wsdl.panels.loadtest;
14  
15  import java.awt.Color;
16  import java.awt.Component;
17  import java.awt.Dimension;
18  import java.awt.event.ItemEvent;
19  import java.awt.event.ItemListener;
20  import java.beans.PropertyChangeEvent;
21  import java.beans.PropertyChangeListener;
22  
23  import javax.swing.AbstractListModel;
24  import javax.swing.ComboBoxModel;
25  import javax.swing.DefaultListCellRenderer;
26  import javax.swing.JButton;
27  import javax.swing.JComboBox;
28  import javax.swing.JComponent;
29  import javax.swing.JLabel;
30  import javax.swing.JList;
31  import javax.swing.JPanel;
32  import javax.swing.JScrollPane;
33  import javax.swing.ScrollPaneConstants;
34  
35  import com.eviware.soapui.impl.support.actions.ShowOnlineHelpAction;
36  import com.eviware.soapui.impl.wsdl.loadtest.WsdlLoadTest;
37  import com.eviware.soapui.impl.wsdl.loadtest.data.actions.ExportStatisticsHistoryAction;
38  import com.eviware.soapui.impl.wsdl.support.HelpUrls;
39  import com.eviware.soapui.model.support.TestSuiteListenerAdapter;
40  import com.eviware.soapui.model.testsuite.TestStep;
41  import com.eviware.soapui.support.UISupport;
42  import com.eviware.soapui.support.components.JXToolBar;
43  import com.eviware.soapui.ui.support.DefaultDesktopPanel;
44  
45  /***
46   * DesktopPanel for Statistics Graphs
47   * 
48   * @author Ole.Matzura
49   */
50  
51  public class StatisticsDesktopPanel extends DefaultDesktopPanel
52  {
53  	private JPanel panel;
54  	private final WsdlLoadTest loadTest;
55  	private JStatisticsGraph statisticsGraph;
56  	private JButton exportButton;
57  	private SelectStepComboBoxModel selectStepComboBoxModel;
58  	private InternalPropertyChangeListener propertyChangeListener = new InternalPropertyChangeListener();
59  	private JComboBox resolutionCombo;
60  
61  	public StatisticsDesktopPanel( WsdlLoadTest loadTest )
62  	{
63  		super( "Statistics for [" + loadTest.getName() + "]", null, null );
64  		this.loadTest = loadTest;
65  
66  		loadTest.addPropertyChangeListener( propertyChangeListener );
67  
68  		buildUI();
69  	}
70  
71  	private void buildUI()
72  	{
73  		statisticsGraph = new JStatisticsGraph( loadTest );
74  
75  		JScrollPane scrollPane = new JScrollPane( statisticsGraph );
76  		scrollPane.getViewport().setBackground( Color.WHITE );
77  		scrollPane.setHorizontalScrollBarPolicy( ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS );
78  
79  		panel = UISupport.buildPanelWithToolbarAndStatusBar( buildToolbar(), scrollPane, statisticsGraph.getLegend() );
80  		panel.setPreferredSize( new Dimension( 600, 400 ) );
81  	}
82  
83  	private JComponent buildToolbar()
84  	{
85  		exportButton = UISupport.createToolbarButton( new ExportStatisticsHistoryAction( statisticsGraph ) );
86  
87  		JXToolBar toolbar = UISupport.createToolbar();
88  
89  		toolbar.addSpace( 5 );
90  		toolbar.addLabeledFixed( "Select Step:", buildSelectStepCombo() );
91  		toolbar.addUnrelatedGap();
92  		toolbar.addLabeledFixed( "Resolution:", buildResolutionCombo() );
93  		toolbar.addGlue();
94  		toolbar.addFixed( exportButton );
95  		toolbar.addFixed( UISupport.createToolbarButton( new ShowOnlineHelpAction( HelpUrls.STATISTICSGRAPH_HELP_URL ) ) );
96  
97  		return toolbar;
98  	}
99  
100 	private JComponent buildResolutionCombo()
101 	{
102 		resolutionCombo = new JComboBox( new String[] { "data", "250", "500", "1000" } );
103 		resolutionCombo.setEditable( true );
104 		resolutionCombo.setToolTipText( "Sets update interval of graph in milliseconds" );
105 		long resolution = statisticsGraph.getResolution();
106 		resolutionCombo.setSelectedItem( resolution == 0 ? "data" : String.valueOf( resolution ) );
107 		resolutionCombo.addItemListener( new ItemListener()
108 		{
109 
110 			public void itemStateChanged( ItemEvent e )
111 			{
112 				try
113 				{
114 					String value = resolutionCombo.getSelectedItem().toString();
115 					long resolution = value.equals( "data" ) ? 0 : Long.parseLong( value );
116 					if( resolution != statisticsGraph.getResolution() )
117 					{
118 						statisticsGraph.setResolution( resolution );
119 					}
120 				}
121 				catch( Exception ex )
122 				{
123 					long resolution = statisticsGraph.getResolution();
124 					resolutionCombo.setSelectedItem( resolution == 0 ? "data" : String.valueOf( resolution ) );
125 				}
126 			}
127 		} );
128 		return resolutionCombo;
129 	}
130 
131 	private JComponent buildSelectStepCombo()
132 	{
133 		selectStepComboBoxModel = new SelectStepComboBoxModel();
134 		JComboBox selectStepCombo = new JComboBox( selectStepComboBoxModel );
135 		selectStepCombo.setRenderer( new TestStepCellRenderer() );
136 		return selectStepCombo;
137 	}
138 
139 	public JComponent getComponent()
140 	{
141 		return panel;
142 	}
143 
144 	private final class InternalPropertyChangeListener implements PropertyChangeListener
145 	{
146 		public void propertyChange( PropertyChangeEvent evt )
147 		{
148 			if( evt.getPropertyName().equals( WsdlLoadTest.NAME_PROPERTY ) )
149 			{
150 				setTitle( "Statistics for [" + loadTest.getName() + "]" );
151 			}
152 		}
153 	}
154 
155 	private class SelectStepComboBoxModel extends AbstractListModel implements ComboBoxModel
156 	{
157 		private TestStep selectedStep;
158 		private InternalTestSuiteListener testSuiteListener = new InternalTestSuiteListener();
159 
160 		public SelectStepComboBoxModel()
161 		{
162 			loadTest.getTestCase().getTestSuite().addTestSuiteListener( testSuiteListener );
163 		}
164 
165 		public void setSelectedItem( Object anItem )
166 		{
167 			if( anItem == selectedStep )
168 				return;
169 
170 			if( anItem == null || anItem.equals( "Total" ) )
171 				selectedStep = null;
172 
173 			if( anItem instanceof TestStep )
174 			{
175 				selectedStep = ( TestStep )anItem;
176 			}
177 
178 			statisticsGraph.setTestStep( selectedStep );
179 		}
180 
181 		public Object getSelectedItem()
182 		{
183 			return selectedStep == null ? "Total" : selectedStep;
184 		}
185 
186 		public int getSize()
187 		{
188 			return loadTest.getTestCase().getTestStepCount() + 1;
189 		}
190 
191 		public Object getElementAt( int index )
192 		{
193 			return index == getSize() - 1 ? "Total" : loadTest.getTestCase().getTestStepAt( index );
194 		}
195 
196 		private final class InternalTestSuiteListener extends TestSuiteListenerAdapter
197 		{
198 			public void testStepAdded( TestStep testStep, int index )
199 			{
200 				if( testStep.getTestCase() == loadTest.getTestCase() )
201 				{
202 					fireIntervalAdded( SelectStepComboBoxModel.this, index, index );
203 				}
204 			}
205 
206 			public void testStepRemoved( TestStep testStep, int index )
207 			{
208 				if( testStep.getTestCase() == loadTest.getTestCase() )
209 				{
210 					if( selectedStep == testStep )
211 					{
212 						setSelectedItem( null );
213 						fireContentsChanged( SelectStepComboBoxModel.this, -1, -1 );
214 					}
215 
216 					fireIntervalRemoved( SelectStepComboBoxModel.this, index, index );
217 				}
218 			}
219 		}
220 
221 		public void release()
222 		{
223 			loadTest.getTestCase().getTestSuite().removeTestSuiteListener( testSuiteListener );
224 		}
225 	}
226 
227 	private final static class TestStepCellRenderer extends DefaultListCellRenderer
228 	{
229 		public Component getListCellRendererComponent( JList list, Object value, int index, boolean isSelected,
230 				boolean cellHasFocus )
231 		{
232 			JLabel label = ( JLabel )super.getListCellRendererComponent( list, value, index, isSelected, cellHasFocus );
233 
234 			if( value instanceof TestStep )
235 				label.setText( ( ( TestStep )value ).getName() );
236 
237 			return label;
238 		}
239 	}
240 
241 	public boolean onClose( boolean canCancel )
242 	{
243 		selectStepComboBoxModel.release();
244 		loadTest.removePropertyChangeListener( propertyChangeListener );
245 		statisticsGraph.release();
246 
247 		return super.onClose( canCancel );
248 	}
249 }