package com.wasu.cs.widget;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;

/**
 * 实现ActionBar.Tab
 */
public abstract class TabImpl extends Tab {

	private final Context mContext;

	private ActionBar.TabListener mCallback;
	private Object mTag;
	private Drawable mIcon;
	private CharSequence mText;
	private int mPosition;
	private View mCustomView;

	public TabImpl(Context context) {
		mContext = context;
	}

	@Override
	public Object getTag() {
		return mTag;
	}

	@Override
	public Tab setTag(Object tag) {
		mTag = tag;
		return this;
	}

	@Override
	public Tab setTabListener(ActionBar.TabListener callback) {
		mCallback = callback;
		return this;
	}

	public ActionBar.TabListener getTabListener() {
		return mCallback;
	}

	@Override
	public View getCustomView() {
		return mCustomView;
	}

	@Override
	public Tab setCustomView(View view) {
		mCustomView = view;
		return this;
	}

	@Override
	public Tab setCustomView(int layoutResId) {
		return setCustomView(LayoutInflater.from(mContext).inflate(layoutResId,
				null));
	}

	@Override
	public Drawable getIcon() {
		return mIcon;
	}

	@Override
	public int getPosition() {
		return mPosition;
	}

	public void setPosition(int position) {
		mPosition = position;
	}

	@Override
	public CharSequence getText() {
		return mText;
	}

	@Override
	public Tab setIcon(Drawable icon) {
		mIcon = icon;
		return this;
	}

	@Override
	public Tab setIcon(int resId) {
		return setIcon(mContext.getResources().getDrawable(resId));
	}

	@Override
	public Tab setText(CharSequence text) {
		mText = text;
		return this;
	}

	@Override
	public Tab setText(int resId) {
		return setText(mContext.getResources().getText(resId));
	}

	// ----------------------------------------------------------------------------------------------
	// Non-API.

	public ActionBar.TabListener getCallback() {
		return mCallback;
	}

	boolean hasCustomView() {
		return mCustomView != null;
	}

	@Override
	public String toString() {
		Object source = mTag != null ? mTag : mText;
		return "Tab:" + (source != null ? source.toString() : "<no id>");
	}
}