org.primefaces.component.autocomplete.AutoCompleteRenderer 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.
/**
* Copyright 2009-2018 PrimeTek.
*
* 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.
*/
package org.primefaces.component.autocomplete;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
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 javax.faces.event.PhaseId;
import org.primefaces.component.column.Column;
import org.primefaces.context.RequestContext;
import org.primefaces.event.AutoCompleteEvent;
import org.primefaces.expression.SearchExpressionFacade;
import org.primefaces.renderkit.InputRenderer;
import org.primefaces.util.ArrayUtils;
import org.primefaces.util.ComponentUtils;
import org.primefaces.util.HTML;
import org.primefaces.util.WidgetBuilder;
public class AutoCompleteRenderer extends InputRenderer {
@Override
public void decode(FacesContext context, UIComponent component) {
AutoComplete ac = (AutoComplete) component;
String clientId = ac.getClientId(context);
Map params = context.getExternalContext().getRequestParameterMap();
if (ac.isDisabled() || ac.isReadonly()) {
return;
}
if (ac.isMultiple()) {
decodeMultiple(context, ac);
}
else {
decodeSingle(context, ac);
}
decodeBehaviors(context, ac);
// AutoComplete event
String query = params.get(clientId + "_query");
if (query != null) {
AutoCompleteEvent autoCompleteEvent = new AutoCompleteEvent(ac, query);
autoCompleteEvent.setPhaseId(PhaseId.APPLY_REQUEST_VALUES);
ac.queueEvent(autoCompleteEvent);
}
}
protected void decodeSingle(FacesContext context, AutoComplete ac) {
Map params = context.getExternalContext().getRequestParameterMap();
String clientId = ac.getClientId(context);
String valueParam = (ac.getVar() != null) ? clientId + "_hinput" : clientId + "_input";
String submittedValue = params.get(valueParam);
if (submittedValue != null) {
ac.setSubmittedValue(submittedValue);
}
}
protected void decodeMultiple(FacesContext context, AutoComplete ac) {
Map paramValues = context.getExternalContext().getRequestParameterValuesMap();
Map params = context.getExternalContext().getRequestParameterMap();
String clientId = ac.getClientId(context);
String[] hinputValues = paramValues.get(clientId + "_hinput");
String[] submittedValues = (hinputValues != null) ? hinputValues : new String[]{};
String inputValue = params.get(clientId + "_input");
if (!isValueBlank(inputValue)) {
submittedValues = ArrayUtils.concat(submittedValues, new String[]{inputValue});
}
if (submittedValues.length > 0) {
ac.setSubmittedValue(submittedValues);
}
else {
ac.setSubmittedValue("");
}
}
@Override
public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
AutoComplete autoComplete = (AutoComplete) component;
Map params = context.getExternalContext().getRequestParameterMap();
String query = params.get(autoComplete.getClientId(context) + "_query");
if (query != null) {
if (autoComplete.isDynamicLoadRequest(context)) {
encodePanel(context, autoComplete);
}
else {
encodeResults(context, component, query);
}
}
else {
encodeMarkup(context, autoComplete);
encodeScript(context, autoComplete);
}
}
@SuppressWarnings("unchecked")
public void encodeResults(FacesContext context, UIComponent component, String query) throws IOException {
AutoComplete ac = (AutoComplete) component;
List results = ac.getSuggestions();
int maxResults = ac.getMaxResults();
if (maxResults != Integer.MAX_VALUE && results != null && results.size() > maxResults) {
results = results.subList(0, ac.getMaxResults());
}
encodeSuggestions(context, ac, results);
}
protected void encodeMarkup(FacesContext context, AutoComplete ac) throws IOException {
if (ac.isMultiple()) {
encodeMultipleMarkup(context, ac);
}
else {
encodeSingleMarkup(context, ac);
}
}
protected void encodeSingleMarkup(FacesContext context, AutoComplete ac) throws IOException {
ResponseWriter writer = context.getResponseWriter();
String clientId = ac.getClientId(context);
String styleClass = ac.getStyleClass();
styleClass = styleClass == null ? AutoComplete.STYLE_CLASS : AutoComplete.STYLE_CLASS + " " + styleClass;
writer.startElement("span", null);
writer.writeAttribute("id", clientId, null);
writer.writeAttribute("class", styleClass, null);
if (ac.getStyle() != null) {
writer.writeAttribute("style", ac.getStyle(), null);
}
encodeInput(context, ac, clientId);
if (ac.getVar() != null) {
encodeHiddenInput(context, ac, clientId);
}
if (ac.isDropdown()) {
encodeDropDown(context, ac);
}
if (!ac.isDynamic()) {
encodePanel(context, ac);
}
writer.endElement("span");
}
protected void encodeInput(FacesContext context, AutoComplete ac, String clientId) throws IOException {
ResponseWriter writer = context.getResponseWriter();
boolean disabled = ac.isDisabled();
String var = ac.getVar();
String itemLabel;
String defaultStyleClass = ac.isDropdown() ? AutoComplete.INPUT_WITH_DROPDOWN_CLASS : AutoComplete.INPUT_CLASS;
String styleClass = disabled ? defaultStyleClass + " ui-state-disabled" : defaultStyleClass;
styleClass = ac.isValid() ? styleClass : styleClass + " ui-state-error";
String inputStyle = ac.getInputStyle();
String inputStyleClass = ac.getInputStyleClass();
inputStyleClass = (inputStyleClass == null) ? styleClass : styleClass + " " + inputStyleClass;
String labelledBy = ac.getLabelledBy();
writer.startElement("input", null);
writer.writeAttribute("id", clientId + "_input", null);
writer.writeAttribute("name", clientId + "_input", null);
writer.writeAttribute("type", ac.getType(), null);
writer.writeAttribute("class", inputStyleClass, null);
writer.writeAttribute("autocomplete", "off", null);
if (labelledBy != null) {
writer.writeAttribute("aria-labelledby", labelledBy, null);
}
if (inputStyle != null) {
writer.writeAttribute("style", inputStyle, null);
}
renderPassThruAttributes(context, ac, HTML.INPUT_TEXT_ATTRS_WITHOUT_EVENTS);
renderDomEvents(context, ac, HTML.INPUT_TEXT_EVENTS);
if (var == null) {
itemLabel = ComponentUtils.getValueToRender(context, ac);
if (itemLabel != null) {
writer.writeAttribute("value", itemLabel, null);
}
}
else {
Map requestMap = context.getExternalContext().getRequestMap();
if (ac.isValid()) {
requestMap.put(var, ac.getValue());
itemLabel = ac.getItemLabel();
}
else {
Object submittedValue = ac.getSubmittedValue();
Object value = ac.getValue();
if (submittedValue == null && value != null) {
requestMap.put(var, value);
itemLabel = ac.getItemLabel();
}
else if (submittedValue != null) {
// retrieve the actual item (pojo) from the converter
try {
Object item = getConvertedValue(context, ac, String.valueOf(submittedValue));
requestMap.put(var, item);
itemLabel = ac.getItemLabel();
}
catch (ConverterException ce) {
itemLabel = String.valueOf(submittedValue);
}
}
else {
itemLabel = null;
}
}
if (itemLabel != null) {
writer.writeAttribute("value", itemLabel, null);
}
requestMap.remove(var);
}
if (disabled) {
writer.writeAttribute("disabled", "disabled", null);
}
if (ac.isReadonly()) {
writer.writeAttribute("readonly", "readonly", null);
}
if (ac.isRequired()) {
writer.writeAttribute("aria-required", "true", null);
}
if (RequestContext.getCurrentInstance(context).getApplicationContext().getConfig().isClientSideValidationEnabled()) {
renderValidationMetadata(context, ac);
}
writer.endElement("input");
}
protected void encodeHiddenInput(FacesContext context, AutoComplete ac, String clientId) throws IOException {
ResponseWriter writer = context.getResponseWriter();
String valueToRender = ComponentUtils.getValueToRender(context, ac);
writer.startElement("input", null);
writer.writeAttribute("id", clientId + "_hinput", null);
writer.writeAttribute("name", clientId + "_hinput", null);
writer.writeAttribute("type", "hidden", null);
writer.writeAttribute("autocomplete", "off", null);
if (valueToRender != null) {
writer.writeAttribute("value", valueToRender, null);
}
writer.endElement("input");
}
protected void encodeHiddenSelect(FacesContext context, AutoComplete ac, String clientId, List values) throws IOException {
ResponseWriter writer = context.getResponseWriter();
String id = clientId + "_hinput";
writer.startElement("select", null);
writer.writeAttribute("id", id, null);
writer.writeAttribute("name", id, null);
writer.writeAttribute("multiple", "multiple", null);
writer.writeAttribute("class", "ui-helper-hidden-accessible", null);
if (ac.isDisabled()) {
writer.writeAttribute("disabled", "disabled", "disabled");
}
for (String value : values) {
writer.startElement("option", null);
writer.writeAttribute("value", value, null);
writer.writeAttribute("selected", "selected", null);
writer.endElement("option");
}
if (RequestContext.getCurrentInstance().getApplicationContext().getConfig().isClientSideValidationEnabled()) {
renderValidationMetadata(context, ac);
}
writer.endElement("select");
}
protected void encodeDropDown(FacesContext context, AutoComplete ac) throws IOException {
ResponseWriter writer = context.getResponseWriter();
String dropdownClass = AutoComplete.DROPDOWN_CLASS;
boolean disabled = ac.isDisabled() || ac.isReadonly();
if (disabled) {
dropdownClass += " ui-state-disabled";
}
writer.startElement("button", ac);
writer.writeAttribute("class", dropdownClass, null);
writer.writeAttribute("type", "button", null);
if (disabled) {
writer.writeAttribute("disabled", "disabled", null);
}
if (ac.getTabindex() != null) {
writer.writeAttribute("tabindex", ac.getTabindex(), null);
}
writer.startElement("span", null);
writer.writeAttribute("class", "ui-button-icon-primary ui-icon ui-icon-triangle-1-s", null);
writer.endElement("span");
writer.startElement("span", null);
writer.writeAttribute("class", "ui-button-text", null);
writer.write(" ");
writer.endElement("span");
writer.endElement("button");
}
protected void encodePanel(FacesContext context, AutoComplete ac) throws IOException {
ResponseWriter writer = context.getResponseWriter();
String styleClass = ac.getPanelStyleClass();
styleClass = styleClass == null ? AutoComplete.PANEL_CLASS : AutoComplete.PANEL_CLASS + " " + styleClass;
writer.startElement("span", null);
writer.writeAttribute("id", ac.getClientId(context) + "_panel", null);
writer.writeAttribute("class", styleClass, null);
writer.writeAttribute("role", "listbox", null);
if (ac.getPanelStyle() != null) {
writer.writeAttribute("style", ac.getPanelStyle(), null);
}
if (ac.isDynamic() && ac.isDynamicLoadRequest(context)) {
Map params = context.getExternalContext().getRequestParameterMap();
String query = params.get(ac.getClientId(context) + "_query");
encodeResults(context, ac, query);
}
writer.endElement("span");
}
protected void encodeMultipleMarkup(FacesContext context, AutoComplete ac) throws IOException {
ResponseWriter writer = context.getResponseWriter();
String clientId = ac.getClientId(context);
String inputId = clientId + "_input";
List values;
if (ac.isValid()) {
values = (List) ac.getValue();
}
else {
Object submittedValue = ac.getSubmittedValue();
try {
values = (List) getConvertedValue(context, ac, submittedValue);
}
catch (ConverterException ce) {
values = Arrays.asList((String[]) submittedValue);
}
}
List stringValues = new ArrayList();
boolean disabled = ac.isDisabled();
String title = ac.getTitle();
String style = ac.getStyle();
String styleClass = ac.getStyleClass();
styleClass = styleClass == null ? AutoComplete.MULTIPLE_STYLE_CLASS : AutoComplete.MULTIPLE_STYLE_CLASS + " " + styleClass;
String listClass = ac.isDropdown() ? AutoComplete.MULTIPLE_CONTAINER_WITH_DROPDOWN_CLASS : AutoComplete.MULTIPLE_CONTAINER_CLASS;
listClass = disabled ? listClass + " ui-state-disabled" : listClass;
listClass = ac.isValid() ? listClass : listClass + " ui-state-error";
writer.startElement("div", null);
writer.writeAttribute("id", clientId, null);
writer.writeAttribute("class", styleClass, null);
if (style != null) {
writer.writeAttribute("style", style, null);
}
if (title != null) {
writer.writeAttribute("title", title, null);
}
writer.startElement("ul", null);
writer.writeAttribute("class", listClass, null);
if (values != null && !values.isEmpty()) {
Converter converter = ComponentUtils.getConverter(context, ac);
String var = ac.getVar();
boolean pojo = var != null;
Collection