package org.richfaces.component;

import javax.faces.component.ActionSource;
import javax.faces.component.UIComponent;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.faces.event.FacesEvent;
import javax.faces.event.PhaseId;

import org.ajax4jsf.component.AjaxComponent;
import org.ajax4jsf.context.AjaxContext;

public abstract class DialogEvent extends FacesEvent {
	UIViewRoot root;
	/**
	 * View id of page that sends this event. 
	 * It is to be keeped to override view that will 
	 * be set by navigation handler, because that other 
	 * view is needed for the parent window.
	 */
	String sourceViewId;
	AjaxContext ajaxContext;

	public DialogEvent(UIComponent component) {
		super(component);
		setPhaseId(PhaseId.INVOKE_APPLICATION);
		if(component instanceof ActionSource && ((ActionSource)component).isImmediate()) {
			setPhaseId(PhaseId.APPLY_REQUEST_VALUES);
		} else if(component instanceof AjaxComponent && ((AjaxComponent)component).isBypassUpdates()) {
			setPhaseId(PhaseId.UPDATE_MODEL_VALUES);
		}
		FacesContext context = FacesContext.getCurrentInstance();
		root = context.getViewRoot();
		sourceViewId = context.getViewRoot().getViewId();
		ajaxContext = AjaxContext.getCurrentInstance(context);
	}

	public String getSourceViewId() {
		return sourceViewId;
	}
	
	public UIViewRoot getSourceViewRoot() {
		return root;
	}
	
	public AjaxContext getAjaxContext() {
		return ajaxContext;
	}
	
}
