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.mockservice;
14  
15  import java.awt.event.ActionEvent;
16  import java.util.ArrayList;
17  import java.util.List;
18  
19  import javax.swing.AbstractAction;
20  import javax.swing.Action;
21  
22  import com.eviware.soapui.impl.wsdl.WsdlOperation;
23  import com.eviware.soapui.impl.wsdl.mock.WsdlMockOperation;
24  import com.eviware.soapui.impl.wsdl.mock.WsdlMockResponse;
25  import com.eviware.soapui.impl.wsdl.mock.WsdlMockService;
26  import com.eviware.soapui.model.iface.Interface;
27  import com.eviware.soapui.support.UISupport;
28  
29  /***
30   * Adds a new WsdlLoadMock to a WsdlMockService
31   * 
32   * @author Ole.Matzura
33   */
34  
35  public class AddNewMockOperationAction extends AbstractAction
36  {
37     private final WsdlMockService mockService;
38  
39  	public AddNewMockOperationAction( WsdlMockService mockService ) 
40     {
41        super( "New MockOperation" );
42  		this.mockService = mockService;
43        putValue( Action.SHORT_DESCRIPTION, "Creates a new MockOperation for this MockService" );
44        putValue( Action.ACCELERATOR_KEY, UISupport.getKeyStroke( "menu N" ));
45     }
46     
47     public void actionPerformed(ActionEvent e)
48  	{
49        List<OperationWrapper> operations = new ArrayList<OperationWrapper>();
50        
51        for( int c = 0; c < mockService.getProject().getInterfaceCount(); c++ )
52        {
53        	Interface iface = mockService.getProject().getInterfaceAt( c );
54        	for( int i = 0; i < iface.getOperationCount(); i++ )
55        	{
56        		if( !mockService.hasMockOperation( iface.getOperationAt( i  )))
57        			operations.add( new OperationWrapper( ( WsdlOperation ) iface.getOperationAt( i )));
58        	}
59        }
60        
61        if( operations.isEmpty() )
62        {
63        	UISupport.showErrorMessage( "No unique operations to mock in project!" );
64        	return;
65        }
66        
67        Object result = UISupport.prompt( "Select Operation to Mock", "New MockOperation", operations.toArray() );
68        if( result != null )
69        {
70        	WsdlMockOperation mockOperation = mockService.addNewMockOperation( ((OperationWrapper)result).getOperation());
71        	WsdlMockResponse mockResponse = mockOperation.addNewMockResponse( "Response 1", true );
72        	UISupport.selectAndShow( mockResponse );
73        }
74     }
75     
76     public class OperationWrapper
77  	{
78  		private final WsdlOperation operation;
79  
80  		public OperationWrapper( WsdlOperation operation )
81  		{
82  			this.operation = operation;
83  		}
84  
85  		public WsdlOperation getOperation()
86  		{
87  			return operation;
88  		}
89  
90  		public String toString()
91  		{
92  			return operation.getInterface().getName() + " - " + operation.getName();
93  		}
94  	}
95  }