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.mockoperation;
014:
015: import java.awt.BorderLayout;
016: import java.awt.Component;
017: import java.awt.Dimension;
018: import java.awt.event.ActionEvent;
019: import java.awt.event.FocusAdapter;
020: import java.awt.event.FocusEvent;
021: import java.awt.event.FocusListener;
022: import java.beans.PropertyChangeEvent;
023: import java.beans.PropertyChangeListener;
024:
025: import javax.swing.AbstractAction;
026: import javax.swing.Action;
027: import javax.swing.BorderFactory;
028: import javax.swing.Box;
029: import javax.swing.JButton;
030: import javax.swing.JComponent;
031: import javax.swing.JLabel;
032: import javax.swing.JPanel;
033: import javax.swing.JPopupMenu;
034: import javax.swing.JScrollPane;
035: import javax.swing.JSeparator;
036: import javax.swing.JSplitPane;
037: import javax.swing.JTabbedPane;
038: import javax.swing.JTable;
039: import javax.swing.JToggleButton;
040: import javax.swing.JToolBar;
041: import javax.swing.event.TableModelEvent;
042: import javax.swing.event.TableModelListener;
043:
044: import com.eviware.soapui.SoapUI;
045: import com.eviware.soapui.impl.wsdl.actions.mockresponse.OpenRequestForMockResponseAction;
046: import com.eviware.soapui.impl.wsdl.actions.support.ShowOnlineHelpAction;
047: import com.eviware.soapui.impl.wsdl.mock.WsdlMockRequest;
048: import com.eviware.soapui.impl.wsdl.mock.WsdlMockResponse;
049: import com.eviware.soapui.impl.wsdl.mock.WsdlMockResult;
050: import com.eviware.soapui.impl.wsdl.panels.attachments.MockAttachmentPanel;
051: import com.eviware.soapui.impl.wsdl.panels.mockoperation.actions.CreateEmptyMockResponseAction;
052: import com.eviware.soapui.impl.wsdl.panels.mockoperation.actions.CreateFaultMockResponseAction;
053: import com.eviware.soapui.impl.wsdl.panels.mockoperation.actions.RecreateMockResponseAction;
054: import com.eviware.soapui.impl.wsdl.panels.mockoperation.actions.WSIValidateResponseAction;
055: import com.eviware.soapui.impl.wsdl.panels.request.StringToStringMapTableModel;
056: import com.eviware.soapui.impl.wsdl.panels.request.components.RequestMessageXmlEditor;
057: import com.eviware.soapui.impl.wsdl.panels.request.components.ResponseMessageXmlEditor;
058: import com.eviware.soapui.impl.wsdl.panels.request.components.SoapMessageXmlEditor;
059: import com.eviware.soapui.impl.wsdl.panels.request.components.editor.XmlDocument;
060: import com.eviware.soapui.impl.wsdl.panels.request.components.editor.views.source.XmlSourceEditorView;
061: import com.eviware.soapui.impl.wsdl.panels.teststeps.support.GroovyEditor;
062: import com.eviware.soapui.impl.wsdl.panels.teststeps.support.GroovyEditorModel;
063: import com.eviware.soapui.impl.wsdl.support.HelpUrls;
064: import com.eviware.soapui.model.ModelItem;
065: import com.eviware.soapui.model.mock.MockRunner;
066: import com.eviware.soapui.model.settings.Settings;
067: import com.eviware.soapui.settings.UISettings;
068: import com.eviware.soapui.support.UISupport;
069: import com.eviware.soapui.support.action.swing.SwingActionDelegate;
070: import com.eviware.soapui.support.actions.ChangeSplitPaneOrientationAction;
071: import com.eviware.soapui.support.components.JEditorStatusBarWithProgress;
072: import com.eviware.soapui.support.components.JXToolBar;
073: import com.eviware.soapui.support.swing.SoapUISplitPaneUI;
074: import com.eviware.soapui.support.types.StringToStringMap;
075: import com.eviware.soapui.support.xml.JXEditTextArea;
076: import com.eviware.soapui.ui.support.ModelItemDesktopPanel;
077:
078: /**
079: * Abstract base DesktopPanel for WsdlMockResponses
080: *
081: * @author Ole.Matzura
082: */
083:
084: public class AbstractWsdlMockResponseDesktopPanel<T extends ModelItem, T2 extends WsdlMockResponse>
085: extends ModelItemDesktopPanel<T> {
086: //private final static Log log = Logger.getLogger(WsdlMockOperationDesktopPanel.class);
087: private JEditorStatusBarWithProgress statusBar;
088: private JButton splitButton;
089: private MockRunner mockRunner;
090: private JButton recreateButton;
091: private JButton createEmptyButton;
092: private JSplitPane requestSplitPane;
093: private MoveFocusAction moveFocusAction;
094: private ClosePanelAction closePanelAction = new ClosePanelAction();
095: private RunScriptAction runScriptAction = new RunScriptAction();
096:
097: private SoapMessageXmlEditor requestEditor;
098: private SoapMessageXmlEditor responseEditor;
099:
100: public AbstractAction wsiValidateAction;
101:
102: private JTabbedPane requestTabs;
103: private JPanel requestTabPanel;
104: private JToggleButton tabsButton;
105:
106: public boolean responseHasFocus;
107:
108: private JTabbedPane requestEditorTabs;
109: private JTabbedPane responseEditorTabs;
110:
111: private JPanel requestEditorTabPanel;
112: private JPanel responseEditorTabPanel;
113: private InternalPropertyChangeListener propertyChangeListener = new InternalPropertyChangeListener();
114: private StringToStringMapTableModel responseHeadersModel;
115: private JTable responseHeadersTable;
116: private StringToStringMapTableModel requestHeadersModel;
117: private GroovyEditor responseScriptEditor;
118: private JButton createFaultButton;
119: private T2 mockResponse;
120: private JButton openRequestButton;
121: private MockAttachmentPanel requestAttachmentPanel;
122: private MockAttachmentPanel responseAttachmentPanel;;
123:
124: public AbstractWsdlMockResponseDesktopPanel(T modelItem) {
125: super (modelItem);
126: }
127:
128: protected void init(T2 mockResponse) {
129: this .mockResponse = mockResponse;
130:
131: add(buildContent(), BorderLayout.CENTER);
132: add(buildToolbar(), BorderLayout.NORTH);
133: add(buildStatusLabel(), BorderLayout.SOUTH);
134:
135: setPreferredSize(new Dimension(600, 500));
136:
137: mockResponse.addPropertyChangeListener(propertyChangeListener);
138:
139: addFocusListener(new FocusAdapter() {
140:
141: @Override
142: public void focusGained(FocusEvent e) {
143: if (requestTabs.getSelectedIndex() == 1
144: || responseHasFocus)
145: responseEditor.requestFocus();
146: else
147: requestEditor.requestFocus();
148: }
149: });
150: }
151:
152: protected WsdlMockResponse getMockResponse() {
153: return mockResponse;
154: }
155:
156: public final SoapMessageXmlEditor getRequestEditor() {
157: return requestEditor;
158: }
159:
160: public final SoapMessageXmlEditor getResponseEditor() {
161: return responseEditor;
162: }
163:
164: public MockRunner getSubmit() {
165: return mockRunner;
166: }
167:
168: protected JComponent buildStatusLabel() {
169: statusBar = new JEditorStatusBarWithProgress();
170: statusBar
171: .setBorder(BorderFactory.createEmptyBorder(1, 0, 0, 0));
172:
173: return statusBar;
174: }
175:
176: public JEditorStatusBarWithProgress getStatusBar() {
177: return statusBar;
178: }
179:
180: @SuppressWarnings("unchecked")
181: protected JComponent buildContent() {
182: requestSplitPane = UISupport.createHorizontalSplit();
183: requestSplitPane.setResizeWeight(0.5);
184: requestSplitPane.setBorder(null);
185:
186: splitButton = createActionButton(
187: new ChangeSplitPaneOrientationAction(requestSplitPane),
188: true);
189:
190: tabsButton = new JToggleButton(new ChangeToTabsAction());
191: tabsButton.setPreferredSize(UISupport.TOOLBAR_BUTTON_DIMENSION);
192:
193: openRequestButton = createActionButton(
194: SwingActionDelegate
195: .createDelegate(
196: OpenRequestForMockResponseAction.SOAPUI_ACTION_ID,
197: mockResponse, null, "/open_request.gif"),
198: true);
199: recreateButton = createActionButton(
200: new RecreateMockResponseAction(mockResponse), true);
201: createEmptyButton = createActionButton(
202: new CreateEmptyMockResponseAction(mockResponse), true);
203: createFaultButton = createActionButton(
204: new CreateFaultMockResponseAction(mockResponse), true);
205:
206: moveFocusAction = new MoveFocusAction();
207: wsiValidateAction = //new WSIValidateResponseAction( mockResponse );
208: SwingActionDelegate.createDelegate(
209: new WSIValidateResponseAction(), mockResponse, "alt W");
210:
211: requestEditor = buildRequestEditor();
212: responseEditor = buildResponseEditor();
213: JComponent scriptEditor = buildResponseScriptEditor();
214:
215: WsdlMockResult mockResult = mockResponse.getMockResult();
216: int requestHeadersSize = mockResult == null ? -1 : mockResult
217: .getMockRequest().getRequestHeaders().size();
218: int requestAttachmentsSize = mockResult == null ? -1
219: : mockResult.getMockRequest().getRequestAttachments().length;
220:
221: requestEditorTabs = new JTabbedPane(JTabbedPane.BOTTOM);
222: requestEditorTabs
223: .setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
224: requestEditorTabs.addTab("SOAP Request", requestEditor);
225: requestAttachmentPanel = new MockAttachmentPanel(mockResponse,
226: false);
227: requestEditorTabs.addTab("Request Attachments ("
228: + (requestAttachmentsSize == -1 ? "-"
229: : requestAttachmentsSize) + ")",
230: requestAttachmentPanel);
231: requestEditorTabs.addTab("HTTP Headers ("
232: + (requestHeadersSize == -1 ? "-" : requestHeadersSize)
233: + ")", createRequestHttpHeadersPanel());
234:
235: responseEditorTabs = new JTabbedPane(JTabbedPane.BOTTOM);
236: responseEditorTabs
237: .setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
238: responseEditorTabs.addTab("Mock Response", responseEditor);
239: responseEditorTabs.addTab("Response Script", scriptEditor);
240: responseAttachmentPanel = new MockAttachmentPanel(mockResponse,
241: true);
242: responseEditorTabs.addTab("Response Attachments ("
243: + mockResponse.getAttachments().length + ")",
244: responseAttachmentPanel);
245: responseEditorTabs.addTab("Response HTTP Headers",
246: createResponseHttpHeadersPanel());
247: responseEditorTabs.setTitleAt(3, "Response HTTP Headers ("
248: + responseHeadersModel.getRowCount() + ")");
249:
250: requestTabs = new JTabbedPane();
251: requestTabPanel = UISupport.createTabPanel(requestTabs, true);
252:
253: requestEditorTabPanel = UISupport.createTabPanel(
254: requestEditorTabs, false);
255: responseEditorTabPanel = UISupport.createTabPanel(
256: responseEditorTabs, false);
257:
258: JComponent component = null;
259:
260: if (mockResponse.getSettings().getBoolean(
261: UISettings.START_WITH_REQUEST_TABS)) {
262: requestTabs.addTab("Last Request", requestEditorTabPanel);
263: requestTabs.addTab("Mock Response", responseEditorTabPanel);
264: splitButton.setEnabled(false);
265: tabsButton.setSelected(true);
266: component = requestTabPanel;
267:
268: requestTabs.setSelectedIndex(1);
269: } else {
270: requestSplitPane.setTopComponent(requestEditorTabPanel);
271: requestSplitPane.setBottomComponent(responseEditorTabPanel);
272: requestSplitPane.setDividerLocation(0.5);
273: component = requestSplitPane;
274: }
275:
276: return component;
277: }
278:
279: private Component createRequestHttpHeadersPanel() {
280: requestHeadersModel = new StringToStringMapTableModel(
281: new StringToStringMap(), "Header", "Value", false);
282: requestHeadersModel
283: .addTableModelListener(new TableModelListener() {
284: public void tableChanged(TableModelEvent arg0) {
285: requestEditorTabs.setTitleAt(2,
286: "HTTP Headers ("
287: + requestHeadersModel
288: .getRowCount() + ")");
289: }
290: });
291:
292: JTable requestHeadersTable = new JTable(requestHeadersModel);
293: return new JScrollPane(requestHeadersTable);
294: }
295:
296: private Component createResponseHttpHeadersPanel() {
297: responseHeadersModel = new StringToStringMapTableModel(
298: mockResponse.getResponseHeaders(), "Header", "Value",
299: true);
300: responseHeadersModel
301: .addTableModelListener(new TableModelListener() {
302: public void tableChanged(TableModelEvent arg0) {
303: mockResponse
304: .setResponseHeaders(responseHeadersModel
305: .getData());
306: responseEditorTabs.setTitleAt(3,
307: "Response HTTP Headers ("
308: + responseHeadersModel
309: .getRowCount() + ")");
310: }
311: });
312:
313: responseHeadersTable = new JTable(responseHeadersModel);
314: JPanel panel = new JPanel(new BorderLayout());
315:
316: panel.add(new JScrollPane(responseHeadersTable),
317: BorderLayout.CENTER);
318:
319: JXToolBar builder = UISupport.createToolbar();
320: builder.addFixed(new JButton(new AbstractAction("Add") {
321:
322: public void actionPerformed(ActionEvent arg0) {
323: Object header = UISupport.prompt(
324: "Specify name of header to add",
325: "Add HTTP Header", "");
326: if (header != null) {
327: responseHeadersModel.add(header.toString(), "");
328: }
329: }
330: }));
331: builder.addRelatedGap();
332: builder.addFixed(new JButton(new AbstractAction("Remove") {
333:
334: public void actionPerformed(ActionEvent arg0) {
335: int row = responseHeadersTable.getSelectedRow();
336: if (row != -1
337: && UISupport.confirm("Delete selected header?",
338: "Remove Header")) {
339: responseHeadersModel.remove(row);
340: }
341: }
342: }));
343:
344: builder.addGlue();
345: builder.addFixed(UISupport
346: .createToolbarButton(new ShowOnlineHelpAction(
347: HelpUrls.HEADERS_HELP_URL)));
348:
349: panel.add(builder, BorderLayout.SOUTH);
350:
351: return panel;
352: }
353:
354: protected JComponent buildResponseScriptEditor() {
355: responseScriptEditor = new GroovyEditor(
356: new MockResponseGroovyEditorModel());
357:
358: JPanel panel = new JPanel(new BorderLayout());
359: panel.add(buildScriptToolbar(), BorderLayout.NORTH);
360: panel.add(new JScrollPane(responseScriptEditor),
361: BorderLayout.CENTER);
362: return panel;
363: }
364:
365: private JComponent buildScriptToolbar() {
366: JXToolBar toolBar = UISupport.createToolbar();
367: JButton runButton = UISupport
368: .createToolbarButton(runScriptAction);
369: toolBar.add(runButton);
370: toolBar.add(Box.createHorizontalGlue());
371: JLabel label = new JLabel(
372: "<html>Script is invoked with <code>log</code>, <code>context</code>, "
373: + "<code>requestContext</code>, <code>mockRequest</code> and <code>mockResponse</code> variables</html>");
374: label.setToolTipText(label.getText());
375: label.setMaximumSize(label.getPreferredSize());
376:
377: toolBar.add(label);
378: toolBar.addUnrelatedGap();
379: toolBar.addFixed(createActionButton(new ShowOnlineHelpAction(
380: HelpUrls.MOCKRESPONSE_SCRIPT_HELP_URL), true));
381:
382: return toolBar;
383: }
384:
385: protected SoapMessageXmlEditor buildResponseEditor() {
386: return new ResponseMessageEditor(new MockResponseXmlDocument(
387: mockResponse));
388: }
389:
390: protected SoapMessageXmlEditor buildRequestEditor() {
391: return new RequestMessageEditor(new MockRequestXmlDocument(
392: mockResponse));
393: }
394:
395: protected JComponent buildToolbar() {
396: JXToolBar toolbar = UISupport.createToolbar();
397:
398: toolbar.add(openRequestButton);
399: toolbar.addUnrelatedGap();
400: toolbar.add(recreateButton);
401: toolbar.add(createEmptyButton);
402: toolbar.add(createFaultButton);
403:
404: createToolbar(toolbar);
405:
406: toolbar.add(Box.createHorizontalGlue());
407: toolbar.add(tabsButton);
408: toolbar.add(splitButton);
409: toolbar.add(UISupport
410: .createToolbarButton(new ShowOnlineHelpAction(
411: getHelpUrl())));
412:
413: return toolbar;
414: }
415:
416: protected void createToolbar(JXToolBar toolbar) {
417: }
418:
419: protected String getHelpUrl() {
420: return HelpUrls.REQUESTEDITOR_HELP_URL;
421: }
422:
423: protected void insertButtons(JToolBar toolbar) {
424: }
425:
426: public void setEnabled(boolean enabled) {
427: requestEditor.getSourceEditor().setEditable(enabled);
428: responseEditor.getSourceEditor().setEditable(enabled);
429: recreateButton.setEnabled(enabled);
430: createEmptyButton.setEnabled(enabled);
431:
432: statusBar.setIndeterminate(!enabled);
433: }
434:
435: private final class InternalPropertyChangeListener implements
436: PropertyChangeListener {
437: public void propertyChange(PropertyChangeEvent evt) {
438: if (evt.getPropertyName().equals(
439: WsdlMockResponse.ATTACHMENTS_PROPERTY)) {
440: responseEditorTabs.setTitleAt(1,
441: "Response Attachments ("
442: + mockResponse.getAttachmentCount()
443: + ")");
444: } else if (evt.getPropertyName().equals(
445: WsdlMockResponse.HEADERS_PROPERTY)) {
446: responseHeadersModel.setData((StringToStringMap) evt
447: .getNewValue());
448: } else if (evt.getPropertyName().equals(
449: WsdlMockResponse.MOCKRESULT_PROPERTY)) {
450: WsdlMockRequest mockRequest = mockResponse
451: .getMockResult().getMockRequest();
452: requestEditor.getXmlDocument().setXml(
453: mockRequest == null ? "" : mockRequest
454: .getRequestContent());
455:
456: // update headers
457: requestHeadersModel.setData(mockRequest
458: .getRequestHeaders());
459: requestEditorTabs.setTitleAt(2, "HTTP Headers ("
460: + (mockRequest == null ? "-" : mockRequest
461: .getRequestHeaders().size()
462: + ")"));
463:
464: // update attachments
465: requestEditorTabs.setTitleAt(1, "Request Attachments ("
466: + (mockRequest == null ? 0 : mockRequest
467: .getRequestAttachments().length) + ")");
468:
469: wsiValidateAction.setEnabled(true);
470: }
471: }
472: }
473:
474: public class RequestMessageEditor extends
475: RequestMessageXmlEditor<WsdlMockResponse> {
476: public RequestMessageEditor(XmlDocument document) {
477: super (document, mockResponse);
478: }
479:
480: protected XmlSourceEditorView buildSourceEditor() {
481: XmlSourceEditorView editor = getSourceEditor();
482: JXEditTextArea inputArea = editor.getInputArea();
483:
484: inputArea.addFocusListener(new InputAreaFocusListener());
485:
486: inputArea.getInputHandler().addKeyBinding("AC+TAB",
487: moveFocusAction);
488: inputArea.getInputHandler().addKeyBinding("F5",
489: recreateButton.getAction());
490: inputArea.getInputHandler().addKeyBinding("C+F4",
491: closePanelAction);
492:
493: return editor;
494: }
495: }
496:
497: public class ResponseMessageEditor extends
498: ResponseMessageXmlEditor<WsdlMockResponse> {
499: public ResponseMessageEditor(XmlDocument document) {
500: super (document, mockResponse);
501: }
502:
503: protected XmlSourceEditorView buildSourceEditor() {
504: XmlSourceEditorView editor = getSourceEditor();
505:
506: JXEditTextArea inputArea = editor.getInputArea();
507: inputArea.addFocusListener(new ResultAreaFocusListener());
508:
509: inputArea.getInputHandler().addKeyBinding("AC+TAB",
510: moveFocusAction);
511: inputArea.getInputHandler().addKeyBinding("C+F4",
512: closePanelAction);
513:
514: JPopupMenu inputPopup = editor.getEditorPopup();
515: inputPopup.insert(new JSeparator(), 2);
516: inputPopup.insert(wsiValidateAction, 3);
517:
518: return editor;
519: }
520: }
521:
522: protected final class InputAreaFocusListener implements
523: FocusListener {
524: public void focusGained(FocusEvent e) {
525: responseHasFocus = false;
526:
527: statusBar.setTarget(requestEditor.getSourceEditor()
528: .getInputArea());
529: if (!splitButton.isEnabled()) {
530: requestTabs.setSelectedIndex(0);
531: return;
532: }
533:
534: if (getModelItem().getSettings().getBoolean(
535: UISettings.NO_RESIZE_REQUEST_EDITOR))
536: return;
537:
538: // dont resize if split has been dragged
539: if (((SoapUISplitPaneUI) requestSplitPane.getUI())
540: .hasBeenDragged())
541: return;
542:
543: int pos = requestSplitPane.getDividerLocation();
544: if (pos >= 600)
545: return;
546: if (requestSplitPane.getMaximumDividerLocation() > 700)
547: requestSplitPane.setDividerLocation(600);
548: else
549: requestSplitPane.setDividerLocation(0.8);
550: }
551:
552: public void focusLost(FocusEvent e) {
553: }
554: }
555:
556: protected final class ResultAreaFocusListener implements
557: FocusListener {
558: public void focusGained(FocusEvent e) {
559: responseHasFocus = true;
560:
561: statusBar.setTarget(responseEditor.getSourceEditor()
562: .getInputArea());
563: if (!splitButton.isEnabled()) {
564: requestTabs.setSelectedIndex(1);
565: return;
566: }
567:
568: if (getModelItem().getSettings().getBoolean(
569: UISettings.NO_RESIZE_REQUEST_EDITOR))
570: return;
571:
572: // dont resize if split has been dragged or result is empty
573: if (((SoapUISplitPaneUI) requestSplitPane.getUI())
574: .hasBeenDragged())
575: return;
576:
577: int pos = requestSplitPane.getDividerLocation();
578: int maximumDividerLocation = requestSplitPane
579: .getMaximumDividerLocation();
580: if (pos + 600 < maximumDividerLocation)
581: return;
582:
583: if (maximumDividerLocation > 700)
584: requestSplitPane
585: .setDividerLocation(maximumDividerLocation - 600);
586: else
587: requestSplitPane.setDividerLocation(0.2);
588: }
589:
590: public void focusLost(FocusEvent e) {
591: }
592: }
593:
594: private class ClosePanelAction extends AbstractAction {
595: public void actionPerformed(ActionEvent e) {
596: SoapUI.getDesktop().closeDesktopPanel(getModelItem());
597: }
598: }
599:
600: private class MoveFocusAction extends AbstractAction {
601: public void actionPerformed(ActionEvent e) {
602: if (requestEditor.hasFocus()) {
603: responseEditor.requestFocus();
604: } else {
605: requestEditor.requestFocus();
606: }
607: }
608: }
609:
610: public boolean dependsOn(ModelItem modelItem) {
611: return modelItem == getModelItem()
612: || modelItem == mockResponse.getMockOperation()
613: || modelItem == mockResponse.getMockOperation()
614: .getMockService()
615: || modelItem == mockResponse.getMockOperation()
616: .getMockService().getProject();
617: }
618:
619: public String getDescription() {
620: return "MockResponse: [" + getModelItem().getName() + "] for "
621: + mockResponse.getMockOperation().getName();
622: }
623:
624: private final class ChangeToTabsAction extends AbstractAction {
625: public ChangeToTabsAction() {
626: putValue(Action.SMALL_ICON, UISupport
627: .createImageIcon("/toggle_tabs.gif"));
628: putValue(Action.SHORT_DESCRIPTION,
629: "Toggles to tab-based layout");
630: }
631:
632: public void actionPerformed(ActionEvent e) {
633: if (splitButton.isEnabled()) {
634: splitButton.setEnabled(false);
635: removeContent(requestSplitPane);
636: setContent(requestTabPanel);
637: requestTabs.addTab("Last Request",
638: requestEditorTabPanel);
639: requestTabs.addTab("Mock Response",
640: responseEditorTabPanel);
641:
642: if (responseHasFocus) {
643: requestTabs.setSelectedIndex(1);
644: requestEditorTabs.requestFocus();
645: } else {
646: responseEditorTabs.requestFocus();
647: }
648: } else {
649: int selectedIndex = requestTabs.getSelectedIndex();
650:
651: splitButton.setEnabled(true);
652: removeContent(requestTabPanel);
653: setContent(requestSplitPane);
654: requestSplitPane.setTopComponent(requestEditorTabPanel);
655: requestSplitPane
656: .setBottomComponent(responseEditorTabPanel);
657: requestSplitPane.setDividerLocation(0.5);
658:
659: if (selectedIndex == 0)
660: requestEditor.requestFocus();
661: else
662: responseEditor.requestFocus();
663: }
664:
665: revalidate();
666: }
667: }
668:
669: public void setContent(JComponent content) {
670: add(content, BorderLayout.CENTER);
671: }
672:
673: public void removeContent(JComponent content) {
674: remove(content);
675: }
676:
677: public boolean onClose(boolean canCancel) {
678: mockResponse
679: .removePropertyChangeListener(propertyChangeListener);
680:
681: requestEditor.release();
682: responseEditor.release();
683: responseScriptEditor.release();
684:
685: requestAttachmentPanel.release();
686: responseAttachmentPanel.release();
687:
688: responseEditor.getParent().remove(responseEditor);
689: responseEditor = null;
690: requestEditor.getParent().remove(requestEditor);
691: requestEditor = null;
692:
693: return release();
694: }
695:
696: private class RunScriptAction extends AbstractAction {
697: public RunScriptAction() {
698: putValue(Action.SMALL_ICON, UISupport
699: .createImageIcon("/run_groovy_script.gif"));
700: putValue(Action.SHORT_DESCRIPTION,
701: "Runs this script using mock httpRequest/httpResponse objects");
702: }
703:
704: public void actionPerformed(ActionEvent e) {
705: try {
706: mockResponse.evaluateScript(mockResponse
707: .getMockResult() == null ? null : mockResponse
708: .getMockResult().getMockRequest());
709:
710: StringToStringMap values = null;
711: if (mockResponse.getMockResult() != null)
712: values = mockResponse.getMockResult()
713: .getMockRequest().getContext()
714: .toStringToStringMap();
715:
716: if (values == null || values.isEmpty()) {
717: UISupport
718: .showInfoMessage("No values were returned");
719: } else {
720: String msg = "<html><body>Returned values:<br>";
721:
722: for (String name : values.keySet()) {
723: msg += name + " : " + values.get(name) + "<br>";
724: }
725:
726: msg += "</body></html>";
727:
728: UISupport.showInfoMessage(msg);
729: }
730: } catch (Throwable e1) {
731: responseScriptEditor.selectError(e1.getMessage());
732: UISupport.showErrorMessage(e1.toString());
733: }
734: }
735: }
736:
737: private class MockResponseGroovyEditorModel implements
738: GroovyEditorModel {
739: public String[] getKeywords() {
740: return new String[] { "context", "mockRequest",
741: "mockResponse", "log", "requestContext" };
742: }
743:
744: public Action getRunAction() {
745: return runScriptAction;
746: }
747:
748: public String getScript() {
749: return mockResponse.getScript();
750: }
751:
752: public void setScript(String text) {
753: mockResponse.setScript(text);
754: }
755:
756: public Settings getSettings() {
757: return SoapUI.getSettings();
758: }
759: }
760: }
|