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.BorderLayout;
16  import java.awt.Color;
17  import java.awt.Component;
18  import java.awt.event.ActionEvent;
19  import java.awt.event.MouseAdapter;
20  import java.awt.event.MouseEvent;
21  
22  import javax.swing.AbstractAction;
23  import javax.swing.BorderFactory;
24  import javax.swing.JMenu;
25  import javax.swing.JPanel;
26  import javax.swing.JPopupMenu;
27  import javax.swing.JScrollPane;
28  import javax.swing.JTable;
29  import javax.swing.table.TableCellRenderer;
30  import javax.swing.table.TableColumnModel;
31  
32  import org.jdesktop.swingx.JXTable;
33  
34  import com.eviware.soapui.impl.wsdl.loadtest.LoadTestAssertion;
35  import com.eviware.soapui.impl.wsdl.loadtest.WsdlLoadTest;
36  import com.eviware.soapui.impl.wsdl.loadtest.assertions.LoadTestAssertionRegistry;
37  import com.eviware.soapui.model.ModelItem;
38  import com.eviware.soapui.model.testsuite.TestStep;
39  import com.eviware.soapui.support.action.swing.ActionList;
40  import com.eviware.soapui.support.action.swing.ActionListBuilder;
41  import com.eviware.soapui.support.action.swing.ActionSupport;
42  
43  /***
44   * Table for displaying real-time LoadTest Statistics
45   * 
46   * @author Ole.Matzura
47   */
48  
49  public class JStatisticsTable extends JPanel
50  {
51  	private final WsdlLoadTest loadTest;
52  	private JXTable statisticsTable;
53  	private JPopupMenu popup;
54  
55  	public JStatisticsTable( WsdlLoadTest loadTest )
56  	{
57  		super( new BorderLayout() );
58  		this.loadTest = loadTest;
59  
60  		statisticsTable = new JXTable( loadTest.getStatisticsModel() );
61  		statisticsTable.setColumnControlVisible( true );
62  		statisticsTable.getTableHeader().setReorderingAllowed( false );
63  
64  		statisticsTable.addMouseListener( new StatisticsTableMouseListener() );
65  
66  		TableColumnModel columnModel = statisticsTable.getColumnModel();
67  		columnModel.getColumn( 0 ).setMaxWidth( 5 );
68  		columnModel.getColumn( 0 ).setCellRenderer( new ColorLabelTableCellRenderer() );
69  		columnModel.getColumn( 1 ).setPreferredWidth( 150 );
70  		columnModel.getColumn( 2 ).setPreferredWidth( 20 );
71  		columnModel.getColumn( 3 ).setPreferredWidth( 20 );
72  		columnModel.getColumn( 4 ).setPreferredWidth( 20 );
73  		columnModel.getColumn( 5 ).setPreferredWidth( 20 );
74  		columnModel.getColumn( 6 ).setPreferredWidth( 20 );
75  		columnModel.getColumn( 7 ).setPreferredWidth( 20 );
76  		columnModel.getColumn( 8 ).setPreferredWidth( 20 );
77  		columnModel.getColumn( 9 ).setPreferredWidth( 20 );
78  		columnModel.getColumn( 10 ).setPreferredWidth( 20 );
79  		columnModel.getColumn( 11 ).setPreferredWidth( 20 );
80  
81  		JScrollPane scrollPane = new JScrollPane( statisticsTable );
82  		scrollPane.setBorder( BorderFactory.createEmptyBorder( 3, 3, 3, 3 ) );
83  		add( scrollPane, BorderLayout.CENTER );
84  
85  		JMenu assertionsMenu = new JMenu( "Add Assertion" );
86  		for( String assertion : LoadTestAssertionRegistry.getAvailableAssertions() )
87  		{
88  			assertionsMenu.add( new AddAssertionAction( assertion ) );
89  		}
90  
91  		popup = new JPopupMenu();
92  		popup.add( assertionsMenu );
93  		popup.setInvoker( statisticsTable );
94  	}
95  
96  	public void release()
97  	{
98  		loadTest.getStatisticsModel().removeTableModelListener( statisticsTable );
99  	}
100 
101 	private final class StatisticsTableMouseListener extends MouseAdapter
102 	{
103 		public void mouseClicked( MouseEvent e )
104 		{
105 			if( statisticsTable.getSelectedColumn() == 1 && e.getClickCount() > 1 )
106 			{
107 				int row = statisticsTable.getSelectedRow();
108 				if( row < 0 )
109 					return;
110 
111 				row = statisticsTable.convertRowIndexToModel( row );
112 
113 				ModelItem modelItem = row == statisticsTable.getRowCount() - 1 ? loadTest.getTestCase() : loadTest
114 						.getStatisticsModel().getTestStepAtRow( row );
115 
116 				ActionList actions = ActionListBuilder.buildActions( modelItem );
117 				if( actions != null )
118 					actions.performDefaultAction( new ActionEvent( statisticsTable, 0, null ) );
119 			}
120 		}
121 
122 		public void mousePressed( MouseEvent e )
123 		{
124 			if( e.isPopupTrigger() )
125 			{
126 				showPopup( e );
127 			}
128 		}
129 
130 		public void mouseReleased( MouseEvent e )
131 		{
132 			if( e.isPopupTrigger() )
133 			{
134 				showPopup( e );
135 			}
136 		}
137 	}
138 
139 	private static final class ColorLabelTableCellRenderer extends JPanel implements TableCellRenderer
140 	{
141 		private Color bgColor;
142 
143 		public ColorLabelTableCellRenderer()
144 		{
145 			super();
146 
147 			bgColor = getBackground();
148 		}
149 
150 		public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus,
151 				int row, int column )
152 		{
153 			if( value instanceof Color )
154 				setBackground( ( Color )value );
155 			else
156 				setBackground( bgColor );
157 
158 			return this;
159 		}
160 	}
161 
162 	public void showPopup( MouseEvent e )
163 	{
164 		int row = statisticsTable.rowAtPoint( e.getPoint() );
165 		if( row == -1 )
166 			return;
167 
168 		if( statisticsTable.getSelectedRow() != row )
169 		{
170 			statisticsTable.getSelectionModel().setSelectionInterval( row, row );
171 		}
172 
173 		row = statisticsTable.convertRowIndexToModel( row );
174 
175 		while( popup.getComponentCount() > 1 )
176 			popup.remove( 1 );
177 
178 		if( row < statisticsTable.getRowCount() - 1 )
179 		{
180 			TestStep testStep = loadTest.getStatisticsModel().getTestStepAtRow( row );
181 			ActionSupport.addActions( ActionListBuilder.buildActions( testStep ), popup );
182 		}
183 
184 		popup.setLocation( ( int )( statisticsTable.getLocationOnScreen().getX() + e.getPoint().getX() ),
185 				( int )( statisticsTable.getLocationOnScreen().getY() + e.getPoint().getY() ) );
186 		popup.setVisible( true );
187 	}
188 
189 	private class AddAssertionAction extends AbstractAction
190 	{
191 		private final String type;
192 
193 		public AddAssertionAction( String type )
194 		{
195 			super( type );
196 			this.type = type;
197 		}
198 
199 		public void actionPerformed( ActionEvent e )
200 		{
201 			int row = statisticsTable.getSelectedRow();
202 			if( row == -1 )
203 				return;
204 
205 			String target = LoadTestAssertion.ANY_TEST_STEP;
206 
207 			row = statisticsTable.convertRowIndexToModel( row );
208 
209 			if( row == statisticsTable.getRowCount() - 1 )
210 				target = LoadTestAssertion.ALL_TEST_STEPS;
211 			else if( row >= 0 )
212 				target = loadTest.getTestCase().getTestStepAt( row ).getName();
213 
214 			loadTest.addAssertion( type, target, true );
215 		}
216 	}
217 }