fc.web.forms
Class DependentSelect

java.lang.Object
  extended by fc.web.forms.Field
      extended by fc.web.forms.DependentField
          extended by fc.web.forms.DependentSelect

public final class DependentSelect
extends DependentField

A dependent HTML DependentSelect field. The values of this field change based on the chosen value of some other field in the form. These new values are calculated/set on the server side.

Another good (and equally valid) approach is to instead use javascript arrays (with different sets of values) and swap out values on the client and/or use AJAX on the client side.


Nested Class Summary
static class DependentSelect.Data
           
 
Nested classes/interfaces inherited from class fc.web.forms.Field
Field.Type
 
Constructor Summary
DependentSelect(String name)
          Creates a new select element with no initial values.
 
Method Summary
 DependentSelect allowMultiple(FormData fd, boolean allow)
          true if multiple selections are allowed, false otherwise.
 boolean getBooleanValue(FormData fd)
          Convenience method that returns the single value of this field as a boolean.
 int getIntValue(FormData fd)
          Convenience method that returns the single value of this field as a Integer.
 String getStringValue(FormData fd)
          Convenience method that returns the selected option as a String.
 Field.Type getType()
          Subclasses should return an appropriate Field.Type.
 List getValue(FormData fd)
          Returns a List containing the selected options.
 boolean isFilled(FormData fd)
          Returns true if some option has been selected by the user, false otherwise.
 void renderImpl(FormData fd, Writer writer)
           
 DependentSelect setSize(FormData fd, int size)
          This value (if set) is rendered as the html SIZE tag.
 void setValue(DependentSelect.Data data, List values)
          Convenience method to set values in the specified DependentSelect.Data object.
 void setValue(FormData fd, List values)
          Sets the selected values for this select in the specified form data.
 void setValueFromSubmit(FormData fd, javax.servlet.http.HttpServletRequest req)
          This method sets the value of this field from the parameters obtained from the specified request.
 
Methods inherited from class fc.web.forms.DependentField
addDependency
 
Methods inherited from class fc.web.forms.Field
add, addError, addLabel, addString, disable, disable, enable, enable, getLabel, getName, getValidateErrors, isEnabled, render, render, render, renderError, renderError, renderStyleTag, setStyleTag, toString, validate
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

DependentSelect

public DependentSelect(String name)
Creates a new select element with no initial values. Multiple selections are not initially allowed.

Parameters:
name - the field name
Method Detail

getType

public Field.Type getType()
Description copied from class: Field
Subclasses should return an appropriate Field.Type. This type is rendered as part of <input type= ...

Specified by:
getType in class Field

getValue

public List getValue(FormData fd)
Returns a List containing the selected options. Each object in the collection will be of type Select.Option. If there are no selected options, the returned list will be an empty list.

Parameters:
fd - the submited form data

getStringValue

public String getStringValue(FormData fd)
Convenience method that returns the selected option as a String. No guarantees are made as to which selection is returned when more than one selection is selected (this method is really meant for when the select only allows single selections as a dropdown).

The returned value is of type String obtained by called the selected Select.Option's Select.Option.getValue() method.

If there is no selection, returns null


getIntValue

public int getIntValue(FormData fd)
Convenience method that returns the single value of this field as a Integer.

All the caveats of #getSingleValueAsString() apply.

Throws:
NumberFormatException - if the value could not be returned as in integer.

getBooleanValue

public boolean getBooleanValue(FormData fd)
Convenience method that returns the single value of this field as a boolean.

All the caveats of #getSingleValueAsString() apply. In particular, the formdata should contain non-null data with at least one selection.


isFilled

public boolean isFilled(FormData fd)
Returns true if some option has been selected by the user, false otherwise. (also returns false is the specified form data is null).

Specified by:
isFilled in class Field

setValueFromSubmit

public void setValueFromSubmit(FormData fd,
                               javax.servlet.http.HttpServletRequest req)
Description copied from class: Field
This method sets the value of this field from the parameters obtained from the specified request. The name of the parameter to obtain this value will typically be the name of this field itself.

Specified by:
setValueFromSubmit in class Field
Parameters:
fd - the form data object to store the value in
See Also:
Select.setValueFromSubmit(fc.web.forms.FormData, javax.servlet.http.HttpServletRequest)

renderImpl

public void renderImpl(FormData fd,
                       Writer writer)
                throws SQLException,
                       IOException
Specified by:
renderImpl in class Field
Throws:
SQLException
IOException

allowMultiple

public DependentSelect allowMultiple(FormData fd,
                                     boolean allow)
true if multiple selections are allowed, false otherwise. This value (if set) is rendered as the html MULTIPLE tag.

Returns:
this object for method chaining convenience

setSize

public DependentSelect setSize(FormData fd,
                               int size)
This value (if set) is rendered as the html SIZE tag. If the list contains more options than specified by size, the browser will display the selection list with scrollbars.

Returns:
this object for method chaining convenience

setValue

public void setValue(FormData fd,
                     List values)
Sets the selected values for this select in the specified form data. This is useful for showing different initial values to each user (before the form has been submitted by that user). The values are set in a newly created DependentSelect.Data object which is then stored inside the form data. Since these are set in the FormData object, these values are request specific and can differ per user.

If the form has not been submitted, there is no form data object. A form data object should be manually created if needed for storing the value.

Parameters:
data - an existing form data object.
values - a list of Select.Option objects

setValue

public void setValue(DependentSelect.Data data,
                     List values)
Convenience method to set values in the specified DependentSelect.Data object. The following two are equivalent:
        List values .....
        DependentSelect.Data data = new DependentSelect.Data();
        fd.putData(name, data);
        setValue(data, values);
and
        List values .....
        setValue(fd, values);

Parameters:
data - an existing DependentSelect.Data object.
values - a list of Select.Option objects