View Javadoc

1   /*
2    *  soapUI, copyright (C) 2006 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of the GNU Lesser General Public License as published by the Free Software Foundation; 
6    *  either version 2.1 of the License, or (at your option) any later version.
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.testsuite.TestStep;
38  import com.eviware.soapui.support.action.ActionList;
39  import com.eviware.soapui.support.action.ActionSupport;
40  
41  /***
42   * Table for displaying real-time LoadTest Statistics
43   * 
44   * @author Ole.Matzura
45   */
46  
47  public class JStatisticsTable extends JPanel
48  {
49  	private final WsdlLoadTest loadTest;
50  	private JXTable statisticsTable;
51  	private JPopupMenu popup;
52  
53  	public JStatisticsTable( WsdlLoadTest loadTest )
54  	{
55  		super( new BorderLayout() );
56  		this.loadTest = loadTest;
57  		
58  		statisticsTable = new JXTable( loadTest.getStatisticsModel() );
59  		statisticsTable.setColumnControlVisible( true );
60  		statisticsTable.getTableHeader().setReorderingAllowed( false );
61  		
62  		statisticsTable.addMouseListener( new StatisticsTableMouseListener() );
63  		
64  		TableColumnModel columnModel = statisticsTable.getColumnModel();
65  		columnModel.getColumn( 0 ).setMaxWidth( 5 );
66  		columnModel.getColumn( 0 ).setCellRenderer( new ColorLabelTableCellRenderer() );
67  		columnModel.getColumn( 1 ).setPreferredWidth( 150 );
68  		columnModel.getColumn( 2 ).setPreferredWidth( 20 );
69  		columnModel.getColumn( 3 ).setPreferredWidth( 20 );
70  		columnModel.getColumn( 4 ).setPreferredWidth( 20 );
71  		columnModel.getColumn( 5 ).setPreferredWidth( 20 );
72  		columnModel.getColumn( 6 ).setPreferredWidth( 20 );
73  		columnModel.getColumn( 7 ).setPreferredWidth( 20 );
74  		columnModel.getColumn( 8 ).setPreferredWidth( 20 );
75  		columnModel.getColumn( 9 ).setPreferredWidth( 20 );
76  		columnModel.getColumn( 10 ).setPreferredWidth( 20 );
77  		
78  		JScrollPane scrollPane = new JScrollPane( statisticsTable );
79  		scrollPane.setBorder( BorderFactory.createEmptyBorder( 3, 3, 3, 3 ) );
80  		add( scrollPane, BorderLayout.CENTER );
81  		
82  		JMenu assertionsMenu = new JMenu( "Add Assertion" );
83  		for( String assertion : LoadTestAssertionRegistry.getAvailableAssertions())
84  		{
85  			assertionsMenu.add( new AddAssertionAction( assertion ));
86  		}
87  
88  		popup = new JPopupMenu();
89  		popup.add( assertionsMenu );		
90  		popup.setInvoker( statisticsTable );
91  	}
92  	
93  	private final class StatisticsTableMouseListener extends MouseAdapter
94  	{
95  		public void mouseClicked(MouseEvent e)
96  		{
97  			if( statisticsTable.getSelectedColumn() == 1 &&	e.getClickCount() > 1 )
98  			{
99  				int row = statisticsTable.getSelectedRow();
100 				if( row < 0 )
101 					return;
102 				
103 				row = statisticsTable.convertRowIndexToModel( row );
104 				if( row == statisticsTable.getRowCount()-1 )
105 					return;
106 				
107 				TestStep testStep = loadTest.getStatisticsModel().getTestStepAtRow( row );
108 				ActionList actions = testStep.getActions();
109 				if( actions != null )
110 					actions.performDefaultAction( new ActionEvent( statisticsTable, 0, null ));
111 			}
112 		}
113 
114 		public void mousePressed(MouseEvent e)
115 		{
116 			if( e.isPopupTrigger() )
117 			{
118 				showPopup( e );
119 			}
120 		}
121 
122 		public void mouseReleased(MouseEvent e)
123 		{
124 			if( e.isPopupTrigger() )
125 			{
126 				showPopup( e );
127 			}
128 		}
129 	}
130 	
131 	private static final class ColorLabelTableCellRenderer extends JPanel implements TableCellRenderer
132 	{
133 		private Color bgColor;
134 
135 		public ColorLabelTableCellRenderer()
136 		{
137 			super();
138 			
139 			bgColor = getBackground();
140 		}
141 		
142 		public Component getTableCellRendererComponent(JTable table,
143 				Object value, boolean isSelected, boolean hasFocus, int row,
144 				int column)
145 		{
146 			if( value instanceof Color )
147 				setBackground( (Color)value );
148 			else
149 				setBackground( bgColor );
150 			
151 			return this;
152 		}
153 	}
154 
155 	public void showPopup(MouseEvent e)
156 	{
157 		int row = statisticsTable.rowAtPoint( e.getPoint() );
158 		if( row == -1 )
159 			return;
160 		
161 		if( statisticsTable.getSelectedRow() != row )
162 		{
163 			statisticsTable.getSelectionModel().setSelectionInterval( row, row );
164 		}
165 		
166 		row = statisticsTable.convertRowIndexToModel( row );
167 		
168 		while( popup.getComponentCount() > 1 )
169 			popup.remove( 1 );
170 		
171 		if( row < statisticsTable.getRowCount()-1 )
172 		{
173 			TestStep testStep = loadTest.getStatisticsModel().getTestStepAtRow( row );
174 			ActionSupport.addActions( testStep.getActions(), popup );
175 		}
176 		
177 		popup.setLocation( (int)(statisticsTable.getLocationOnScreen().getX() + e.getPoint().getX()), 
178 				(int)(statisticsTable.getLocationOnScreen().getY() + e.getPoint().getY()));
179 		popup.setVisible( true );
180 	}
181 	
182 	private class AddAssertionAction extends AbstractAction
183 	{
184 		private final String type;
185 
186 		public AddAssertionAction( String type )
187 		{
188 			super( type );
189 			this.type = type;
190 		}
191 		
192 		public void actionPerformed(ActionEvent e)
193 		{
194 			int row = statisticsTable.getSelectedRow();
195 			if( row == -1 )
196 				return;
197 			
198 			String target = LoadTestAssertion.ANY_TEST_STEP;
199 			
200 			row = statisticsTable.convertRowIndexToModel( row );
201 			
202 			if( row == statisticsTable.getRowCount()-1 )
203 				target = LoadTestAssertion.ALL_TEST_STEPS;
204 			else if( row >= 0 )
205 				target = loadTest.getTestCase().getTestStepAt( row ).getName();
206 			
207 			loadTest.addAssertion( type, target, true );		
208 		}
209 	}
210 }