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( 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 			if( evt.getPropertyName().equals( WsdlLoadTest.NAME_PROPERTY ))
148 			{
149 				setTitle( "Statistics for [" + loadTest.getName() + "]" );
150 			}
151 		}
152 	}
153 
154 	private class SelectStepComboBoxModel extends AbstractListModel implements ComboBoxModel 
155 	{
156 		private TestStep selectedStep;
157 		private InternalTestSuiteListener testSuiteListener = new InternalTestSuiteListener();
158 		
159 		public SelectStepComboBoxModel()
160 		{
161 			loadTest.getTestCase().getTestSuite().addTestSuiteListener( testSuiteListener);
162 		}
163 		
164 		public void setSelectedItem(Object anItem)
165 		{
166 			if( anItem == selectedStep )
167 				return;
168 			
169 			if( anItem == null || anItem.equals( "Total") )
170 				selectedStep = null;
171 			
172 			if( anItem instanceof TestStep )
173 			{
174 				selectedStep = (TestStep) anItem;
175 			}
176 
177 			statisticsGraph.setTestStep( selectedStep );
178 		}
179 
180 		public Object getSelectedItem()
181 		{
182 			return selectedStep == null ? "Total" : selectedStep;
183 		}
184 
185 		public int getSize()
186 		{
187 			return loadTest.getTestCase().getTestStepCount()+1;
188 		}
189 
190 		public Object getElementAt(int index)
191 		{
192 			return index == getSize()-1 ? "Total" : loadTest.getTestCase().getTestStepAt( index );
193 		}
194 		
195 		private final class InternalTestSuiteListener extends TestSuiteListenerAdapter
196 		{
197 			public void testStepAdded(TestStep testStep, int index)
198 			{
199 				if( testStep.getTestCase() == loadTest.getTestCase() )
200 				{
201 					fireIntervalAdded( SelectStepComboBoxModel.this, index, index );
202 				}
203 			}
204 
205 			public void testStepRemoved(TestStep testStep, int index)
206 			{
207 				if( testStep.getTestCase() == loadTest.getTestCase() )
208 				{
209 					if( selectedStep == testStep )
210 					{
211 						setSelectedItem( null );
212 						fireContentsChanged( SelectStepComboBoxModel.this, -1, -1 );
213 					}
214 					
215 					fireIntervalRemoved( SelectStepComboBoxModel.this, index, index );
216 				}
217 			}
218 		}
219 
220 		public void release()
221 		{
222 			loadTest.getTestCase().getTestSuite().removeTestSuiteListener( testSuiteListener);
223 		}
224 	}
225 	
226 	private final static class TestStepCellRenderer extends DefaultListCellRenderer
227 	{
228 		public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
229 		{
230 			JLabel label = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
231 			
232 			if( value instanceof TestStep )
233 				label.setText( ((TestStep)value).getName() );
234 			
235 			return label;
236 		}
237 	}
238 
239 	public boolean onClose(boolean canCancel)
240 	{
241 		selectStepComboBoxModel.release();
242 		loadTest.removePropertyChangeListener( propertyChangeListener);
243 		statisticsGraph.release();
244 		
245 		return super.onClose(canCancel);
246 	}
247 }