org.primefaces.component.selectbooleancheckbox.SelectBooleanCheckboxRenderer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of primefaces Show documentation
Show all versions of primefaces Show documentation
PrimeFaces is one of the most popular UI libraries in Java EE Ecosystem and widely used by software companies, world renowned brands, banks, financial institutions, insurance companies, universities and more.
/**
* The MIT License
*
* Copyright (c) 2009-2019 PrimeTek
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.primefaces.component.selectbooleancheckbox;
import java.io.IOException;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.faces.convert.ConverterException;
import org.primefaces.renderkit.InputRenderer;
import org.primefaces.util.ComponentUtils;
import org.primefaces.util.HTML;
import org.primefaces.util.WidgetBuilder;
public class SelectBooleanCheckboxRenderer extends InputRenderer {
@Override
public void decode(FacesContext context, UIComponent component) {
SelectBooleanCheckbox checkbox = (SelectBooleanCheckbox) component;
if (!shouldDecode(checkbox)) {
return;
}
decodeBehaviors(context, checkbox);
String clientId = checkbox.getClientId(context);
String submittedValue = context.getExternalContext().getRequestParameterMap().get(clientId + "_input");
if (submittedValue != null && isChecked(submittedValue)) {
checkbox.setSubmittedValue(true);
}
else {
checkbox.setSubmittedValue(false);
}
}
protected boolean isChecked(String value) {
return value.equalsIgnoreCase("on") || value.equalsIgnoreCase("yes") || value.equalsIgnoreCase("true");
}
@Override
public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
SelectBooleanCheckbox checkbox = (SelectBooleanCheckbox) component;
encodeMarkup(context, checkbox);
encodeScript(context, checkbox);
}
protected void encodeMarkup(FacesContext context, SelectBooleanCheckbox checkbox) throws IOException {
ResponseWriter writer = context.getResponseWriter();
String clientId = checkbox.getClientId(context);
boolean checked = Boolean.parseBoolean(ComponentUtils.getValueToRender(context, checkbox));
boolean disabled = checkbox.isDisabled();
String title = checkbox.getTitle();
String style = checkbox.getStyle();
String styleClass = checkbox.getStyleClass();
styleClass = styleClass == null ? HTML.CHECKBOX_CLASS : HTML.CHECKBOX_CLASS + " " + styleClass;
styleClass = "ui-selectbooleancheckbox " + styleClass;
writer.startElement("div", checkbox);
writer.writeAttribute("id", clientId, "id");
writer.writeAttribute("class", styleClass, "styleClass");
if (style != null) {
writer.writeAttribute("style", style, "style");
}
if (title != null) {
writer.writeAttribute("title", title, "title");
}
encodeInput(context, checkbox, clientId, checked);
encodeOutput(context, checkbox, checked, disabled);
encodeItemLabel(context, checkbox, clientId);
writer.endElement("div");
}
protected void encodeInput(FacesContext context, SelectBooleanCheckbox checkbox, String clientId, boolean checked) throws IOException {
ResponseWriter writer = context.getResponseWriter();
String inputId = clientId + "_input";
writer.startElement("div", checkbox);
writer.writeAttribute("class", "ui-helper-hidden-accessible", null);
writer.startElement("input", null);
writer.writeAttribute("id", inputId, "id");
writer.writeAttribute("name", inputId, null);
writer.writeAttribute("type", "checkbox", null);
writer.writeAttribute("autocomplete", "off", null);
writer.writeAttribute(HTML.ARIA_HIDDEN, "true", null);
if (checked) {
writer.writeAttribute("checked", "checked", null);
writer.writeAttribute(HTML.ARIA_CHECKED, "true", null);
}
else {
writer.writeAttribute(HTML.ARIA_CHECKED, "false", null);
}
renderValidationMetadata(context, checkbox);
renderAccessibilityAttributes(context, checkbox);
renderPassThruAttributes(context, checkbox, HTML.TAB_INDEX);
renderOnchange(context, checkbox);
renderDomEvents(context, checkbox, HTML.BLUR_FOCUS_EVENTS);
writer.endElement("input");
writer.endElement("div");
}
protected void encodeOutput(FacesContext context, SelectBooleanCheckbox checkbox, boolean checked, boolean disabled) throws IOException {
ResponseWriter writer = context.getResponseWriter();
String styleClass = HTML.CHECKBOX_BOX_CLASS;
styleClass = checked ? styleClass + " ui-state-active" : styleClass;
styleClass = !checkbox.isValid() ? styleClass + " ui-state-error" : styleClass;
styleClass = disabled ? styleClass + " ui-state-disabled" : styleClass;
String iconClass = checked ? HTML.CHECKBOX_CHECKED_ICON_CLASS : HTML.CHECKBOX_UNCHECKED_ICON_CLASS;
writer.startElement("div", null);
writer.writeAttribute("class", styleClass, null);
writer.startElement("span", null);
writer.writeAttribute("class", iconClass, null);
writer.endElement("span");
writer.endElement("div");
}
protected void encodeItemLabel(FacesContext context, SelectBooleanCheckbox checkbox, String clientId) throws IOException {
String label = checkbox.getItemLabel();
if (label != null) {
ResponseWriter writer = context.getResponseWriter();
writer.startElement("span", null);
writer.writeAttribute("class", HTML.CHECKBOX_LABEL_CLASS, null);
if (checkbox.isEscape()) {
writer.writeText(label, "itemLabel");
}
else {
writer.write(label);
}
writer.endElement("span");
}
}
protected void encodeScript(FacesContext context, SelectBooleanCheckbox checkbox) throws IOException {
String clientId = checkbox.getClientId(context);
WidgetBuilder wb = getWidgetBuilder(context);
wb.init("SelectBooleanCheckbox", checkbox.resolveWidgetVar(), clientId).finish();
}
@Override
public Object getConvertedValue(FacesContext context, UIComponent component, Object submittedValue) throws ConverterException {
return ((submittedValue instanceof Boolean) ? submittedValue : Boolean.valueOf(submittedValue.toString()));
}
@Override
public String getHighlighter() {
return "booleanchkbox";
}
}