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