View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2007 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.wsdl.actions.support.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( WsdlLoadTest.NAME_PROPERTY, 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(), 
80  				scrollPane, statisticsGraph.getLegend() );
81  		panel.setPreferredSize( new Dimension( 600, 400 ));
82  	}
83  
84  	private JComponent buildToolbar()
85  	{
86  		exportButton = UISupport.createToolbarButton( new ExportStatisticsHistoryAction( statisticsGraph ) );
87  		
88  		JXToolBar toolbar = UISupport.createToolbar();
89  		
90  		toolbar.addSpace( 5 );
91  		toolbar.addLabeledFixed( "Select Step:", buildSelectStepCombo() );
92  		toolbar.addUnrelatedGap();
93  		toolbar.addLabeledFixed( "Resolution:", buildResolutionCombo() );
94  		toolbar.addGlue();
95  		toolbar.addFixed( exportButton );
96  		toolbar.addFixed( UISupport.createToolbarButton( new ShowOnlineHelpAction( HelpUrls.STATISTICSGRAPH_HELP_URL )));
97  
98  		return toolbar; 
99  	}
100 
101 	private JComponent buildResolutionCombo()
102 	{
103 		resolutionCombo = new JComboBox( new String[] {"data", "250", "500", "1000"} );
104 		resolutionCombo.setEditable( true );
105 		resolutionCombo.setToolTipText( "Sets update interval of graph in milliseconds" );
106 		long resolution = statisticsGraph.getResolution();
107 		resolutionCombo.setSelectedItem( resolution == 0 ? "data" : String.valueOf( resolution ));
108 		resolutionCombo.addItemListener( new ItemListener() {
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 		return resolutionCombo;
128 	}
129 
130 	private JComponent buildSelectStepCombo()
131 	{
132 		selectStepComboBoxModel = new SelectStepComboBoxModel();
133 		JComboBox selectStepCombo = new JComboBox( selectStepComboBoxModel );
134 		selectStepCombo.setRenderer( new TestStepCellRenderer() );
135 		return selectStepCombo;
136 	}
137 
138 	public JComponent getComponent()
139 	{
140 		return panel;
141 	}
142 	
143 	private final class InternalPropertyChangeListener implements PropertyChangeListener
144 	{
145 		public void propertyChange(PropertyChangeEvent evt)
146 		{
147 			setTitle( "Statistics for [" + loadTest.getName() + "]" );
148 		}
149 	}
150 
151 	private class SelectStepComboBoxModel extends AbstractListModel implements ComboBoxModel 
152 	{
153 		private TestStep selectedStep;
154 		private InternalTestSuiteListener testSuiteListener = new InternalTestSuiteListener();;
155 		
156 		public SelectStepComboBoxModel()
157 		{
158 			loadTest.getTestCase().getTestSuite().addTestSuiteListener( testSuiteListener);
159 		}
160 		
161 		public void setSelectedItem(Object anItem)
162 		{
163 			if( anItem == selectedStep )
164 				return;
165 			
166 			if( anItem == null || anItem.equals( "Total") )
167 				selectedStep = null;
168 			
169 			if( anItem instanceof TestStep )
170 			{
171 				selectedStep = (TestStep) anItem;
172 			}
173 
174 			statisticsGraph.setTestStep( selectedStep );
175 		}
176 
177 		public Object getSelectedItem()
178 		{
179 			return selectedStep == null ? "Total" : selectedStep;
180 		}
181 
182 		public int getSize()
183 		{
184 			return loadTest.getTestCase().getTestStepCount()+1;
185 		}
186 
187 		public Object getElementAt(int index)
188 		{
189 			return index == getSize()-1 ? "Total" : loadTest.getTestCase().getTestStepAt( index );
190 		}
191 		
192 		private final class InternalTestSuiteListener extends TestSuiteListenerAdapter
193 		{
194 			public void testStepAdded(TestStep testStep, int index)
195 			{
196 				if( testStep.getTestCase() == loadTest.getTestCase() )
197 				{
198 					fireIntervalAdded( SelectStepComboBoxModel.this, index, index );
199 				}
200 			}
201 
202 			public void testStepRemoved(TestStep testStep, int index)
203 			{
204 				if( testStep.getTestCase() == loadTest.getTestCase() )
205 				{
206 					if( selectedStep == testStep )
207 					{
208 						setSelectedItem( null );
209 						fireContentsChanged( SelectStepComboBoxModel.this, -1, -1 );
210 					}
211 					
212 					fireIntervalRemoved( SelectStepComboBoxModel.this, index, index );
213 				}
214 			}
215 		}
216 
217 		public void release()
218 		{
219 			loadTest.getTestCase().getTestSuite().removeTestSuiteListener( testSuiteListener);
220 		}
221 	}
222 	
223 	private final static class TestStepCellRenderer extends DefaultListCellRenderer
224 	{
225 		public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
226 		{
227 			JLabel label = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
228 			
229 			if( value instanceof TestStep )
230 				label.setText( ((TestStep)value).getName() );
231 			
232 			return label;
233 		}
234 	}
235 
236 	public boolean onClose(boolean canCancel)
237 	{
238 		selectStepComboBoxModel.release();
239 		loadTest.removePropertyChangeListener( propertyChangeListener);
240 		
241 		return super.onClose(canCancel);
242 	}
243 }