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.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.awt.event.KeyEvent;
21  import java.util.ArrayList;
22  import java.util.Arrays;
23  import java.util.List;
24  
25  import javax.swing.AbstractAction;
26  import javax.swing.Action;
27  import javax.swing.BorderFactory;
28  import javax.swing.DefaultListModel;
29  import javax.swing.JButton;
30  import javax.swing.JDialog;
31  import javax.swing.JLabel;
32  import javax.swing.JList;
33  import javax.swing.JOptionPane;
34  import javax.swing.JPanel;
35  import javax.swing.JScrollPane;
36  import javax.swing.KeyStroke;
37  import javax.swing.ListSelectionModel;
38  import javax.swing.ScrollPaneConstants;
39  
40  import org.apache.log4j.Logger;
41  
42  import com.eviware.soapui.SoapUI;
43  import com.eviware.soapui.impl.wsdl.WsdlInterface;
44  import com.eviware.soapui.model.iface.Operation;
45  import com.eviware.soapui.model.iface.Request;
46  import com.eviware.soapui.support.UISupport;
47  import com.jgoodies.forms.builder.ButtonBarBuilder;
48  
49  /***
50   * Edits the service endpoints for a WsdlInterface
51   * 
52   * @author Ole.Matzura
53   */
54  
55  public class InterfaceEndpointsAction extends AbstractAction
56  {
57     private JDialog dialog;
58     private JList list;
59     private WsdlInterface iface;
60     private DefaultListModel listModel;
61     private final static Logger log = Logger.getLogger( InterfaceEndpointsAction.class );
62  
63     public InterfaceEndpointsAction( WsdlInterface iface )
64     {
65        super( "Service Endpoints" );
66        putValue( Action.SHORT_DESCRIPTION, "Manage service endpoints available for this interface" );
67        this.iface = iface;
68     }
69  
70     public KeyStroke getAccelerator()
71  	{
72  		return KeyStroke.getKeyStroke( KeyEvent.VK_E, KeyEvent.CTRL_MASK );
73  	}
74  
75  	private void buildDialog()
76  	{
77  		dialog = new JDialog( SoapUI.getInstance().getFrame() );
78        dialog.setTitle("Interface Service Endpoints" );
79        
80        JPanel contentPanel = new JPanel( new BorderLayout() );
81        listModel = new DefaultListModel();
82        list = new JList( listModel );
83        list.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
84        list.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );
85        JScrollPane scrollPane = new JScrollPane( list );
86        
87        scrollPane.setBorder( BorderFactory.createCompoundBorder(
88        		BorderFactory.createEmptyBorder( 0, 3, 0, 3 ), BorderFactory.createLineBorder( Color.GRAY )));
89        
90        scrollPane.setVerticalScrollBarPolicy( ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS );
91        
92        contentPanel.add( scrollPane, BorderLayout.CENTER );
93        contentPanel.add( createButtons(), BorderLayout.SOUTH );
94        JLabel label = new JLabel( "Edit available service endpoints for this interface in list below" );
95        label.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
96        contentPanel.add( label, BorderLayout.NORTH );
97        
98        dialog.setContentPane( contentPanel );
99        dialog.setSize(400, 300);
100       
101       dialog.setModal( true );
102 	}
103 	
104    public void actionPerformed(ActionEvent e)
105 	{
106    	if( dialog == null )
107    		buildDialog();
108    	
109       listModel.clear();
110       
111       String[] endpoints = iface.getEndpoints();
112       for( int c = 0; c < endpoints.length; c++ )
113       {
114          listModel.addElement( endpoints[c] );
115       }
116       
117       UISupport.showDialog( dialog );
118    }
119 
120    private Component createButtons()
121    {
122       ButtonBarBuilder builder = ButtonBarBuilder.createLeftToRightBuilder();
123       builder.addFixed( new JButton( new AddAction() ));
124       builder.addRelatedGap();
125       builder.addFixed( new JButton( new EditAction() ));
126       builder.addRelatedGap();
127       builder.addFixed( new JButton( new DeleteAction() ));
128       builder.addRelatedGap();
129       builder.addFixed( new JButton( new AssignAction() ));
130       builder.addGlue();
131       builder.addFixed( new JButton( new OkAction() ));
132       builder.getPanel().setBorder( BorderFactory.createEmptyBorder(5, 5, 5, 5));
133       return builder.getPanel();
134    }
135    
136    private class AddAction extends AbstractAction
137    {
138       public AddAction()
139       {
140          super( "Add" );
141       }
142       
143       public void actionPerformed(ActionEvent e)
144       {
145          String endpoint = JOptionPane.showInputDialog( dialog, "Enter new endpoint address", list.getSelectedValue() );
146          if( endpoint == null ) return;
147          
148          listModel.addElement( endpoint );
149          iface.addEndpoint( endpoint );
150       }
151    }
152    
153    private class AssignAction extends AbstractAction
154    {
155       private static final String ALL_REQUESTS = "- all requests -";
156 		private static final String ALL_REQUESTS_WITH_NO_ENDPOINT = "- all requests with no endpoint -";
157 
158 		public AssignAction()
159       {
160          super( "Assign" );
161       }
162       
163       public void actionPerformed(ActionEvent e)
164       {
165       	int selectedIndex = list.getSelectedIndex();
166          if( selectedIndex == -1 ) 
167          {
168          	Toolkit.getDefaultToolkit().beep();
169          	return;
170          }
171          
172          String selectedEndpoint = (String) listModel.getElementAt( selectedIndex );
173          
174          List<String> list = new ArrayList<String>( Arrays.asList( iface.getEndpoints()) );
175          list.add( 0, ALL_REQUESTS );
176          list.add( 0, ALL_REQUESTS_WITH_NO_ENDPOINT );
177          
178          Object endpoint = JOptionPane.showInputDialog( SoapUI.getInstance().getFrame(), 
179          		"Assign selected endpoint to..", "Assign Endpoint", 
180          		JOptionPane.OK_CANCEL_OPTION, null, list.toArray(), ALL_REQUESTS_WITH_NO_ENDPOINT );
181          
182          if( endpoint == null )
183          	return;
184          
185          int changeCount = 0;
186          
187          for( int c = 0; c < iface.getOperationCount(); c++ )
188          {
189          	Operation operation = iface.getOperationAt( c );
190          	for( int i = 0; i < operation.getRequestCount(); i++ )
191          	{
192          		Request request = operation.getRequestAt( i );
193          		String ep = request.getEndpoint();
194 
195                if( endpoint.equals( ALL_REQUESTS ) || 
196                	 ( endpoint.equals(  ALL_REQUESTS_WITH_NO_ENDPOINT ) && ep == null ) ||
197                	 ( ep.equals( endpoint )))
198                {
199                	request.setEndpoint( selectedEndpoint.toString() );
200                	changeCount++;
201                }
202          	}
203          }
204          
205          log.info( "Assigned endpoint [" + selectedEndpoint + "] to " + changeCount + " requests" );
206       }
207    }
208    
209    private class EditAction extends AbstractAction
210    {
211       public EditAction()
212       {
213          super( "Edit" );
214       }
215       
216       public void actionPerformed(ActionEvent e)
217       {
218          int selectedIndex = list.getSelectedIndex();
219          if( selectedIndex == -1 ) 
220          {
221          	Toolkit.getDefaultToolkit().beep();
222          	return;
223          }
224          	
225          String oldEndpoint = (String) listModel.getElementAt( selectedIndex );
226          String newEndpoint = JOptionPane.showInputDialog( dialog, "Edit endpoint address", oldEndpoint );
227          if( newEndpoint == null ) return;
228          
229          listModel.setElementAt( newEndpoint, selectedIndex );
230          iface.changeEndpoint( oldEndpoint, newEndpoint );
231       }
232    }
233    
234    private class DeleteAction extends AbstractAction
235    {
236       public DeleteAction()
237       {
238          super( "Delete" );
239       }
240       
241       public void actionPerformed(ActionEvent e )
242       {   
243          int index = list.getSelectedIndex();
244          if( index == -1)
245          {
246          	Toolkit.getDefaultToolkit().beep();
247          	return;
248          }
249          
250          if( UISupport.confirm( "Delete selected endpoint?", "Delete Endpoint" ))
251          {
252 	         String oldEndpoint = (String) listModel.getElementAt( index );
253 	         listModel.removeElementAt( index );
254 	         iface.removeEndpoint( oldEndpoint );
255          }
256       }
257    }
258 
259    private class OkAction extends AbstractAction
260    {
261       public OkAction()
262       {
263          super( "OK" );
264       }
265       
266       public void actionPerformed(ActionEvent e)
267       {
268          dialog.setVisible( false );
269       }
270    }
271 }