View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 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.support;
14  
15  import java.beans.PropertyChangeEvent;
16  import java.beans.PropertyChangeListener;
17  import java.util.HashSet;
18  import java.util.Iterator;
19  import java.util.Set;
20  
21  import javax.swing.ComboBoxModel;
22  import javax.swing.SwingUtilities;
23  import javax.swing.event.ListDataEvent;
24  import javax.swing.event.ListDataListener;
25  
26  import com.eviware.soapui.impl.wsdl.WsdlInterface;
27  import com.eviware.soapui.support.UISupport;
28  
29  /***
30   * ComboBox model for a request endpoint
31   * 
32   * @author Ole.Matzura
33   */
34  
35  public class EndpointsComboBoxModel implements ComboBoxModel, PropertyChangeListener
36  {
37  	private static final String ADD_NEW_ENDPOINT = "[add new endpoint..]";
38  	private static final String EDIT_ENDPOINT = "[edit current..]";
39  	private static final String DELETE_ENDPOINT = "[delete current]";
40  
41  	private Set<ListDataListener> listeners = new HashSet<ListDataListener>();
42  	private String[] endpoints;
43  	private AbstractHttpRequestInterface<?> request;
44  
45  	public EndpointsComboBoxModel( AbstractHttpRequestInterface<?> request )
46  	{
47  		this.request = request;
48  		initEndpoints();
49  		request.addPropertyChangeListener( this );
50  		if( request.getOperation() != null )
51  			request.getOperation().getInterface().addPropertyChangeListener( this );
52  	}
53  
54  	public void setSelectedItem( Object anItem )
55  	{
56  		final String endpoint = request.getEndpoint();
57  		if( anItem != null && anItem.equals( ADD_NEW_ENDPOINT ) )
58  		{
59  			SwingUtilities.invokeLater( new Runnable()
60  			{
61  				public void run()
62  				{
63  					String value = UISupport.prompt( "Add new endpoint for interface ["
64  							+ request.getOperation().getInterface().getName() + "]", "Add new endpoint", endpoint );
65  
66  					if( value != null )
67  					{
68  						if( request.getOperation() != null )
69  							request.getOperation().getInterface().addEndpoint( value );
70  						request.setEndpoint( value );
71  					}
72  
73  				}
74  			} );
75  
76  		}
77  		else if( anItem != null && anItem.equals( EDIT_ENDPOINT ) )
78  		{
79  			SwingUtilities.invokeLater( new Runnable()
80  			{
81  				public void run()
82  				{
83  					String value = UISupport.prompt( "Edit endpoint for interface ["
84  							+ request.getOperation().getInterface().getName() + "]", "Edit endpoint", endpoint );
85  
86  					if( value != null )
87  					{
88  						if( request.getOperation() != null )
89  							request.getOperation().getInterface().changeEndpoint( endpoint, value );
90  						request.setEndpoint( value );
91  					}
92  				}
93  			} );
94  		}
95  		else if( anItem != null && anItem.equals( DELETE_ENDPOINT ) )
96  		{
97  			SwingUtilities.invokeLater( new Runnable()
98  			{
99  				public void run()
100 				{
101 					if( UISupport.confirm( "Delete endpoint [" + endpoint + "]", "Delete endpoint" ) )
102 					{
103 						if( request.getOperation() != null )
104 							request.getOperation().getInterface().removeEndpoint( endpoint );
105 						request.setEndpoint( null );
106 					}
107 				}
108 			} );
109 		}
110 		else
111 		{
112 			request.setEndpoint( ( String )anItem );
113 		}
114 
115 		notifyContentsChanged();
116 	}
117 
118 	public void refresh()
119 	{
120 		initEndpoints();
121 		notifyContentsChanged();
122 	}
123 
124 	private void initEndpoints()
125 	{
126 		if( request.getOperation() != null )
127 			endpoints = request.getOperation().getInterface().getEndpoints();
128 		else
129 			endpoints = new String[0];
130 	}
131 
132 	private void notifyContentsChanged()
133 	{
134 		Iterator<ListDataListener> iterator = listeners.iterator();
135 		ListDataEvent e = new ListDataEvent( this, ListDataEvent.CONTENTS_CHANGED, 0, getSize() );
136 		while( iterator.hasNext() )
137 		{
138 			iterator.next().contentsChanged( e );
139 		}
140 	}
141 
142 	public Object getSelectedItem()
143 	{
144 		String endpoint = request.getEndpoint();
145 		return endpoint == null ? "- no endpoint set -" : endpoint;
146 	}
147 
148 	public int getSize()
149 	{
150 		return endpoints.length + 3;
151 	}
152 
153 	public Object getElementAt( int index )
154 	{
155 		if( index == endpoints.length )
156 			return EndpointsComboBoxModel.EDIT_ENDPOINT;
157 		else if( index == endpoints.length + 1 )
158 			return EndpointsComboBoxModel.ADD_NEW_ENDPOINT;
159 		else if( index == endpoints.length + 2 )
160 			return EndpointsComboBoxModel.DELETE_ENDPOINT;
161 		else
162 			return endpoints[index];
163 	}
164 
165 	public void addListDataListener( ListDataListener l )
166 	{
167 		listeners.add( l );
168 	}
169 
170 	public void removeListDataListener( ListDataListener l )
171 	{
172 		listeners.remove( l );
173 	}
174 
175 	public void propertyChange( PropertyChangeEvent evt )
176 	{
177 		String propertyName = evt.getPropertyName();
178 
179 		if( propertyName.equals( AbstractHttpRequest.ENDPOINT_PROPERTY ) )
180 		{
181 			notifyContentsChanged();
182 		}
183 		else if( propertyName.equals( WsdlInterface.ENDPOINT_PROPERTY ) )
184 		{
185 			refresh();
186 		}
187 	}
188 
189 	public void release()
190 	{
191 		request.removePropertyChangeListener( this );
192 		if( request.getOperation() != null )
193 			request.getOperation().getInterface().removePropertyChangeListener( this );
194 	}
195 }