View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2007 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of version 2.1 of the GNU Lesser General Public License as published by 
6    *  the Free Software Foundation.
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.actions.iface;
14  
15  import java.awt.BorderLayout;
16  import java.awt.Color;
17  import java.awt.Component;
18  import java.awt.Toolkit;
19  import java.awt.event.ActionEvent;
20  import java.util.ArrayList;
21  import java.util.Arrays;
22  import java.util.List;
23  
24  import javax.swing.AbstractAction;
25  import javax.swing.Action;
26  import javax.swing.BorderFactory;
27  import javax.swing.DefaultListModel;
28  import javax.swing.JButton;
29  import javax.swing.JDialog;
30  import javax.swing.JList;
31  import javax.swing.JPanel;
32  import javax.swing.JScrollPane;
33  import javax.swing.ListSelectionModel;
34  import javax.swing.ScrollPaneConstants;
35  import javax.swing.event.ListSelectionEvent;
36  import javax.swing.event.ListSelectionListener;
37  
38  import org.apache.log4j.Logger;
39  
40  import com.eviware.soapui.impl.wsdl.WsdlInterface;
41  import com.eviware.soapui.impl.wsdl.actions.support.ShowOnlineHelpAction;
42  import com.eviware.soapui.impl.wsdl.support.HelpUrls;
43  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequest;
44  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep;
45  import com.eviware.soapui.model.iface.Operation;
46  import com.eviware.soapui.model.iface.Request;
47  import com.eviware.soapui.model.testsuite.TestCase;
48  import com.eviware.soapui.model.testsuite.TestStep;
49  import com.eviware.soapui.model.testsuite.TestSuite;
50  import com.eviware.soapui.support.UISupport;
51  import com.eviware.soapui.support.action.support.AbstractSoapUIAction;
52  import com.jgoodies.forms.builder.ButtonBarBuilder;
53  
54  /***
55   * Manages the service endpoints for a WsdlInterface
56   * 
57   * @author Ole.Matzura
58   */
59  
60  public class InterfaceEndpointsAction extends AbstractSoapUIAction<WsdlInterface>
61  {
62     private JDialog dialog;
63     private JList list;
64     private DefaultListModel listModel;
65  	private WsdlInterface iface;
66     private final static Logger log = Logger.getLogger( InterfaceEndpointsAction.class );
67  	private JButton editButton;
68  	private JButton deleteButton;
69  	private JButton assignButton;
70  
71     public InterfaceEndpointsAction()
72     {
73        super( "Service Endpoints", "Manage service endpoints available for this interface" );
74     }
75  
76  	private void buildDialog()
77  	{
78  		dialog = new JDialog( UISupport.getMainFrame() );
79        dialog.setTitle("Interface Service Endpoints" );
80        
81        JPanel contentPanel = new JPanel( new BorderLayout() );
82        listModel = new DefaultListModel();
83        list = new JList( listModel );
84        list.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
85        list.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );
86        list.getSelectionModel().addListSelectionListener( new ListSelectionListener() {
87  
88  			public void valueChanged( ListSelectionEvent e )
89  			{
90  				enableButtons();
91  			}} );
92        
93        JScrollPane scrollPane = new JScrollPane( list );
94        
95        scrollPane.setBorder( BorderFactory.createCompoundBorder(
96        		BorderFactory.createEmptyBorder( 5, 5, 5, 5 ), BorderFactory.createLineBorder( Color.GRAY )));
97        
98        scrollPane.setVerticalScrollBarPolicy( ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS );
99        
100       contentPanel.add( scrollPane, BorderLayout.CENTER );
101       contentPanel.add( createButtons(), BorderLayout.SOUTH );
102       Component descriptionPanel = UISupport.buildDescription( "Service Endpoints", 
103       			"Edit available service endpoints for this interface in list below", null );
104       contentPanel.add( descriptionPanel, BorderLayout.NORTH );
105       
106       dialog.setContentPane( contentPanel );
107       dialog.setSize(400, 300);
108       
109       dialog.setModal( true );
110 	}
111 	
112    protected void enableButtons()
113 	{
114 		editButton.setEnabled( list.getSelectedIndex() != -1 );
115 		deleteButton.setEnabled( list.getSelectedIndex() != -1 );
116 		assignButton.setEnabled( list.getSelectedIndex() != -1 );
117 	}
118 
119 	public void perform( WsdlInterface iface, Object param )
120 	{
121    	if( dialog == null )
122    		buildDialog();
123    	
124       listModel.clear();
125       
126       String[] endpoints = iface.getEndpoints();
127       for( int c = 0; c < endpoints.length; c++ )
128       {
129          listModel.addElement( endpoints[c] );
130       }
131       
132       list.setSelectedIndex( -1 );
133       
134       this.iface = iface;
135       enableButtons();
136       UISupport.showDialog( dialog );
137    }
138 
139    private Component createButtons()
140    {
141       ButtonBarBuilder builder = ButtonBarBuilder.createLeftToRightBuilder();
142 		builder.addFixed( UISupport.createToolbarButton( new ShowOnlineHelpAction( HelpUrls.ENDPOINTSEDITOR_HELP_URL )));
143 		builder.addGlue();
144 		builder.addFixed( new JButton( new AddAction() ));
145       builder.addRelatedGap();
146       editButton = new JButton( new EditAction() );
147 		builder.addFixed( editButton);
148       builder.addRelatedGap();
149       deleteButton = new JButton( new DeleteAction() );
150 		builder.addFixed( deleteButton);
151       builder.addRelatedGap();
152       assignButton = new JButton( new AssignAction() );
153 		builder.addFixed( assignButton);
154       builder.addRelatedGap();
155       builder.addFixed( new JButton( new OkAction() ));
156       
157       builder.setBorder(
158    				BorderFactory.createCompoundBorder( 
159    						BorderFactory.createCompoundBorder( 
160    								BorderFactory.createMatteBorder( 1, 0, 0, 0, Color.GRAY ),
161    								BorderFactory.createMatteBorder( 1, 0, 0, 0, Color.WHITE )),
162    						BorderFactory.createEmptyBorder( 3, 5, 3, 5 )));
163       
164       return builder.getPanel();
165    }
166    
167    private class AddAction extends AbstractAction
168    {
169       public AddAction()
170       {
171          super( "Add" );
172          putValue( Action.SHORT_DESCRIPTION, "Adds a new endpoint to the list" );
173       }
174       
175       public void actionPerformed(ActionEvent e)
176       {
177          Object selectedValue = list.getSelectedValue();
178 			String endpoint = UISupport.prompt( "Enter new endpoint URL", "Add Endpoint", 
179          		selectedValue == null ? "" : selectedValue.toString() );
180 			
181          if( endpoint == null ) return;
182          
183          listModel.addElement( endpoint );
184          iface.addEndpoint( endpoint );
185       }
186    }
187    
188    private class AssignAction extends AbstractAction
189    {
190       private static final String ALL_REQUESTS = "- All Requests -";
191       private static final String ALL_TEST_REQUESTS = "- All Test Requests -";
192       private static final String ALL_REQUESTS_AND_TEST_REQUESTS = "- All Requests and TestRequests -";
193 		private static final String ALL_REQUESTS_WITH_NO_ENDPOINT = "- All Requests with no endpoint -";
194 
195 		public AssignAction()
196       {
197          super( "Assign" );
198          putValue( Action.SHORT_DESCRIPTION, "Assigns the selected endpoint to Requests/TestRequests for this Interface" );
199       }
200       
201       public void actionPerformed(ActionEvent e)
202       {
203       	int selectedIndex = list.getSelectedIndex();
204          if( selectedIndex == -1 ) 
205          {
206          	Toolkit.getDefaultToolkit().beep();
207          	return;
208          }
209          
210          String selectedEndpoint = (String) listModel.getElementAt( selectedIndex );
211          
212          List<String> list = new ArrayList<String>( Arrays.asList( iface.getEndpoints()) );
213          list.add( 0, ALL_REQUESTS );
214          list.add( 1, ALL_TEST_REQUESTS );
215          list.add( 2, ALL_REQUESTS_AND_TEST_REQUESTS );
216          list.add( 3, ALL_REQUESTS_WITH_NO_ENDPOINT );
217          
218          Object endpoint = UISupport.prompt(	"Assign selected endpoint to..", "Assign Endpoint", 
219          		 list.toArray(), ALL_REQUESTS_WITH_NO_ENDPOINT );
220          
221          if( endpoint == null )
222          	return;
223          
224          int changeCount = 0;
225          
226          if( endpoint.equals( ALL_REQUESTS ) || endpoint.equals( ALL_REQUESTS_WITH_NO_ENDPOINT )
227          			|| endpoint.equals( ALL_REQUESTS_AND_TEST_REQUESTS ))
228          {
229 	         for( int c = 0; c < iface.getOperationCount(); c++ )
230 	         {
231 	         	Operation operation = iface.getOperationAt( c );
232 	         	for( int i = 0; i < operation.getRequestCount(); i++ )
233 	         	{
234 	         		Request request = operation.getRequestAt( i );
235 	         		String ep = request.getEndpoint();
236 	
237 	               if( endpoint.equals( ALL_REQUESTS ) || endpoint.equals( ALL_REQUESTS_AND_TEST_REQUESTS ) ||
238 	               	 ( endpoint.equals(  ALL_REQUESTS_WITH_NO_ENDPOINT ) && ep == null ) ||
239 	               	 ( ep.equals( endpoint )))
240 	               {
241 	               	request.setEndpoint( selectedEndpoint );
242 	               	changeCount++;
243 	               }
244 	         	}
245 	         }
246          }
247          
248          if( endpoint.equals( ALL_REQUESTS_AND_TEST_REQUESTS ) || endpoint.equals( ALL_TEST_REQUESTS ))
249          {
250          	for( TestSuite testSuite : iface.getProject().getTestSuites() )
251 	         {
252 	         	for( TestCase testCase : testSuite.getTestCaseList())
253 	         	{
254 	         		for( TestStep testStep : testCase.getTestStepList())
255 	         		{
256 	         			if( testStep instanceof WsdlTestRequestStep )
257 	         			{
258 	         				WsdlTestRequest testRequest = ((WsdlTestRequestStep)testStep).getTestRequest();
259 	         				testRequest.setEndpoint( selectedEndpoint );
260 	         				changeCount++;
261 	         			}
262 	         		}
263 	         	}
264 	         }
265          }
266          
267          log.info( "Assigned endpoint [" + selectedEndpoint + "] to " + changeCount + " Requests" );
268       }
269    }
270    
271    private class EditAction extends AbstractAction
272    {
273       public EditAction()
274       {
275          super( "Edit" );
276          putValue( Action.SHORT_DESCRIPTION, "Edit the selected endpoint" );
277       }
278       
279       public void actionPerformed(ActionEvent e)
280       {
281          int selectedIndex = list.getSelectedIndex();
282          if( selectedIndex == -1 ) 
283          {
284          	Toolkit.getDefaultToolkit().beep();
285          	return;
286          }
287          	
288          String oldEndpoint = (String) listModel.getElementAt( selectedIndex );
289          String newEndpoint = UISupport.prompt( "Edit endpoint address", "Edit Endpoint", oldEndpoint );
290          if( newEndpoint == null ) return;
291          
292          listModel.setElementAt( newEndpoint, selectedIndex );
293          iface.changeEndpoint( oldEndpoint, newEndpoint );
294       }
295    }
296    
297    private class DeleteAction extends AbstractAction
298    {
299       public DeleteAction()
300       {
301          super( "Delete" );
302          putValue( Action.SHORT_DESCRIPTION, "Deletes the selected endpoint from the list" );
303       }
304       
305       public void actionPerformed(ActionEvent e )
306       {   
307          int index = list.getSelectedIndex();
308          if( index == -1)
309          {
310          	Toolkit.getDefaultToolkit().beep();
311          	return;
312          }
313          
314          if( UISupport.confirm( "Delete selected endpoint?", "Delete Endpoint" ))
315          {
316 	         String oldEndpoint = (String) listModel.getElementAt( index );
317 	         listModel.removeElementAt( index );
318 	         iface.removeEndpoint( oldEndpoint );
319          }
320       }
321    }
322 
323    private class OkAction extends AbstractAction
324    {
325       public OkAction()
326       {
327          super( "OK" );
328          putValue( Action.SHORT_DESCRIPTION, "Closes this dialog" );
329       }
330       
331       public void actionPerformed(ActionEvent e)
332       {
333          dialog.setVisible( false );
334       }
335    }
336 }