1
2
3
4
5
6
7
8
9
10
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 }