View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2008 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  		
80  		JScrollPane scrollPane = new JScrollPane( statisticsTable );
81  		scrollPane.setBorder( BorderFactory.createEmptyBorder( 3, 3, 3, 3 ) );
82  		add( scrollPane, BorderLayout.CENTER );
83  		
84  		JMenu assertionsMenu = new JMenu( "Add Assertion" );
85  		for( String assertion : LoadTestAssertionRegistry.getAvailableAssertions())
86  		{
87  			assertionsMenu.add( new AddAssertionAction( assertion ));
88  		}
89  
90  		popup = new JPopupMenu();
91  		popup.add( assertionsMenu );		
92  		popup.setInvoker( statisticsTable );
93  	}
94  	
95  	public void release()
96  	{
97  		loadTest.getStatisticsModel().removeTableModelListener( statisticsTable );
98  	}
99  	
100 	private final class StatisticsTableMouseListener extends MouseAdapter
101 	{
102 		public void mouseClicked(MouseEvent e)
103 		{
104 			if( statisticsTable.getSelectedColumn() == 1 &&	e.getClickCount() > 1 )
105 			{
106 				int row = statisticsTable.getSelectedRow();
107 				if( row < 0 )
108 					return;
109 				
110 				row = statisticsTable.convertRowIndexToModel( row );
111 				
112 				ModelItem modelItem = row == statisticsTable.getRowCount()-1 ? loadTest.getTestCase() : 
113 					 loadTest.getStatisticsModel().getTestStepAtRow( row );
114 				
115 				ActionList actions = ActionListBuilder.buildActions( modelItem );
116 				if( actions != null )
117 					actions.performDefaultAction( new ActionEvent( statisticsTable, 0, null ));
118 			}
119 		}
120 
121 		public void mousePressed(MouseEvent e)
122 		{
123 			if( e.isPopupTrigger() )
124 			{
125 				showPopup( e );
126 			}
127 		}
128 
129 		public void mouseReleased(MouseEvent e)
130 		{
131 			if( e.isPopupTrigger() )
132 			{
133 				showPopup( e );
134 			}
135 		}
136 	}
137 	
138 	private static final class ColorLabelTableCellRenderer extends JPanel implements TableCellRenderer
139 	{
140 		private Color bgColor;
141 
142 		public ColorLabelTableCellRenderer()
143 		{
144 			super();
145 			
146 			bgColor = getBackground();
147 		}
148 		
149 		public Component getTableCellRendererComponent(JTable table,
150 				Object value, boolean isSelected, boolean hasFocus, int row,
151 				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 }