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.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,
    45      * because <code>field</code> may be a Proxy (on the Eclipse platform).
    46      * Instead, call <code>addComponentEnablesFor(field, value)</code> on the combo box.
    47      * @param field
    48      * @param value
    49      */
    50  	void add(XFormField field, String value)
    51  	{
    52  		String fieldValue = formField.getValue();
    53  		boolean enable = (fieldValue == null ? value == null : fieldValue.equals( value ));
    54        field.setEnabled( enable);
    55  		fields.add( new FieldValue(field, value) );
    56  	}
    57
    58  	public void valueChanged(XFormField sourceField, String newValue, String oldValue)
    59  	{
    60  		for( FieldValue f : fields)
    61  		{
    62  			boolean enable = newValue.equals( f.value);
    63           f.field.setEnabled( enable );
    64  		}
    65  	}
    66  }