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