com.sun.faces.renderkit.html_basic.ButtonRenderer Maven / Gradle / Ivy
Show all versions of javax.faces Show documentation
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
* or packager/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at packager/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* Oracle designates this particular file as subject to the "Classpath"
* exception as provided by Oracle in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
// ButtonRenderer.java
package com.sun.faces.renderkit.html_basic;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import javax.faces.component.UICommand;
import javax.faces.component.UIComponent;
import javax.faces.component.behavior.ClientBehaviorContext;
import javax.faces.component.behavior.ClientBehavior;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.faces.event.ActionEvent;
import com.sun.faces.renderkit.Attribute;
import com.sun.faces.renderkit.AttributeManager;
import com.sun.faces.renderkit.RenderKitUtils;
/**
* ButtonRenderer is a class that renders the current value of
* UICommand as a Button.
*/
public class ButtonRenderer extends HtmlBasicRenderer {
private static final Attribute[] ATTRIBUTES =
AttributeManager.getAttributes(AttributeManager.Key.COMMANDBUTTON);
// ---------------------------------------------------------- Public Methods
@Override
public void decode(FacesContext context, UIComponent component) {
rendererParamsNotNull(context, component);
if (!shouldDecode(component)) {
return;
}
String clientId = decodeBehaviors(context, component);
if (wasClicked(context, component, clientId) && !isReset(component)) {
component.queueEvent(new ActionEvent(component));
if (logger.isLoggable(Level.FINE)) {
logger.fine("This command resulted in form submission " +
" ActionEvent queued.");
logger.log(Level.FINE,
"End decoding component {0}",
component.getId());
}
}
}
@Override
public void encodeBegin(FacesContext context, UIComponent component)
throws IOException {
rendererParamsNotNull(context, component);
if (!shouldEncode(component)) {
return;
}
// Which button type (SUBMIT, RESET, or BUTTON) should we generate?
String type = getButtonType(component);
ResponseWriter writer = context.getResponseWriter();
assert(writer != null);
String label = "";
Object value = ((UICommand) component).getValue();
if (value != null) {
label = value.toString();
}
/*
* If we have any parameters and the button type is submit or button,
* then render Javascript to use later.
* RELEASE_PENDING this logic is slightly wrong - we should buffer the user onclick, and use it later.
* Leaving it for when we decide how to do script injection.
*/
Collection params = getBehaviorParameters(component);
if ( !params.isEmpty() && (type.equals("submit") || type.equals("button"))) {
RenderKitUtils.renderJsfJs(context);
}
String imageSrc = (String) component.getAttributes().get("image");
writer.startElement("input", component);
writeIdAttributeIfNecessary(context, writer, component);
String clientId = component.getClientId(context);
if (imageSrc != null) {
writer.writeAttribute("type", "image", "type");
writer.writeURIAttribute("src", RenderKitUtils.getImageSource(context, component, "image"), "image");
writer.writeAttribute("name", clientId, "clientId");
} else {
writer.writeAttribute("type", type, "type");
writer.writeAttribute("name", clientId, "clientId");
writer.writeAttribute("value", label, "value");
}
RenderKitUtils.renderPassThruAttributes(context,
writer,
component,
ATTRIBUTES,
getNonOnClickBehaviors(component));
RenderKitUtils.renderXHTMLStyleBooleanAttributes(writer, component);
String styleClass = (String)
component.getAttributes().get("styleClass");
if (styleClass != null && styleClass.length() > 0) {
writer.writeAttribute("class", styleClass, "styleClass");
}
RenderKitUtils.renderOnclick(context,
component,
params,
null,
false);
// PENDING(edburns): Prior to i_spec_1111, this element
// was rendered unconditionally
if(component.getChildCount() == 0) {
writer.endElement("input");
}
}
@Override
public void encodeEnd(FacesContext context, UIComponent component)
throws IOException {
rendererParamsNotNull(context, component);
// PENDING(edburns): Prior to i_spec_1111, this element
// was rendered unconditionally
if(component.getChildCount() > 0) {
context.getResponseWriter().endElement("input");
}
}
// --------------------------------------------------------- Private Methods
/**
* Determine if this component was activated on the client side.
*
* @param context the FacesContext
for the current request
* @param component the component of interest
* @param clientId the client id, if it has been retrieved, otherwise null
* @return true
if this component was in fact activated,
* otherwise false
*/
private static boolean wasClicked(FacesContext context,
UIComponent component,
String clientId) {
// Was our command the one that caused this submission?
// we don' have to worry about getting the value from request parameter
// because we just need to know if this command caused the submission. We
// can get the command name by calling currentValue. This way we can
// get around the IE bug.
if (clientId == null) {
clientId = component.getClientId(context);
}
Map requestParameterMap = context.getExternalContext()
.getRequestParameterMap();
if (requestParameterMap.get(clientId) == null) {
// Check to see whether we've got an action event
// as a result of a partial/behavior postback.
if (RenderKitUtils.isPartialOrBehaviorAction(context, clientId)) {
return true;
}
StringBuilder builder = new StringBuilder(clientId);
String xValue = builder.append(".x").toString();
builder.setLength(clientId.length());
String yValue = builder.append(".y").toString();
return (requestParameterMap.get(xValue) != null
&& requestParameterMap.get(yValue) != null);
}
return true;
}
/**
* @param component the component of interest
* @return true
if the button represents a reset
* button, otherwise false
*/
private static boolean isReset(UIComponent component) {
return ("reset".equals(component.getAttributes().get("type")));
}
/**
* If the component's type attribute is null or not equal
* to reset
, submit
or button
,
* default to submit
.
* @param component the component of interest
* @return the type for this button
*/
private static String getButtonType(UIComponent component) {
String type = (String) component.getAttributes().get("type");
if (type == null || (!"reset".equals(type) &&
!"submit".equals(type) && !"button".equals(type))) {
type = "submit";
// This is needed in the decode method
component.getAttributes().put("type", type);
}
return type;
}
// Returns the Behaviors map, but only if it contains some entry other
// than those handled by renderOnclick(). This helps us optimize
// renderPassThruAttributes() in the very common case where the
// button only contains an "action" (or "click") Behavior. In that
// we pass a null Behaviors map into renderPassThruAttributes(),
// which allows us to take a more optimized code path.
private static Map> getNonOnClickBehaviors(UIComponent component) {
return getPassThruBehaviors(component, "click", "action");
}
} // end of class ButtonRenderer