com.sun.faces.taglib.jsf_core.ValueChangeListenerTag Maven / Gradle / Ivy
Show all versions of jakarta.faces Show documentation
/*
* Copyright (c) 1997, 2018 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
*/
package com.sun.faces.taglib.jsf_core;
import com.sun.faces.util.MessageUtils;
import com.sun.faces.util.Util;
import com.sun.faces.util.FacesLogger;
import javax.el.ValueExpression;
import javax.faces.component.EditableValueHolder;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.ValueChangeEvent;
import javax.faces.event.ValueChangeListener;
import javax.faces.webapp.UIComponentClassicTagBase;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Tag implementation that creates a {@link ValueChangeListener} instance
* and registers it on the {@link UIComponent} associated with our most
* immediate surrounding instance of a tag whose implementation class
* is a subclass of {@link UIComponentClassicTagBase}. This tag creates no output to the
* page currently being created.
*
* This class may be used directly to implement a generic event handler
* registration tag (based on the fully qualified Java class name specified
* by the type
attribute), or as a base class for tag instances
* that support specific {@link ValueChangeListener} subclasses.
*
* Subclasses of this class must implement the
* createValueChangeListener()
method, which creates and returns a
* {@link ValueChangeListener} instance. Any configuration properties that
* are required by this {@link ValueChangeListener} instance must have been
* set by the createValueChangeListener()
method. Generally,
* this occurs by copying corresponding attribute values on the tag
* instance.
*
* This tag creates no output to the page currently being created. It
* is used solely for the side effect of {@link ValueChangeListener}
* creation.
*/
public class ValueChangeListenerTag extends TagSupport {
// ------------------------------------------------------------- Attributes
private static final long serialVersionUID = -212845116876281363L;
private static final Logger LOGGER = FacesLogger.TAGLIB.getLogger();
/**
* The fully qualified class name of the {@link ValueChangeListener}
* instance to be created.
*/
private ValueExpression type = null;
/**
* The value expression used to create a listener instance and it
* is also used to wire up this listener to an {@link
* ValueChangeListener} property of a JavaBean class.
*/
private ValueExpression binding = null;
/**
* Set the fully qualified class name of the
* {@link ValueChangeListener} instance to be created.
*
* @param type The new class name
*/
public void setType(ValueExpression type) {
this.type = type;
}
/*
*
Set the value binding expression for this listener.
*
* @param binding The new value binding expression
*/
public void setBinding(ValueExpression binding) {
this.binding = binding;
}
// --------------------------------------------------------- Public Methods
/**
* Create a new instance of the specified {@link ValueChangeListener}
* class, and register it with the {@link UIComponent} instance associated
* with our most immediately surrounding {@link UIComponentClassicTagBase} instance, if
* the {@link UIComponent} instance was created by this execution of the
* containing JSP page.
*
* @throws JspException if a JSP error occurs
*/
@Override
public int doStartTag() throws JspException {
// Locate our parent UIComponentTag
UIComponentClassicTagBase tag =
UIComponentClassicTagBase.getParentUIComponentClassicTagBase(pageContext);
if (tag == null) {
// Object[] params = {this.getClass().getName()};
// PENDING(rogerk): do something with params
throw new JspException(
MessageUtils.getExceptionMessageString(
MessageUtils.NOT_NESTED_IN_FACES_TAG_ERROR_MESSAGE_ID));
}
// Nothing to do unless this tag created a component
if (!tag.getCreated()) {
return (SKIP_BODY);
}
UIComponent component = tag.getComponentInstance();
if (component == null) {
throw new JspException(
MessageUtils.getExceptionMessageString(MessageUtils.NULL_COMPONENT_ERROR_MESSAGE_ID));
}
if (!(component instanceof EditableValueHolder)) {
Object[] params = {"valueChangeListener", "javax.faces.component.EditableValueHolder"};
throw new JspException(
MessageUtils.getExceptionMessageString(
MessageUtils.NOT_NESTED_IN_TYPE_TAG_ERROR_MESSAGE_ID, params));
}
// If binding is null, type is set and is a literal value,
// then don't bother wrapping. Just instantiate and
// set.
ValueChangeListener listener;
if (binding == null && type != null && type.isLiteralText()) {
try {
listener = (ValueChangeListener)
Util.getListenerInstance(type, null);
} catch (Exception e) {
throw new JspException(e.getMessage(), e.getCause());
}
} else {
listener = new BindingValueChangeListener(type, binding);
}
((EditableValueHolder) component).addValueChangeListener(listener);
return (SKIP_BODY);
}
/**
* Release references to any acquired resources.
*/
@Override
public void release() {
this.type = null;
}
// ----------------------------------------------------------- Inner Classes
private static class BindingValueChangeListener
implements ValueChangeListener, Serializable {
private static final long serialVersionUID = -703503904910636450L;
private ValueExpression type;
private ValueExpression binding;
// -------------------------------------------------------- Constructors
public BindingValueChangeListener(ValueExpression type,
ValueExpression binding) {
this.type = type;
this.binding = binding;
}
// ------------------------------------ Methods from ValueChangeListener
/**
*
Invoked when the value change described by the specified
* {@link javax.faces.event.ValueChangeEvent} occurs.
*
* @param event The {@link javax.faces.event.ValueChangeEvent} that has occurred
* @throws javax.faces.event.AbortProcessingException
* Signal the JavaServer Faces
* implementation that no further processing on the current event
* should be performed
*/
@Override
public void processValueChange(ValueChangeEvent event) throws AbortProcessingException {
ValueChangeListener instance = (ValueChangeListener)
Util.getListenerInstance(type, binding);
if (instance != null) {
instance.processValueChange(event);
} else {
if (LOGGER.isLoggable(Level.WARNING)) {
LOGGER.log(Level.WARNING,
"jsf.core.taglib.action_or_valuechange_listener.null_type_binding",
new Object[] {
"ValueChangeListener",
event.getComponent().getClientId(FacesContext.getCurrentInstance())});
}
}
}
}
}