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