View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2008 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.support.components;
14  
15  import com.eviware.soapui.impl.support.actions.ShowOnlineHelpAction;
16  import com.eviware.soapui.impl.wsdl.panels.teststeps.support.GroovyEditor;
17  import com.eviware.soapui.impl.wsdl.panels.teststeps.support.GroovyEditorModel;
18  import com.eviware.soapui.support.UISupport;
19  
20  import javax.swing.*;
21  import java.awt.*;
22  import java.awt.event.ActionEvent;
23  
24  public class GroovyEditorComponent extends JPanel
25  {
26  	private GroovyEditor editor;
27  	private JButton insertCodeButton;
28  
29  	public GroovyEditorComponent( GroovyEditorModel editorModel, String helpUrl )
30  	{
31  		super( new BorderLayout() );
32  		
33  		editor = new GroovyEditor( editorModel );
34  		editor.setBorder( BorderFactory.createCompoundBorder( BorderFactory.createEmptyBorder( 0, 3, 0, 3 ), 
35  					editor.getBorder() ));
36  		add( editor, BorderLayout.CENTER );
37  		addToolbar( editorModel, helpUrl );
38  	}
39  
40  	public GroovyEditor getEditor()
41  	{
42  		return editor;
43  	}
44  
45  	private void addToolbar( GroovyEditorModel editorModel, String helpUrl )
46  	{
47  		JXToolBar toolBar = UISupport.createSmallToolbar();
48  		
49  		Action runAction = editorModel.getRunAction();
50  		if( runAction != null )
51  		{
52  			JButton runButton = UISupport.createToolbarButton( runAction );
53  			if( runButton.getIcon() == null )
54  				runButton.setIcon( UISupport.createImageIcon( "/run_testcase.gif" ) );
55  			
56  			if( runButton.getToolTipText() == null )
57  				runButton.setToolTipText( "Runs this script" );
58  			
59  			toolBar.add( runButton );
60  			toolBar.addRelatedGap();
61  		}
62  		
63  		insertCodeButton = new JButton( new InsertCodeAction() );
64  		insertCodeButton.setIcon( UISupport.createImageIcon( "/down_arrow.gif" ) );
65  		insertCodeButton.setHorizontalTextPosition( SwingConstants.LEFT );
66  		toolBar.addFixed( insertCodeButton );
67  		
68  		toolBar.add( Box.createHorizontalGlue() );
69  		
70  		String [] args = editorModel.getKeywords();
71  		if( args != null && args.length > 0 )
72  		{
73  			String scriptName = editorModel.getScriptName();
74  			if( scriptName == null )
75  				scriptName = "";
76  			else 
77  				scriptName = scriptName.trim() + " ";
78  			
79  			StringBuilder text = new StringBuilder( "<html>" + scriptName + "Script is invoked with " );
80  			for( int c = 0; c < args.length; c++ )
81  			{
82  				if( c > 0 )
83  					text.append( ", " );
84  				
85  				text.append( "<font face=\"courier\">" ).append( args[c] ).append( "</font>" );
86  			}
87  			text.append( " variables</html>" );
88  			
89  			JLabel label = new JLabel( text.toString() );
90  			label.setToolTipText( label.getText() );
91  			label.setMaximumSize( label.getPreferredSize() );
92  			
93  			toolBar.addFixed( label);
94  			toolBar.addUnrelatedGap();
95  		}
96  		
97  		if( helpUrl != null )
98  		{
99  			toolBar.addFixed( UISupport.createToolbarButton( new ShowOnlineHelpAction( helpUrl ) ) );
100 		}
101 		
102 		add( toolBar, BorderLayout.NORTH );
103 	}
104 	
105 	public class InsertCodeAction extends AbstractAction
106    {
107 		public InsertCodeAction()
108 		{
109 			super( "Edit" );
110 			putValue( Action.SHORT_DESCRIPTION, "Inserts code at caret" );
111 		}
112 		
113 		public void actionPerformed( ActionEvent e )
114 		{
115 			JPopupMenu popup = editor.getEditArea().getComponentPopupMenu();
116 			popup.show( insertCodeButton, insertCodeButton.getWidth()/2, insertCodeButton.getHeight()/2 );
117 		}
118    }
119 }