All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ajax4jsf.renderers.ajax.AjaxStatusRenderer Maven / Gradle / Ivy

Go to download

Ajax4jsf is an open source extension to the JavaServer Faces standard that adds AJAX capability to JSF applications without requiring the writing of any JavaScript.

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.ajax4jsf.renderers.ajax;

import java.io.IOException;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;

import org.ajax4jsf.ajax.UIAjaxStatus;
import org.ajax4jsf.framework.renderer.RendererBase;
import org.ajax4jsf.framework.renderer.RendererUtils.HTML;


/**
 * @author shura (latest modification by $Author: alexsmirnov $)
 * @version $Revision: 1.7 $ $Date: 2006/10/19 17:57:23 $
 *
 * Render status component as two span's, one for start request,
 * one for complete.
 */
public class AjaxStatusRenderer extends RendererBase
{
    /**
     *
     */
    public static final String RENDERER_TYPE = "org.ajax4jsf.components.AjaxStatusRenderer";
    
    /**
     *
     */
    public static final String START_STYLE = "display: none";

    /* (non-Javadoc)
     * @see javax.faces.render.Renderer#encodeEnd(javax.faces.context.FacesContext, javax.faces.component.UIComponent)
     */
    public void doEncodeEnd(ResponseWriter writer, FacesContext context, UIComponent component) throws IOException
    {
        // find form clientId for wich we render status.
        String statusId = component.getClientId(context);
        // render 2 span components for different states
        encodeSpan(writer, context, component,statusId, "start");
        encodeSpan(writer, context, component,statusId, "stop");
    }
    
    /**
     * Encode one span for start or stop status
     * @param context - current context
     * @param component - status component
     * @param state - name of state ( start or stop ) for span.
     * @throws IOException 
     */
    protected void encodeSpan(ResponseWriter writer, FacesContext context, UIComponent component, String id, String state) throws IOException {
    	String tag = getTag(component);
        writer.startElement(tag, component);
        writer.writeAttribute(HTML.id_ATTRIBUTE, id+"."+state , null);
        // Styles for concrete state.
        String style = getNamedAttribute(component, "Style", state);
        // for start state rendered style always disable display of status 
        // ( since it will enabled by JavaScript on start request ) 
        if("start".equals(state)) {
            if(null == style) {
                style = START_STYLE;
            } else {
                style += "; "+START_STYLE;
            }
        }
//        HtmlRendererUtils.renderHTMLAttribute(writer, "style", "style", style );
//        HtmlRendererUtils.renderHTMLAttribute(writer, "styleClass", "styleClass", getNamedAttribute(component, "StyleClass", state));
        if (null != style) {
			writer.writeAttribute("style", style, null);
		}
        String styleClass = getNamedAttribute(component, "StyleClass", state);
        if(null != styleClass){
			writer.writeAttribute("class", styleClass, null);        	
        }
//        getUtils().encodeAttribute(context,component,"class");
        getUtils().encodePassThru(context , component);
        // render facet or text.
        UIComponent facet = component.getFacet(state);
        if(null != facet) {
            renderChild(context, facet);
        } else {
            String namedAttribute = getNamedAttribute(component, "Text", state);
			if (null != namedAttribute) {
				writer.writeText(namedAttribute, "text");
			} 
//			else {
//				throw new FacesException("Status component must have one of '"+state+"' facet or '"+state+"Text' atribute");
//			}
        }
        writer.endElement(tag);
    }
    
    /**
     * @param component 
     * @param name name of attribute for current state
     * @param state 'start' or 'stop' . all dublicated attributes foe Ajax state
     * have same syntax : startStyle/stopStyle ...
     * @return value of attribute for name and state
     */
    protected String getNamedAttribute(UIComponent component, String name, String state)
    {
        String fullName = (new StringBuffer(state).append(name)).toString();
        return (String) component.getAttributes().get(fullName);
    }

	protected Class getComponentClass() {
		// TODO Auto-generated method stub
		return UIAjaxStatus.class;
	}

	/**
	 * @param status
	 * @return
	 */
	private String getTag(UIComponent status) {
		// TODO Auto-generated method stub
		return "block".equals(status.getAttributes().get("layout"))?HTML.DIV_ELEM:HTML.SPAN_ELEM;
	}

	/* (non-Javadoc)
	 * @see javax.faces.render.Renderer#getRendersChildren()
	 */
	public boolean getRendersChildren() {
		// TODO Auto-generated method stub
		return true;
	}
	
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy