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.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 }