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