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