View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2009 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  import java.beans.PropertyChangeEvent;
18  import java.beans.PropertyChangeListener;
19  
20  import javax.swing.AbstractAction;
21  import javax.swing.Action;
22  import javax.swing.BorderFactory;
23  import javax.swing.Box;
24  import javax.swing.JButton;
25  import javax.swing.JLabel;
26  import javax.swing.JPanel;
27  import javax.swing.JPopupMenu;
28  import javax.swing.SwingConstants;
29  
30  import com.eviware.soapui.impl.support.actions.ShowOnlineHelpAction;
31  import com.eviware.soapui.impl.wsdl.panels.teststeps.support.GroovyEditor;
32  import com.eviware.soapui.impl.wsdl.panels.teststeps.support.GroovyEditorModel;
33  import com.eviware.soapui.support.UISupport;
34  
35  public class GroovyEditorComponent extends JPanel implements PropertyChangeListener
36  {
37  	private GroovyEditor editor;
38  	private JButton insertCodeButton;
39  	private Action runAction;
40  	private JXToolBar toolBar;
41  	private final GroovyEditorModel editorModel;
42  	private final String helpUrl;
43  
44  	public GroovyEditorComponent( GroovyEditorModel editorModel, String helpUrl )
45  	{
46  		super( new BorderLayout() );
47  		this.editorModel = editorModel;
48  		this.helpUrl = helpUrl;
49  
50  		editor = new GroovyEditor( editorModel );
51  		editor.setBorder( BorderFactory.createCompoundBorder( BorderFactory.createEmptyBorder( 0, 3, 0, 3 ), editor
52  				.getBorder() ) );
53  		add( editor, BorderLayout.CENTER );
54  		buildToolbar( editorModel, helpUrl );
55  		
56  		editorModel.addPropertyChangeListener( this );
57  	}
58  
59  	public GroovyEditor getEditor()
60  	{
61  		return editor;
62  	}
63  
64  	@Override
65  	public void setEnabled( boolean enabled )
66  	{
67  		super.setEnabled( enabled );
68  		
69  		editor.setEnabled( enabled );
70  		if( runAction != null )
71  			runAction.setEnabled( enabled );
72  		
73  		insertCodeButton.setEnabled( enabled );
74  	}
75  
76  	private void buildToolbar( GroovyEditorModel editorModel, String helpUrl )
77  	{
78  		if( toolBar == null )
79  		{
80  			toolBar = UISupport.createSmallToolbar();
81  		}
82  		else
83  		{
84  			remove( toolBar );
85  			toolBar.removeAll();
86  		}
87  
88  		runAction = editorModel.getRunAction();
89  		if( runAction != null )
90  		{
91  			JButton runButton = UISupport.createToolbarButton( runAction );
92  			if( runButton.getIcon() == null )
93  				runButton.setIcon( UISupport.createImageIcon( "/run_testcase.gif" ) );
94  
95  			if( runButton.getToolTipText() == null )
96  				runButton.setToolTipText( "Runs this script" );
97  
98  			toolBar.add( runButton );
99  			toolBar.addRelatedGap();
100 		}
101 
102 		if( insertCodeButton == null )
103 		{
104 			insertCodeButton = new JButton( new InsertCodeAction() );
105 			insertCodeButton.setIcon( UISupport.createImageIcon( "/down_arrow.gif" ) );
106 			insertCodeButton.setHorizontalTextPosition( SwingConstants.LEFT );
107 		}
108 		
109 		toolBar.addFixed( insertCodeButton );
110 
111 		toolBar.add( Box.createHorizontalGlue() );
112 
113 		String[] args = editorModel.getKeywords();
114 		if( args != null && args.length > 0 )
115 		{
116 			String scriptName = editorModel.getScriptName();
117 			if( scriptName == null )
118 				scriptName = "";
119 			else
120 				scriptName = scriptName.trim() + " ";
121 
122 			StringBuilder text = new StringBuilder( "<html>" + scriptName + "Script is invoked with " );
123 			for( int c = 0; c < args.length; c++ )
124 			{
125 				if( c > 0 )
126 					text.append( ", " );
127 
128 				text.append( "<font face=\"courier\">" ).append( args[c] ).append( "</font>" );
129 			}
130 			text.append( " variables</html>" );
131 
132 			JLabel label = new JLabel( text.toString() );
133 			label.setToolTipText( label.getText() );
134 			label.setMaximumSize( label.getPreferredSize() );
135 
136 			toolBar.addFixed( label );
137 			toolBar.addUnrelatedGap();
138 		}
139 
140 		if( helpUrl != null )
141 		{
142 			toolBar.addFixed( UISupport.createToolbarButton( new ShowOnlineHelpAction( helpUrl ) ) );
143 		}
144 
145 		add( toolBar, BorderLayout.NORTH );
146 		revalidate();
147 		repaint();
148 	}
149 
150 	public class InsertCodeAction extends AbstractAction
151 	{
152 		public InsertCodeAction()
153 		{
154 			super( "Edit" );
155 			putValue( Action.SHORT_DESCRIPTION, "Inserts code at caret" );
156 		}
157 
158 		public void actionPerformed( ActionEvent e )
159 		{
160 			JPopupMenu popup = editor.getEditArea().getComponentPopupMenu();
161 			popup.show( insertCodeButton, insertCodeButton.getWidth() / 2, insertCodeButton.getHeight() / 2 );
162 		}
163 	}
164 
165 	public void release()
166 	{
167 		editorModel.removePropertyChangeListener( this );
168 		getEditor().release();
169 	}
170 
171 	public void propertyChange( PropertyChangeEvent evt )
172 	{
173 		if( !evt.getPropertyName().equals( "script" ))
174 		{
175 			buildToolbar( editorModel, helpUrl );
176 		}
177 	}
178 }