org.primefaces.extensions.component.inputnumber.InputNumberRenderer Maven / Gradle / Ivy
/*
* Copyright 2011-2015 PrimeFaces Extensions
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* $Id$
*/
package org.primefaces.extensions.component.inputnumber;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import org.primefaces.component.inputtext.InputText;
import org.primefaces.extensions.util.ExtWidgetBuilder;
import org.primefaces.renderkit.InputRenderer;
import org.primefaces.util.ComponentUtils;
import org.primefaces.util.HTML;
/**
* InputNumberRenderer
*
* @author Mauricio Fenoglio / last modified by $Author$
* @version $Revision$
* @since 0.3
*/
public class InputNumberRenderer extends InputRenderer {
@Override
public Object getConvertedValue(final FacesContext context, final UIComponent component, final Object submittedValue)
throws ConverterException {
String submittedValueString = (String) submittedValue;
if (ComponentUtils.isValueBlank(submittedValueString)) {
return null;
}
Converter converter = ComponentUtils.getConverter(context, component);
if (converter != null) {
return converter.getAsObject(context, component, submittedValueString);
}
return submittedValue;
}
@Override
public void decode(final FacesContext context, final UIComponent component) {
InputNumber inputNumber = (InputNumber) component;
if (inputNumber.isDisabled() || inputNumber.isReadonly()) {
return;
}
decodeBehaviors(context, inputNumber);
String inputId = inputNumber.getClientId(context) + "_hinput";
String submittedValue = context.getExternalContext().getRequestParameterMap().get(inputId);
if (submittedValue != null) {
inputNumber.setSubmittedValue(submittedValue);
}
}
@Override
public void encodeEnd(final FacesContext context, final UIComponent component) throws IOException {
InputNumber inputNumber = (InputNumber) component;
encodeMarkup(context, inputNumber);
encodeScript(context, inputNumber);
}
protected void encodeMarkup(final FacesContext context, final InputNumber inputNumber) throws IOException {
ResponseWriter writer = context.getResponseWriter();
String clientId = inputNumber.getClientId(context);
String styleClass = inputNumber.getStyleClass();
styleClass = styleClass == null ? InputNumber.INPUTNUMBER_CLASS : InputNumber.INPUTNUMBER_CLASS + " " + styleClass;
writer.startElement("span", null);
writer.writeAttribute("id", clientId, null);
writer.writeAttribute("class", styleClass, "styleClass");
if (inputNumber.getStyle() != null) {
writer.writeAttribute("style", inputNumber.getStyle(), "style");
}
encodeOutput(context, inputNumber, clientId);
encodeInput(context, inputNumber, clientId);
writer.endElement("span");
}
protected void encodeInput(final FacesContext context, final InputNumber inputNumber, final String clientId) throws IOException {
ResponseWriter writer = context.getResponseWriter();
String inputId = clientId + "_hinput";
writer.startElement("input", null);
writer.writeAttribute("id", inputId, null);
writer.writeAttribute("name", inputId, null);
writer.writeAttribute("type", "hidden", null);
writer.writeAttribute("autocomplete", "off", null);
if (inputNumber.getOnchange() != null) {
writer.writeAttribute("onchange", inputNumber.getOnchange(), null);
}
writer.endElement("input");
}
protected void encodeOutput(final FacesContext context, final InputNumber inputNumber, final String clientId) throws IOException {
ResponseWriter writer = context.getResponseWriter();
String inputId = clientId + "_input";
String defaultClass = InputText.STYLE_CLASS + " pe-inputNumber";
defaultClass = inputNumber.isValid() ? defaultClass : defaultClass + " ui-state-error";
defaultClass = !inputNumber.isDisabled() ? defaultClass : defaultClass + " ui-state-disabled";
writer.startElement("input", null);
writer.writeAttribute("id", inputId, null);
writer.writeAttribute("name", inputId, null);
writer.writeAttribute("type", inputNumber.getType(), null);
renderPassThruAttributes(context, inputNumber, HTML.INPUT_TEXT_ATTRS_WITHOUT_EVENTS);
renderDomEvents(context, inputNumber, HTML.INPUT_TEXT_EVENTS);
if (inputNumber.isReadonly()) {
writer.writeAttribute("readonly", "readonly", "readonly");
}
if (inputNumber.isDisabled()) {
writer.writeAttribute("disabled", "disabled", "disabled");
}
writer.writeAttribute("class", defaultClass, "");
writer.endElement("input");
}
protected void encodeScript(final FacesContext context, final InputNumber inputNumber) throws IOException {
Object value = inputNumber.getValue();
String valueToRender = ComponentUtils.getValueToRender(context, inputNumber, value);
if (valueToRender == null) {
valueToRender = "";
}
ExtWidgetBuilder wb = ExtWidgetBuilder.get(context);
wb.initWithDomReady(InputNumber.class.getSimpleName(), inputNumber.resolveWidgetVar(), inputNumber.getClientId());
wb.attr("disabled", inputNumber.isDisabled())
.attr("valueToRender", formatForPlugin(valueToRender, inputNumber, value));
String metaOptions = getOptions(inputNumber);
if (!metaOptions.isEmpty()) {
wb.nativeAttr("pluginOptions", metaOptions);
}
wb.finish();
}
private String getOptions(final InputNumber inputNumber) {
String decimalSeparator = inputNumber.getDecimalSeparator();
String thousandSeparator = inputNumber.getThousandSeparator();
String symbol = inputNumber.getSymbol();
String symbolPosition = inputNumber.getSymbolPosition();
String minValue = inputNumber.getMinValue();
String maxValue = inputNumber.getMaxValue();
String roundMethod = inputNumber.getRoundMethod();
String decimalPlaces = inputNumber.getDecimalPlaces();
String emptyValue = inputNumber.getEmptyValue();
String options = "";
options += decimalSeparator.isEmpty() ? "" : "aDec:\"" + escapeText(decimalSeparator) + "\",";
//empty thousandSeparator must be explicity defined.
options += thousandSeparator.isEmpty() ? "aSep:''," : "aSep:\"" + escapeText(thousandSeparator) + "\",";
options += symbol.isEmpty() ? "" : "aSign:\"" + escapeText(symbol) + "\",";
options += symbolPosition.isEmpty() ? "" : "pSign:\"" + escapeText(symbolPosition) + "\",";
options += minValue.isEmpty() ? "" : "vMin:\"" + escapeText(minValue) + "\",";
options += maxValue.isEmpty() ? "" : "vMax:\"" + escapeText(maxValue) + "\",";
options += roundMethod.isEmpty() ? "" : "mRound:\"" + escapeText(roundMethod) + "\",";
options += decimalPlaces.isEmpty() ? "" : "mDec:\"" + escapeText(decimalPlaces) + "\",";
options += "wEmpty:\"" + escapeText(emptyValue) + "\",";
//if all options are empty return empty
if (options.isEmpty()) {
return "";
}
//delete the last comma
int lastInd = options.length() - 1;
if (options.charAt(lastInd) == ',') {
options = options.substring(0, lastInd);
}
return "{" + options + "}";
}
private String formatForPlugin(final String valueToRender, final InputNumber inputNumber, final Object value) {
if (valueToRender == null || valueToRender.isEmpty()) {
return "";
} else {
try {
Object objectToRender;
if (value instanceof BigDecimal) {
objectToRender = new BigDecimal(valueToRender);
} else {
objectToRender = new Double(valueToRender);
}
NumberFormat formatter = new DecimalFormat("#0.0#");
formatter.setRoundingMode(RoundingMode.FLOOR);
//autoNumeric jquery plugin max and min limits
formatter.setMinimumFractionDigits(15);
formatter.setMaximumFractionDigits(15);
formatter.setMaximumIntegerDigits(20);
String f = formatter.format(objectToRender);
//force to english decimal separator
f = f.replace(',', '.');
return f;
} catch (Exception e) {
throw new IllegalArgumentException(
"Error converting [" + valueToRender + "] to a double value;", e);
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy