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 javax.swing.JTextField;
10  import javax.swing.text.JTextComponent;
11  
12  import com.eviware.soapui.model.ModelItem;
13  import com.eviware.soapui.model.propertyexpansion.PropertyExpansion;
14  
15  public class JTextComponentPropertyExpansionTarget extends AbstractPropertyExpansionTarget
16  {
17  	private final JTextComponent textField;
18  
19  	public JTextComponentPropertyExpansionTarget( JTextComponent textField, ModelItem modelItem )
20  	{
21  		super( modelItem );
22  		this.textField = textField;
23  	}
24  
25  	public void insertPropertyExpansion( PropertyExpansion expansion, Point pt )
26  	{
27  		int pos = pt == null ? -1 : textField.viewToModel( pt );
28  		if( pos == -1 )
29  			pos = textField.getCaretPosition();
30  		
31  		if( pos == -1 || textField instanceof JTextField )
32  		{
33  			textField.setText( expansion.toString() );
34  		}
35  		else
36  		{
37  			String text = textField.getText();
38  			if( textField.getSelectionStart() < textField.getSelectionEnd() )
39  			{
40  				textField.setText( text.substring( 0, textField.getSelectionStart() ) + expansion + 
41  							text.substring( textField.getSelectionEnd() ));
42  				textField.setCaretPosition( textField.getSelectionStart() );
43  			}
44  			else
45  			{
46  				textField.setText( text.substring( 0, pos ) + expansion + text.substring( pos ));
47  				textField.setCaretPosition( pos );
48  			}
49  			
50  			textField.requestFocusInWindow();
51  		}
52  	}
53  
54  	public String getValueForCreation()
55  	{
56  		return textField.getSelectedText() == null ? textField.getText() : textField.getSelectedText();
57  	}
58  
59  	public String getNameForCreation()
60  	{
61  		return textField.getName();
62  	}
63  }