Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (c) 1997, 2020 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
// MenuRenderer.java
package com.sun.faces.renderkit.html_basic;
import static com.sun.faces.RIConstants.NO_VALUE;
import static com.sun.faces.renderkit.RenderKitUtils.getSelectItems;
import static com.sun.faces.renderkit.RenderKitUtils.renderOnchange;
import static com.sun.faces.renderkit.RenderKitUtils.renderPassThruAttributes;
import static com.sun.faces.renderkit.RenderKitUtils.renderXHTMLStyleBooleanAttributes;
import static com.sun.faces.util.MessageUtils.CONVERSION_ERROR_MESSAGE_ID;
import static com.sun.faces.util.MessageUtils.getExceptionMessage;
import static com.sun.faces.util.ReflectionUtils.lookupMethod;
import static com.sun.faces.util.RequestStateManager.TARGET_COMPONENT_ATTRIBUTE_NAME;
import static com.sun.faces.util.Util.getConverterForClass;
import static com.sun.faces.util.Util.isAllNull;
import static java.lang.Integer.MIN_VALUE;
import static java.lang.reflect.Array.get;
import static java.lang.reflect.Array.getLength;
import static java.lang.reflect.Array.set;
import static java.lang.reflect.Modifier.isAbstract;
import static java.util.Arrays.stream;
import static java.util.logging.Level.FINE;
import static java.util.logging.Level.FINER;
import static java.util.logging.Level.SEVERE;
import static java.util.stream.Collectors.joining;
import java.io.IOException;
import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import jakarta.el.ELException;
import jakarta.el.ExpressionFactory;
import jakarta.el.ValueExpression;
import jakarta.faces.FacesException;
import jakarta.faces.component.UIComponent;
import jakarta.faces.component.UISelectMany;
import jakarta.faces.component.UISelectOne;
import jakarta.faces.component.ValueHolder;
import jakarta.faces.component.html.HtmlSelectManyListbox;
import jakarta.faces.component.html.HtmlSelectOneListbox;
import jakarta.faces.context.FacesContext;
import jakarta.faces.context.ResponseWriter;
import jakarta.faces.convert.Converter;
import jakarta.faces.convert.ConverterException;
import jakarta.faces.model.SelectItem;
import jakarta.faces.model.SelectItemGroup;
import com.sun.faces.RIConstants;
import com.sun.faces.io.FastStringWriter;
import com.sun.faces.renderkit.Attribute;
import com.sun.faces.renderkit.AttributeManager;
import com.sun.faces.renderkit.RenderKitUtils;
import com.sun.faces.renderkit.SelectItemsIterator;
import com.sun.faces.util.RequestStateManager;
import com.sun.faces.util.Util;
/**
* MenuRenderer is a class that renders the current value of UISelectOne or UISelectMany
* component as a list of menu options.
*/
public class MenuRenderer extends HtmlBasicInputRenderer {
private static final Attribute[] ATTRIBUTES = AttributeManager.getAttributes(AttributeManager.Key.SELECTMANYMENU);
// ---------------------------------------------------------- Public Methods
public Object convertSelectManyValue(FacesContext context, UISelectMany uiSelectMany, String[] newValues) throws ConverterException {
// If we have no local value, try to get the valueExpression.
ValueExpression valueExpression = uiSelectMany.getValueExpression("value");
Object convertedValue = newValues; // default case, set local value
// If we have a ValueExpression
if (valueExpression != null) {
Class> modelType = valueExpression.getType(context.getELContext());
// Does the valueExpression resolve properly to something with a type?
if (modelType != null) {
convertedValue = convertSelectManyValuesForModel(context, uiSelectMany, modelType, newValues);
}
// If it could not be converted, as a fall back try the type of
// the valueExpression's current value covering some edge cases such
// as where the current value came from a Map.
if (convertedValue == null) {
Object value = valueExpression.getValue(context.getELContext());
if (value != null) {
convertedValue = convertSelectManyValuesForModel(context, uiSelectMany, value.getClass(), newValues);
}
}
if (convertedValue == null) {
Object[] params = { newValues == null ? "" : stream(newValues).collect(joining("")), valueExpression.getExpressionString() };
throw new ConverterException(getExceptionMessage(CONVERSION_ERROR_MESSAGE_ID, params));
}
} else {
// No ValueExpression, just use Object array.
convertedValue = convertSelectManyValuesForArray(context, uiSelectMany, Object.class, newValues);
}
// At this point, result is ready to be set as the value
if (logger.isLoggable(FINE)) {
logger.fine("SelectMany Component " + uiSelectMany.getId() + " convertedValues " + convertedValue);
}
return convertedValue;
}
public Object convertSelectOneValue(FacesContext context, UISelectOne uiSelectOne, String newValue) throws ConverterException {
if (isNoValueOrNull(newValue, uiSelectOne)) {
return null;
}
Object convertedValue = super.getConvertedValue(context, uiSelectOne, newValue);
if (logger.isLoggable(FINE)) {
logger.fine("SelectOne Component " + uiSelectOne.getId() + " convertedValue " + convertedValue);
}
return convertedValue;
}
@Override
public void decode(FacesContext context, UIComponent component) {
rendererParamsNotNull(context, component);
if (!shouldDecode(component)) {
return;
}
String clientId = decodeBehaviors(context, component);
if (clientId == null) {
clientId = component.getClientId(context);
}
// Currently we assume the model type to be of type string or
// convertible to string and localized by the application.
if (component instanceof UISelectMany) {
decodeUISelectMany(context, (UISelectMany) component, clientId);
} else {
decodeUISelectOne(context, component, clientId);
}
}
@Override
public void encodeBegin(FacesContext context, UIComponent component) throws IOException {
rendererParamsNotNull(context, component);
}
@Override
public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
rendererParamsNotNull(context, component);
if (!shouldEncode(component)) {
return;
}
renderSelect(context, component);
}
@Override
public Object getConvertedValue(FacesContext context, UIComponent component, Object submittedValue) throws ConverterException {
if (component instanceof UISelectMany) {
// Need to set the 'TARGET_COMPONENT_ATTRIBUTE_NAME' request attr so the
// coerce-value call in the faces-api UISelectMany.matchValue will work
// (need a better way to determine the currently processing UIComponent ...)
RequestStateManager.set(context, TARGET_COMPONENT_ATTRIBUTE_NAME, component);
return convertSelectManyValue(context, (UISelectMany) component, (String[]) submittedValue);
} else {
return convertSelectOneValue(context, (UISelectOne) component, (String) submittedValue);
}
}
// ------------------------------------------------------- Protected Methods
/*
* Converts the provided string array and places them into the correct provided model type.
*/
@SuppressWarnings("unchecked")
protected Object convertSelectManyValuesForModel(FacesContext context, UISelectMany uiSelectMany, Class> modelType, String[] newValues) {
if (modelType.isArray()) {
return convertSelectManyValuesForArray(context, uiSelectMany, modelType.getComponentType(), newValues);
}
if (Collection.class.isAssignableFrom(modelType)) {
return convertSelectManyValuesForCollection(context, uiSelectMany, (Class extends Collection