View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2009 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.Dimension;
17  import java.awt.event.ItemEvent;
18  import java.awt.event.ItemListener;
19  import java.beans.PropertyChangeEvent;
20  import java.beans.PropertyChangeListener;
21  
22  import javax.swing.DefaultComboBoxModel;
23  import javax.swing.JButton;
24  import javax.swing.JComboBox;
25  import javax.swing.JComponent;
26  import javax.swing.JPanel;
27  import javax.swing.JScrollPane;
28  import javax.swing.ScrollPaneConstants;
29  
30  import com.eviware.soapui.impl.support.actions.ShowOnlineHelpAction;
31  import com.eviware.soapui.impl.wsdl.loadtest.WsdlLoadTest;
32  import com.eviware.soapui.impl.wsdl.loadtest.data.LoadTestStatistics.Statistic;
33  import com.eviware.soapui.impl.wsdl.loadtest.data.actions.ExportSamplesHistoryAction;
34  import com.eviware.soapui.impl.wsdl.support.HelpUrls;
35  import com.eviware.soapui.support.UISupport;
36  import com.eviware.soapui.support.components.JXToolBar;
37  import com.eviware.soapui.ui.support.DefaultDesktopPanel;
38  
39  /***
40   * DesktopPanel for StatisticsHistory Graphs
41   * 
42   * @author Ole.Matzura
43   */
44  
45  public class StatisticsHistoryDesktopPanel extends DefaultDesktopPanel
46  {
47  	private JPanel panel;
48  	private final WsdlLoadTest loadTest;
49  	private JStatisticsHistoryGraph historyGraph;
50  	private JButton exportButton;
51  	private JComboBox selectStatisticCombo;
52  	private StatisticsHistoryDesktopPanel.InternalPropertyChangeListener propertyChangeListener;
53  	private JComboBox resolutionCombo;
54  
55  	public StatisticsHistoryDesktopPanel( WsdlLoadTest loadTest )
56  	{
57  		super( "Statistics History for [" + loadTest.getName() + "]", null, null );
58  		this.loadTest = loadTest;
59  
60  		propertyChangeListener = new InternalPropertyChangeListener();
61  		loadTest.addPropertyChangeListener( WsdlLoadTest.NAME_PROPERTY, propertyChangeListener );
62  
63  		buildUI();
64  	}
65  
66  	private void buildUI()
67  	{
68  		historyGraph = new JStatisticsHistoryGraph( loadTest );
69  
70  		JScrollPane scrollPane = new JScrollPane( historyGraph );
71  		scrollPane.getViewport().setBackground( Color.WHITE );
72  		scrollPane.setHorizontalScrollBarPolicy( ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS );
73  
74  		panel = UISupport.buildPanelWithToolbarAndStatusBar( buildToolbar(), scrollPane, historyGraph.getLegend() );
75  		panel.setPreferredSize( new Dimension( 600, 400 ) );
76  	}
77  
78  	private JComponent buildToolbar()
79  	{
80  		exportButton = UISupport.createToolbarButton( new ExportSamplesHistoryAction( historyGraph ) );
81  
82  		JXToolBar toolbar = UISupport.createToolbar();
83  
84  		toolbar.addSpace( 5 );
85  		toolbar.addLabeledFixed( "Select Statistic:", buildSelectStatisticCombo() );
86  		toolbar.addUnrelatedGap();
87  		toolbar.addLabeledFixed( "Resolution:", buildResolutionCombo() );
88  		toolbar.addGlue();
89  		toolbar.addFixed( exportButton );
90  		toolbar.addFixed( UISupport.createToolbarButton( new ShowOnlineHelpAction( HelpUrls.STATISTICSGRAPH_HELP_URL ) ) );
91  
92  		return toolbar;
93  	}
94  
95  	private JComponent buildResolutionCombo()
96  	{
97  		resolutionCombo = new JComboBox( new String[] { "data", "250", "500", "1000" } );
98  		resolutionCombo.setEditable( true );
99  		resolutionCombo.setToolTipText( "Sets update interval of graph in milliseconds" );
100 		long resolution = historyGraph.getResolution();
101 		resolutionCombo.setSelectedItem( resolution == 0 ? "data" : String.valueOf( resolution ) );
102 		resolutionCombo.addItemListener( new ItemListener()
103 		{
104 
105 			public void itemStateChanged( ItemEvent e )
106 			{
107 				try
108 				{
109 					String value = resolutionCombo.getSelectedItem().toString();
110 					long resolution = value.equals( "data" ) ? 0 : Long.parseLong( value );
111 					if( resolution != historyGraph.getResolution() )
112 					{
113 						historyGraph.setResolution( resolution );
114 					}
115 				}
116 				catch( Exception ex )
117 				{
118 					long resolution = historyGraph.getResolution();
119 					resolutionCombo.setSelectedItem( resolution == 0 ? "data" : String.valueOf( resolution ) );
120 				}
121 			}
122 		} );
123 		return resolutionCombo;
124 	}
125 
126 	private JComponent buildSelectStatisticCombo()
127 	{
128 		DefaultComboBoxModel model = new DefaultComboBoxModel();
129 		model.addElement( Statistic.AVERAGE );
130 		model.addElement( Statistic.TPS );
131 		model.addElement( Statistic.ERRORS );
132 		model.addElement( Statistic.BPS );
133 
134 		selectStatisticCombo = new JComboBox( model );
135 		selectStatisticCombo.addItemListener( new ItemListener()
136 		{
137 
138 			public void itemStateChanged( ItemEvent e )
139 			{
140 				historyGraph.setStatistic( Statistic.valueOf( selectStatisticCombo.getSelectedItem().toString() ) );
141 			}
142 		} );
143 
144 		return selectStatisticCombo;
145 	}
146 
147 	public JComponent getComponent()
148 	{
149 		return panel;
150 	}
151 
152 	public boolean onClose( boolean canCancel )
153 	{
154 		loadTest.removePropertyChangeListener( WsdlLoadTest.NAME_PROPERTY, propertyChangeListener );
155 		historyGraph.release();
156 
157 		return super.onClose( canCancel );
158 	}
159 
160 	private final class InternalPropertyChangeListener implements PropertyChangeListener
161 	{
162 		public void propertyChange( PropertyChangeEvent evt )
163 		{
164 			setTitle( "Statistics History for [" + loadTest.getName() + "]" );
165 		}
166 	}
167 }