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.actions;
14  
15  import java.awt.BorderLayout;
16  import java.awt.Dimension;
17  import java.awt.event.ActionEvent;
18  
19  import javax.swing.AbstractAction;
20  import javax.swing.Action;
21  import javax.swing.BorderFactory;
22  import javax.swing.JButton;
23  import javax.swing.JDialog;
24  import javax.swing.JPanel;
25  
26  import com.eviware.soapui.SoapUI;
27  import com.eviware.soapui.impl.wsdl.WsdlTestCase;
28  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep;
29  import com.eviware.soapui.model.testsuite.TestStep;
30  import com.eviware.soapui.support.SimpleForm;
31  import com.eviware.soapui.support.UISupport;
32  import com.jgoodies.forms.factories.ButtonBarFactory;
33  
34  /***
35   * Set the credentials for all requests in a testcase
36   * 
37   * @author Ole.Matzura
38   */
39  
40  public class SetCredentialsAction extends AbstractAction
41  {
42  	private final WsdlTestCase testCase;
43  	private JDialog dialog;
44  	private SimpleForm form;
45  
46  	private static final String DOMAIN = "Domain";
47  	private static final String PASSWORD = "Password";
48  	private static final String USERNAME = "Username";
49  
50  	public SetCredentialsAction( WsdlTestCase testCase )
51     {
52        this.testCase = testCase;
53  		putValue( Action.SMALL_ICON, SoapUI.createImageIcon( "/set_credentials.gif"));
54        putValue( Action.SHORT_DESCRIPTION, "Sets the credentials for all requests in this testcase" );
55     }
56     
57  	public void actionPerformed(ActionEvent e)
58  	{
59  		if( dialog == null )
60  		{
61  			buildDialog();
62  		}
63  		
64  		UISupport.showDialog( dialog );
65     }
66  
67  	private void buildDialog()
68  	{
69  		dialog = new JDialog( SoapUI.getInstance().getFrame(), "Set TestCase Credentials" );
70  		form = new SimpleForm();
71  		form.appendTextField( USERNAME );
72  		form.appendTextField( PASSWORD );
73  		form.appendTextField( DOMAIN );
74  		form.getPanel().setBorder( BorderFactory.createEmptyBorder( 0, 0, 10, 0 ));
75  		
76  		JPanel panel = new JPanel( new BorderLayout() );
77  		panel.add( form.getPanel(), BorderLayout.CENTER );
78  		
79  		JPanel buttonBar = ButtonBarFactory.buildOKCancelBar( new JButton( new OkAction() ), new JButton( new CancelAction() ));
80  		panel.add( buttonBar, BorderLayout.SOUTH );
81  		panel.setBorder( BorderFactory.createEmptyBorder( 10, 10, 10, 10 ));
82  		panel.setPreferredSize( new Dimension( 250, (int) panel.getPreferredSize().getHeight() ));
83  		
84  		dialog.getContentPane().add( panel );
85  		dialog.pack();
86  	}
87  	
88  	private class OkAction extends AbstractAction
89  	{
90  		public OkAction()
91  		{
92  			super( "Ok" );
93  		}
94  		
95  		public void actionPerformed(ActionEvent e)
96  		{
97  			for( int c = 0; c < testCase.getTestStepCount(); c++ )
98  	   	{
99  	   		TestStep step = testCase.getTestStepAt( c );
100 	   		if( step instanceof WsdlTestRequestStep )
101 	   		{
102 	   			WsdlTestRequestStep requestStep = (WsdlTestRequestStep) step;
103 	   			requestStep.getTestRequest().setUsername( form.getComponentValue( USERNAME ));
104 	   			requestStep.getTestRequest().setPassword( form.getComponentValue( PASSWORD ));
105 	   			requestStep.getTestRequest().setDomain( form.getComponentValue( DOMAIN ));
106 	   		}
107 	   	}
108 			
109 			dialog.setVisible( false );
110 		}
111 	}
112 
113 	private class CancelAction extends AbstractAction
114 	{
115 		public CancelAction()
116 		{
117 			super( "Cancel" );
118 		}
119 
120 		public void actionPerformed(ActionEvent e)
121 		{
122 			dialog.setVisible( false );
123 		}
124 	}
125 }