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