
org.primefaces.component.autocomplete.AutoCompleteRenderer Maven / Gradle / Ivy
/*
* The MIT License
*
* Copyright (c) 2009-2021 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.autocomplete;
import java.io.IOException;
import java.util.*;
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.event.AutoCompleteEvent;
import org.primefaces.expression.SearchExpressionFacade;
import org.primefaces.expression.SearchExpressionUtils;
import org.primefaces.renderkit.InputRenderer;
import org.primefaces.util.ComponentUtils;
import org.primefaces.util.HTML;
import org.primefaces.util.LangUtils;
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 (!shouldDecode(ac)) {
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 || ac.isClientCacheRequest(context)) {
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 = LangUtils.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 ac = (AutoComplete) component;
Map params = context.getExternalContext().getRequestParameterMap();
String query = params.get(ac.getClientId(context) + "_query");
if ((ac.isClientQueryMode() || ac.isHybridQueryMode()) && !ac.isCache()) {
ac.setCache(true);
}
if (query != null) {
if (ac.isDynamicLoadRequest(context)) {
encodePanel(context, ac);
}
else {
encodeResults(context, component);
}
}
else if (ac.isClientCacheRequest(context)) {
encodeResults(context, ac);
}
else {
encodeMarkup(context, ac);
encodeScript(context, ac);
}
}
@SuppressWarnings("unchecked")
public void encodeResults(FacesContext context, UIComponent component) throws IOException {
AutoComplete ac = (AutoComplete) component;
Object results = ac.getSuggestions();
int maxResults = ac.getMaxResults();
if (ac.isServerQueryMode() && maxResults != Integer.MAX_VALUE && results != null && ((List) results).size() > maxResults) {
results = ((List) 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();
String var = ac.getVar();
String itemLabel;
String inputStyle = ac.getInputStyle();
String defaultStyleClass = ac.isDropdown() ? AutoComplete.INPUT_WITH_DROPDOWN_CLASS : AutoComplete.INPUT_CLASS;
String inputStyleClass = createStyleClass(ac, AutoComplete.PropertyKeys.inputStyleClass.name(), defaultStyleClass);
String autocompleteProp = (ac.getAutocomplete() != null) ? ac.getAutocomplete() : "off";
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", autocompleteProp, null);
if (inputStyle != null) {
writer.writeAttribute("style", inputStyle, null);
}
renderAccessibilityAttributes(context, ac);
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);
}
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);
String autocompleteProp = (ac.getAutocomplete() != null) ? ac.getAutocomplete() : "off";
writer.startElement("input", null);
writer.writeAttribute("id", clientId + "_hinput", null);
writer.writeAttribute("name", clientId + "_hinput", null);
writer.writeAttribute("type", "hidden", null);
writer.writeAttribute("autocomplete", autocompleteProp, null);
writer.writeAttribute(HTML.ARIA_HIDDEN, "true", 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);
writer.writeAttribute("tabindex", "-1", null);
writer.writeAttribute(HTML.ARIA_HIDDEN, "true", null);
if (ac.isDisabled()) {
writer.writeAttribute("disabled", "disabled", "disabled");
}
renderValidationMetadata(context, ac);
for (int i = 0; i < values.size(); i++) {
String value = values.get(i);
writer.startElement("option", null);
writer.writeAttribute("value", value, null);
writer.writeAttribute("selected", "selected", null);
writer.endElement("option");
}
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.getDropdownTabindex() != null) {
writer.writeAttribute("tabindex", ac.getDropdownTabindex(), null);
}
else 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);
writer.writeAttribute("tabindex", "-1", null);
if (ac.getPanelStyle() != null) {
writer.writeAttribute("style", ac.getPanelStyle(), null);
}
if (ac.isDynamic() && ac.isDynamicLoadRequest(context)) {
encodeResults(context, ac);
}
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 = createStyleClass(ac, null, listClass);
String autocompleteProp = (ac.getAutocomplete() != null) ? ac.getAutocomplete() : "off";
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
© 2015 - 2025 Weber Informatics LLC | Privacy Policy