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.teststeps;
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.MouseAdapter;
022: import java.awt.event.MouseEvent;
023:
024: import javax.swing.AbstractAction;
025: import javax.swing.Action;
026: import javax.swing.Box;
027: import javax.swing.Icon;
028: import javax.swing.JButton;
029: import javax.swing.JComponent;
030: import javax.swing.JLabel;
031: import javax.swing.JPanel;
032: import javax.swing.JScrollPane;
033: import javax.swing.JSplitPane;
034:
035: import org.apache.log4j.Logger;
036:
037: import com.eviware.soapui.SoapUI;
038: import com.eviware.soapui.impl.wsdl.actions.support.ShowOnlineHelpAction;
039: import com.eviware.soapui.impl.wsdl.panels.support.MockTestRunContext;
040: import com.eviware.soapui.impl.wsdl.panels.support.MockTestRunner;
041: import com.eviware.soapui.impl.wsdl.panels.support.TestRunComponentEnabler;
042: import com.eviware.soapui.impl.wsdl.panels.teststeps.support.GroovyEditor;
043: import com.eviware.soapui.impl.wsdl.panels.teststeps.support.GroovyEditorModel;
044: import com.eviware.soapui.impl.wsdl.support.HelpUrls;
045: import com.eviware.soapui.impl.wsdl.teststeps.WsdlGroovyScriptTestStep;
046: import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestStepResult;
047: import com.eviware.soapui.model.ModelItem;
048: import com.eviware.soapui.model.settings.Settings;
049: import com.eviware.soapui.model.settings.SettingsListener;
050: import com.eviware.soapui.support.UISupport;
051: import com.eviware.soapui.support.components.JEditorStatusBarWithProgress;
052: import com.eviware.soapui.support.components.JXToolBar;
053: import com.eviware.soapui.support.log.JLogList;
054: import com.eviware.soapui.ui.desktop.DesktopPanel;
055:
056: /**
057: * DesktopPanel for WsdlGroovyTestSteps
058: *
059: * @author Ole.Matzura
060: */
061:
062: public class GroovyScriptStepDesktopPanel extends JPanel implements
063: DesktopPanel {
064: private final WsdlGroovyScriptTestStep groovyStep;
065: private GroovyEditor editor;
066: private JSplitPane mainSplit;
067: private JLogList logArea;
068: private Logger logger;
069: private TestRunComponentEnabler componentEnabler;
070: private RunAction runAction = new RunAction();
071: private JEditorStatusBarWithProgress statusBar;
072: private SettingsListener settingsListener;
073:
074: public GroovyScriptStepDesktopPanel(
075: WsdlGroovyScriptTestStep groovyStep) {
076: super (new BorderLayout());
077: this .groovyStep = groovyStep;
078: componentEnabler = new TestRunComponentEnabler(groovyStep
079: .getTestCase());
080:
081: buildUI();
082: setPreferredSize(new Dimension(600, 440));
083:
084: logger = Logger.getLogger(groovyStep.getName() + "#"
085: + hashCode());
086:
087: addFocusListener(new FocusAdapter() {
088:
089: public void focusGained(FocusEvent e) {
090: editor.requestFocusInWindow();
091: }
092:
093: });
094: }
095:
096: private void buildUI() {
097: editor = new GroovyEditor(new ScriptStepGroovyEditorModel());
098:
099: logArea = new JLogList("Groovy Test Log");
100: logArea
101: .addLogger(groovyStep.getName() + "#" + hashCode(),
102: true);
103: logArea.getLogList().addMouseListener(new MouseAdapter() {
104:
105: public void mouseClicked(MouseEvent e) {
106: if (e.getClickCount() < 2)
107: return;
108:
109: String value = logArea.getLogList().getSelectedValue()
110: .toString();
111: if (value == null)
112: return;
113:
114: editor.selectError(value);
115: }
116: });
117:
118: mainSplit = UISupport.createVerticalSplit(new JScrollPane(
119: editor), logArea);
120: mainSplit.setDividerLocation(280);
121: mainSplit.setResizeWeight(0.8);
122: add(mainSplit, BorderLayout.CENTER);
123: add(buildToolbar(), BorderLayout.NORTH);
124: add(buildStatusBar(), BorderLayout.SOUTH);
125:
126: componentEnabler.add(editor);
127: }
128:
129: private Component buildStatusBar() {
130: statusBar = new JEditorStatusBarWithProgress(editor);
131: return statusBar;
132: }
133:
134: private JComponent buildToolbar() {
135: JXToolBar toolBar = UISupport.createToolbar();
136: JButton runButton = UISupport.createToolbarButton(runAction);
137: toolBar.add(runButton);
138: toolBar.add(Box.createHorizontalGlue());
139: JLabel label = new JLabel(
140: "<html>Script is invoked with <code>log</code>, <code>context</code> "
141: + "and <code>testRunner</code> variables</html>");
142: label.setToolTipText(label.getText());
143: label.setMaximumSize(label.getPreferredSize());
144:
145: toolBar.add(label);
146: toolBar.addRelatedGap();
147: toolBar.add(UISupport
148: .createToolbarButton(new ShowOnlineHelpAction(
149: HelpUrls.GROOVYSTEPEDITOR_HELP_URL)));
150:
151: componentEnabler.add(runButton);
152:
153: return toolBar;
154: }
155:
156: public ModelItem getModelItem() {
157: return groovyStep;
158: }
159:
160: public boolean onClose(boolean canCancel) {
161: componentEnabler.release();
162: editor.release();
163: SoapUI.getSettings().removeSettingsListener(settingsListener);
164: logger.removeAllAppenders();
165: logger = null;
166: logArea.release();
167: return true;
168: }
169:
170: public JComponent getComponent() {
171: return this ;
172: }
173:
174: public boolean dependsOn(ModelItem modelItem) {
175: return modelItem == groovyStep
176: || modelItem == groovyStep.getTestCase()
177: || modelItem == groovyStep.getTestCase().getTestSuite()
178: || modelItem == groovyStep.getTestCase().getTestSuite()
179: .getProject();
180: }
181:
182: public String getTitle() {
183: return groovyStep.getTestCase().getName() + " - "
184: + groovyStep.getName();
185: }
186:
187: public String getDescription() {
188: return "Goto: [" + groovyStep.getName() + "] - "
189: + groovyStep.getTestStepTitle();
190: }
191:
192: public Icon getIcon() {
193: return getModelItem().getIcon();
194: }
195:
196: private class ScriptStepGroovyEditorModel implements
197: GroovyEditorModel {
198: public String[] getKeywords() {
199: return new String[] { "log", "context", "testRunner" };
200: }
201:
202: public Action getRunAction() {
203: return runAction;
204: }
205:
206: public String getScript() {
207: return groovyStep.getScript();
208: }
209:
210: public void setScript(String text) {
211: groovyStep.setScript(text);
212: }
213:
214: public Settings getSettings() {
215: return SoapUI.getSettings();
216: }
217: }
218:
219: private class RunAction extends AbstractAction {
220: public RunAction() {
221: putValue(Action.SMALL_ICON, UISupport
222: .createImageIcon("/run_groovy_script.gif"));
223: putValue(Action.SHORT_DESCRIPTION,
224: "Runs this script using a mock testRunner and testContext");
225: }
226:
227: public void actionPerformed(ActionEvent e) {
228: MockTestRunner mockTestRunner = new MockTestRunner(
229: groovyStep.getTestCase(), logger);
230: statusBar.setIndeterminate(true);
231: WsdlTestStepResult result = (WsdlTestStepResult) groovyStep
232: .run(mockTestRunner, new MockTestRunContext(
233: mockTestRunner, groovyStep));
234: statusBar.setIndeterminate(false);
235:
236: if (result.getError() != null) {
237: String message = result.getError().getMessage();
238:
239: // ugly...
240: editor.selectError(message);
241:
242: UISupport
243: .showErrorMessage(result.getError().toString());
244: editor.requestFocus();
245: }
246: }
247: }
248: }
|