001: /*
002: * soapUI, copyright (C) 2004-2007 eviware.com
003: *
004: * soapUI is free software; you can redistribute it and/or modify it under the
005: * terms of version 2.1 of the GNU Lesser General Public License as published by
006: * the Free Software Foundation.
007: *
008: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
010: * See the GNU Lesser General Public License for more details at gnu.org.
011: */
012:
013: package com.eviware.soapui.impl.wsdl.panels.support;
014:
015: import java.beans.PropertyChangeEvent;
016: import java.beans.PropertyChangeListener;
017: import java.util.HashSet;
018: import java.util.Iterator;
019: import java.util.Set;
020:
021: import javax.swing.ComboBoxModel;
022: import javax.swing.event.ListDataEvent;
023: import javax.swing.event.ListDataListener;
024:
025: import com.eviware.soapui.impl.wsdl.WsdlInterface;
026: import com.eviware.soapui.impl.wsdl.WsdlRequest;
027: import com.eviware.soapui.support.UISupport;
028:
029: /**
030: * ComboBox model for a request endpoint
031: *
032: * @author Ole.Matzura
033: */
034:
035: public class EndpointsComboBoxModel implements ComboBoxModel,
036: PropertyChangeListener {
037: private static final String ADD_NEW_ENDPOINT = "[add new endpoint..]";
038: private static final String EDIT_ENDPOINT = "[edit current..]";
039: private static final String DELETE_ENDPOINT = "[delete current]";
040:
041: private Set<ListDataListener> listeners = new HashSet<ListDataListener>();
042: private String[] endpoints;
043: private WsdlRequest request;
044:
045: public EndpointsComboBoxModel(WsdlRequest request) {
046: this .request = request;
047: initEndpoints();
048: request.addPropertyChangeListener(this );
049: request.getOperation().getInterface()
050: .addPropertyChangeListener(this );
051: }
052:
053: public void setSelectedItem(Object anItem) {
054: String endpoint = request.getEndpoint();
055: if (anItem != null && anItem.equals(ADD_NEW_ENDPOINT)) {
056: String value = UISupport.prompt(
057: "Add new endpoint for interface ["
058: + request.getOperation().getInterface()
059: .getName() + "]",
060: "Add new endpoint", endpoint);
061:
062: if (value != null) {
063: request.getOperation().getInterface()
064: .addEndpoint(value);
065: request.setEndpoint(value);
066: }
067: } else if (anItem != null && anItem.equals(EDIT_ENDPOINT)) {
068: String value = UISupport.prompt(
069: "Edit endpoint for interface ["
070: + request.getOperation().getInterface()
071: .getName() + "]", "Edit endpoint",
072: endpoint);
073:
074: if (value != null) {
075: request.getOperation().getInterface().changeEndpoint(
076: endpoint, value);
077: request.setEndpoint(value);
078: }
079: } else if (anItem != null && anItem.equals(DELETE_ENDPOINT)) {
080: if (UISupport.confirm("Delete endpoint [" + endpoint + "]",
081: "Delete endpoint")) {
082: request.getOperation().getInterface().removeEndpoint(
083: endpoint);
084: request.setEndpoint(null);
085: }
086: } else {
087: request.setEndpoint((String) anItem);
088: }
089:
090: notifyContentsChanged();
091: }
092:
093: public void refresh() {
094: initEndpoints();
095: notifyContentsChanged();
096: }
097:
098: private void initEndpoints() {
099: endpoints = request.getOperation().getInterface()
100: .getEndpoints();
101: }
102:
103: private void notifyContentsChanged() {
104: Iterator<ListDataListener> iterator = listeners.iterator();
105: ListDataEvent e = new ListDataEvent(this ,
106: ListDataEvent.CONTENTS_CHANGED, 0, getSize());
107: while (iterator.hasNext()) {
108: iterator.next().contentsChanged(e);
109: }
110: }
111:
112: public Object getSelectedItem() {
113: String endpoint = request.getEndpoint();
114: return endpoint == null ? "- no endpoint set -" : endpoint;
115: }
116:
117: public int getSize() {
118: return endpoints.length + 3;
119: }
120:
121: public Object getElementAt(int index) {
122: if (index == endpoints.length)
123: return EndpointsComboBoxModel.EDIT_ENDPOINT;
124: else if (index == endpoints.length + 1)
125: return EndpointsComboBoxModel.ADD_NEW_ENDPOINT;
126: else if (index == endpoints.length + 2)
127: return EndpointsComboBoxModel.DELETE_ENDPOINT;
128: else
129: return endpoints[index];
130: }
131:
132: public void addListDataListener(ListDataListener l) {
133: listeners.add(l);
134: }
135:
136: public void removeListDataListener(ListDataListener l) {
137: listeners.remove(l);
138: }
139:
140: public void propertyChange(PropertyChangeEvent evt) {
141: String propertyName = evt.getPropertyName();
142:
143: if (propertyName.equals(WsdlRequest.ENDPOINT_PROPERTY)) {
144: notifyContentsChanged();
145: } else if (propertyName.equals(WsdlInterface.ENDPOINT_PROPERTY)) {
146: refresh();
147: }
148: }
149:
150: public void release() {
151: request.removePropertyChangeListener(this);
152: request.getOperation().getInterface()
153: .removePropertyChangeListener(this);
154: }
155: }
|