public abstract class ChoiceGroup extends Field
This class allows for grouped fields i.e., more than 1 form fields having the same name. The main difference between a choicebox and a radio group is that only 1 item can be selected within a radio group whereas choiceboxes allow multiple selections within the group.
There are 3 different kinds of choice groups (and their concrete subclasses).
Modifier and Type | Class and Description |
---|---|
static class |
ChoiceGroup.Choice
Creates a new choice for this choice group.
|
Field.Type
Constructor and Description |
---|
ChoiceGroup(String name)
Creates a new grouped choice object that intitially contains no choice
fields
|
ChoiceGroup(String name,
List choices)
Creates a new grouped choice object that intitially contains the specified
choices.
|
Modifier and Type | Method and Description |
---|---|
void |
add(ChoiceGroup.Choice choice) |
void |
addAll(ChoiceGroup cg)
Adds all the elements of the specified choice group to this choice group.
|
Map |
getAllElements()
Returns a map containing
ChoiceGroup.Choice all elements contained within this
group. |
ChoiceGroup.Choice |
getChoice(int n)
Returns the choice specified by the index n.
|
abstract Field.Type |
getType()
Subclasses should return an appropriate
Field.Type . |
Collection |
getValue(FormData fd)
Returns a Collection containing the choices selected by the user.
|
boolean |
isFilled(FormData fd)
Returns true if some option has been selected by the user,
false otherwise.
|
boolean |
isSelected(FormData fd,
ChoiceGroup.Choice choice,
boolean default_val)
Returns true is the specified choice was selected by the user
(the user's submission is provided by the formdata argument).
|
void |
renderImpl(FormData fd,
Writer writer)
Renders all elements of this group one after onether, seperated by a
whitespace.
|
void |
renderImpl(FormData fd,
Writer writer,
String prefix,
String suffix)
Renders the elements of this form by prefixing and suffixing each element
with the specified arguments.
|
void |
reset()
Clears all values in this group.
|
void |
reverseRender(boolean val)
If called with true, renders the choices contained in this group
in the reverse order in which they were added (by default, choices are
rendered in the order they were added)
|
void |
setValue(Collection values)
Sets the values for this choice group.
|
void |
setValue(FormData fd,
Collection values)
Sets the selected values for this choicegroup 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.
|
String |
toString() |
void |
writeLabel(boolean val)
Utility methods that calls the
ChoiceGroup.Choice.writeLabel method
for each choice contained in this group |
add, addError, addLabel, addString, disable, disable, enable, enable, getLabel, getName, getValidateErrors, isEnabled, render, render, render, renderError, renderError, renderStyleTag, setStyleTag, validate
public ChoiceGroup(String name)
name
- the field namepublic ChoiceGroup(String name, List choices)
name
- the field namechoices
- a list of ChoiceGroup.Choice
objects.public abstract Field.Type getType()
Field
Field.Type
. This type
is rendered as part of <input type= ...public void add(ChoiceGroup.Choice choice)
public void addAll(ChoiceGroup cg)
IllegalArgumentException
- if the specified choicegrop
was null or is the same choicegroup as the source.public Collection getValue(FormData fd)
ChoiceGroup.Choice
. If there
are no selected options, returns an empty unmodifiable collection.fd
- the submited form datapublic void setValue(FormData fd, Collection values)
To set the choices themselves, use the appropriate constructor or call
the setValue(Collection)
method. .The specified form data must
not be null.
fd
- the non-null form data object for rendering the formvalues
- a collection of ChoiceGroup.Choice
objectspublic void setValue(Collection values)
values
- a collection of ChoiceGroup.Choice
objectspublic ChoiceGroup.Choice getChoice(int n)
IndexOutOfBoundsException
- if the specified index is out
of range.public boolean isFilled(FormData fd)
public void setValueFromSubmit(FormData fd, javax.servlet.http.HttpServletRequest req) throws SubmitHackedException
Field
setValueFromSubmit
in class Field
fd
- the form data object to store the value inSubmitHackedException
public void reverseRender(boolean val)
public void renderImpl(FormData fd, Writer writer) throws IOException
Field.render(FormData, Writer, String, String)
method. It's also
possible to render each item in a group individually by not calling
this method but getting a Map/List of elements in this group and rendering
them in whichever location desired. (by calling render on each specific
choice itself).
Specify null for the formdata if rendering this element for the first time (before it has been submitted to by the user).
renderImpl
in class Field
IOException
public void renderImpl(FormData fd, Writer writer, String prefix, String suffix) throws IOException
writer
- the output destinationprefix
- prefix value for each element. specify
null or an empty string if not
needed.suffix
- suffix value for each element. specify
null or an empty string if not
needed.IOException
public void reset()
public Map getAllElements()
ChoiceGroup.Choice
all elements contained within this
group. This is useful if the elements need to be rendered individually for
custom positioning on an html page.
Each elements in the map will reflect the original selection state when it
was created. Each element can optionally be checked to see if it was
selected by the user by calling the isSelected(fc.web.forms.FormData, fc.web.forms.ChoiceGroup.Choice, boolean)
method.
public boolean isSelected(FormData fd, ChoiceGroup.Choice choice, boolean default_val)
fd
- form data submitted by user (can be null)choice
- a choice option belonging to this groupdefault_val
- the default value to return in case the form
data was null or did not contain this
choicegroup. This will typically be
false or
choice.isOrigSelected()public void writeLabel(boolean val)
ChoiceGroup.Choice.writeLabel
method
for each choice contained in this group