org.apache.click.extras.control
Class NumberField

java.lang.Object
  extended by org.apache.click.control.AbstractControl
      extended by org.apache.click.control.Field
          extended by org.apache.click.control.TextField
              extended by org.apache.click.extras.control.NumberField
All Implemented Interfaces:
Serializable, Control, Stateful
Direct Known Subclasses:
DoubleField, IntegerField, LongField

public class NumberField
extends TextField

Provides a Number Field control:   <input type='text'>.

Number Field
NumberField uses a NumberFormat to format, parse and validate the input text. The number format can either directly be set through setNumberFormat(NumberFormat) or by setting the number format pattern with setPattern(String).

When NumberField is validated and the input string can be parsed by the NumberFormat then the string value of this field (@link org.apache.click.control.Field#getValue()} is set to the formatted value of the input.

For example if you define an integer pattern of "#,##0" and the users enters "2.54" then the resulting Number is 3. For all such cases the NumberFormat does recognize the input as valid and does not mark the field as invalid. To get the exact string the user entered either turn validation off or call Field.getValue().

When the Number is set through setNumber(Number) the value of the field is also set to the formatted number. The number returned from getNumber() is then the formatted number. It is not the original Number passed in. To circumvent formatting use setValue().

CSS and JavaScript resources

The NumberField control makes use of the following resources (which Click automatically deploys to the application directory, /click): The NumberField uses a JavaScript onkeypress() doubleFilter() method to prevent users from entering invalid characters. To enable number key filtering reference the variables $jsElements and $headElements in your page template. For example:
 <html>
 <head>
 $headElements
 </head>
 <body>

 $form

 $jsElements
 </body>
 </html> 
The NumberField has right justified horizontal text alignment, Field.setTextAlign(String).

See also W3C HTML reference INPUT

See Also:
Serialized Form

Field Summary
protected  double maxvalue
          The maximum field value.
protected  double minvalue
          The minimum field value.
protected  NumberFormat numberFormat
          The NumberFormat for formatting the output.
protected  String pattern
          The decimal pattern to use for a NumberFormat.
protected static String VALIDATE_NUMBER_FIELD_FUNCTION
          The field validation JavaScript function template.
 
Fields inherited from class org.apache.click.control.TextField
maxLength, minLength, size, VALIDATE_TEXTFIELD_FUNCTION
 
Fields inherited from class org.apache.click.control.Field
disabled, error, focus, form, help, label, labelStyle, labelStyleClass, parentStyleClassHint, parentStyleHint, readonly, required, tabindex, title, trim, validate, value
 
Fields inherited from class org.apache.click.control.AbstractControl
actionListener, attributes, behaviors, headElements, listener, listenerMethod, messages, name, parent, styles
 
Fields inherited from interface org.apache.click.Control
CONTROL_MESSAGES
 
Constructor Summary
NumberField()
          Create a NumberField with no name defined.
NumberField(String name)
          Construct a NumberField with the given name.
NumberField(String name, boolean required)
          Construct a NumberField with the given name and required status.
NumberField(String name, String label)
          Construct a NumberField with the given name and label.
NumberField(String name, String label, boolean required)
          Construct a NumberField with the given name, label and required status.
NumberField(String name, String label, int size)
          Construct the NumberField with the given name, label and size.
NumberField(String name, String label, int size, boolean required)
          Construct the NumberField with the given name, label, size and required status.
 
Method Summary
 List<Element> getHeadElements()
          Returns the NumberField HTML HEAD elements for the click/extras-control.js resource.
 double getMaxValue()
          Return the maximum valid double field value.
 double getMinValue()
          Return the minimum valid double field value.
 Number getNumber()
          Return the field Number value, or null if value was empty or a parsing error occurred.
 NumberFormat getNumberFormat()
          Return the NumberFormat for formatting/parsing the field value.
 String getPattern()
          Return the number pattern used for formatting and parsing.
 String getValidationJavaScript()
          Return the field JavaScript client side validation function.
 Object getValueObject()
          Return the field Number value, or null if value was empty or a parsing error occurred.
 void setMaxValue(double value)
          Set the maximum valid double field value.
 void setMinValue(double value)
          Set the minimum valid double field value.
 void setNumber(Number number)
          Set the Number value of the field.
 void setNumberFormat(NumberFormat format)
          Set the NumberFormat for this field.
 void setPattern(String pattern)
          Set the number pattern used for formatting and parsing.
 void setValueObject(Object object)
          Set the Number value of the field using the given object.
 void validate()
          Validates the NumberField request submission.
 
Methods inherited from class org.apache.click.control.TextField
getControlSizeEst, getMaxLength, getMinLength, getSize, getTag, getType, render, setMaxLength, setMinLength, setSize
 
Methods inherited from class org.apache.click.control.Field
bindRequestValue, getError, getErrorLabel, getFocus, getFocusJavaScript, getForm, getHelp, getId, getLabel, getLabelStyle, getLabelStyleClass, getParentStyleClassHint, getParentStyleHint, getRequestValue, getState, getTabIndex, getTextAlign, getTitle, getValidate, getValue, getWidth, isDisabled, isHidden, isReadonly, isRequired, isTrim, isValid, onProcess, removeState, renderTagBegin, restoreState, saveState, setDisabled, setError, setErrorMessage, setErrorMessage, setFocus, setForm, setHelp, setLabel, setLabelStyle, setLabelStyleClass, setListener, setParent, setParentStyleClassHint, setParentStyleHint, setReadonly, setRequired, setState, setTabIndex, setTextAlign, setTitle, setTrim, setValidate, setValue, setWidth
 
Methods inherited from class org.apache.click.control.AbstractControl
addBehavior, addStyleClass, appendAttributes, dispatchActionEvent, getActionListener, getAttribute, getAttributes, getBehaviors, getContext, getHtmlImports, getMessage, getMessage, getMessages, getName, getPage, getParent, getStyle, getStyles, hasAttribute, hasAttributes, hasBehaviors, hasStyles, isAjaxTarget, onDeploy, onDestroy, onInit, onRender, removeBehavior, removeStyleClass, renderTagEnd, setActionListener, setAttribute, setId, setName, setStyle, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

VALIDATE_NUMBER_FIELD_FUNCTION

protected static final String VALIDATE_NUMBER_FIELD_FUNCTION
The field validation JavaScript function template. The function template arguments are:

See Also:
Constant Field Values

maxvalue

protected double maxvalue
The maximum field value.


minvalue

protected double minvalue
The minimum field value.


numberFormat

protected NumberFormat numberFormat
The NumberFormat for formatting the output.


pattern

protected String pattern
The decimal pattern to use for a NumberFormat.

Constructor Detail

NumberField

public NumberField(String name)
Construct a NumberField with the given name.

Parameters:
name - the name of the field

NumberField

public NumberField(String name,
                   String label)
Construct a NumberField with the given name and label.

Parameters:
name - the name of the field
label - the label of the field

NumberField

public NumberField(String name,
                   boolean required)
Construct a NumberField with the given name and required status.

Parameters:
name - the name of the field
required - the field required status

NumberField

public NumberField(String name,
                   String label,
                   boolean required)
Construct a NumberField with the given name, label and required status.

Parameters:
name - the name of the field
label - the label of the field
required - the field required status

NumberField

public NumberField(String name,
                   String label,
                   int size)
Construct the NumberField with the given name, label and size.

Parameters:
name - the name of the field
label - the label of the field
size - the size of the field

NumberField

public NumberField(String name,
                   String label,
                   int size,
                   boolean required)
Construct the NumberField with the given name, label, size and required status.

Parameters:
name - the name of the field
label - the label of the field
size - the size of the field
required - the field required status

NumberField

public NumberField()
Create a NumberField with no name defined.

Please note the control's name must be defined before it is valid.

Method Detail

getMaxValue

public double getMaxValue()
Return the maximum valid double field value.

Returns:
the maximum valid double field value

setMaxValue

public void setMaxValue(double value)
Set the maximum valid double field value.

Parameters:
value - the maximum valid double field value

setMinValue

public void setMinValue(double value)
Set the minimum valid double field value.

Parameters:
value - the minimum valid double field value

getMinValue

public double getMinValue()
Return the minimum valid double field value.

Returns:
the minimum valid double field value.

getNumber

public Number getNumber()
Return the field Number value, or null if value was empty or a parsing error occurred.

Returns:
the field Number value

setNumber

public void setNumber(Number number)
Set the Number value of the field.

Parameters:
number - the field number value to set

getNumberFormat

public NumberFormat getNumberFormat()
Return the NumberFormat for formatting/parsing the field value. If no NumberFormat has been set before, the NumberFormat for the requests locale is used. If this format is a DecimalNumberFormat the pattern is applied to it.

This method is used through-out this class to obtain the NumberFormat. By overriding this method full control is given onto which NumberFormat is used for formatting, parsing and validating.

Returns:
the NumberFormat to format/parse the field's value

setNumberFormat

public void setNumberFormat(NumberFormat format)
Set the NumberFormat for this field.

By default the format of the request locale is used and the pattern is set. If the getPattern() is set then the pattern will be applied to the new Format if it a DecimalFormat.

Parameters:
format - the number format

getPattern

public String getPattern()
Return the number pattern used for formatting and parsing.

Returns:
the number pattern used for formatting and parsing

setPattern

public void setPattern(String pattern)
Set the number pattern used for formatting and parsing.

By default the pattern is null and the default number pattern of the context locale is used. If set the pattern will be also applied to an already set NumberFormat.

Parameters:
pattern - the pattern used for formatting and parsing

getValueObject

public Object getValueObject()
Return the field Number value, or null if value was empty or a parsing error occurred.

Overrides:
getValueObject in class Field
Returns:
the Number object representation of the Field value

setValueObject

public void setValueObject(Object object)
Set the Number value of the field using the given object.

Overrides:
setValueObject in class Field
Parameters:
object - the object value to set

getHeadElements

public List<Element> getHeadElements()
Returns the NumberField HTML HEAD elements for the click/extras-control.js resource.

Specified by:
getHeadElements in interface Control
Overrides:
getHeadElements in class AbstractControl
Returns:
the HTML HEAD elements for the control
See Also:
Control.getHeadElements()

getValidationJavaScript

public String getValidationJavaScript()
Return the field JavaScript client side validation function.

The function name must follow the format validate_[id], where the id is the DOM element id of the fields focusable HTML element, to ensure the function has a unique name.

Overrides:
getValidationJavaScript in class TextField
Returns:
the field JavaScript client side validation function

validate

public void validate()
Validates the NumberField request submission. If the value entered by the user can be parsed by the NumberFormat the string value of this Field (Field.getValue()) is set to the formatted value of the user input.

A field error message is displayed if a validation error occurs. These messages are defined in the resource bundle:

  • /click-control.properties
    • field-required-error
    • number-format-error
    • number-maxvalue-error
    • number-minvalue-error

Overrides:
validate in class TextField