Created
May 17, 2013 12:37
-
-
Save andreluizf/5598751 to your computer and use it in GitHub Desktop.
SelectCheckBox Menu prime faces NxM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<render-kit> | |
<renderer> | |
<component-family>org.primefaces.component</component-family> | |
<renderer-type>org.primefaces.component.SelectManyCheckboxRenderer</renderer-type> | |
<renderer-class>com.xkey.principal.renderers.MySelectManyCheckBoxRenderer</renderer-class> | |
</renderer> | |
</render-kit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.xkey.principal.renderers; | |
import java.io.IOException; | |
import java.util.List; | |
import javax.faces.component.UIComponent; | |
import javax.faces.component.UIInput; | |
import javax.faces.component.UINamingContainer; | |
import javax.faces.component.UISelectMany; | |
import javax.faces.context.FacesContext; | |
import javax.faces.context.ResponseWriter; | |
import javax.faces.convert.Converter; | |
import javax.faces.convert.ConverterException; | |
import javax.faces.model.SelectItem; | |
import org.primefaces.component.selectmanycheckbox.SelectManyCheckbox; | |
import org.primefaces.component.selectmanycheckbox.SelectManyCheckboxRenderer; | |
import org.primefaces.util.HTML; | |
public class MySelectManyCheckBoxRenderer extends SelectManyCheckboxRenderer { | |
@Override | |
public Object getConvertedValue(FacesContext context, UIComponent component, Object submittedValue) throws ConverterException { | |
return context.getRenderKit().getRenderer("javax.faces.SelectMany", "javax.faces.Checkbox").getConvertedValue(context, component, submittedValue); | |
} | |
@Override | |
public void encodeEnd(FacesContext context, UIComponent component) throws IOException { | |
SelectManyCheckbox checkbox = (SelectManyCheckbox) component; | |
encodeMarkup(context, checkbox); | |
encodeScript(context, checkbox); | |
} | |
@Override | |
protected void encodeMarkup(FacesContext context, SelectManyCheckbox checkbox) throws IOException { | |
ResponseWriter writer = context.getResponseWriter(); | |
String clientId = checkbox.getClientId(context); | |
String style = checkbox.getStyle(); | |
String styleClass = checkbox.getStyleClass(); | |
styleClass = styleClass == null ? SelectManyCheckbox.STYLE_CLASS : SelectManyCheckbox.STYLE_CLASS + " " + styleClass; | |
writer.startElement("table", checkbox); | |
writer.writeAttribute("id", clientId, "id"); | |
writer.writeAttribute("class", styleClass, "styleClass"); | |
if (style != null) { | |
writer.writeAttribute("style", style, "style"); | |
} | |
encodeSelectItems(context, checkbox); | |
writer.endElement("table"); | |
} | |
@Override | |
protected void encodeScript(FacesContext context, SelectManyCheckbox checkbox) throws IOException { | |
ResponseWriter writer = context.getResponseWriter(); | |
String clientId = checkbox.getClientId(context); | |
startScript(writer, clientId); | |
writer.write("PrimeFaces.cw('SelectManyCheckbox','" + checkbox.resolveWidgetVar() + "',{"); | |
writer.write("id:'" + clientId + "'"); | |
encodeClientBehaviors(context, checkbox); | |
writer.write("});"); | |
endScript(writer); | |
} | |
@Override | |
protected void encodeOptionInput(FacesContext context, SelectManyCheckbox checkbox, String id, String name, boolean checked, boolean disabled, String value) throws IOException { | |
ResponseWriter writer = context.getResponseWriter(); | |
writer.startElement("div", null); | |
writer.writeAttribute("class", "ui-helper-hidden-accessible", null); | |
writer.startElement("input", null); | |
writer.writeAttribute("id", id, null); | |
writer.writeAttribute("name", name, null); | |
writer.writeAttribute("type", "checkbox", null); | |
writer.writeAttribute("value", value, null); | |
if (checked) { | |
writer.writeAttribute("checked", "checked", null); | |
} | |
if (disabled) { | |
writer.writeAttribute("disabled", "disabled", null); | |
} | |
if (checkbox.getOnchange() != null) { | |
writer.writeAttribute("onchange", checkbox.getOnchange(), null); | |
} | |
writer.endElement("input"); | |
writer.endElement("div"); | |
} | |
@Override | |
protected void encodeOptionLabel(FacesContext context, SelectManyCheckbox checkbox, String containerClientId, SelectItem option, boolean disabled) throws IOException { | |
ResponseWriter writer = context.getResponseWriter(); | |
writer.startElement("label", null); | |
writer.writeAttribute("for", containerClientId, null); | |
if (disabled) { | |
writer.writeAttribute("class", "ui-state-disabled", null); | |
} | |
if (option.isEscape()) { | |
writer.writeText(option.getLabel(), null); | |
} else { | |
writer.write(option.getLabel()); | |
} | |
writer.endElement("label"); | |
} | |
@Override | |
protected void encodeOptionOutput(FacesContext context, SelectManyCheckbox checkbox, boolean checked, boolean disabled) throws IOException { | |
ResponseWriter writer = context.getResponseWriter(); | |
String boxClass = HTML.CHECKBOX_BOX_CLASS; | |
boxClass = checked ? boxClass + " ui-state-active" : boxClass; | |
boxClass = disabled ? boxClass + " ui-state-disabled" : boxClass; | |
boxClass = !checkbox.isValid() ? boxClass + " ui-state-error" : boxClass; | |
String iconClass = HTML.CHECKBOX_ICON_CLASS; | |
iconClass = checked ? iconClass + " " + HTML.CHECKBOX_CHECKED_ICON_CLASS : iconClass; | |
writer.startElement("div", null); | |
writer.writeAttribute("class", boxClass, null); | |
writer.startElement("span", null); | |
writer.writeAttribute("class", iconClass, null); | |
writer.endElement("span"); | |
writer.endElement("div"); | |
} | |
@Override | |
protected void encodeSelectItems(FacesContext context, SelectManyCheckbox checkbox) throws IOException { | |
ResponseWriter writer = context.getResponseWriter(); | |
List<SelectItem> selectItems = getSelectItems(context, checkbox); | |
Converter converter = checkbox.getConverter(); | |
Object values = getValues(checkbox); | |
Object submittedValues = getSubmittedValues(checkbox); | |
String layout = checkbox.getLayout(); | |
if (layout.startsWith("c")) { | |
String data = layout.replace("c", ""); | |
int columns = 1, columnCounter = 0; | |
try { | |
columns = Integer.valueOf(data); | |
if (columns <= 0) { | |
columns = 1; | |
} | |
} catch (NumberFormatException ex) { | |
} | |
int idx = -1; | |
for (SelectItem selectItem : selectItems) { | |
idx++; | |
if (columnCounter == columns) { | |
writer.startElement("tr", null); | |
} | |
encodeOption(context, checkbox, values, submittedValues, converter, selectItem, idx); | |
columnCounter++; | |
if (columnCounter == columns) { | |
writer.endElement("tr"); | |
columnCounter = 0; | |
} | |
} | |
} else { | |
boolean pageDirection = layout != null && layout.equals("pageDirection"); | |
int idx = -1; | |
for (SelectItem selectItem : selectItems) { | |
idx++; | |
if (pageDirection) { | |
writer.startElement("tr", null); | |
} | |
encodeOption(context, checkbox, values, submittedValues, converter, selectItem, idx); | |
if (pageDirection) { | |
writer.endElement("tr"); | |
} | |
} | |
} | |
} | |
@Override | |
protected void encodeOption(FacesContext context, UIInput component, Object values, Object submittedValues, Converter converter, SelectItem option, int idx) throws IOException { | |
ResponseWriter writer = context.getResponseWriter(); | |
SelectManyCheckbox checkbox = (SelectManyCheckbox) component; | |
String itemValueAsString = getOptionAsString(context, component, converter, option.getValue()); | |
String name = checkbox.getClientId(context); | |
String id = name + UINamingContainer.getSeparatorChar(context) + idx; | |
boolean disabled = option.isDisabled() || checkbox.isDisabled(); | |
Object valuesArray; | |
Object itemValue; | |
if (submittedValues != null) { | |
valuesArray = submittedValues; | |
itemValue = itemValueAsString; | |
} else { | |
valuesArray = values; | |
itemValue = option.getValue(); | |
} | |
boolean selected = isSelected(context, component, itemValue, valuesArray, converter); | |
if (option.isNoSelectionOption() && values != null && !selected) { | |
return; | |
} | |
writer.startElement("td", null); | |
writer.startElement("div", null); | |
writer.writeAttribute("class", HTML.CHECKBOX_CLASS, null); | |
encodeOptionInput(context, checkbox, id, name, selected, disabled, itemValueAsString); | |
encodeOptionOutput(context, checkbox, selected, disabled); | |
writer.endElement("div"); | |
writer.endElement("td"); | |
writer.startElement("td", null); | |
encodeOptionLabel(context, checkbox, id, option, disabled); | |
writer.endElement("td"); | |
} | |
@Override | |
protected String getSubmitParam(FacesContext context, UISelectMany selectMany) { | |
return selectMany.getClientId(context); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version='1.0' encoding='UTF-8' ?> | |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> | |
<html xmlns="http://www.w3.org/1999/xhtml" | |
xmlns:h="http://java.sun.com/jsf/html" | |
xmlns:p="http://primefaces.org/ui" | |
xmlns:f="http://java.sun.com/jsf/core"> | |
<head> | |
<title>Facelet Title</title> | |
</head> | |
<body> | |
<p:selectManyCheckbox layout="c3" > | |
<f:selectItem itemLabel="1" itemValue="1"/> | |
<f:selectItem itemLabel="2" itemValue="2"/> | |
<f:selectItem itemLabel="3" itemValue="3"/> | |
<f:selectItem itemLabel="4" itemValue="4"/> | |
<f:selectItem itemLabel="5" itemValue="5"/> | |
<f:selectItem itemLabel="6" itemValue="6"/> | |
<f:selectItem itemLabel="7" itemValue="7"/> | |
<f:selectItem itemLabel="8" itemValue="8"/> | |
<f:selectItem itemLabel="9" itemValue="9"/> | |
<f:selectItem itemLabel="10" itemValue="10"/> | |
</p:selectManyCheckbox> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment