javax.faces.component.behavior.ClientBehaviorContext 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 javax.faces.component.behavior;
import java.util.Collection;
import java.util.Collections;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
/**
* ClientBehaviorContext
* provides context information that may be useful to
* {@link javax.faces.component.behavior.ClientBehavior#getScript}
* implementations.
*
*
* @since 2.0
*/
public abstract class ClientBehaviorContext {
/**
*
* The request parameter name whose request parameter value identifies the source component of behavior event.
*
* @since 2.3
*/
public static final String BEHAVIOR_SOURCE_PARAM_NAME = "javax.faces.source";
/**
*
* The request parameter name whose request parameter value identifies the type of behavior event.
*
* @since 2.3
*/
public static final String BEHAVIOR_EVENT_PARAM_NAME = "javax.faces.behavior.event";
/**
* Creates a ClientBehaviorContext instance.
*
* @param context the FacesContext
for the current request.
* @param component the component instance to which the
* ClientBehavior
is attached.
* @param eventName the name of the behavior event to which the
* ClientBehavior
is attached.
* @param sourceId the id to use as the ClientBehavior's "source".
* @param parameters the collection of parameters for submitting
* ClientBehaviors to include in the request.
* @return a ClientBehaviorContext
instance configured with the
* provided values.
* @throws NullPointerException if context
,
* component
or eventName
* is null
*
* @since 2.0
*/
public static ClientBehaviorContext createClientBehaviorContext(FacesContext context,
UIComponent component,
String eventName,
String sourceId,
Collection parameters) {
return new ClientBehaviorContextImpl(context, component, eventName, sourceId, parameters);
}
/**
* Returns the {@link FacesContext} for
* the current request.
*
* @return the {@link FacesContext}.
*
* @since 2.0
*/
abstract public FacesContext getFacesContext();
/**
* Returns the {@link UIComponent} that is
* requesting the {@link ClientBehavior} script.
*
* @return the component.
*
* @since 2.0
*/
abstract public UIComponent getComponent();
/**
* Returns the name of the behavior event
* for which the ClientBehavior script is being requested.
*
* @return the event name.
*
* @since 2.0
*/
abstract public String getEventName();
/**
* Returns an id for use as the
* {@link ClientBehavior} source. ClientBehavior implementations that submit back
* to the Faces lifecycle are required to identify which component
* triggered the ClientBehavior-initiated request via the
* javax.faces.source
request parameter. In
* most cases, th source id can be trivially derived from the element
* to which the behavior's client-side script is attached - ie. the
* source id is typically the id of this element. However, in components
* which produce more complex content, the behavior script may not be able to
* determine the correct id to use for the javax.faces.source
* value. The {@link ClientBehaviorContext#getSourceId} method allows the component
* to pass this information into the {@link ClientBehavior#getScript}
* implementation.
*
* @return the id for the behavior's script to use as the "source", or
* null if the Behavior's script can identify the source from the DOM.
*
* @since 2.0
*/
abstract public String getSourceId();
/**
* Returns parameters that "submitting"
* {@link ClientBehavior} implementations should include when posting back data
* into the Faces lifecycle. If no parameters are specified, this method
* returns an empty (non-null) collection.
*
* @return the parameters.
*
* @since 2.0
*/
abstract public Collection getParameters();
// Little static member class that provides a default implementation
private static final class ClientBehaviorContextImpl extends ClientBehaviorContext {
private FacesContext context;
private UIComponent component;
private String eventName;
private String sourceId;
private Collection parameters;
private ClientBehaviorContextImpl(FacesContext context,
UIComponent component,
String eventName,
String sourceId,
Collection parameters) {
if (null == context) {
throw new NullPointerException();
}
if (null == component) {
throw new NullPointerException();
}
if (null == eventName) {
throw new NullPointerException();
}
this.context = context;
this.component = component;
this.eventName = eventName;
this.sourceId = sourceId;
this.parameters = (parameters == null) ?
Collections.emptyList() :
parameters;
}
@Override
public FacesContext getFacesContext() {
return context;
}
@Override
public UIComponent getComponent() {
return component;
}
@Override
public String getEventName() {
return eventName;
}
@Override
public String getSourceId() {
return sourceId;
}
@Override
public Collection getParameters() {
return parameters;
}
}
/**
* Parameter instances
* represent name/value pairs that "submitting" ClientBehavior implementations
* should include when posting back into the Faces lifecycle. ClientBehavior
* implementations can determine which Parameters to include by calling
* ClientBehaviorContext.getParameters().
*
*
* @since 2.0
*/
public static class Parameter {
private String name;
private Object value;
/**
* Creates a Parameter instance.
* @param name the name of the parameter
* @param value the value of the parameter
* @throws NullPointerException if name
* is null.
*
* @since 2.0
*/
public Parameter(String name, Object value) {
if (null == name) {
throw new NullPointerException();
}
this.name = name;
this.value = value;
}
/**
* Returns the Parameter's name.
*
* @return the parameter's name.
*
* @since 2.0
*/
public String getName() {
return name;
}
/**
* Returns the Parameter's value.
*
* @return the parameter's value.
*
* @since 2.0
*/
public Object getValue() {
return value;
}
}
}