View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2007 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.x.impl.swing;
14  
15  import java.awt.Color;
16  
17  import javax.swing.BorderFactory;
18  import javax.swing.JTextField;
19  import javax.swing.text.Document;
20  
21  import com.eviware.soapui.support.DocumentListenerAdapter;
22  import com.eviware.x.form.XFormTextField;
23  
24  public class JTextFieldFormField extends AbstractSwingXFormField<JTextField> implements XFormTextField
25  {
26  	private boolean updating;
27  	private String oldValue;
28  	
29  	public JTextFieldFormField()
30  	{
31  		super( new JTextField() );
32  		
33  		getComponent().getDocument().addDocumentListener( new DocumentListenerAdapter() {
34  
35  			@Override
36  			public void update( Document document )
37  			{
38  				String text = getComponent().getText();
39  				
40  				if( !updating )
41  					fireValueChanged( text, oldValue );
42  				
43  				oldValue = text;
44  			}} );
45  	}
46  	
47  	public void setRequired(boolean required, String message)
48  	{
49  		super.setRequired(required, message);
50  		
51  		if( required )
52  			getComponent().setBorder( 
53  					BorderFactory.createCompoundBorder(
54  							BorderFactory.createLineBorder( Color.RED ), 
55  							BorderFactory.createEmptyBorder( 2, 2, 2, 2 )));
56  		else
57  			getComponent().setBorder( 
58  					BorderFactory.createCompoundBorder(
59  							BorderFactory.createLineBorder( Color.GRAY ), 
60  							BorderFactory.createEmptyBorder( 2, 2, 2, 2 )));
61  	}
62  
63  	public void setValue(String value)
64  	{
65  		updating = true;
66  		oldValue = null;
67  		getComponent().setText( value );
68  		updating = false;
69  	}
70  
71  	public String getValue()
72  	{
73  		return getComponent().getText();
74  	}
75  
76  	public void setWidth(int columns)
77  	{
78  		getComponent().setColumns( columns );
79  	}
80  }