package org.seasar.jsf.component;

import java.io.IOException;

import javax.faces.application.Application;
import javax.faces.application.NavigationHandler;
import javax.faces.application.ViewHandler;
import javax.faces.component.UICommand;
import javax.faces.context.FacesContext;
import javax.faces.el.MethodBinding;

/**
 * @author higa
 *  
 */
public class Invoke extends UICommand {

	public static final String COMPONENT_TYPE = "org.seasar.jsf.Invoke";

	public static final String COMPONENT_FAMILY = "org.seasar.jsf.Null";
	
	private static final String DEFAULT_RENDERER_TYPE = "org.seasar.jsf.Null";

	public Invoke() {
		setRendererType(DEFAULT_RENDERER_TYPE);
	}

	/**
	 * @see javax.faces.component.UIComponent#getFamily()
	 */
	public String getFamily() {
		return COMPONENT_FAMILY;
	}
	
	public boolean getRendersChildren() {
		return true;
	}

	public void encodeBegin(FacesContext context) throws IOException {
		if (context == null) {
			throw new NullPointerException("context");
		}
		if (!isRendered()) {
			return;
		}
		MethodBinding action = getAction();
		if (action == null) {
			return;
		}
		String outcome = (String) action.invoke(context, null);
		if (outcome != null) {
			Application app = context.getApplication();
			NavigationHandler nh = app.getNavigationHandler();
			nh.handleNavigation(context, action.getExpressionString(), outcome);
			ViewHandler vh = app.getViewHandler();
			vh.renderView(context, context.getViewRoot());
			context.responseComplete();
		}
	}
	
	/**
	 * @see javax.faces.component.UIComponent#encodeChildren(javax.faces.context.FacesContext)
	 */
	public void encodeChildren(FacesContext context) throws IOException {
	}

	public void encodeEnd(FacesContext context) throws IOException {
	}
}