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 type Object for
the properties. Some components such as InputText work just fine. For other components such as
DatePicker, you can explicitly define a converter that converts between the string representation and the
object (e.g. Date). Alternatively, you can also use a custom IConvertibleMap 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_NAMEFields inherited from class javax.faces.component.UIComponent
ATTRS_WITH_DECLARED_DEFAULT_VALUES, BEANINFO_KEY, bindings, COMPOSITE_COMPONENT_TYPE_KEY, COMPOSITE_FACET_NAME, CURRENT_COMPONENT, CURRENT_COMPOSITE_COMPONENT, FACETS_KEY, HONOR_CURRENT_COMPONENT_ATTRIBUTES_PARAM_NAME, VIEW_LOCATION_KEYFields inherited from interface javax.faces.component.NamingContainer
SEPARATOR_CHAR -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidencodeBegin(javax.faces.context.FacesContext context) Set the selected and available values of the day, month and year fields based on the model.voidencodeChildren(javax.faces.context.FacesContext context) protected ObjectgetConvertedValue(javax.faces.context.FacesContext context, Object submittedValue) booleanvoidprocessDecodes(javax.faces.context.FacesContext context) voidprocessRestoreState(javax.faces.context.FacesContext context, Object state) processSaveState(javax.faces.context.FacesContext context) voidprocessUpdates(javax.faces.context.FacesContext context) voidprocessValidators(javax.faces.context.FacesContext context) voidupdateOptions(IConvertibleMap<Object> options) Sets the options of this convertible to the given value.booleanvisitTree(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, validateValueMethods inherited from class javax.faces.component.UIOutput
getConverter, getLocalValue, setConverterMethods 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, unsubscribeFromEventMethods 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, setValueExpressionMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface javax.faces.component.ValueHolder
getConverter, getLocalValue, setConverter
-
Constructor Details
-
ConvertibleComponent
public ConvertibleComponent()
-
-
Method Details
-
encodeBegin
Set the selected and available values of the day, month and year fields based on the model.- Overrides:
encodeBeginin classjavax.faces.component.UIComponentBase- Throws:
IOException
-
encodeChildren
- Overrides:
encodeChildrenin classjavax.faces.component.UIComponentBase- Throws:
IOException
-
getFamily
- Overrides:
getFamilyin classjavax.faces.component.UIInput
-
getSubmittedValue
- Specified by:
getSubmittedValuein interfacejavax.faces.component.EditableValueHolder- Overrides:
getSubmittedValuein classjavax.faces.component.UIInput
-
isRequired
public boolean isRequired()- Specified by:
isRequiredin interfacejavax.faces.component.EditableValueHolder- Overrides:
isRequiredin classjavax.faces.component.UIInput
-
processDecodes
public void processDecodes(javax.faces.context.FacesContext context) - Overrides:
processDecodesin classjavax.faces.component.UIInput
-
processRestoreState
- Overrides:
processRestoreStatein classjavax.faces.component.UIComponentBase
-
processSaveState
- Overrides:
processSaveStatein classjavax.faces.component.UIComponentBase
-
processUpdates
public void processUpdates(javax.faces.context.FacesContext context) - Overrides:
processUpdatesin classjavax.faces.component.UIInput
-
processValidators
public void processValidators(javax.faces.context.FacesContext context) - Overrides:
processValidatorsin classjavax.faces.component.UIInput
-
updateOptions
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:
visitTreein classjavax.faces.component.UIComponent
-
getConvertedValue
- Overrides:
getConvertedValuein classjavax.faces.component.UIInput
-