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