View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2007 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of version 2.1 of the GNU Lesser General Public License as published by 
6    *  the Free Software Foundation.
7    *
8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
10   *  See the GNU Lesser General Public License for more details at gnu.org.
11   */
12  
13  package com.eviware.soapui.impl.wsdl.panels.teststeps;
14  
15  import java.awt.BorderLayout;
16  import java.awt.Component;
17  import java.awt.Dimension;
18  import java.awt.event.ActionEvent;
19  import java.awt.event.FocusAdapter;
20  import java.awt.event.FocusEvent;
21  import java.awt.event.MouseAdapter;
22  import java.awt.event.MouseEvent;
23  
24  import javax.swing.AbstractAction;
25  import javax.swing.Action;
26  import javax.swing.Box;
27  import javax.swing.Icon;
28  import javax.swing.JButton;
29  import javax.swing.JComponent;
30  import javax.swing.JLabel;
31  import javax.swing.JPanel;
32  import javax.swing.JScrollPane;
33  import javax.swing.JSplitPane;
34  
35  import org.apache.log4j.Logger;
36  
37  import com.eviware.soapui.SoapUI;
38  import com.eviware.soapui.impl.wsdl.actions.support.ShowOnlineHelpAction;
39  import com.eviware.soapui.impl.wsdl.panels.support.MockTestRunContext;
40  import com.eviware.soapui.impl.wsdl.panels.support.MockTestRunner;
41  import com.eviware.soapui.impl.wsdl.panels.support.TestRunComponentEnabler;
42  import com.eviware.soapui.impl.wsdl.panels.teststeps.support.GroovyEditor;
43  import com.eviware.soapui.impl.wsdl.panels.teststeps.support.GroovyEditorModel;
44  import com.eviware.soapui.impl.wsdl.support.HelpUrls;
45  import com.eviware.soapui.impl.wsdl.teststeps.WsdlGroovyScriptTestStep;
46  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestStepResult;
47  import com.eviware.soapui.model.ModelItem;
48  import com.eviware.soapui.model.settings.Settings;
49  import com.eviware.soapui.model.settings.SettingsListener;
50  import com.eviware.soapui.support.UISupport;
51  import com.eviware.soapui.support.components.JEditorStatusBarWithProgress;
52  import com.eviware.soapui.support.components.JXToolBar;
53  import com.eviware.soapui.support.log.JLogList;
54  import com.eviware.soapui.ui.desktop.DesktopPanel;
55  
56  /***
57   * DesktopPanel for WsdlGroovyTestSteps
58   * 
59   * @author Ole.Matzura
60   */
61  
62  public class GroovyScriptStepDesktopPanel extends JPanel implements DesktopPanel
63  {
64  	private final WsdlGroovyScriptTestStep groovyStep;
65  	private GroovyEditor editor;
66  	private JSplitPane mainSplit;
67  	private JLogList logArea;
68  	private Logger logger;
69  	private TestRunComponentEnabler componentEnabler;
70  	private RunAction runAction = new RunAction();
71  	private JEditorStatusBarWithProgress statusBar;
72  	private SettingsListener settingsListener;
73  
74  	public GroovyScriptStepDesktopPanel(WsdlGroovyScriptTestStep groovyStep)
75  	{
76  		super( new BorderLayout() );
77  		this.groovyStep = groovyStep;
78  		componentEnabler = new TestRunComponentEnabler( groovyStep.getTestCase() );
79  		
80  		buildUI();
81  		setPreferredSize( new Dimension( 600, 440 ));
82  		
83  		logger = Logger.getLogger( groovyStep.getName() );
84  		
85  		addFocusListener( new FocusAdapter() {
86  
87  			public void focusGained( FocusEvent e )
88  			{
89  				editor.requestFocusInWindow();
90  			}
91  
92  			} );
93  	}
94  
95  	private void buildUI()
96  	{
97  		editor = new GroovyEditor( new ScriptStepGroovyEditorModel( ));
98  		
99  		logArea = new JLogList( "Groovy Test Log" );
100 		logArea.addLogger( groovyStep.getName(), true );
101 		logArea.getLogList().addMouseListener( new MouseAdapter() {
102 
103 			public void mouseClicked(MouseEvent e)
104 			{
105 				if( e.getClickCount() < 2 )
106 					return;
107 				
108 				String value = logArea.getLogList().getSelectedValue().toString();
109 				if( value == null )
110 					return;
111 				
112 				editor.selectError( value );
113 			}} );
114 		
115 		mainSplit = UISupport.createVerticalSplit( new JScrollPane( editor ), logArea);
116 		mainSplit.setDividerLocation( 280 );
117 		mainSplit.setResizeWeight( 0.8 );
118 		add( mainSplit, BorderLayout.CENTER );
119 		add( buildToolbar(), BorderLayout.NORTH );
120 		add( buildStatusBar(), BorderLayout.SOUTH );
121 		
122 		componentEnabler.add( editor );
123 	}
124 	
125 	private Component buildStatusBar()
126 	{
127 		statusBar = new JEditorStatusBarWithProgress( editor );
128 		return statusBar;
129 	}
130 
131 	private JComponent buildToolbar()
132 	{
133 		JXToolBar toolBar = UISupport.createToolbar();
134 		JButton runButton = UISupport.createToolbarButton( runAction );
135 		toolBar.add( runButton );
136 		toolBar.add( Box.createHorizontalGlue() );
137 		JLabel label = new JLabel("<html>Script is invoked with <code>log</code>, <code>context</code> " +
138 						"and <code>testRunner</code> variables</html>");
139 		label.setToolTipText( label.getText() );
140 		label.setMaximumSize( label.getPreferredSize() );
141 		
142 		toolBar.add( label);
143 		toolBar.addRelatedGap();
144 		toolBar.add( UISupport.createToolbarButton( new ShowOnlineHelpAction( HelpUrls.GROOVYSTEPEDITOR_HELP_URL )));
145 		
146 		componentEnabler.add( runButton );
147 		
148 		return toolBar;
149 	}
150 
151 	public ModelItem getModelItem()
152 	{
153 		return groovyStep;
154 	}
155 
156 	public boolean onClose( boolean canCancel )
157 	{
158 		componentEnabler.release();
159 		SoapUI.getSettings().removeSettingsListener( settingsListener );
160 		return true;
161 	}
162 
163 	public JComponent getComponent()
164 	{
165 		return this;
166 	}
167 
168 	public boolean dependsOn(ModelItem modelItem)
169 	{
170 		return modelItem == groovyStep || modelItem == groovyStep.getTestCase() ||
171 				modelItem == groovyStep.getTestCase().getTestSuite() ||
172 				modelItem == groovyStep.getTestCase().getTestSuite().getProject();
173 	}
174 
175 	public String getTitle()
176 	{
177 		return groovyStep.getTestCase().getName() + " - " + groovyStep.getName();
178 	}
179 	
180 	public String getDescription()
181 	{
182 		return "Goto: [" + groovyStep.getName() + "] - " + groovyStep.getTestStepTitle();
183 	}
184 
185 	public Icon getIcon()
186 	{
187 		return getModelItem().getIcon();
188 	}
189 	
190 	private class ScriptStepGroovyEditorModel implements GroovyEditorModel
191 	{
192 		public String[] getKeywords()
193 		{
194 			return new String[] {"log", "context", "testRunner"};
195 		}
196 
197 		public Action getRunAction()
198 		{
199 			return runAction;
200 		}
201 
202 		public String getScript()
203 		{
204 			return groovyStep.getScript();
205 		}
206 
207 		public void setScript( String text )
208 		{
209 			groovyStep.setScript( text );
210 		}
211 
212 		public Settings getSettings()
213 		{
214 			return SoapUI.getSettings();
215 		}}
216 	
217 	private class RunAction extends AbstractAction
218 	{
219 		public RunAction()
220 		{
221 			putValue( Action.SMALL_ICON, UISupport.createImageIcon( "/run_groovy_script.gif" ));
222 			putValue( Action.SHORT_DESCRIPTION, "Runs this script using a mock testRunner and testContext" );
223 		}
224 
225 		public void actionPerformed(ActionEvent e)
226 		{
227 			MockTestRunner mockTestRunner = new MockTestRunner( groovyStep.getTestCase(), logger );
228 			statusBar.setIndeterminate( true );
229 			WsdlTestStepResult result = (WsdlTestStepResult) groovyStep.run( mockTestRunner, 
230 					new MockTestRunContext( mockTestRunner, groovyStep ) );
231 			statusBar.setIndeterminate( false );
232 			
233 			if( result.getError() != null )
234 			{
235 				String message = result.getError().getMessage();
236 				
237 				// ugly...
238 				editor.selectError( message);
239 				
240 				UISupport.showErrorMessage( result.getError().toString() );
241 				editor.requestFocus();
242 			}
243 		}
244 	}
245 }