public class ConvertibleComponent
extends javax.faces.component.UIInput
implements javax.faces.component.NamingContainer
<xi:convertible id="dateSelect" value="#{dateBean.date}"
valueConverter="#{dateBean.dateConverter}">
days: #{cc.options}
<div id="#{cc.id}">
<p:spinner id="year" binding="#{cc.components.year}" min="0" max="3000" stepFactor="1"/>
<p:spinner id="month" binding="#{cc.components.month}" min="0" max="12" stepFactor="1">
<p:ajax process="day month" update="day month" listener="#{dateBean.updateDays}" />
</p:spinner>
<p:spinner id="day" binding="#{cc.components.day}" min="1" max="#{cc.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);
}
COMPONENT_FAMILY, COMPONENT_TYPE, CONVERSION_MESSAGE_ID, REQUIRED_MESSAGE_ID, UPDATE_MESSAGE_ID, VALIDATE_EMPTY_FIELDS_PARAM_NAME| Constructor and Description |
|---|
ConvertibleComponent() |
| Modifier and Type | Method and 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.
|
Map<String,javax.faces.component.UIInput> |
getComponents() |
protected Object |
getConvertedValue(javax.faces.context.FacesContext context,
Object submittedValue)
Converts the submitted value to concrete
Date instance. |
String |
getFamily() |
Map<String,Object> |
getOptions() |
Object |
getSubmittedValue()
Returns the submitted value in dd-MM-yyyy format.
|
void |
updateOptions(Map<String,Object> options) |
addValidator, addValueChangeListener, clearInitialState, compareValues, decode, getConverterMessage, getRequiredMessage, getValidator, getValidatorMessage, getValidators, getValue, getValueChangeListener, getValueChangeListeners, isEmpty, isImmediate, isLocalValueSet, isRequired, isValid, markInitialState, processDecodes, processUpdates, processValidators, removeValidator, removeValueChangeListener, resetValue, restoreState, saveState, setConverterMessage, setImmediate, setLocalValueSet, setRequired, setRequiredMessage, setSubmittedValue, setValid, setValidator, setValidatorMessage, setValue, setValueChangeListener, updateModel, validate, validateValuegetConverter, getLocalValue, setConverteraddClientBehavior, addFacesListener, broadcast, encodeChildren, 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, processRestoreState, processSaveState, queueEvent, removeFacesListener, restoreAttachedState, saveAttachedState, setId, setParent, setRendered, setRendererType, setTransient, setValueBinding, subscribeToEvent, unsubscribeFromEventencodeAll, getClientId, getCompositeComponentParent, getContainerClientId, getCurrentComponent, getCurrentCompositeComponent, getNamingContainer, getPassThroughAttributes, getResourceBundleMap, getStateHelper, getStateHelper, getTransientStateHelper, getTransientStateHelper, getValueExpression, initialStateMarked, isCompositeComponent, isInView, isVisitable, popComponentFromEL, processEvent, pushComponentToEL, restoreTransientState, saveTransientState, setInView, setValueExpression, visitTreepublic String getFamily()
getFamily in class javax.faces.component.UIInputpublic void encodeBegin(javax.faces.context.FacesContext context)
throws IOException
encodeBegin in class javax.faces.component.UIComponentBaseIOExceptionpublic Object getSubmittedValue()
getSubmittedValue in interface javax.faces.component.EditableValueHoldergetSubmittedValue in class javax.faces.component.UIInputprotected Object getConvertedValue(javax.faces.context.FacesContext context, Object submittedValue)
Date instance.getConvertedValue in class javax.faces.component.UIInputCopyright © 2019 XIMA MEDIA GmbH. All rights reserved.