View Javadoc

1   package com.eviware.soapui.impl.wsdl.endpoint;
2   
3   import java.awt.BorderLayout;
4   import java.awt.Color;
5   import java.awt.Component;
6   import java.awt.Toolkit;
7   import java.awt.event.ActionEvent;
8   import java.util.ArrayList;
9   import java.util.Arrays;
10  import java.util.List;
11  
12  import javax.swing.AbstractAction;
13  import javax.swing.Action;
14  import javax.swing.BorderFactory;
15  import javax.swing.DefaultCellEditor;
16  import javax.swing.DefaultComboBoxModel;
17  import javax.swing.JButton;
18  import javax.swing.JComboBox;
19  import javax.swing.JPanel;
20  import javax.swing.JScrollPane;
21  import javax.swing.JTable;
22  import javax.swing.ListSelectionModel;
23  import javax.swing.ScrollPaneConstants;
24  import javax.swing.event.ListSelectionEvent;
25  import javax.swing.event.ListSelectionListener;
26  import javax.swing.table.AbstractTableModel;
27  
28  import org.jdesktop.swingx.JXTable;
29  
30  import com.eviware.soapui.config.EndpointConfig;
31  import com.eviware.soapui.impl.wsdl.WsdlInterface;
32  import com.eviware.soapui.impl.wsdl.WsdlRequest;
33  import com.eviware.soapui.impl.wsdl.actions.support.ShowOnlineHelpAction;
34  import com.eviware.soapui.impl.wsdl.endpoint.DefaultEndpointStrategy.EndpointDefaults;
35  import com.eviware.soapui.impl.wsdl.support.HelpUrls;
36  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequest;
37  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep;
38  import com.eviware.soapui.model.iface.Operation;
39  import com.eviware.soapui.model.testsuite.TestCase;
40  import com.eviware.soapui.model.testsuite.TestStep;
41  import com.eviware.soapui.model.testsuite.TestSuite;
42  import com.eviware.soapui.support.UISupport;
43  import com.eviware.soapui.support.components.JXToolBar;
44  import com.eviware.soapui.support.components.MetricsPanel;
45  
46  public class DefaultEndpointStrategyConfigurationPanel extends JPanel
47  {
48  	private EndpointsTableModel tableModel;
49  	private JXTable table;
50  	private JButton deleteButton;
51  	private JButton assignButton;
52  	private WsdlInterface iface;
53  	private final DefaultEndpointStrategy strategy;
54  
55  	public DefaultEndpointStrategyConfigurationPanel( WsdlInterface iface, DefaultEndpointStrategy strategy )
56  	{
57  		super( new BorderLayout() );
58  		
59  		this.iface = iface;
60  		this.strategy = strategy;
61  		
62  		buildUI();
63  		
64  		enableButtons();
65  	}
66  
67  	private void buildUI()
68  	{
69  		tableModel = new EndpointsTableModel();
70  		table = new JXTable( tableModel );
71  		table.setHorizontalScrollEnabled( true );
72  		table.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );
73  		table.getSelectionModel().addListSelectionListener( new ListSelectionListener()
74  		{
75  			public void valueChanged( ListSelectionEvent e )
76  			{
77  				enableButtons();
78  			}
79  		} );
80  		
81  		for( int c = 0; c < table.getColumnCount(); c++ )
82  			table.getColumn( c ).setHeaderRenderer( new MetricsPanel.InternalHeaderRenderer( getBackground() ) );
83  		
84  		table.getColumn( 0 ).setPreferredWidth( 250 );
85  		table.getColumn( 4 ).setCellEditor( new DefaultCellEditor(
86  					new JComboBox( new String[] {WsdlRequest.PW_TYPE_NONE, WsdlRequest.PW_TYPE_TEXT, WsdlRequest.PW_TYPE_DIGEST})) );
87  		
88  		table.getColumn( 6 ).setCellEditor( new OutgoingWssCellEditor() );
89  		table.getColumn( 7 ).setCellEditor( new IncomingWssCellEditor() );
90  		table.getColumn( 8 ).setCellEditor( new DefaultCellEditor(
91  					new JComboBox( new String[] {
92  								EndpointConfig.Mode.OVERRIDE.toString(),
93  								EndpointConfig.Mode.COMPLEMENT.toString(),
94  								EndpointConfig.Mode.COPY.toString()
95  					})) );
96  
97  		table.getTableHeader().setReorderingAllowed( false );
98  		
99  		setBackground( Color.WHITE );
100 		setOpaque( true );
101 		
102 		JScrollPane scrollPane = new JScrollPane( table );
103 		scrollPane.setBorder( BorderFactory.createEmptyBorder( 5, 2, 5, 2 ));
104 		scrollPane.setVerticalScrollBarPolicy( ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS );
105 		scrollPane.setBackground( Color.WHITE );
106 
107 		add( scrollPane, BorderLayout.CENTER );
108 		add( createButtons(), BorderLayout.NORTH );
109 	}
110 
111 	protected void enableButtons()
112 	{
113 		deleteButton.setEnabled( table.getSelectedRow() != -1 );
114 		assignButton.setEnabled( table.getSelectedRow() != -1 );
115 	}
116 
117 	private Component createButtons()
118 	{
119 		JXToolBar toolbar = UISupport.createToolbar();
120 		
121 		toolbar.addFixed( UISupport.createToolbarButton( new AddAction() ) );
122 		deleteButton = UISupport.createToolbarButton( new DeleteAction() );
123 		toolbar.addFixed( deleteButton );
124 		toolbar.addRelatedGap();
125 		assignButton = UISupport.createToolbarButton( new AssignAction() );
126 		toolbar.addFixed( assignButton );
127 
128 		toolbar.addGlue();
129 		ShowOnlineHelpAction showOnlineHelpAction = new ShowOnlineHelpAction( HelpUrls.ENDPOINTSEDITOR_HELP_URL );
130 		toolbar.addFixed( UISupport.createToolbarButton( showOnlineHelpAction ) );
131 		
132 		return toolbar;
133 	}
134 
135 	private class AddAction extends AbstractAction
136 	{
137 		public AddAction()
138 		{
139 			putValue( SMALL_ICON, UISupport.createImageIcon( "/add_property.gif" ));
140 			putValue( Action.SHORT_DESCRIPTION, "Adds a new endpoint to the list" );
141 		}
142 
143 		public void actionPerformed( ActionEvent e )
144 		{
145 			String endpoint = UISupport.prompt( "Enter new endpoint URL", "Add Endpoint", "" );
146 
147 			if( endpoint == null )
148 				return;
149 
150 			tableModel.addEndpoint( endpoint );
151 		}
152 	}
153 
154 	private class AssignAction extends AbstractAction
155 	{
156 		private static final String ALL_REQUESTS = "- All Requests -";
157 		private static final String ALL_TEST_REQUESTS = "- All Test Requests -";
158 		private static final String ALL_REQUESTS_AND_TEST_REQUESTS = "- All Requests and TestRequests -";
159 		private static final String ALL_REQUESTS_WITH_NO_ENDPOINT = "- All Requests with no endpoint -";
160 
161 		public AssignAction()
162 		{
163 			super( "Assign" );
164 			putValue( Action.SHORT_DESCRIPTION,
165 						"Assigns the selected endpoint to Requests/TestRequests for this Interface" );
166 		}
167 
168 		public void actionPerformed( ActionEvent e )
169 		{
170 			int selectedIndex = table.getSelectedRow();
171 			if( selectedIndex == -1 )
172 			{
173 				Toolkit.getDefaultToolkit().beep();
174 				return;
175 			}
176 
177 			String selectedEndpoint = tableModel.getEndpointAt( selectedIndex );
178 			EndpointDefaults defaults = tableModel.getDefaultsAt( selectedIndex );
179 
180 			List<String> list = new ArrayList<String>( Arrays.asList( iface.getEndpoints() ) );
181 			list.add( 0, ALL_REQUESTS );
182 			list.add( 1, ALL_TEST_REQUESTS );
183 			list.add( 2, ALL_REQUESTS_AND_TEST_REQUESTS );
184 			list.add( 3, ALL_REQUESTS_WITH_NO_ENDPOINT );
185 
186 			Object endpoint = UISupport.prompt( "Assign selected endpoint and authorization to..", "Assign Endpoint", list.toArray(),
187 						ALL_REQUESTS_WITH_NO_ENDPOINT );
188 
189 			if( endpoint == null )
190 				return;
191 
192 			int changeCount = 0;
193 
194 			if( endpoint.equals( ALL_REQUESTS ) || endpoint.equals( ALL_REQUESTS_WITH_NO_ENDPOINT )
195 						|| endpoint.equals( ALL_REQUESTS_AND_TEST_REQUESTS ) )
196 			{
197 				for( int c = 0; c < iface.getOperationCount(); c++ )
198 				{
199 					Operation operation = iface.getOperationAt( c );
200 					for( int i = 0; i < operation.getRequestCount(); i++ )
201 					{
202 						WsdlRequest request = ( WsdlRequest ) operation.getRequestAt( i );
203 						String ep = request.getEndpoint();
204 
205 						if( endpoint.equals( ALL_REQUESTS ) || endpoint.equals( ALL_REQUESTS_AND_TEST_REQUESTS )
206 									|| ( endpoint.equals( ALL_REQUESTS_WITH_NO_ENDPOINT ) && ep == null )
207 									|| ( ep.equals( endpoint ) ) )
208 						{
209 							request.setEndpoint( selectedEndpoint );
210 							
211 							request.setUsername( defaults.getUsername() );
212 							request.setPassword( defaults.getPassword() );
213 							request.setDomain( defaults.getDomain() );
214 							request.setWssPasswordType( defaults.getWssType() );
215 							request.setWssTimeToLive( defaults.getWssTimeToLive() );
216 							request.setOutgoingWss( defaults.getOutgoingWss() );
217 							request.setIncomingWss( defaults.getIncomingWss() );
218 							
219 							changeCount++;
220 						}
221 					}
222 				}
223 			}
224 
225 			if( endpoint.equals( ALL_REQUESTS_AND_TEST_REQUESTS ) || endpoint.equals( ALL_TEST_REQUESTS ) )
226 			{
227 				for( TestSuite testSuite : iface.getProject().getTestSuiteList() )
228 				{
229 					for( TestCase testCase : testSuite.getTestCaseList() )
230 					{
231 						for( TestStep testStep : testCase.getTestStepList() )
232 						{
233 							if( testStep instanceof WsdlTestRequestStep )
234 							{
235 								WsdlTestRequest testRequest = ( ( WsdlTestRequestStep ) testStep ).getTestRequest();
236 								testRequest.setEndpoint( selectedEndpoint );
237 								
238 								testRequest.setUsername( defaults.getUsername() );
239 								testRequest.setPassword( defaults.getPassword() );
240 								testRequest.setDomain( defaults.getDomain() );
241 								testRequest.setWssPasswordType( defaults.getWssType() );
242 								testRequest.setWssTimeToLive( defaults.getWssTimeToLive() );
243 								testRequest.setOutgoingWss( defaults.getOutgoingWss() );
244 								testRequest.setIncomingWss( defaults.getIncomingWss() );
245 								
246 								changeCount++;
247 							}
248 						}
249 					}
250 				}
251 			}
252 		}
253 	}
254 
255 	private class DeleteAction extends AbstractAction
256 	{
257 		public DeleteAction()
258 		{
259 			putValue( SMALL_ICON, UISupport.createImageIcon( "/remove_property.gif" ));
260 			putValue( Action.SHORT_DESCRIPTION, "Deletes the selected endpoint from the list" );
261 		}
262 
263 		public void actionPerformed( ActionEvent e )
264 		{
265 			int index = table.getSelectedRow();
266 			if( index == -1 )
267 			{
268 				Toolkit.getDefaultToolkit().beep();
269 				return;
270 			}
271 
272 			if( UISupport.confirm( "Delete selected endpoint?", "Delete Endpoint" ) )
273 			{
274 				tableModel.removeEndpoint( index );
275 			}
276 		}
277 	}
278 
279 	private class EndpointsTableModel extends AbstractTableModel
280 	{
281 		public int getColumnCount()
282 		{
283 			return 9;
284 		}
285 
286 		@Override
287 		public String getColumnName( int column )
288 		{
289 			switch( column )
290 			{
291 			case 0:
292 				return "Endpoint";
293 			case 1:
294 				return "Username";
295 			case 2:
296 				return "Password";
297 			case 3:
298 				return "Domain";
299 			case 4:
300 				return "WSS-Type";
301 			case 5:
302 				return "WSS-TimeToLive";
303 			case 6 :
304 				return "Outgoing WSS";
305 			case 7 : 
306 				return "Incoming WSS";
307 			case 8:
308 				return "Mode";
309 			}
310 
311 			return null;
312 		}
313 
314 		public String getEndpointAt( int selectedIndex )
315 		{
316 			return iface.getEndpoints()[selectedIndex];
317 		}
318 		
319 		public EndpointDefaults getDefaultsAt( int selectedIndex )
320 		{
321 			String endpoint = getEndpointAt( selectedIndex );
322 			return strategy.getEndpointDefaults( endpoint );
323 		}
324 
325 		public void addEndpoint( String endpoint )
326 		{
327 			int rowCount = getRowCount();
328 			iface.addEndpoint( endpoint );
329 			
330 			fireTableRowsInserted( rowCount, rowCount );
331 		}
332 		
333 		public void removeEndpoint( int index )
334 		{
335 			String ep = getEndpointAt( index );
336 			iface.removeEndpoint( ep );
337 			fireTableRowsDeleted( index, index );
338 		}
339 		
340 		public int getRowCount()
341 		{
342 			return iface == null ? 0 : iface.getEndpoints().length;
343 		}
344 
345 		public Object getValueAt( int rowIndex, int columnIndex )
346 		{
347 			String endpoint = getEndpointAt( rowIndex );
348 			EndpointDefaults defaults = strategy.getEndpointDefaults( endpoint );
349 
350 			switch( columnIndex )
351 			{
352 			case 0:
353 				return endpoint;
354 			case 1:
355 				return defaults.getUsername();
356 			case 2:
357 				return defaults.getPassword();
358 			case 3:
359 				return defaults.getDomain();
360 			case 4:
361 				return defaults.getWssType();
362 			case 5:
363 				return defaults.getWssTimeToLive();
364 			case 6:
365 				return defaults.getOutgoingWss();
366 			case 7:
367 				return defaults.getIncomingWss();
368 			case 8:
369 				return defaults.getMode();
370 			}
371 
372 			return null;
373 		}
374 
375 		@Override
376 		public boolean isCellEditable( int rowIndex, int columnIndex )
377 		{
378 			return true;
379 		}
380 
381 		@Override
382 		public void setValueAt( Object aValue, int rowIndex, int columnIndex )
383 		{
384 			String endpoint = getEndpointAt( rowIndex );
385 			EndpointDefaults defaults = strategy.getEndpointDefaults( endpoint );
386 			
387 			if( aValue == null )
388 				aValue = "";
389 			
390 			switch( columnIndex )
391 			{
392 				case 0 :
393 				{
394 					iface.changeEndpoint( endpoint, aValue.toString() );
395 					break;
396 				}
397 				case 1 :
398 				{
399 					defaults.setUsername( aValue.toString() );
400 					break;
401 				}
402 				case 2 :
403 				{
404 					defaults.setPassword( aValue.toString() );
405 					break;
406 				}
407 				case 3 :
408 				{
409 					defaults.setDomain( aValue.toString() );
410 					break;
411 				}
412 				case 4 :
413 				{
414 					defaults.setWssType( aValue.toString() );
415 					break;
416 				}
417 				case 5 :
418 				{
419 					defaults.setWssTimeToLive( aValue.toString() );
420 					break;
421 				}
422 				case 6 :
423 				{
424 					defaults.setOutgoingWss( aValue.toString() );
425 					break;
426 				}
427 				case 7 :
428 				{
429 					defaults.setIncomingWss( aValue.toString() );
430 					break;
431 				}
432 				case 8 :
433 				{
434 					defaults.setMode( EndpointConfig.Mode.Enum.forString( aValue.toString() ) );
435 					break;
436 				}
437 			}
438 		}
439 	}
440 	
441 	private class IncomingWssCellEditor extends DefaultCellEditor
442 	{
443 		public IncomingWssCellEditor()
444 		{
445 			super( new JComboBox() );
446 		}
447 
448 		@Override
449 		public Component getTableCellEditorComponent( JTable table, Object value, boolean isSelected, int row, int column )
450 		{
451 			JComboBox comboBox = ( JComboBox ) super.getTableCellEditorComponent( table, value, isSelected, row, column );
452 			
453 			DefaultComboBoxModel model = new DefaultComboBoxModel( iface.getProject().getWssContainer().getIncomingNames() );
454 			model.addElement( "" );
455 			
456 			comboBox.setModel( model );
457 			
458 			return comboBox;
459 		}
460 	}
461 	
462 	private class OutgoingWssCellEditor extends DefaultCellEditor
463 	{
464 		public OutgoingWssCellEditor()
465 		{
466 			super( new JComboBox() );
467 		}
468 
469 		@Override
470 		public Component getTableCellEditorComponent( JTable table, Object value, boolean isSelected, int row, int column )
471 		{
472 			JComboBox comboBox = ( JComboBox ) super.getTableCellEditorComponent( table, value, isSelected, row, column );
473 			
474 			DefaultComboBoxModel model = new DefaultComboBoxModel( iface.getProject().getWssContainer().getOutgoingNames() );
475 			model.addElement( "" );
476 			
477 			comboBox.setModel( model );
478 			
479 			return comboBox;
480 		}
481 	}
482 }