org.apache.cocoon.forms.formmodel
Class CalculatedField

java.lang.Object
  extended by org.apache.cocoon.forms.formmodel.AbstractWidget
      extended by org.apache.cocoon.forms.formmodel.Field
          extended by org.apache.cocoon.forms.formmodel.CalculatedField
All Implemented Interfaces:
ValueChangedListenerEnabled, DataWidget, SelectableWidget, Widget, ValidationErrorAware, Locatable

public class CalculatedField
extends Field

A field which calculates its value.

A calculated field is useful to create fields containing a sum, or a percentage, or any other value derived from other fields in the form.

The way the field calculates its value is determined by its CalculatedFieldAlgorithm. The algorithm is also responsible for determining which other form widgets will trigger a value calculation for this field.

Version:
$Id: CalculatedField.html 1304258 2012-03-23 10:09:27Z ilgrosso $

Field Summary
 
Fields inherited from class org.apache.cocoon.forms.formmodel.Field
enteredValue, required, selectionList, SUGGESTED_LABEL_ATTR, suggestionList, validationError, value, VALUE_DISPLAY_PARSE_ERROR, VALUE_DISPLAY_VALIDATION, VALUE_PARSE_ERROR, VALUE_PARSED, VALUE_UNPARSED, VALUE_VALIDATED, VALUE_VALIDATING, valueState
 
Fields inherited from class org.apache.cocoon.forms.formmodel.AbstractWidget
wasValid
 
Fields inherited from interface org.apache.cocoon.forms.formmodel.Widget
PATH_SEPARATOR
 
Constructor Summary
protected CalculatedField(CalculatedFieldDefinition definition)
           
 
Method Summary
protected  Object convert(Object ret, Datatype datatype)
          Tries to convert the return value of the algorithm to the right value for this field datatype.
 CalculatedFieldAlgorithm getAlgorithm()
           
 Object getValue()
          Get the value of a widget.
 void initialize()
          Called after widget's environment has been setup, to allow for any contextual initalization, such as looking up case widgets for union widgets.
protected  void installHandlers()
          Installs handlers on other widgets.
protected  void readFromRequest(String newEnteredValue)
           
protected  Object recalculate()
          Calls the algorithm to perform a recaulculation.
 
Methods inherited from class org.apache.cocoon.forms.formmodel.Field
addValueChangedListener, applyWhitespaceTrim, broadcastEvent, generateItemSaxFragment, getDatatype, getDefinition, getFieldDefinition, getSuggestionLabel, getSuggestionList, getValidationError, getXMLElementAttributes, getXMLElementName, hasValueChangedListeners, isRequired, isSuggested, readFromRequest, removeValueChangedListener, setRequired, setSelectionList, setSelectionList, setSelectionList, setSuggestionLabel, setSuggestionList, setSuggestionList, setValidationError, setValue, validate
 
Methods inherited from class org.apache.cocoon.forms.formmodel.AbstractWidget
addValidator, generateDisplayData, generateLabel, generateSaxFragment, getAttribute, getChild, getCombinedState, getForm, getFullName, getId, getLocation, getName, getParent, getRequestParameterName, getState, getWidget, isValid, lookupWidget, removeAttribute, removeValidator, setAttribute, setParent, setState, toString, widgetNameChanged
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.apache.cocoon.forms.formmodel.Widget
addValidator, generateLabel, generateSaxFragment, getAttribute, getCombinedState, getForm, getFullName, getId, getLocation, getName, getParent, getRequestParameterName, getState, getWidget, isValid, lookupWidget, removeAttribute, removeValidator, setAttribute, setParent, setState
 

Constructor Detail

CalculatedField

protected CalculatedField(CalculatedFieldDefinition definition)
Parameters:
definition -
Method Detail

initialize

public void initialize()
Description copied from class: AbstractWidget
Called after widget's environment has been setup, to allow for any contextual initalization, such as looking up case widgets for union widgets.

Specified by:
initialize in interface Widget
Overrides:
initialize in class Field

installHandlers

protected void installHandlers()
Installs handlers on other widgets. This both forces other widget to submit the form when their values change, and also gives this field a good optimization on calls to its algorithm.


readFromRequest

protected void readFromRequest(String newEnteredValue)
Overrides:
readFromRequest in class Field

getValue

public Object getValue()
Description copied from interface: Widget
Get the value of a widget.

Not all widgets do have a value (notably ContainerWidgets, but this method is provided here as a convenience to ease writing and avoiding casts.

Specified by:
getValue in interface Widget
Overrides:
getValue in class Field
Returns:
the value of the widget.

recalculate

protected Object recalculate()
Calls the algorithm to perform a recaulculation.

Returns:
The calculated value for this field.

convert

protected Object convert(Object ret,
                         Datatype datatype)
                  throws Exception
Tries to convert the return value of the algorithm to the right value for this field datatype.

Parameters:
ret - The return value fo the algorithm.
datatype - The target datatype.
Returns:
A converted value, or the given ret value if no conversion was possible.
Throws:
Exception

getAlgorithm

public CalculatedFieldAlgorithm getAlgorithm()
Returns:
Returns the algorithm.


Copyright © 1999-2010 The Apache Software Foundation. All Rights Reserved.