package W3C.PICS;
import java.awt.*;
import java.util.Vector;

/**
 * A scrollbar for scrolling Panels. It's used when there're too
 * many Categories to be fitted in the window.
 */

class PScrollbar extends Scrollbar
{
    public PScrollbar(Panel WidgetPanel,Vector PanelList,int orientation,int pagesize)
    {
     super(orientation);
     int min=0;
     int max=PanelList.size()/pagesize;
     if (PanelList.size() % pagesize ==0) max--;
     setValues(0,0,min,max);
     setLineIncrement(1);
     setPageIncrement(2);

//     System.out.println("min" + min);
//     System.out.println("max" + max);

/*
     WidgetPanel.setLayout(new GridLayout(0,1));
     this.WidgetPanel=WidgetPanel;
     this.PanelList=PanelList;
          for (int i=getValue();i<(getValue()+4);i++)
            {
             WidgetPanel.add((Panel)PanelList.elementAt(i));
            }
*/
     this.WidgetPanel=WidgetPanel;
     this.PanelList=PanelList;

    layout=new CardLayout();
    WidgetPanel.setLayout(layout);

    for (int k=min;k<=max;k++)
     {Panel PagePanel=new Panel();
      PagePanel.setLayout(new GridLayout(0,1));
      int i=k*pagesize;
      for (int j=i;j<i+pagesize;j++)
         {
           if (j==PanelList.size()) break;
           PagePanel.add((Panel)PanelList.elementAt(j));
         }
      WidgetPanel.add(k+"",PagePanel);

     }

    }
    public boolean handleEvent(Event evt)
    {
     if (evt.id== Event.SCROLL_ABSOLUTE || evt.id == Event.SCROLL_LINE_DOWN ||
         evt.id==Event.SCROLL_LINE_UP || evt.id==Event.SCROLL_PAGE_DOWN ||
         evt.id==Event.SCROLL_PAGE_UP )
         {
          boolean b=super.handleEvent(evt);
      //    updateWidgetPanel();
           layout.show(WidgetPanel,getValue()+"");
          return b;
        }
      return super.handleEvent(evt);
    }


    synchronized void updateWidgetPanel()
    {
          WidgetPanel.removeAll();
          for (int i=getValue();i<getValue()+4;i++)
            {
             WidgetPanel.add((Panel)PanelList.elementAt(i));
            }
          WidgetPanel.validate();
    }
    Panel WidgetPanel;
    Vector PanelList;

    private Vector pages;
    private CardLayout layout;
}