net.sf.click.extras.control
Class NumberField

java.lang.Object
  extended bynet.sf.click.control.AbstractControl
      extended bynet.sf.click.control.Field
          extended bynet.sf.click.control.TextField
              extended bynet.sf.click.extras.control.NumberField
All Implemented Interfaces:
Control, Serializable
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 net.sf.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 formated number. The number returned from getNumber() is then the formatted number. It is not the orginal Number passed in. To circumvent formatting use setValue().

The NumberField uses a JavaScript onkeypress() doubleFilter() method to prevent users from entering invalid characters. To enable number key filtering reference $jsImports and $cssImports in your page template. For example:

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

See also W3C HTML reference INPUT

Author:
Christian Essl, Malcolm Edgar
See Also:
Serialized Form

Field Summary
static String HTML_IMPORTS
          The NumberField.js imports statement.
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 net.sf.click.control.TextField
maxLength, minLength, size, VALIDATE_TEXTFIELD_FUNCTION
 
Fields inherited from class net.sf.click.control.Field
disabled, error, focus, form, help, label, readonly, required, tabindex, title, validate, value
 
Fields inherited from class net.sf.click.control.AbstractControl
actionListener, attributes, listener, listenerMethod, messages, name, parent, styles
 
Fields inherited from interface net.sf.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
 String getHtmlImports()
          Return the HTML head import statements for the JavaScript (click/extras-control.js) file.
 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 occured.
 NumberFormat getNumberFormat()
          Return the NumberFormat for formating/parsing the field value.
 String getPattern()
          Return the number pattern used for formatting and parsing.
protected  String getRequestValue()
           
 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 occured.
 void onDeploy(ServletContext servletContext)
          Deploy the extras-control.js file to the click web directory when the application is initialized.
 void setMaxValue(double value)
          Set the maximum valid double field value.
 void setMinValue(double value)
          Set the miminum 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 net.sf.click.control.TextField
getControlSizeEst, getMaxLength, getMinLength, getSize, getTag, getType, render, setMaxLength, setMinLength, setSize
 
Methods inherited from class net.sf.click.control.Field
bindRequestValue, getError, getErrorLabel, getFocus, getFocusJavaScript, getForm, getHelp, getId, getLabel, getTabIndex, getTextAlign, getTitle, getValidate, getValue, getWidth, isDisabled, isHidden, isReadonly, isRequired, isValid, onInit, onProcess, setDisabled, setError, setErrorMessage, setErrorMessage, setErrorMessage, setErrorMessage, setFocus, setForm, setHelp, setLabel, setListener, setParent, setReadonly, setRequired, setTabIndex, setTextAlign, setTitle, setValidate, setValue, setWidth
 
Methods inherited from class net.sf.click.control.AbstractControl
addStyleClass, appendAttributes, getActionListener, getAttribute, getAttributes, getContext, getMessage, getMessage, getMessage, getMessages, getName, getPage, getParent, getStyle, getStyles, hasAttribute, hasAttributes, hasStyles, onDestroy, onRender, registerActionEvent, removeStyleClass, renderTagBegin, 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

HTML_IMPORTS

public static final String HTML_IMPORTS
The NumberField.js imports statement.

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 miminum valid double field value.

Parameters:
value - the miminum 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 occured.

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 formating/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 occured.

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.

Parameters:
object - the object value to set

getHtmlImports

public String getHtmlImports()
Return the HTML head import statements for the JavaScript (click/extras-control.js) file.

Returns:
the HTML head import statements for the JavaScript file
See Also:
Control.getHtmlImports()

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.

Returns:
the field JavaScript client side validation function

onDeploy

public void onDeploy(ServletContext servletContext)
Deploy the extras-control.js file to the click web directory when the application is initialized.

Parameters:
servletContext - the servlet context
See Also:
Control.onDeploy(ServletContext)

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


getRequestValue

protected String getRequestValue()
Returns:
the field's value from the request
See Also:
Field.getRequestValue()