View Javadoc

1   /*
2    * soapUI Pro, copyright (C) 2007 eviware software ab 
3    */
4    
5   package com.eviware.soapui.support.propertyexpansion;
6   
7   import java.awt.Point;
8   
9   import com.eviware.soapui.impl.wsdl.panels.teststeps.support.GroovyEditor;
10  import com.eviware.soapui.model.ModelItem;
11  import com.eviware.soapui.model.propertyexpansion.PropertyExpansion;
12  import com.eviware.soapui.support.UISupport;
13  import com.eviware.soapui.support.xml.JXEditTextArea;
14  
15  public class GroovyEditorPropertyExpansionTarget extends AbstractPropertyExpansionTarget
16  {
17  	private final JXEditTextArea textField;
18  
19  	public GroovyEditorPropertyExpansionTarget( GroovyEditor textField, ModelItem modelItem )
20  	{
21  		super( modelItem );
22  		this.textField = textField.getEditArea();
23  	}
24  
25  	public void insertPropertyExpansion( PropertyExpansion expansion, Point pt )
26  	{
27  		int pos = pt == null ? -1 : textField.pointToOffset( pt );
28  		if( pos == -1 )
29  			pos = textField.getCaretPosition();
30  		
31  		String name = expansion.getProperty().getName();
32  		String javaName = createJavaName( name );
33  		
34  		javaName = UISupport.prompt( "Specify name of variable for property", "Get Property", javaName );
35  		if( javaName == null )
36  			return;
37  		
38  		String txt = "def " + javaName + " = context.expand( '" + expansion +"' )\n";
39  		
40  		int line = textField.getLineOfOffset( pos );
41  		pos = textField.getLineStartOffset( line );
42  		
43  		textField.setCaretPosition( pos );
44  		textField.setSelectedText( txt );
45  		textField.requestFocusInWindow();
46  	}
47  
48  	private String createJavaName( String name )
49  	{
50  		StringBuffer buf = new StringBuffer();
51  		for( int c = 0; c < name.length(); c++ )
52  		{
53  			char ch = c == 0 ? name.toLowerCase().charAt( c ) : name.charAt( c );
54  			if( buf.length() == 0 && Character.isJavaIdentifierStart( ch ))
55  				buf.append( ch );
56  			else if( buf.length() > 0 && Character.isJavaIdentifierPart( ch ))
57  				buf.append( ch );
58  		}
59  		
60  		return buf.toString();
61  	}
62  
63  	public String getValueForCreation()
64  	{
65  		return textField.getSelectedText();
66  	}
67  
68  	public String getNameForCreation()
69  	{
70  		return null;
71  	}
72  }