View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2009 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.form;
14  
15  import java.util.ArrayList;
16  
17  public class ComponentEnabler implements XFormFieldListener
18  {
19  	private final XFormField formField;
20  
21  	// Cannot use HashMap, because the XFormField may be a Proxy.
22  	private ArrayList<FieldValue> fields = new ArrayList<FieldValue>();
23  
24  	private static class FieldValue
25  	{
26  		XFormField field;
27  		String value;
28  
29  		public FieldValue( XFormField field, String value )
30  		{
31  			this.field = field;
32  			this.value = value;
33  		}
34  	}
35  
36  	public ComponentEnabler( XFormField formField )
37  	{
38  		this.formField = formField;
39  
40  		formField.addFormFieldListener( this );
41  	}
42  
43  	/***
44  	 * This should not be called directly from the dialog builders, because
45  	 * <code>field</code> may be a Proxy (on the Eclipse platform). Instead, call
46  	 * <code>addComponentEnablesFor(field, value)</code> on the combo box.
47  	 * 
48  	 * @param field
49  	 * @param value
50  	 */
51  	void add( XFormField field, String value )
52  	{
53  		String fieldValue = formField.getValue();
54  		boolean enable = ( fieldValue == null ? value == null : fieldValue.equals( value ) );
55  		field.setEnabled( enable );
56  		fields.add( new FieldValue( field, value ) );
57  	}
58  
59  	public void valueChanged( XFormField sourceField, String newValue, String oldValue )
60  	{
61  		for( FieldValue f : fields )
62  		{
63  			boolean enable = newValue.equals( f.value );
64  			f.field.setEnabled( enable );
65  		}
66  	}
67  }