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.propertyexpansion;
14  
15  import com.eviware.soapui.impl.wsdl.panels.teststeps.support.GroovyEditor;
16  import com.eviware.soapui.model.ModelItem;
17  import com.eviware.soapui.model.propertyexpansion.PropertyExpansion;
18  import com.eviware.soapui.support.UISupport;
19  import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
20  
21  import javax.swing.text.BadLocationException;
22  import java.awt.*;
23  
24  public class GroovyEditorPropertyExpansionTarget extends AbstractPropertyExpansionTarget
25  {
26  	private final RSyntaxTextArea textField;
27  
28  	public GroovyEditorPropertyExpansionTarget( GroovyEditor textField, ModelItem modelItem )
29  	{
30  		super( modelItem );
31  		this.textField = textField.getEditArea();
32  	}
33  
34  	public void insertPropertyExpansion( PropertyExpansion expansion, Point pt )
35  	{
36  		int pos = pt == null ? -1 : textField.viewToModel( pt );
37  		if( pos == -1 )
38  			pos = textField.getCaretPosition();
39  		
40  		String name = expansion.getProperty().getName();
41  		String javaName = createJavaName( name );
42  		
43  		javaName = UISupport.prompt( "Specify name of variable for property", "Get Property", javaName );
44  		if( javaName == null )
45  			return;
46  		
47  		String txt = "def " + javaName + " = context.expand( '" + expansion +"' )\n";
48  
49        try
50        {
51           int line = textField.getLineOfOffset( pos );
52           pos = textField.getLineStartOffset( line );
53  
54           textField.setCaretPosition( pos );
55           textField.insert( txt,pos );
56           textField.setSelectionStart( pos );
57           textField.setSelectionEnd( pos + txt.length() );
58           textField.requestFocusInWindow();
59        }
60        catch( BadLocationException e )
61        {
62           e.printStackTrace();
63        }
64     }
65  
66  	private String createJavaName( String name )
67  	{
68  		StringBuffer buf = new StringBuffer();
69  		for( int c = 0; c < name.length(); c++ )
70  		{
71  			char ch = c == 0 ? name.toLowerCase().charAt( c ) : name.charAt( c );
72  			if( buf.length() == 0 && Character.isJavaIdentifierStart( ch ))
73  				buf.append( ch );
74  			else if( buf.length() > 0 && Character.isJavaIdentifierPart( ch ))
75  				buf.append( ch );
76  		}
77  		
78  		return buf.toString();
79  	}
80  
81  	public String getValueForCreation()
82  	{
83  		return textField.getSelectedText();
84  	}
85  
86  	public String getNameForCreation()
87  	{
88  		return null;
89  	}
90  }