View Javadoc

1   /*
2    *  soapui, copyright (C) 2005 Ole Matzura / 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.testcase;
14  
15  import java.awt.BorderLayout;
16  import java.awt.Color;
17  import java.awt.Component;
18  import java.awt.Dimension;
19  import java.awt.event.ActionEvent;
20  
21  import javax.swing.AbstractAction;
22  import javax.swing.Action;
23  import javax.swing.BorderFactory;
24  import javax.swing.DefaultListModel;
25  import javax.swing.JButton;
26  import javax.swing.JComponent;
27  import javax.swing.JLabel;
28  import javax.swing.JList;
29  import javax.swing.JPanel;
30  import javax.swing.JScrollPane;
31  import javax.swing.JSplitPane;
32  import javax.swing.JTextArea;
33  import javax.swing.ListCellRenderer;
34  import javax.swing.ListSelectionModel;
35  import javax.swing.event.DocumentEvent;
36  import javax.swing.event.DocumentListener;
37  import javax.swing.event.ListSelectionEvent;
38  import javax.swing.event.ListSelectionListener;
39  
40  import com.eviware.soapui.SoapUI;
41  import com.eviware.soapui.config.TransferValuesStepConfig;
42  import com.eviware.soapui.config.ValueTransferConfig;
43  import com.eviware.soapui.impl.wsdl.WsdlTestCase;
44  import com.eviware.soapui.impl.wsdl.teststeps.TransferResponseValuesTestStep;
45  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep;
46  import com.eviware.soapui.model.DesktopPanel;
47  import com.eviware.soapui.model.ModelItem;
48  import com.eviware.soapui.model.testsuite.TestStep;
49  import com.eviware.soapui.support.UISupport;
50  import com.eviware.soapui.support.XmlUtils;
51  import com.jgoodies.forms.builder.ButtonBarBuilder;
52  
53  /***
54   * DesktopPanel for TransferResponseValuesTestStep
55   * 
56   * @author Ole.Matzura
57   */
58  
59  public class TransferResponseValuesDesktopPanel extends JPanel implements DesktopPanel
60  {
61  	private final TransferResponseValuesTestStep testStep;
62  	private TransferValuesStepConfig transferConfig;
63  	private DefaultListModel listModel;
64  	private JList transferList;
65  	private JTextArea sourceArea;
66  	private JTextArea targetArea;
67  	private JButton copyButton;
68  	private JButton deleteButton;
69  	private JButton declareButton;
70  
71  	public TransferResponseValuesDesktopPanel(TransferResponseValuesTestStep testStep)
72  	{
73  		super( new BorderLayout() );
74  		this.testStep = testStep;
75  		transferConfig = testStep.getTransferConfig();
76  		buildUI();
77  	}
78  
79  	private void buildUI()
80  	{
81  		JSplitPane splitPane = UISupport.createHorizontalSplit();
82  		
83  		listModel = new DefaultListModel();
84  		ValueTransferConfig[] transfers = transferConfig.getTransfersArray();
85  		for( int c = 0; c < transfers.length; c++ )
86  		{
87  			listModel.addElement( transfers[c] );
88  		}
89  		
90  		transferList = new JList( listModel );
91  		transferList.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );
92  		transferList.addListSelectionListener( new TransferListSelectionListener());
93  		transferList.setCellRenderer( new TransferListCellRenderer());
94  		
95  		JScrollPane listScrollPane = new JScrollPane( transferList );
96  		listScrollPane.setBorder( 
97  				BorderFactory.createCompoundBorder(
98  				BorderFactory.createTitledBorder( BorderFactory.createEmptyBorder(), "Value Transfers" ), 
99  				BorderFactory.createLineBorder( Color.GRAY )));
100 		
101 		splitPane.setLeftComponent( listScrollPane);
102 		
103 		JSplitPane innerSplit = UISupport.createVerticalSplit();
104 		sourceArea = new JTextArea();
105 		sourceArea.setEnabled( false );
106 		sourceArea.getDocument().addDocumentListener( new SourceAreaDocumentListener());
107 		
108 		targetArea = new JTextArea();
109 		targetArea.setEnabled( false );
110 		targetArea.getDocument().addDocumentListener( new TargetAreaDocumentListener());
111 		
112 		JScrollPane sourceScrollPane = new JScrollPane( sourceArea );
113 		sourceScrollPane.setBorder( 
114 				BorderFactory.createCompoundBorder(
115 				BorderFactory.createTitledBorder( BorderFactory.createEmptyBorder(), "Source XPath" ), 
116 				BorderFactory.createLineBorder( Color.GRAY )));
117 		
118 		innerSplit.setTopComponent( sourceScrollPane);
119 		JScrollPane targetScrollPane = new JScrollPane( targetArea );
120 		targetScrollPane.setBorder( 
121 				BorderFactory.createCompoundBorder(
122 				BorderFactory.createTitledBorder( BorderFactory.createEmptyBorder(), "Target XPath" ), 
123 				BorderFactory.createLineBorder( Color.GRAY )));
124 		
125 		innerSplit.setBottomComponent( targetScrollPane);
126 		
127 		innerSplit.setResizeWeight( 0.5 );
128 		innerSplit.setDividerLocation( 0.5 );
129 		
130 		splitPane.setRightComponent( innerSplit );
131 		splitPane.setResizeWeight( 0.1 );
132 		splitPane.setDividerLocation( 120 );
133 		
134 		add( splitPane, BorderLayout.CENTER );
135 		
136 		ButtonBarBuilder builder = new ButtonBarBuilder();
137 		builder.addFixed( new JButton( new AddAction() ) );
138 		builder.addRelatedGap();
139 		copyButton = new JButton( new CopyAction() );
140 		copyButton.setEnabled( false );
141 		builder.addFixed( copyButton);
142 		builder.addRelatedGap();
143 		deleteButton = new JButton( new DeleteAction() );
144 		deleteButton.setEnabled( false );
145 		builder.addFixed( deleteButton);
146 		builder.addRelatedGap();
147 		declareButton = new JButton( new DeclareNamespacesAction() );
148 		declareButton.setEnabled( false );
149 		builder.addFixed( declareButton);
150 		builder.addRelatedGap();
151 		builder.addFixed( new JButton( new RunAction() ));
152 		builder.addGlue();
153 		builder.addFixed( new JButton( new CloseAction() ));
154 		builder.setBorder( BorderFactory.createEmptyBorder( 0, 3, 3, 3 ) );
155 		
156 		add( builder.getPanel(), BorderLayout.SOUTH );
157 		
158 		setBorder( BorderFactory.createEmptyBorder( 3, 3, 3, 3 ));
159 		setPreferredSize( new Dimension( 400, 300 ));
160 		
161 		if( listModel.getSize() > 0 ) 
162 			transferList.setSelectedIndex( 0 );
163 	}
164 	
165 	private final class TransferListCellRenderer extends JLabel implements ListCellRenderer 
166    {
167       public Component getListCellRendererComponent(
168         JList list,
169         Object value,           
170         int index,               
171         boolean isSelected,      
172         boolean cellHasFocus)    
173       {
174       	setText(((ValueTransferConfig)value).getName());
175 
176       	if (isSelected) 
177           {
178              setBackground(list.getSelectionBackground());
179              setForeground(list.getSelectionForeground());
180           }
181           else 
182           {
183              setBackground(list.getBackground());
184              setForeground(list.getForeground());
185           }
186           
187           setEnabled(list.isEnabled());
188           setFont(list.getFont());
189           setOpaque(true);
190           
191           return this;
192       }
193   }
194 
195 	private final class SourceAreaDocumentListener implements DocumentListener
196 	{
197 		public void insertUpdate(DocumentEvent e)
198 		{
199 			update();
200 		}
201 
202 		private void update()
203 		{
204 			int ix = transferList.getSelectedIndex();
205 			if( ix != -1 )
206 			{
207 				transferConfig.getTransfersArray( ix ).setSourcePath( sourceArea.getText() );
208 			}
209 		}
210 
211 		public void removeUpdate(DocumentEvent e)
212 		{
213 			update();
214 		}
215 
216 		public void changedUpdate(DocumentEvent e)
217 		{
218 		}
219 	}
220 	
221 	private final class TargetAreaDocumentListener implements DocumentListener
222 	{
223 		public void insertUpdate(DocumentEvent e)
224 		{
225 			update();
226 		}
227 
228 		private void update()
229 		{
230 			int ix = transferList.getSelectedIndex();
231 			if( ix != -1 )
232 			{
233 				transferConfig.getTransfersArray( ix ).setTargetPath( targetArea.getText() );
234 			}
235 		}
236 
237 		public void removeUpdate(DocumentEvent e)
238 		{
239 			update();
240 		}
241 
242 		public void changedUpdate(DocumentEvent e)
243 		{
244 		}
245 	}
246 
247 	private final class TransferListSelectionListener implements ListSelectionListener
248 	{
249 		public void valueChanged(ListSelectionEvent e)
250 		{
251 			int ix = transferList.getSelectedIndex();
252 			if( ix == -1 )
253 			{
254 				sourceArea.setText( "" );
255 				targetArea.setText( "" );
256 			}
257 			else
258 			{
259 				ValueTransferConfig config = transferConfig.getTransfersArray( ix );
260 				sourceArea.setText( config.getSourcePath() );
261 				targetArea.setText( config.getTargetPath() );
262 			}
263 			
264 			sourceArea.setEnabled( ix != -1 );
265 			targetArea.setEnabled( ix != -1 );
266 			copyButton.setEnabled( ix != -1 );
267 			deleteButton.setEnabled( ix != -1 );
268 			declareButton.setEnabled( ix != -1 );
269 		}
270 	}
271 
272 	private final class AddAction extends AbstractAction
273 	{
274 		public AddAction()
275 		{
276 			super( "Add" );
277 		}
278 		
279 		public void actionPerformed(ActionEvent e)
280 		{
281 			String name = UISupport.prompt( "Specify name for value transfer", "Add Transfer" );
282 			if( name == null || name.trim().length() == 0 ) return;
283 			
284 			ValueTransferConfig transfer = transferConfig.addNewTransfers();
285 			transfer.setName( name );
286 			
287 			listModel.addElement( transfer );
288 			transferList.setSelectedIndex( listModel.getSize()-1 );
289 		}
290 	}
291 	
292 	private final class CopyAction extends AbstractAction
293 	{
294 		public CopyAction()
295 		{
296 			super( "Copy" );
297 		}
298 		
299 		public void actionPerformed(ActionEvent e)
300 		{
301 			int ix = transferList.getSelectedIndex();
302 			ValueTransferConfig config = transferConfig.getTransfersArray( ix );
303 			
304 			String name = UISupport.prompt( "Specify name for value transfer", "Copy Transfer", config.getName() );
305 			if( name == null || name.trim().length() == 0 ) return;
306 			
307 			ValueTransferConfig transfer = transferConfig.addNewTransfers();
308 			transfer.setName( name );
309 			transfer.setSourcePath( config.getSourcePath() );
310 			transfer.setTargetPath( config.getTargetPath() );
311 			
312 			listModel.addElement( transfer );
313 			transferList.setSelectedIndex( listModel.getSize()-1 );
314 		}
315 	}
316 	
317 	private final class DeleteAction extends AbstractAction
318 	{
319 		public DeleteAction()
320 		{
321 			super( "Delete" );
322 		}
323 		
324 		public void actionPerformed(ActionEvent e)
325 		{
326 			if( UISupport.confirm( "Delete selected transfer", "Delete Transfer" )) 
327 			{
328 				int ix = transferList.getSelectedIndex();
329 				transferConfig.removeTransfers( ix );
330 				listModel.remove( ix );
331 				
332 				if( listModel.getSize() > 0 )
333 					transferList.setSelectedIndex( ix > listModel.getSize()-1 ? listModel.getSize()-1 : ix );
334 			}
335 		}
336 	}
337 	
338 	private final class DeclareNamespacesAction extends AbstractAction
339 	{
340 		public DeclareNamespacesAction()
341 		{
342 			super( "Declare" );
343 			putValue( Action.SHORT_DESCRIPTION, "Declare available response/request namespaces in source/target expressions" );
344 		}
345 		
346 		public void actionPerformed(ActionEvent e)
347 		{
348 			try
349 			{
350 				WsdlTestCase testCase = (WsdlTestCase) testStep.getTestCase();
351 				int ix = testCase.getIndexOfTestStep(testStep);
352 
353 				if (ix > 0)
354 				{
355 					TestStep previousStep = testCase.getTestStepAt(ix - 1);
356 					if (previousStep instanceof WsdlTestRequestStep)
357 					{
358 						String xml = ((WsdlTestRequestStep) previousStep)
359 								.getTestRequest().getResponseContent();
360 						if ( xml != null && xml.trim().length() > 0)
361 							sourceArea.setText(XmlUtils.declareXPathNamespaces(xml)
362 									+ sourceArea.getText());
363 					}
364 				}
365 
366 				if (ix < testCase.getTestStepCount() - 1)
367 				{
368 					TestStep nextStep = testCase.getTestStepAt(ix + 1);
369 					if (nextStep instanceof WsdlTestRequestStep)
370 					{
371 						String xml = ((WsdlTestRequestStep) nextStep).getTestRequest().getRequestContent();
372 						if ( xml != null && xml.trim().length() > 0)
373 								targetArea.setText(XmlUtils.declareXPathNamespaces(xml)
374 										+ targetArea.getText());
375 					}
376 				}
377 			}
378 			catch (Exception e1)
379 			{
380 				e1.printStackTrace();
381 			}
382 		}
383 	}
384 	
385 	private final class RunAction extends AbstractAction
386 	{
387 		public RunAction()
388 		{
389 			super( "Run" );
390 		}
391 		
392 		public void actionPerformed(ActionEvent e)
393 		{
394 			if( listModel.getSize() == 0 )
395 			{
396 				UISupport.showErrorMessage( "Missing transfers!" );
397 				return;
398 			}
399 			
400 			WsdlTestCase testCase = (WsdlTestCase)testStep.getTestCase();
401 			int ix = testCase.getIndexOfTestStep( testStep );
402 			if( ix == 0 )
403 			{
404 				UISupport.showErrorMessage("Previous step is not a request step" );
405 				return;
406 			}
407 			
408 			if( ix == testCase.getTestStepCount()-1 )
409 			{
410 				UISupport.showErrorMessage("Following step is not a request step" );
411 				return;
412 			}
413 			
414 			TestStep previousStep = testCase.getTestStepAt( ix-1 );
415 			if( !(previousStep instanceof WsdlTestRequestStep )) 
416 			{
417 				UISupport.showErrorMessage("Previous step is not a request step" );
418 				return;
419 			}
420 
421 			TestStep nextStep = testCase.getTestStepAt( ix+1 );
422 			if( !(nextStep instanceof WsdlTestRequestStep )) 
423 			{
424 				UISupport.showErrorMessage("Following step is not a request step" );
425 				return;
426 			}
427 			
428 			testStep.runTransfer( ((WsdlTestRequestStep) previousStep)
429 						.getTestRequest(), ((WsdlTestRequestStep) nextStep)
430 						.getTestRequest());
431 		}
432 	}
433 	
434 	private final class CloseAction extends AbstractAction
435 	{
436 		public CloseAction()
437 		{
438 			super( "Close" );
439 		}
440 		
441 		public void actionPerformed(ActionEvent e)
442 		{
443 			SoapUI.getInstance().closeTab( testStep );
444 		}
445 	}
446 
447 	public ModelItem getModelItem()
448 	{
449 		return testStep;
450 	}
451 
452 	public boolean onClose()
453 	{
454 		return true;
455 	}
456 
457 	public JComponent getComponent()
458 	{
459 		return this;
460 	}
461 
462 }