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