View Javadoc

1   /*
2    *  soapUI, copyright (C) 2006 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.panels.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.event.ListDataEvent;
23  import javax.swing.event.ListDataListener;
24  
25  import com.eviware.soapui.impl.wsdl.WsdlInterface;
26  import com.eviware.soapui.impl.wsdl.WsdlRequest;
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 WsdlRequest request;
44  	
45  	public EndpointsComboBoxModel( WsdlRequest request )
46  	{
47  		this.request = request;
48  		initEndpoints();
49  		request.addPropertyChangeListener( this );
50  		request.getOperation().getInterface().addPropertyChangeListener( this );
51  	}
52  	
53  	public void setSelectedItem(Object anItem)
54  	{
55  		String endpoint = request.getEndpoint();
56  		if( anItem != null && anItem.equals( ADD_NEW_ENDPOINT ))
57  		{
58  			String value = UISupport.prompt( 
59  					"Add new endpoint for interface [" + request.getOperation().getInterface().getName() + "]", 
60  					"Add new endpoint",  endpoint );
61  			
62  			if( value != null )
63  			{
64  				request.getOperation().getInterface().addEndpoint( value );
65  				request.setEndpoint( value );
66  			}
67  		}
68  		else if( anItem != null && anItem.equals( EDIT_ENDPOINT ))
69  		{
70  			String value = UISupport.prompt(  
71  					"Edit endpoint for interface [" + request.getOperation().getInterface().getName() + "]", 
72  					"Edit endpoint", endpoint );
73  			
74  			if( value != null )
75  			{
76  				request.getOperation().getInterface().changeEndpoint( endpoint, value );
77  				request.setEndpoint( value );
78  			}
79  		}
80  		else if( anItem != null && anItem.equals( DELETE_ENDPOINT ))
81  		{
82  			if( UISupport.confirm( "Delete endpoint [" + endpoint + "]", "Delete endpoint"))
83  			{
84  				request.getOperation().getInterface().removeEndpoint( endpoint );
85  				request.setEndpoint( null );
86  			}
87  		}
88  		else
89  		{
90  			request.setEndpoint( (String)anItem );
91  		}
92  		
93  		notifyContentsChanged();
94  	}
95  
96  	public void refresh()
97  	{
98  		initEndpoints();
99  		notifyContentsChanged();
100 	}
101 
102 	private void initEndpoints()
103 	{
104 		endpoints = request.getOperation().getInterface().getEndpoints();
105 	}
106 
107 	private void notifyContentsChanged()
108 	{
109 		Iterator<ListDataListener> iterator = listeners.iterator();
110 		ListDataEvent e = new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED, 0, getSize() );
111 		while( iterator.hasNext() )
112 		{
113 			iterator.next().contentsChanged( e );
114 		}
115 	}
116 
117 	public Object getSelectedItem()
118 	{
119 		String endpoint = request.getEndpoint();
120 		return endpoint == null ? "- no endpoint set -" : endpoint;
121 	}
122 
123 	public int getSize()
124 	{
125 		return endpoints.length + 3;
126 	}
127 
128 	public Object getElementAt(int index)
129 	{
130 		if( index == endpoints.length )
131 			return EndpointsComboBoxModel.EDIT_ENDPOINT;
132 		else if( index == endpoints.length+1 ) 
133 			return EndpointsComboBoxModel.ADD_NEW_ENDPOINT;
134 		else if( index == endpoints.length+2 ) 
135 			return EndpointsComboBoxModel.DELETE_ENDPOINT;
136 		else
137 			return endpoints[index];
138 	}
139 
140 	public void addListDataListener(ListDataListener l)
141 	{
142 		listeners.add( l );
143 	}
144 
145 	public void removeListDataListener(ListDataListener l)
146 	{
147 		listeners.remove( l );
148 	}
149 
150 	public void propertyChange(PropertyChangeEvent evt)
151 	{
152 		String propertyName = evt.getPropertyName();
153 		
154 		if( propertyName.equals( WsdlRequest.ENDPOINT_PROPERTY ) )
155       {
156          notifyContentsChanged();
157       }
158 		else if( propertyName.equals( WsdlInterface.ENDPOINT_PROPERTY ) )
159       {
160          refresh();
161       }		
162 	}
163 	
164 	public void release()
165 	{
166 		request.removePropertyChangeListener( this );
167 		request.getOperation().getInterface().removePropertyChangeListener( this );
168 	}
169 }