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.Component;
17  import java.awt.Toolkit;
18  import java.awt.event.ActionEvent;
19  import java.awt.event.MouseAdapter;
20  import java.awt.event.MouseEvent;
21  import java.beans.PropertyChangeEvent;
22  import java.beans.PropertyChangeListener;
23  
24  import javax.swing.AbstractAction;
25  import javax.swing.Action;
26  import javax.swing.Icon;
27  import javax.swing.ImageIcon;
28  import javax.swing.JButton;
29  import javax.swing.JComponent;
30  import javax.swing.JPanel;
31  import javax.swing.JPopupMenu;
32  import javax.swing.JScrollPane;
33  import javax.swing.JTable;
34  import javax.swing.ListSelectionModel;
35  import javax.swing.event.ListSelectionEvent;
36  import javax.swing.event.ListSelectionListener;
37  import javax.swing.table.AbstractTableModel;
38  import javax.swing.table.DefaultTableCellRenderer;
39  import javax.swing.table.TableColumnModel;
40  
41  import org.jdesktop.swingx.JXTable;
42  
43  import com.eviware.soapui.impl.wsdl.actions.support.ShowOnlineHelpAction;
44  import com.eviware.soapui.impl.wsdl.loadtest.LoadTestAssertion;
45  import com.eviware.soapui.impl.wsdl.loadtest.LoadTestListener;
46  import com.eviware.soapui.impl.wsdl.loadtest.WsdlLoadTest;
47  import com.eviware.soapui.impl.wsdl.loadtest.assertions.LoadTestAssertionRegistry;
48  import com.eviware.soapui.impl.wsdl.support.Configurable;
49  import com.eviware.soapui.impl.wsdl.support.HelpUrls;
50  import com.eviware.soapui.support.UISupport;
51  import com.eviware.soapui.support.components.JXToolBar;
52  
53  /***
54   * Table showing configured assertions for a WsdlLoadTest
55   * 
56   * @todo add popup menu
57   * 
58   * @author Ole.Matzura
59   */
60  
61  public class JLoadTestAssertionsTable extends JPanel
62  {
63  	private JXTable table;
64  	private final WsdlLoadTest loadTest;
65  	private ConfigureAssertionAction configureAssertionAction;
66  	private RemoveAssertionAction removeAssertionAction;
67  	private AddLoadTestAssertionAction addLoadTestAssertionAction;
68  	private LoadTestAssertionsTableModel tableModel;
69  	private JPopupMenu assertionPopup;
70  	private InternalLoadTestListener internalLoadTestListener = new InternalLoadTestListener();
71  
72  	public JLoadTestAssertionsTable( WsdlLoadTest wsdlLoadTest )
73  	{
74  		super( new BorderLayout() );
75  		this.loadTest = wsdlLoadTest;
76  		
77  		loadTest.addLoadTestListener( internalLoadTestListener );
78  
79  		tableModel = new LoadTestAssertionsTableModel();
80  		table = new JXTable( tableModel );
81  		table.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );
82  		
83  		TableColumnModel columnModel = table.getColumnModel();
84  		columnModel.getColumn( 0 ).setMaxWidth( 16 );
85  		columnModel.getColumn( 0 ).setCellRenderer( new IconTableCellRenderer() );
86  		columnModel.getColumn( 1 ).setPreferredWidth( 100 );
87  		columnModel.getColumn( 2 ).setPreferredWidth( 100 );
88  		columnModel.getColumn( 3 ).setPreferredWidth( 200 );
89  		
90  		JScrollPane scrollPane = new JScrollPane(table);
91  		add( scrollPane, BorderLayout.CENTER );
92  		
93        table.addMouseListener( new MouseAdapter() {
94  
95  			public void mouseClicked(MouseEvent e)
96           {
97              if( e.getClickCount() < 2 ) return;
98  
99              int ix = table.getSelectedRow();
100             if( ix == -1 ) return;
101             ix = table.convertRowIndexToModel( ix );
102             
103             Object obj = loadTest.getAssertionAt( ix );
104             if( obj instanceof Configurable )
105             {
106 	            ((Configurable)obj).configure();
107             }
108    		   else Toolkit.getDefaultToolkit().beep();
109          }});
110 
111       add( buildToolbar(), BorderLayout.NORTH );
112       
113       table.getSelectionModel().addListSelectionListener( new ListSelectionListener() {
114 
115 			public void valueChanged(ListSelectionEvent e)
116 			{
117 				int ix = table.getSelectedRow();
118 				
119 				configureAssertionAction.setEnabled( ix >= 0 );
120 				removeAssertionAction.setEnabled( ix >= 0 );
121 				
122             if( ix == -1 ) return;
123             
124             ix = table.convertRowIndexToModel( ix );
125             configureAssertionAction.setEnabled( loadTest.getAssertionAt( ix ) instanceof Configurable );
126 			}} );
127       
128       
129       assertionPopup = new JPopupMenu();
130       assertionPopup.add( configureAssertionAction );
131       assertionPopup.addSeparator();
132       assertionPopup.add( addLoadTestAssertionAction );
133       assertionPopup.add( removeAssertionAction );
134       
135       setComponentPopupMenu( assertionPopup );
136       
137       scrollPane.setInheritsPopupMenu( true );
138       table.setComponentPopupMenu( assertionPopup );
139   	}
140 	
141 	public void release()
142 	{
143 		tableModel.release();
144 		loadTest.removeLoadTestListener( internalLoadTestListener );
145 	}
146 	
147 	private JComponent buildToolbar()
148 	{
149 		configureAssertionAction = new ConfigureAssertionAction();
150       removeAssertionAction = new RemoveAssertionAction();
151       addLoadTestAssertionAction = new AddLoadTestAssertionAction();
152 		
153 		JXToolBar toolbar = UISupport.createToolbar();
154 		
155 		JButton button = UISupport.createToolbarButton( addLoadTestAssertionAction );
156 		button.setText(null);
157 		toolbar.addFixed( button);
158 		button = UISupport.createToolbarButton( removeAssertionAction );
159 		button.setText(null);
160 		toolbar.addFixed( button);
161 		button = UISupport.createToolbarButton( configureAssertionAction );
162 		button.setText(null);
163 		toolbar.addFixed( button);
164 		toolbar.addGlue();
165 		toolbar.add( new ShowOnlineHelpAction( HelpUrls.LOADTEST_ASSERTIONS_URL ));
166 		
167 		return toolbar; 
168 	}
169 
170 	private class LoadTestAssertionsTableModel extends AbstractTableModel implements PropertyChangeListener 
171 	{
172 		public LoadTestAssertionsTableModel()
173 		{
174 			for( int c = 0; c < loadTest.getAssertionCount(); c++ )
175 			{
176 				loadTest.getAssertionAt( c ).addPropertyChangeListener( LoadTestAssertion.CONFIGURATION_PROPERTY, this );
177 			}
178 		}
179 		
180 		public void release()
181 		{
182 			for( int c = 0; c < loadTest.getAssertionCount(); c++ )
183 			{
184 				loadTest.getAssertionAt( c ).removePropertyChangeListener( LoadTestAssertion.CONFIGURATION_PROPERTY, this );
185 			}
186 		}
187 		
188 		public int getRowCount()
189 		{
190 			return loadTest.getAssertionCount();
191 		}
192 
193 		public int getColumnCount()
194 		{
195 			return 4;
196 		}
197 		
198 		public Class<?> getColumnClass(int columnIndex)
199 		{
200 			switch( columnIndex )
201 			{
202 			case 0 : return ImageIcon.class;
203 			default : return String.class;
204 			}
205 		}
206 
207 		public String getColumnName(int column)
208 		{
209 			switch( column )
210 			{
211 			case 0 : return " ";
212 			case 1 : return "Name";
213 			case 2 : return "Step";
214 			case 3 : return "Details";
215 			}
216 			
217 			return null;
218 		}
219 
220 		public Object getValueAt(int rowIndex, int columnIndex)
221 		{
222 			LoadTestAssertion assertion = loadTest.getAssertionAt( rowIndex );
223 			
224 			switch( columnIndex )
225 			{
226 				case 0 : return assertion.getIcon();
227 				case 1 : return assertion.getName();
228 				case 2 : return assertion.getTargetStep();
229 				case 3 : return assertion.getDescription();
230 			}
231 
232 			return null;
233 		}
234 
235 		public void assertionRemoved(LoadTestAssertion assertion)
236 		{
237 			assertion.removePropertyChangeListener( LoadTestAssertion.CONFIGURATION_PROPERTY, this );
238 			fireTableDataChanged();
239 		}
240 		
241 		public void assertionAdded(LoadTestAssertion assertion)
242 		{
243 			assertion.addPropertyChangeListener( LoadTestAssertion.CONFIGURATION_PROPERTY, this );
244 			fireTableRowsInserted( getRowCount()-1, getRowCount()-1 );
245 		}
246 
247 		public void propertyChange(PropertyChangeEvent evt)
248 		{
249 			fireTableDataChanged();
250 		}
251 	}
252 
253 	private static final class IconTableCellRenderer extends DefaultTableCellRenderer
254 	{
255 		public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
256 		{
257 			if( value != null )
258 				setIcon( (Icon) value );
259 			
260 			if (isSelected) 
261          {
262             setBackground(table.getSelectionBackground());
263             setForeground(table.getSelectionForeground());
264          }
265          else 
266          {
267             setBackground(table.getBackground());
268             setForeground(table.getForeground());
269          }
270 			
271 			return this;
272 		}
273 	}
274 	
275    public class AddLoadTestAssertionAction extends AbstractAction
276    {
277       public AddLoadTestAssertionAction()
278       {
279       	super( "Add Assertion" );
280       	putValue( Action.SHORT_DESCRIPTION, "Adds an assertion to this LoadTest" );
281       	putValue( Action.SMALL_ICON, UISupport.createImageIcon( "/addAssertion.gif" ));
282       }
283 
284       public void actionPerformed( ActionEvent e )
285       {
286       	String [] types = LoadTestAssertionRegistry.getAvailableAssertions();
287       	String type = (String) UISupport.prompt( "Select assertion type to add", "Add Assertion", types );
288       	if( type != null )
289       	{
290       		loadTest.addAssertion( type, LoadTestAssertion.ANY_TEST_STEP, true );
291       	}
292       }
293    }
294 
295    public class ConfigureAssertionAction extends AbstractAction
296    {
297       ConfigureAssertionAction()
298       {
299       	super( "Configure" );
300       	putValue( Action.SHORT_DESCRIPTION, "Configures the selection assertion" );
301       	putValue( Action.SMALL_ICON, UISupport.createImageIcon( "/options.gif" ));
302       	setEnabled( false );
303       }
304 
305       public void actionPerformed( ActionEvent e )
306       {
307       	int ix = table.getSelectedRow();
308          if( ix == -1 ) return;
309          ix = table.convertRowIndexToModel( ix );
310          
311          Object obj = loadTest.getAssertionAt( ix );
312          if( obj instanceof Configurable )
313          {
314             ((Configurable)obj).configure();
315             tableModel.fireTableRowsUpdated( ix, ix );
316          }
317 		   else Toolkit.getDefaultToolkit().beep();
318       }
319    }
320 
321    public class RemoveAssertionAction extends AbstractAction
322    {
323       public RemoveAssertionAction()
324       {
325       	super( "Remove Assertion" );
326       	putValue( Action.SHORT_DESCRIPTION, "Removes the selected assertion from this LoadTest" );
327       	putValue( Action.SMALL_ICON, UISupport.createImageIcon( "/remove_assertion.gif" ));
328       	setEnabled( false );
329       }
330 
331       public void actionPerformed( ActionEvent e )
332       {
333       	int ix = table.getSelectedRow();
334          if( ix == -1 ) return;
335          ix = table.convertRowIndexToModel( ix );
336          
337          LoadTestAssertion assertion = loadTest.getAssertionAt( ix );
338          if( UISupport.confirm( "Remove assertion [" + assertion.getName() + "]", "Remove Assertion"))
339          {
340          	loadTest.removeAssertion( assertion );
341          }
342       }
343    }
344    
345 	public class InternalLoadTestListener implements LoadTestListener
346 	{
347 		public void assertionAdded(LoadTestAssertion assertion)
348 		{
349    		tableModel.assertionAdded( assertion );
350    		table.getSelectionModel().setSelectionInterval( tableModel.getRowCount()-1, tableModel.getRowCount()-1 );
351 		}
352 
353 		public void assertionRemoved(LoadTestAssertion assertion)
354 		{
355 			tableModel.assertionRemoved( assertion );
356 		}
357 	}   
358 }