/**
 * 
 */
package org.richfaces.component;

import javax.el.ValueExpression;
import javax.faces.component.UIComponent;
import javax.faces.component.UIComponentBase;
import javax.faces.context.FacesContext;

/**
 * @author Maksim Kaszynski
 *
 */
public class UISortableControl extends UIComponentBase{
	
	public static final String COMPONENT_FAMILY = "org.richfaces.SortControl";
	
	public static final String COMPONENT_TYPE = "org.richfaces.SortControl";

	private ValueExpression sortExpression;
	
	private Mode mode;
	
	public ValueExpression getSortExpression() {
		return sortExpression;
	}

	public void setSortExpression(ValueExpression sortExpression) {
		this.sortExpression = sortExpression;
	}
	
	@Override
	public String getFamily() {
		return COMPONENT_FAMILY;
	}

	public Mode getMode() {
		return mode;
	}
	
	public UIDataTable getTable() {
		UIComponent component = getParent();
		while(component != null && !(component instanceof UIDataTable)) {
			component = component.getParent();
		}
		return (UIDataTable) component;
	}
	
	public void setMode(Mode mode) {
		this.mode = mode;
	}

	@Override
	public Object saveState(FacesContext context) {
		Object [] o = new Object[3];
		o[0] = super.saveState(context);
		o[1] = mode;
		o[2] = saveAttachedState(context, sortExpression);
		return o;
	}
	
	@Override
	public void restoreState(FacesContext context, Object state) {
		Object [] o = (Object[]) state;
		super.restoreState(context, o[0]);
		mode = (Mode) o[1];
		sortExpression = (ValueExpression) restoreAttachedState(context, o[2]);
	}
	
}
