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.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.support.actions.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.createSmallToolbar();
154 		
155 		JButton button = UISupport.createToolbarButton( addLoadTestAssertionAction );
156 		toolbar.addFixed( button);
157 		button = UISupport.createToolbarButton( removeAssertionAction );
158 		toolbar.addFixed( button);
159 		button = UISupport.createToolbarButton( configureAssertionAction );
160 		toolbar.addFixed( button);
161 		toolbar.addGlue();
162 		toolbar.add( new ShowOnlineHelpAction( HelpUrls.LOADTEST_ASSERTIONS_URL ));
163 		
164 		return toolbar; 
165 	}
166 
167 	private class LoadTestAssertionsTableModel extends AbstractTableModel implements PropertyChangeListener 
168 	{
169 		public LoadTestAssertionsTableModel()
170 		{
171 			for( int c = 0; c < loadTest.getAssertionCount(); c++ )
172 			{
173 				loadTest.getAssertionAt( c ).addPropertyChangeListener( LoadTestAssertion.CONFIGURATION_PROPERTY, this );
174 			}
175 		}
176 		
177 		public void release()
178 		{
179 			for( int c = 0; c < loadTest.getAssertionCount(); c++ )
180 			{
181 				loadTest.getAssertionAt( c ).removePropertyChangeListener( LoadTestAssertion.CONFIGURATION_PROPERTY, this );
182 			}
183 		}
184 		
185 		public int getRowCount()
186 		{
187 			return loadTest.getAssertionCount();
188 		}
189 
190 		public int getColumnCount()
191 		{
192 			return 4;
193 		}
194 		
195 		public Class<?> getColumnClass(int columnIndex)
196 		{
197 			switch( columnIndex )
198 			{
199 			case 0 : return ImageIcon.class;
200 			default : return String.class;
201 			}
202 		}
203 
204 		public String getColumnName(int column)
205 		{
206 			switch( column )
207 			{
208 			case 0 : return " ";
209 			case 1 : return "Name";
210 			case 2 : return "Step";
211 			case 3 : return "Details";
212 			}
213 			
214 			return null;
215 		}
216 
217 		public Object getValueAt(int rowIndex, int columnIndex)
218 		{
219 			LoadTestAssertion assertion = loadTest.getAssertionAt( rowIndex );
220 			
221 			switch( columnIndex )
222 			{
223 				case 0 : return assertion.getIcon();
224 				case 1 : return assertion.getName();
225 				case 2 : return assertion.getTargetStep();
226 				case 3 : return assertion.getDescription();
227 			}
228 
229 			return null;
230 		}
231 
232 		public void assertionRemoved(LoadTestAssertion assertion)
233 		{
234 			assertion.removePropertyChangeListener( LoadTestAssertion.CONFIGURATION_PROPERTY, this );
235 			fireTableDataChanged();
236 		}
237 		
238 		public void assertionAdded(LoadTestAssertion assertion)
239 		{
240 			assertion.addPropertyChangeListener( LoadTestAssertion.CONFIGURATION_PROPERTY, this );
241 			fireTableRowsInserted( getRowCount()-1, getRowCount()-1 );
242 		}
243 
244 		public void propertyChange(PropertyChangeEvent evt)
245 		{
246 			fireTableDataChanged();
247 		}
248 	}
249 
250 	private static final class IconTableCellRenderer extends DefaultTableCellRenderer
251 	{
252 		public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
253 		{
254 			if( value != null )
255 				setIcon( (Icon) value );
256 			
257 			if (isSelected) 
258          {
259             setBackground(table.getSelectionBackground());
260             setForeground(table.getSelectionForeground());
261          }
262          else 
263          {
264             setBackground(table.getBackground());
265             setForeground(table.getForeground());
266          }
267 			
268 			return this;
269 		}
270 	}
271 	
272    public class AddLoadTestAssertionAction extends AbstractAction
273    {
274       public AddLoadTestAssertionAction()
275       {
276       	super( "Add Assertion" );
277       	putValue( Action.SHORT_DESCRIPTION, "Adds an assertion to this LoadTest" );
278       	putValue( Action.SMALL_ICON, UISupport.createImageIcon( "/addAssertion.gif" ));
279       }
280 
281       public void actionPerformed( ActionEvent e )
282       {
283       	String [] types = LoadTestAssertionRegistry.getAvailableAssertions();
284       	String type = (String) UISupport.prompt( "Select assertion type to add", "Add Assertion", types );
285       	if( type != null )
286       	{
287       		loadTest.addAssertion( type, LoadTestAssertion.ANY_TEST_STEP, true );
288       	}
289       }
290    }
291 
292    public class ConfigureAssertionAction extends AbstractAction
293    {
294       ConfigureAssertionAction()
295       {
296       	super( "Configure" );
297       	putValue( Action.SHORT_DESCRIPTION, "Configures the selection assertion" );
298       	putValue( Action.SMALL_ICON, UISupport.createImageIcon( "/options.gif" ));
299       	setEnabled( false );
300       }
301 
302       public void actionPerformed( ActionEvent e )
303       {
304       	int ix = table.getSelectedRow();
305          if( ix == -1 ) return;
306          ix = table.convertRowIndexToModel( ix );
307          
308          Object obj = loadTest.getAssertionAt( ix );
309          if( obj instanceof Configurable )
310          {
311             ((Configurable)obj).configure();
312             tableModel.fireTableRowsUpdated( ix, ix );
313          }
314 		   else Toolkit.getDefaultToolkit().beep();
315       }
316    }
317 
318    public class RemoveAssertionAction extends AbstractAction
319    {
320       public RemoveAssertionAction()
321       {
322       	super( "Remove Assertion" );
323       	putValue( Action.SHORT_DESCRIPTION, "Removes the selected assertion from this LoadTest" );
324       	putValue( Action.SMALL_ICON, UISupport.createImageIcon( "/remove_assertion.gif" ));
325       	setEnabled( false );
326       }
327 
328       public void actionPerformed( ActionEvent e )
329       {
330       	int ix = table.getSelectedRow();
331          if( ix == -1 ) return;
332          ix = table.convertRowIndexToModel( ix );
333          
334          LoadTestAssertion assertion = loadTest.getAssertionAt( ix );
335          if( UISupport.confirm( "Remove assertion [" + assertion.getName() + "]", "Remove Assertion"))
336          {
337          	loadTest.removeAssertion( assertion );
338          }
339       }
340    }
341    
342 	public class InternalLoadTestListener implements LoadTestListener
343 	{
344 		public void assertionAdded(LoadTestAssertion assertion)
345 		{
346    		tableModel.assertionAdded( assertion );
347    		table.getSelectionModel().setSelectionInterval( tableModel.getRowCount()-1, tableModel.getRowCount()-1 );
348 		}
349 
350 		public void assertionRemoved(LoadTestAssertion assertion)
351 		{
352 			tableModel.assertionRemoved( assertion );
353 		}
354 	}   
355 }