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