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

org.ajax4jsf.framework.renderer.compiler.ChildrensElement 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.framework.renderer.compiler;

import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;

import javax.faces.FacesException;
import javax.faces.component.UIComponent;

import org.ajax4jsf.framework.util.message.Messages;

/**
 * @author [email protected] (latest modification by $Author: slava_kabanovich $)
 * @version $Revision: 1.2 $ $Date: 2006/07/12 09:45:58 $
 * 
 */
public class ChildrensElement extends ElementBase {
	public static final String CURRENT_CHILD = "child_to_render";

	private String iteratorName;
	
	
	public String getIteratorName() {
		return iteratorName;
	}

	public void setIteratorName(String iteratorVar) {
		this.iteratorName = iteratorVar;
		
	}

	public class IteratorWrapper implements Iterator{
		private Iterator iterator;
		private int index = -1;
		
		public IteratorWrapper(Iterator iterator) {
			this.iterator = iterator;
		}
		public Iterator getIterator() {
			return iterator;
		}
		public boolean hasNext() {
			return iterator.hasNext();
		}
		public Object next() {
			Object o = iterator.next();
			index++;
			return o;
		}
		public void remove() {
			iterator.remove();
		}
		public boolean isLast(){
			return !iterator.hasNext();
		}
		public int getIndex() {
			return index;
		}
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.ajax4jsf.framework.renderer.compiler.ElementBase#encode(org.ajax4jsf.framework.renderer.compiler.TemplateContext)
	 */
	public void encode(TemplateContext context) throws IOException {
		UIComponent stored = (UIComponent) context.getParameter(CURRENT_CHILD);
//		Collection children = context.getComponent().getChildren();
		IteratorWrapper iter =new IteratorWrapper(Collections.EMPTY_LIST.iterator());
		Object value = getValue(context);
		if(value != null){
			if (value instanceof UIComponent) {
				UIComponent comp = (UIComponent) value;
				iter = new IteratorWrapper(comp.getChildren().iterator());
			} else if (value instanceof Collection) {
				iter = new IteratorWrapper( ((Collection) value).iterator() );
			} else if (value instanceof String) {
				String componentId = (String) value;
				UIComponent comp = context.getComponent().findComponent(componentId);
				if (null != comp) {
					iter = new IteratorWrapper(comp.getChildren().iterator());
				}
			} else if (value instanceof Iterator) {
				iter = new IteratorWrapper((Iterator) value);
			}
		} else {
			iter = new IteratorWrapper(context.getComponent().getChildren().iterator());
		}
		// For all childrens, iterate over template.
//		IteratorWrapper iter = new IteratorWrapper(children.iterator());
		if(iteratorName != null){
			context.putParameter(iteratorName, iter);
		}
		while (iter.hasNext()) {
			UIComponent child = (UIComponent) iter.next();

			if ( child.isRendered()) {
				if (getChildren().size()>0) {
					// Store facet to render in  element.
					// TODO - create new templatecontext with current iteration component ?
					context.putParameter(CURRENT_CHILD, child);
					// In fact, render all childrens.
					super.encode(context);
				} else {
					// empty element - render children as-is
					context.getRenderer().renderChild(context.getFacesContext(),child);					
				}
			}
		}
		if(iteratorName != null){
			context.removeParameter(iteratorName);
		}
		if (null != stored) {
			context.putParameter(CURRENT_CHILD, stored);
		} else {
			context.removeParameter(CURRENT_CHILD);
		}
	}

	/* (non-Javadoc)
	 * @see org.ajax4jsf.framework.renderer.compiler.ElementBase#encode(org.ajax4jsf.framework.renderer.compiler.TemplateContext, java.lang.String)
	 */
	public void encode(TemplateContext context, String breakPoint) throws IOException {
		// TODO Auto-generated method stub
		throw new FacesException(Messages.getMessage(Messages.BREAKPOINTS_UNSUPPORTED_ERROR_2));
	}

	public String getTag() {
		return HtmlCompiler.NS_UTIL_PREFIX+HtmlCompiler.CHILDREN_TAG;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy