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.actions.iface;
014:
015: import java.awt.BorderLayout;
016: import java.awt.Color;
017: import java.awt.Component;
018: import java.awt.Toolkit;
019: import java.awt.event.ActionEvent;
020: import java.util.ArrayList;
021: import java.util.Arrays;
022: import java.util.List;
023:
024: import javax.swing.AbstractAction;
025: import javax.swing.Action;
026: import javax.swing.BorderFactory;
027: import javax.swing.DefaultListModel;
028: import javax.swing.JButton;
029: import javax.swing.JDialog;
030: import javax.swing.JList;
031: import javax.swing.JPanel;
032: import javax.swing.JScrollPane;
033: import javax.swing.ListSelectionModel;
034: import javax.swing.ScrollPaneConstants;
035: import javax.swing.event.ListSelectionEvent;
036: import javax.swing.event.ListSelectionListener;
037:
038: import org.apache.log4j.Logger;
039:
040: import com.eviware.soapui.impl.wsdl.WsdlInterface;
041: import com.eviware.soapui.impl.wsdl.actions.support.ShowOnlineHelpAction;
042: import com.eviware.soapui.impl.wsdl.support.HelpUrls;
043: import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequest;
044: import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep;
045: import com.eviware.soapui.model.iface.Operation;
046: import com.eviware.soapui.model.iface.Request;
047: import com.eviware.soapui.model.testsuite.TestCase;
048: import com.eviware.soapui.model.testsuite.TestStep;
049: import com.eviware.soapui.model.testsuite.TestSuite;
050: import com.eviware.soapui.support.UISupport;
051: import com.eviware.soapui.support.action.support.AbstractSoapUIAction;
052: import com.jgoodies.forms.builder.ButtonBarBuilder;
053:
054: /**
055: * Manages the service endpoints for a WsdlInterface
056: *
057: * @author Ole.Matzura
058: */
059:
060: public class InterfaceEndpointsAction extends
061: AbstractSoapUIAction<WsdlInterface> {
062: private JDialog dialog;
063: private JList list;
064: private DefaultListModel listModel;
065: private WsdlInterface iface;
066: private final static Logger log = Logger
067: .getLogger(InterfaceEndpointsAction.class);
068: private JButton editButton;
069: private JButton deleteButton;
070: private JButton assignButton;
071:
072: public InterfaceEndpointsAction() {
073: super ("Service Endpoints",
074: "Manage service endpoints available for this interface");
075: }
076:
077: private void buildDialog() {
078: dialog = new JDialog(UISupport.getMainFrame());
079: dialog.setTitle("Interface Service Endpoints");
080:
081: JPanel contentPanel = new JPanel(new BorderLayout());
082: listModel = new DefaultListModel();
083: list = new JList(listModel);
084: list.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
085: list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
086: list.getSelectionModel().addListSelectionListener(
087: new ListSelectionListener() {
088:
089: public void valueChanged(ListSelectionEvent e) {
090: enableButtons();
091: }
092: });
093:
094: JScrollPane scrollPane = new JScrollPane(list);
095:
096: scrollPane.setBorder(BorderFactory.createCompoundBorder(
097: BorderFactory.createEmptyBorder(5, 5, 5, 5),
098: BorderFactory.createLineBorder(Color.GRAY)));
099:
100: scrollPane
101: .setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
102:
103: contentPanel.add(scrollPane, BorderLayout.CENTER);
104: contentPanel.add(createButtons(), BorderLayout.SOUTH);
105: Component descriptionPanel = UISupport
106: .buildDescription(
107: "Service Endpoints",
108: "Edit available service endpoints for this interface in list below",
109: null);
110: contentPanel.add(descriptionPanel, BorderLayout.NORTH);
111:
112: dialog.setContentPane(contentPanel);
113: dialog.setSize(400, 300);
114:
115: dialog.setModal(true);
116: }
117:
118: protected void enableButtons() {
119: editButton.setEnabled(list.getSelectedIndex() != -1);
120: deleteButton.setEnabled(list.getSelectedIndex() != -1);
121: assignButton.setEnabled(list.getSelectedIndex() != -1);
122: }
123:
124: public void perform(WsdlInterface iface, Object param) {
125: if (dialog == null)
126: buildDialog();
127:
128: listModel.clear();
129:
130: String[] endpoints = iface.getEndpoints();
131: for (int c = 0; c < endpoints.length; c++) {
132: listModel.addElement(endpoints[c]);
133: }
134:
135: list.setSelectedIndex(-1);
136:
137: this .iface = iface;
138: enableButtons();
139: UISupport.showDialog(dialog);
140: }
141:
142: private Component createButtons() {
143: ButtonBarBuilder builder = ButtonBarBuilder
144: .createLeftToRightBuilder();
145: builder.addFixed(UISupport
146: .createToolbarButton(new ShowOnlineHelpAction(
147: HelpUrls.ENDPOINTSEDITOR_HELP_URL)));
148: builder.addGlue();
149: builder.addFixed(new JButton(new AddAction()));
150: builder.addRelatedGap();
151: editButton = new JButton(new EditAction());
152: builder.addFixed(editButton);
153: builder.addRelatedGap();
154: deleteButton = new JButton(new DeleteAction());
155: builder.addFixed(deleteButton);
156: builder.addRelatedGap();
157: assignButton = new JButton(new AssignAction());
158: builder.addFixed(assignButton);
159: builder.addRelatedGap();
160: builder.addFixed(new JButton(new OkAction()));
161:
162: builder.setBorder(BorderFactory.createCompoundBorder(
163: BorderFactory.createCompoundBorder(BorderFactory
164: .createMatteBorder(1, 0, 0, 0, Color.GRAY),
165: BorderFactory.createMatteBorder(1, 0, 0, 0,
166: Color.WHITE)), BorderFactory
167: .createEmptyBorder(3, 5, 3, 5)));
168:
169: return builder.getPanel();
170: }
171:
172: private class AddAction extends AbstractAction {
173: public AddAction() {
174: super ("Add");
175: putValue(Action.SHORT_DESCRIPTION,
176: "Adds a new endpoint to the list");
177: }
178:
179: public void actionPerformed(ActionEvent e) {
180: Object selectedValue = list.getSelectedValue();
181: String endpoint = UISupport.prompt(
182: "Enter new endpoint URL", "Add Endpoint",
183: selectedValue == null ? "" : selectedValue
184: .toString());
185:
186: if (endpoint == null)
187: return;
188:
189: listModel.addElement(endpoint);
190: iface.addEndpoint(endpoint);
191: }
192: }
193:
194: private class AssignAction extends AbstractAction {
195: private static final String ALL_REQUESTS = "- All Requests -";
196: private static final String ALL_TEST_REQUESTS = "- All Test Requests -";
197: private static final String ALL_REQUESTS_AND_TEST_REQUESTS = "- All Requests and TestRequests -";
198: private static final String ALL_REQUESTS_WITH_NO_ENDPOINT = "- All Requests with no endpoint -";
199:
200: public AssignAction() {
201: super ("Assign");
202: putValue(Action.SHORT_DESCRIPTION,
203: "Assigns the selected endpoint to Requests/TestRequests for this Interface");
204: }
205:
206: public void actionPerformed(ActionEvent e) {
207: int selectedIndex = list.getSelectedIndex();
208: if (selectedIndex == -1) {
209: Toolkit.getDefaultToolkit().beep();
210: return;
211: }
212:
213: String selectedEndpoint = (String) listModel
214: .getElementAt(selectedIndex);
215:
216: List<String> list = new ArrayList<String>(Arrays
217: .asList(iface.getEndpoints()));
218: list.add(0, ALL_REQUESTS);
219: list.add(1, ALL_TEST_REQUESTS);
220: list.add(2, ALL_REQUESTS_AND_TEST_REQUESTS);
221: list.add(3, ALL_REQUESTS_WITH_NO_ENDPOINT);
222:
223: Object endpoint = UISupport.prompt(
224: "Assign selected endpoint to..", "Assign Endpoint",
225: list.toArray(), ALL_REQUESTS_WITH_NO_ENDPOINT);
226:
227: if (endpoint == null)
228: return;
229:
230: int changeCount = 0;
231:
232: if (endpoint.equals(ALL_REQUESTS)
233: || endpoint.equals(ALL_REQUESTS_WITH_NO_ENDPOINT)
234: || endpoint.equals(ALL_REQUESTS_AND_TEST_REQUESTS)) {
235: for (int c = 0; c < iface.getOperationCount(); c++) {
236: Operation operation = iface.getOperationAt(c);
237: for (int i = 0; i < operation.getRequestCount(); i++) {
238: Request request = operation.getRequestAt(i);
239: String ep = request.getEndpoint();
240:
241: if (endpoint.equals(ALL_REQUESTS)
242: || endpoint
243: .equals(ALL_REQUESTS_AND_TEST_REQUESTS)
244: || (endpoint
245: .equals(ALL_REQUESTS_WITH_NO_ENDPOINT) && ep == null)
246: || (ep.equals(endpoint))) {
247: request.setEndpoint(selectedEndpoint);
248: changeCount++;
249: }
250: }
251: }
252: }
253:
254: if (endpoint.equals(ALL_REQUESTS_AND_TEST_REQUESTS)
255: || endpoint.equals(ALL_TEST_REQUESTS)) {
256: for (TestSuite testSuite : iface.getProject()
257: .getTestSuites()) {
258: for (TestCase testCase : testSuite
259: .getTestCaseList()) {
260: for (TestStep testStep : testCase
261: .getTestStepList()) {
262: if (testStep instanceof WsdlTestRequestStep) {
263: WsdlTestRequest testRequest = ((WsdlTestRequestStep) testStep)
264: .getTestRequest();
265: testRequest
266: .setEndpoint(selectedEndpoint);
267: changeCount++;
268: }
269: }
270: }
271: }
272: }
273:
274: log.info("Assigned endpoint [" + selectedEndpoint + "] to "
275: + changeCount + " Requests");
276: }
277: }
278:
279: private class EditAction extends AbstractAction {
280: public EditAction() {
281: super ("Edit");
282: putValue(Action.SHORT_DESCRIPTION,
283: "Edit the selected endpoint");
284: }
285:
286: public void actionPerformed(ActionEvent e) {
287: int selectedIndex = list.getSelectedIndex();
288: if (selectedIndex == -1) {
289: Toolkit.getDefaultToolkit().beep();
290: return;
291: }
292:
293: String oldEndpoint = (String) listModel
294: .getElementAt(selectedIndex);
295: String newEndpoint = UISupport.prompt(
296: "Edit endpoint address", "Edit Endpoint",
297: oldEndpoint);
298: if (newEndpoint == null)
299: return;
300:
301: listModel.setElementAt(newEndpoint, selectedIndex);
302: iface.changeEndpoint(oldEndpoint, newEndpoint);
303: }
304: }
305:
306: private class DeleteAction extends AbstractAction {
307: public DeleteAction() {
308: super ("Delete");
309: putValue(Action.SHORT_DESCRIPTION,
310: "Deletes the selected endpoint from the list");
311: }
312:
313: public void actionPerformed(ActionEvent e) {
314: int index = list.getSelectedIndex();
315: if (index == -1) {
316: Toolkit.getDefaultToolkit().beep();
317: return;
318: }
319:
320: if (UISupport.confirm("Delete selected endpoint?",
321: "Delete Endpoint")) {
322: String oldEndpoint = (String) listModel
323: .getElementAt(index);
324: listModel.removeElementAt(index);
325: iface.removeEndpoint(oldEndpoint);
326: }
327: }
328: }
329:
330: private class OkAction extends AbstractAction {
331: public OkAction() {
332: super ("OK");
333: putValue(Action.SHORT_DESCRIPTION, "Closes this dialog");
334: }
335:
336: public void actionPerformed(ActionEvent e) {
337: dialog.setVisible(false);
338: }
339: }
340: }
|