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  import java.util.Arrays;
    17  import java.util.HashSet;
    18  import java.util.List;
    19  import java.util.Set;
    20
    21  import com.eviware.x.form.validators.RequiredValidator;
    22
    23
    24  public abstract class AbstractXFormField<T> implements XFormField
    25  {
    26  	private Set<XFormFieldListener> listeners;
    27  	private List<XFormFieldValidator> validators;
    28  	private RequiredValidator requiredValidator;
    29  	private ComponentEnabler enabler = null;
    30
    31     public AbstractXFormField()
    32     {
    33     }
    34
    35     public abstract T getComponent();
    36
    37  	public void addFormFieldListener(XFormFieldListener listener)
    38  	{
    39  		if( listeners == null )
    40  			listeners = new HashSet<XFormFieldListener>();
    41
    42  		listeners.add( listener );
    43  	}
    44
    45  	public void addFormFieldValidator(XFormFieldValidator validator)
    46  	{
    47  		if( validators == null )
    48  			validators = new ArrayList<XFormFieldValidator>();
    49
    50  		validators.add( validator );
    51  	}
    52
    53     public void addComponentEnabler(XFormField tf, String value)
    54     {
    55        if(enabler == null)
    56        {
    57           enabler = new ComponentEnabler(this);
    58        }
    59        enabler.add(tf, value);
    60     }
    61
    62  	public boolean isRequired()
    63  	{
    64  		return requiredValidator != null;
    65  	}
    66
    67  	public void removeFieldListener(XFormFieldListener listener)
    68  	{
    69  		if( listeners != null )
    70  			listeners.remove( listener );
    71  	}
    72
    73  	public void removeFormFieldValidator(XFormFieldValidator validator)
    74  	{
    75  		if( validators != null )
    76  			validators.remove( validator );
    77  	}
    78
    79  	public void setRequired(boolean required, String message)
    80  	{
    81  		if( requiredValidator != null )
    82  			removeFormFieldValidator( requiredValidator );
    83
    84  		if( required )
    85  		{
    86  			requiredValidator = new RequiredValidator( message );
    87  			addFormFieldValidator( requiredValidator );
    88  		}
    89  	}
    90
    91  	public ValidationMessage[] validate()
    92  	{
    93  		if( validators == null || validators.isEmpty() )
    94  			return null;
    95
    96  		ArrayList<ValidationMessage> messages = new ArrayList<ValidationMessage>();
    97
    98  		for( XFormFieldValidator validator : validators )
    99  		{
    100 			ValidationMessage[] validateField = validator.validateField( this );
    101 			if( validateField != null && validateField.length > 0 )
    102 				messages.addAll( Arrays.asList( validateField));
    103 		}
    104
    105 		return messages.toArray( new ValidationMessage[messages.size()] );
    106 	}
    107
    108 	protected void fireValueChanged(String newValue, String oldValue )
    109 	{
    110 		if( listeners == null )
    111 			return;
    112
    113 		for( XFormFieldListener listener : listeners )
    114 		{
    115 			listener.valueChanged( this, newValue, oldValue );
    116 		}
    117 	}
    118
    119 	public Object getProperty(String name)
    120 	{
    121 		return null;
    122 	}
    123
    124 	public void setProperty(String name, Object value)
    125 	{
    126 	}
    127
    128 	public boolean isMultiRow()
    129 	{
    130 		return false;
    131 	}
    132 }