org.apache.myfaces.renderkit.html.HtmlButtonRendererBase Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of myfaces-commons Show documentation
Show all versions of myfaces-commons Show documentation
The MyFaces Commons Subproject provides base classes for usage in both the
MyFaces implementation and the MyFaces Tomahawk components. This is also
a general set of utility classes for usage in your JSF projects independent
of the implementation you might be deciding upon.
The newest version!
/*
* Copyright 2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.myfaces.renderkit.html;
import java.io.IOException;
import java.util.Map;
import javax.faces.component.UICommand;
import javax.faces.component.UIComponent;
import javax.faces.component.UIForm;
import javax.faces.component.ValueHolder;
import javax.faces.component.html.HtmlCommandButton;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.faces.event.ActionEvent;
import org.apache.myfaces.config.MyfacesConfig;
import org.apache.myfaces.renderkit.JSFAttr;
import org.apache.myfaces.renderkit.RendererUtils;
import org.apache.myfaces.renderkit.html.util.DummyFormUtils;
import org.apache.myfaces.renderkit.html.util.JavascriptUtils;
/**
* @author Manfred Geiler (latest modification by $Author: baranda $)
* @author Thomas Spiegl
* @author Anton Koinov
* @version $Revision: 373272 $ $Date: 2006-01-29 02:01:37 +0000 (Sun, 29 Jan 2006) $
*/
public class HtmlButtonRendererBase
extends HtmlRenderer
{
private static final String IMAGE_BUTTON_SUFFIX_X = ".x";
private static final String IMAGE_BUTTON_SUFFIX_Y = ".y";
public static final String ACTION_FOR_LIST = "org.apache.myfaces.ActionForList";
public void decode(FacesContext facesContext, UIComponent uiComponent)
{
RendererUtils.checkParamValidity(facesContext, uiComponent, UICommand.class);
//super.decode must not be called, because value is handled here
if (!isReset(uiComponent) && isSubmitted(facesContext, uiComponent))
{
uiComponent.queueEvent(new ActionEvent(uiComponent));
RendererUtils.initPartialValidationAndModelUpdate(uiComponent, facesContext);
}
}
private static boolean isReset(UIComponent uiComponent)
{
return "reset".equalsIgnoreCase((String) uiComponent.getAttributes().get(HTML.TYPE_ATTR));
}
private static boolean isSubmitted(FacesContext facesContext, UIComponent uiComponent)
{
String clientId = uiComponent.getClientId(facesContext);
Map paramMap = facesContext.getExternalContext().getRequestParameterMap();
return paramMap.containsKey(clientId) || paramMap.containsKey(clientId + IMAGE_BUTTON_SUFFIX_X) || paramMap.containsKey(clientId + IMAGE_BUTTON_SUFFIX_Y);
}
public void encodeEnd(FacesContext facesContext, UIComponent uiComponent)
throws IOException
{
RendererUtils.checkParamValidity(facesContext, uiComponent, UICommand.class);
String clientId = uiComponent.getClientId(facesContext);
ResponseWriter writer = facesContext.getResponseWriter();
writer.startElement(HTML.INPUT_ELEM, uiComponent);
writer.writeAttribute(HTML.ID_ATTR, clientId, JSFAttr.ID_ATTR);
writer.writeAttribute(HTML.NAME_ATTR, clientId, JSFAttr.ID_ATTR);
String image = getImage(uiComponent);
ExternalContext externalContext = facesContext.getExternalContext();
if (image != null)
{
writer.writeAttribute(HTML.TYPE_ATTR, HTML.INPUT_TYPE_IMAGE, JSFAttr.TYPE_ATTR);
String src = facesContext.getApplication().getViewHandler().getResourceURL(
facesContext, image);
writer.writeURIAttribute(HTML.SRC_ATTR, externalContext.encodeResourceURL(src),
JSFAttr.IMAGE_ATTR);
}
else
{
String type = getType(uiComponent);
if (type == null)
{
type = HTML.INPUT_TYPE_SUBMIT;
}
writer.writeAttribute(HTML.TYPE_ATTR, type, JSFAttr.TYPE_ATTR);
Object value = getValue(uiComponent);
if (value != null)
{
writer.writeAttribute(HTML.VALUE_ATTR, value, JSFAttr.VALUE_ATTR);
}
}
if (JavascriptUtils.isJavascriptAllowed(externalContext))
{
StringBuffer onClick = buildOnClick(uiComponent, facesContext, writer);
writer.writeAttribute(HTML.ONCLICK_ATTR, onClick.toString(), null);
HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent,
HTML.BUTTON_PASSTHROUGH_ATTRIBUTES_WITHOUT_DISABLED_AND_ONCLICK);
}
else
{
HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent,
HTML.BUTTON_PASSTHROUGH_ATTRIBUTES_WITHOUT_DISABLED);
}
if (isDisabled(facesContext, uiComponent))
{
writer.writeAttribute(HTML.DISABLED_ATTR, Boolean.TRUE, JSFAttr.DISABLED_ATTR);
}
writer.endElement(HTML.INPUT_ELEM);
}
protected StringBuffer buildOnClick(UIComponent uiComponent, FacesContext facesContext, ResponseWriter writer)
throws IOException
{
//Find form
UIComponent parent = uiComponent.getParent();
while (parent != null && !(parent instanceof UIForm))
{
parent = parent.getParent();
}
UIForm nestingForm = null;
String formName;
if (parent != null)
{
//link is nested inside a form
nestingForm = (UIForm)parent;
formName = nestingForm.getClientId(facesContext);
}
else
{
//not nested in form, we must add a dummy form at the end of the document
formName = DummyFormUtils.DUMMY_FORM_NAME;
//dummyFormResponseWriter = DummyFormUtils.getDummyFormResponseWriter(facesContext);
//dummyFormResponseWriter.setWriteDummyForm(true);
DummyFormUtils.setWriteDummyForm(facesContext, true);
}
StringBuffer onClick = new StringBuffer();
String commandOnClick = (String)uiComponent.getAttributes().get(HTML.ONCLICK_ATTR);
if (commandOnClick != null)
{
onClick.append(commandOnClick);
onClick.append(';');
}
//call the clear_ method
onClick.append(HtmlRendererUtils.getClearHiddenCommandFormParamsFunctionName(formName)).append("();");
if (MyfacesConfig.getCurrentInstance(facesContext.getExternalContext()).isAutoScroll())
{
JavascriptUtils.appendAutoScrollAssignment(onClick, formName);
}
//add hidden field for the case there is no commandLink in the form
String hiddenFieldName = HtmlRendererUtils.getHiddenCommandLinkFieldName(formName);
if (nestingForm != null)
{
HtmlFormRendererBase.addHiddenCommandParameter(nestingForm, hiddenFieldName);
}
else
{
DummyFormUtils.addDummyFormParameter(facesContext, hiddenFieldName);
}
return onClick;
}
protected boolean isDisabled(FacesContext facesContext, UIComponent uiComponent)
{
//TODO: overwrite in extended HtmlButtonRenderer and check for enabledOnUserRole
if (uiComponent instanceof HtmlCommandButton)
{
return ((HtmlCommandButton)uiComponent).isDisabled();
}
else
{
return RendererUtils.getBooleanAttribute(uiComponent, HTML.DISABLED_ATTR, false);
}
}
private String getImage(UIComponent uiComponent)
{
if (uiComponent instanceof HtmlCommandButton)
{
return ((HtmlCommandButton)uiComponent).getImage();
}
return (String)uiComponent.getAttributes().get(JSFAttr.IMAGE_ATTR);
}
private String getType(UIComponent uiComponent)
{
if (uiComponent instanceof HtmlCommandButton)
{
return ((HtmlCommandButton)uiComponent).getType();
}
return (String)uiComponent.getAttributes().get(JSFAttr.TYPE_ATTR);
}
private Object getValue(UIComponent uiComponent)
{
if (uiComponent instanceof ValueHolder)
{
return ((ValueHolder)uiComponent).getValue();
}
return uiComponent.getAttributes().get(JSFAttr.VALUE_ATTR);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy