package org.nick.wwwjdic.history;

import java.util.ArrayList;
import java.util.List;

import android.support.v4.app.ActionBar;
import android.support.v4.app.ActionBar.Tab;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;

public class TabsPagerAdapter extends FragmentPagerAdapter implements
        ViewPager.OnPageChangeListener, ActionBar.TabListener {

    private final ActionBar actionBar;
    private final ViewPager viewPager;
    private final List<HistoryFragmentBase> tabs = new ArrayList<HistoryFragmentBase>();

    public TabsPagerAdapter(FragmentActivity activity, ActionBar actionBar,
            ViewPager pager) {
        super(activity.getSupportFragmentManager());
        this.actionBar = actionBar;
        this.viewPager = pager;
        this.viewPager.setAdapter(this);
        this.viewPager.setOnPageChangeListener(this);
    }

    public void addTab(ActionBar.Tab tab, HistoryFragmentBase tabFragment) {
        tabFragment.setHasOptionsMenu(true);
        tabs.add(tabFragment);
        actionBar.addTab(tab.setTabListener(this));
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return tabs.size();
    }

    @Override
    public Fragment getItem(int position) {
        return tabs.get(position);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset,
            int positionOffsetPixels) {
    }

    @Override
    public void onPageSelected(int position) {
        actionBar.setSelectedNavigationItem(position);
        // TODO -- refreshing kills action bar?
        //        refresh(position);
    }

    @SuppressWarnings("unused")
    private void refresh(int position) {
        HistoryFragmentBase fragment = (HistoryFragmentBase) getItem(position);
        fragment.refresh();
    }

    @Override
    public void onPageScrollStateChanged(int state) {
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        int position = tab.getPosition();
        viewPager.setCurrentItem(position);
        // TODO -- refreshing kills action bar?
        //        refresh(position);
    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    }

}

@@ -81,24 +81,54 @@
 il.List;%0D%0A%0D%0A
+import org.nick.wwwjdic.R;%0D%0A%0D%0A
 import andro
@@ -153,24 +153,24 @@
 ActionBar;%0D%0A
-
 import andro
@@ -577,24 +577,64 @@
 stener %7B%0D%0A%0D%0A
+    private FragmentActivity activity;%0D%0A
     private 
@@ -952,24 +952,59 @@
 anager());%0D%0A
+        this.activity = activity;%0D%0A
         this
@@ -1936,32 +1936,61 @@
 esh(position);%0D%0A
+        setTitle(position);%0D%0A
     %7D%0D%0A%0D%0A    @Su
@@ -2503,32 +2503,61 @@
 esh(position);%0D%0A
+        setTitle(position);%0D%0A
     %7D%0D%0A%0D%0A    @Ov
@@ -2648,32 +2648,32 @@
 %0A    @Override%0D%0A
-
     public void 
@@ -2733,11 +2733,220 @@
    %7D%0D%0A%0D%0A
+    private void setTitle(int tabIdx) %7B%0D%0A        if (tabIdx == 0) %7B%0D%0A            activity.setTitle(R.string.favorites);%0D%0A        %7D else %7B%0D%0A            activity.setTitle(R.string.history);%0D%0A        %7D%0D%0A    %7D%0D%0A%0D%0A
 %7D%0D%0A

switch title on tab change