package org.seasar.jsf.application;

import javax.faces.application.Application;
import javax.faces.application.NavigationHandler;
import javax.faces.component.ActionSource;
import javax.faces.context.FacesContext;
import javax.faces.el.MethodBinding;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.ActionEvent;
import javax.faces.event.ActionListener;

import org.seasar.jsf.util.MethodBindingUtil;

public class ActionListenerImpl implements ActionListener {
	
	public void processAction(ActionEvent actionEvent)
			throws AbortProcessingException {

		FacesContext context = FacesContext.getCurrentInstance();
		Application app = context.getApplication();
		ActionSource actionSource = (ActionSource) actionEvent.getComponent();
		MethodBinding mb = actionSource.getAction();
		String fromAction = null;
		String outcome = null;
		if (mb != null) {
			fromAction = mb.getExpressionString();
			outcome = MethodBindingUtil.invoke(mb, context);
		}
		NavigationHandler navigationHandler = app.getNavigationHandler();
		navigationHandler.handleNavigation(context, fromAction, outcome);
		context.renderResponse();
	}
}