Class RefreshableSelect


public final class RefreshableSelect extends Select
An refreshable HTML Select field. This is different than Select in that is allows the values/options displayed by this select to be changed dynamically/per-user via the setValue(FormData, List) method.

There is no easy way to persistently keep track of these values which may be different for each user. Therefore, unlike other field classes, this class does (by default) no check to see if the returned values by the user match at least one of the values displayed to the user in the first place. This can happen if values are hacked on the client side.

Database constraints (that only accept valid values) should be used to possibly handle bad data returned by this field.

  • Constructor Details

    • RefreshableSelect

      public RefreshableSelect(String name)
      Creates a new select element with no initial values. Multiple selections are not initially allowed.
      Parameters:
      name - the field name
    • RefreshableSelect

      public RefreshableSelect(String name, List values)
      Creates a new select element with the specified initial values and no multiple selections allowed. There is no explicit default selection which typically means the browser will show the first item in the list as the selection item.
      Parameters:
      name - the field name
      values - the initial option values for this class. The objects contained in the list must be of type Select.Option otherwise a ClassCastException may be thrown in future operations on this object.
      Throws:
      IllegalArgumentException - if the values parameter was null (note: an empty but non-null List is ok)
  • Method Details

    • 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= ...
      Overrides:
      getType in class Select
    • setValue

      public void setValue(FormData fd, List values)
      Sets the options 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).

      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:
      fd - the non-null form data used for rendering the form
      values - a list of Select.Option objects
    • setSelectedValue

      public void setSelectedValue(FormData fd, String value)
      Convenience method that sets the options for this select in the specified form data. All of the initial options are used and option corresponding to the specified value is selected. There must be some initial options set for this field and the specified value should match one of the initial options.

      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 selected value.

      Parameters:
      fd - the non-null form data used for rendering the form
      value - the option with this value is selected
    • setSelectedValue

      public void setSelectedValue(FormData fd, int value)
      Convenience method that invokes setSelectedValue(fd, String) after converting the specified integer value to a string.
    • 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.
      Overrides:
      getValue in class Select
      Parameters:
      fd - the submited form data. This object should not be null otherwise a NPE will be thrown.
    • getStringValue

      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 Option's Select.Option.getValue() method.

      If there is no selection, returns null

      Overrides:
      getStringValue in class Select
    • getIntValue

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

      All the caveats of

      invalid reference
      #getSingleValueAsString()
      apply.
      Overrides:
      getIntValue in class Select
      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

      invalid reference
      #getSingleValueAsString()
      apply. In particular, the formdata should contain non-null data with at least one selection.
      Overrides:
      getBooleanValue in class Select
    • 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).
      Overrides:
      isFilled in class Select
    • setValueFromSubmit

      public void setValueFromSubmit(FormData fd, jakarta.servlet.http.HttpServletRequest req) throws SubmitHackedException
      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.
      Overrides:
      setValueFromSubmit in class Select
      Parameters:
      fd - the form data object to store the value in
      Throws:
      SubmitHackedException
    • renderImpl

      public void renderImpl(FormData fd, Writer writer) throws IOException
      Overrides:
      renderImpl in class Select
      Throws:
      IOException
    • add

      public void add(Select.Option opt)
      Adds a new option to the selection list. Replaces any previous option that was added previously with the same value.
      Overrides:
      add in class Select
      Parameters:
      opt - the option to be added
    • add

      public void add(String item)
      Adds a new option to the selection list. Replaces any previous option that was added previously with the same value. This method will have the same effect as if the add(new Select.Option(item)) method was invoked.
      Overrides:
      add in class Select
      Parameters:
      item - the option to be added
    • reset

      public void reset()
      Clears all data in this select.
      Overrides:
      reset in class Select
    • setSize

      public Select setSize(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.
      Overrides:
      setSize in class Select
      Returns:
      this object for method chaining convenience
    • allowMultiple

      public Select allowMultiple(boolean allow)
      true is multiple selections are allowed, false otherwise. This value (if set) is rendered as the html MULTIPLE tag.
      Overrides:
      allowMultiple in class Select
      Returns:
      this object for method chaining convenience