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