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