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

org.ajax4jsf.renderers.ajax.AjaxOutputPanelRenderer 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!
/**
 * Licensed under the Common Development and Distribution License,
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.sun.com/cddl/
 *   
 * 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 java.util.Iterator;
import java.util.Set;

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

import org.ajax4jsf.ajax.UIAjaxOutputPanel;
import org.ajax4jsf.framework.ajax.AjaxContainer;
import org.ajax4jsf.framework.ajax.AjaxContext;
import org.ajax4jsf.framework.renderer.AjaxRendererUtils;
import org.ajax4jsf.framework.renderer.RendererBase;
import org.ajax4jsf.framework.renderer.RendererUtils.HTML;


/**
 * @author [email protected] (latest modification by $Author: sergeysmirnov $)
 * @version $Revision: 1.4 $ $Date: 2006/12/15 03:34:37 $
 *
 */
public class AjaxOutputPanelRenderer extends RendererBase {
	
	private final String[] STYLE_ATTRIBUTES = new String[]{"style","class"};

	/* (non-Javadoc)
	 * @see javax.faces.render.Renderer#encodeChildren(javax.faces.context.FacesContext, javax.faces.component.UIComponent)
	 */
	public void encodeChildren(FacesContext context, UIComponent component) throws IOException {
		// 
		UIAjaxOutputPanel panel = (UIAjaxOutputPanel) component;
		if ("none".equals(panel.getLayout())) {
			if (component.getChildCount() > 0) {
				AjaxContext ajaxContext = AjaxContext.getCurrentInstance(context);
				boolean ajaxRequest = ajaxContext.isAjaxRequest();
				Set ajaxRenderedAreas = ajaxContext.getAjaxRenderedAreas();
				for (Iterator it = component.getChildren().iterator(); it.hasNext();) {
					UIComponent child = (UIComponent) it.next();
					String childId = child.getClientId(context);
					if (child.isRendered()) {
						renderChild(context, child);						
					} else {
						// Render "dummy" span.
						ResponseWriter out = context.getResponseWriter();
						out.startElement(HTML.SPAN_ELEM,child);
						out.writeAttribute(HTML.id_ATTRIBUTE,childId,HTML.id_ATTRIBUTE);
						out.writeAttribute(HTML.style_ATTRIBUTE,"display: none;","style");
						out.endElement(HTML.SPAN_ELEM);
					}
					// register child as rendered
					if(ajaxRequest && null != ajaxRenderedAreas) {
						ajaxRenderedAreas.add(childId);
					}
				}
			}
			
		} else {
			renderChildren(context,component);
		}
	}

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

	/* (non-Javadoc)
	 * @see org.ajax4jsf.framework.renderer.RendererBase#getComponentClass()
	 */
	protected Class getComponentClass() {
		// TODO Auto-generated method stub
		return UIAjaxOutputPanel.class;
	}

	/* (non-Javadoc)
	 * @see org.ajax4jsf.framework.renderer.RendererBase#doEncodeBegin(javax.faces.context.ResponseWriter, javax.faces.context.FacesContext, javax.faces.component.UIComponent)
	 */
	protected void doEncodeBegin(ResponseWriter writer, FacesContext context, UIComponent component) throws IOException {
		UIAjaxOutputPanel panel = (UIAjaxOutputPanel) component;
		if (!"none".equals(panel.getLayout())) {
			writer.startElement(getTag(panel), panel);
			getUtils().encodeId(context, component);
			getUtils().encodePassThru(context, component);
			getUtils().encodeAttributesFromArray(context,component,STYLE_ATTRIBUTES);
		}
	}

	/**
	 * @param panel
	 * @return
	 */
	private String getTag(UIAjaxOutputPanel panel) {
		// TODO Auto-generated method stub
		return "block".equals(panel.getLayout())?HTML.DIV_ELEM:HTML.SPAN_ELEM;
	}

	/* (non-Javadoc)
	 * @see org.ajax4jsf.framework.renderer.RendererBase#doEncodeEnd(javax.faces.context.ResponseWriter, javax.faces.context.FacesContext, javax.faces.component.UIComponent)
	 */
	protected void doEncodeEnd(ResponseWriter writer, FacesContext context, UIComponent component) throws IOException {
		UIAjaxOutputPanel panel = (UIAjaxOutputPanel) component;
		if (!"none".equals(panel.getLayout())) {
			writer.endElement(getTag(panel));
		}
		if (panel.isKeepTransient()) {
			markNoTransient(component);
		}
	}

	/**
	 * Set "transient" flag to false for component and all its children ( recursive ).
	 * @param component
	 */
	private void markNoTransient(UIComponent component) {
		for (Iterator iter = component.getFacetsAndChildren(); iter.hasNext();) {
			UIComponent element = (UIComponent) iter.next();
			markNoTransient(element);
			element.setTransient(false);
		}
		
	}

	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy