package W3C.PICS;

import java.awt.*;
import java.util.Vector;

/**
    A Checkbox with built-in ranking. It will always make sure that
    all the higher-ranked (smaller rank number) checkboxes are checked
    and lower-ranked checkboxes are unchecked.
*/
public class FilterCheckbox extends DCheckbox
{
    public FilterCheckbox(String label,
                          String Description,
                          TextComponent DText,
                          int rank,
                          boolean block,
                          Vector Checkboxes,
                          Vector TheOtherCheckboxes
                          )
    {
      super(label,Description,DText);
      this.rank=rank;
      this.block=block;
      this.Checkboxes=Checkboxes;

      this.TheOtherCheckboxes=TheOtherCheckboxes;
    }

   public void setState(boolean state)
   {
    super.setState(state);
    FilterCheckbox OCheckbox=(FilterCheckbox)TheOtherCheckboxes.elementAt(rank);
    System.out.println(groupsetting);
    if ((state==true || groupsetting) && OCheckbox.getState()==state)
        OCheckbox.setState(!state);
   }

  // The mouse right clik is not functioning in Java for Win95
  /* public boolean handleEvent(Event evt)
   {
         System.out.println("event");

    if (evt.modifiers==Event.CTRL_MASK)
        {
         System.out.println("Right mouse down");
        }
    return super.handleEvent(evt);

   }*/

   /**
    * This function is suitable for common categories that blocking a certain
    * value usually implies blocking all larger values and allowing all smaller
    * values. For example, categories in  rcsa.rat
    * Note: I should have used mouse right click instead. But java.awt.Componenet
    * has a bug that right clicks are ignored.
    *
   */
   public boolean keyDown(Event e,int key)
   {
    if (key=='\n')
        {if (getState())
            {
            for (int i=0;i<rank;i++)
                ((FilterCheckbox)Checkboxes.elementAt(i)).setGroupState(true ^ block);
            }
         for (int i=rank+1;i<Checkboxes.size();i++)
                ((FilterCheckbox)Checkboxes.elementAt(i)).setGroupState(false ^ block);
         return true;
        }
    else return super.keyDown(e,key);
   }
   /**
    * Used keyDown to get around the state==true test in setState()
    */
   public void setGroupState(boolean state)
   {groupsetting=true;
    setState(state);
    groupsetting=false;
   }

   private int rank;
   private boolean block; // true if this checkbox is for _blocking_
   private Vector Checkboxes=null;
   private Vector TheOtherCheckboxes=null;
   private boolean groupsetting=false;

}