Class ConvertibleComponent
- java.lang.Object
-
- javax.faces.component.UIComponent
-
- javax.faces.component.UIComponentBase
-
- javax.faces.component.UIOutput
-
- javax.faces.component.UIInput
-
- de.xima.fc.gui.component.convertible.ConvertibleComponent
-
- All Implemented Interfaces:
EventListener
,javax.faces.component.EditableValueHolder
,javax.faces.component.NamingContainer
,javax.faces.component.PartialStateHolder
,javax.faces.component.StateHolder
,javax.faces.component.TransientStateHolder
,javax.faces.component.ValueHolder
,javax.faces.event.ComponentSystemEventListener
,javax.faces.event.FacesListener
,javax.faces.event.SystemEventListenerHolder
public class ConvertibleComponent extends javax.faces.component.UIInput implements javax.faces.component.NamingContainer
Can be used together with a <xi:convertible> component. Converts between an external value used by the bean and the value that is used for the actual components. For example, consider the following xhtml. The bean has one string field for the date, the xhtml has got tree separate editors for the year, month, and date.<xi:convertible id="dateSelect" var="items" varOptions="options" value="#{dateBean.date}" valueConverter="#{dateBean.dateConverter}"> days: #{options} <div> <p:spinner id="year" value="#{items.year}" min="0" max="3000" stepFactor="1"/> <p:spinner id="month" value="#{items.month}" min="0" max="12" stepFactor="1"> <p:ajax process="day month" update="day month" listener="#{dateBean.updateDays}" /> </p:spinner> <p:spinner id="day" value="#{items.day}" min="1" max="#{options.days}" stepFactor="1"/> </div> </xi:convertible>
The converter might look like this:class ConverterImpl implements IComponentValueConverter { @Override public Map<String, Object> externalToComponentValue(Object value) { Map<String, Object> map = new HashMap<>(); if (value != null) { String[] values = value.toString().split("_"); if (values.length >= 1) map.put("year", XNumberUtils.toInt(values[0], 1)); if (values.length >= 2) map.put("month", XNumberUtils.toInt(values[1], 1)); if (values.length >= 3) map.put("day", XNumberUtils.toInt(values[2], 1)); } return map; } @Override public Object componentToExternalValue(Map<String, Object> componentValues) { return componentValues.get("year") + "_" + componentValues.get("month") + "_" + componentValues.get("day"); } @Override public Map<String, Object> createOptions(Map<String, Object> componentValues) { Map<String, Object> options = new HashMap<>(); Integer month = (Integer)componentValues.get("month"); if (month == null) options.put("days", 31); else if (month == 2) options.put("days", 28); else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) options.put("days", 31); else options.put("days", 30); return options; } }
And the method to update the number of days dynamically as the user changes the values:public void update(AjaxBehaviorEvent event) { ConvertibleComponent c = (ConvertibleComponent)event.getComponent().getNamingContainer(); final int month = XNumberUtils.toInt((String)c.getComponents().get("month").getValue(), 0); Map<String, Object> options = new HashMap<>(); if (month == 2) options.put("days", 28); else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) options.put("days", 31); else if (month >= 1 && month <= 12) options.put("days", 30); else options.put("days", 31); c.updateOptions(options); }
Please note that JSF usually attempts to detect the type of the bean properties automatically via reflection. If you use this component, the properties are entries in a map. This results in JSF only reporting typeObject
for the properties. Some components such asInputText
work just fine. For other components such asDatePicker
, you can explicitly define a converter that converts between the string representation and the object (e.g.Date
). Alternatively, you can also use a customIConvertibleMap
with getters and setters of the correct type.- Author:
- XIMA MEDIA GmbH
-
-
Field Summary
-
Fields inherited from class javax.faces.component.UIInput
ALWAYS_PERFORM_VALIDATION_WHEN_REQUIRED_IS_TRUE, COMPONENT_FAMILY, COMPONENT_TYPE, CONVERSION_MESSAGE_ID, EMPTY_STRING_AS_NULL_PARAM_NAME, REQUIRED_MESSAGE_ID, UPDATE_MESSAGE_ID, VALIDATE_EMPTY_FIELDS_PARAM_NAME
-
-
Constructor Summary
Constructors Constructor Description ConvertibleComponent()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
encodeBegin(javax.faces.context.FacesContext context)
Set the selected and available values of the day, month and year fields based on the model.void
encodeChildren(javax.faces.context.FacesContext context)
protected Object
getConvertedValue(javax.faces.context.FacesContext context, Object submittedValue)
String
getFamily()
Object
getSubmittedValue()
boolean
isRequired()
void
processDecodes(javax.faces.context.FacesContext context)
void
processRestoreState(javax.faces.context.FacesContext context, Object state)
Object
processSaveState(javax.faces.context.FacesContext context)
void
processUpdates(javax.faces.context.FacesContext context)
void
processValidators(javax.faces.context.FacesContext context)
void
updateOptions(IConvertibleMap<Object> options)
Sets the options of this convertible to the given value.boolean
visitTree(javax.faces.component.visit.VisitContext context, javax.faces.component.visit.VisitCallback callback)
-
Methods inherited from class javax.faces.component.UIInput
addValidator, addValueChangeListener, clearInitialState, compareValues, decode, getConverterMessage, getRequiredMessage, getValidator, getValidatorMessage, getValidators, getValue, getValueChangeListener, getValueChangeListeners, isEmpty, isImmediate, isLocalValueSet, isValid, markInitialState, removeValidator, removeValueChangeListener, resetValue, restoreState, saveState, setConverterMessage, setImmediate, setLocalValueSet, setRequired, setRequiredMessage, setSubmittedValue, setValid, setValidator, setValidatorMessage, setValue, setValueChangeListener, updateModel, validate, validateValue
-
Methods inherited from class javax.faces.component.UIOutput
getConverter, getLocalValue, setConverter
-
Methods inherited from class javax.faces.component.UIComponentBase
addClientBehavior, addFacesListener, broadcast, encodeEnd, findComponent, getAttributes, getChildCount, getChildren, getClientBehaviors, getClientId, getDefaultEventName, getEventNames, getFacesContext, getFacesListeners, getFacet, getFacetCount, getFacets, getFacetsAndChildren, getId, getListenersForEventClass, getParent, getPassThroughAttributes, getRenderer, getRendererType, getRendersChildren, getValueBinding, invokeOnComponent, isRendered, isTransient, queueEvent, removeFacesListener, restoreAttachedState, saveAttachedState, setId, setParent, setRendered, setRendererType, setTransient, setValueBinding, subscribeToEvent, unsubscribeFromEvent
-
Methods inherited from class javax.faces.component.UIComponent
encodeAll, getClientId, getCompositeComponentParent, getContainerClientId, getCurrentComponent, getCurrentCompositeComponent, getNamingContainer, getPassThroughAttributes, getResourceBundleMap, getStateHelper, getStateHelper, getTransientStateHelper, getTransientStateHelper, getValueExpression, initialStateMarked, isCompositeComponent, isInView, isVisitable, popComponentFromEL, processEvent, pushComponentToEL, restoreTransientState, saveTransientState, setInView, setValueExpression
-
-
-
-
Method Detail
-
encodeBegin
public void encodeBegin(javax.faces.context.FacesContext context) throws IOException
Set the selected and available values of the day, month and year fields based on the model.- Overrides:
encodeBegin
in classjavax.faces.component.UIComponentBase
- Throws:
IOException
-
encodeChildren
public void encodeChildren(javax.faces.context.FacesContext context) throws IOException
- Overrides:
encodeChildren
in classjavax.faces.component.UIComponentBase
- Throws:
IOException
-
getFamily
public String getFamily()
- Overrides:
getFamily
in classjavax.faces.component.UIInput
-
getSubmittedValue
public Object getSubmittedValue()
- Specified by:
getSubmittedValue
in interfacejavax.faces.component.EditableValueHolder
- Overrides:
getSubmittedValue
in classjavax.faces.component.UIInput
-
isRequired
public boolean isRequired()
- Specified by:
isRequired
in interfacejavax.faces.component.EditableValueHolder
- Overrides:
isRequired
in classjavax.faces.component.UIInput
-
processDecodes
public void processDecodes(javax.faces.context.FacesContext context)
- Overrides:
processDecodes
in classjavax.faces.component.UIInput
-
processRestoreState
public void processRestoreState(javax.faces.context.FacesContext context, Object state)
- Overrides:
processRestoreState
in classjavax.faces.component.UIComponentBase
-
processSaveState
public Object processSaveState(javax.faces.context.FacesContext context)
- Overrides:
processSaveState
in classjavax.faces.component.UIComponentBase
-
processUpdates
public void processUpdates(javax.faces.context.FacesContext context)
- Overrides:
processUpdates
in classjavax.faces.component.UIInput
-
processValidators
public void processValidators(javax.faces.context.FacesContext context)
- Overrides:
processValidators
in classjavax.faces.component.UIInput
-
updateOptions
public void updateOptions(IConvertibleMap<Object> options)
Sets the options of this convertible to the given value.- Parameters:
options
- The new options for this convertible, accessible by the children.
-
visitTree
public boolean visitTree(javax.faces.component.visit.VisitContext context, javax.faces.component.visit.VisitCallback callback)
- Overrides:
visitTree
in classjavax.faces.component.UIComponent
-
-