import java.awt.*;
import java.awt.event.*;

// This appears in Core Web Programming from
// Prentice Hall Publishers, and may be freely used
// or adapted. 1997 Marty Hall, hall@apl.jhu.edu.

/** The beginnings of a better Scrollbar. This one
 *  adjusts for the fact that many implementations
 *  ignore the line and/or page increment. Created
 *  to demonstrate low-level 1.1 scrollbar events.
 */

public class BetterScrollbar extends Scrollbar {
  private int lastValue;

  /** A single constructor for now. */
  
  public BetterScrollbar(int orientation,
                         int initialValue,
                         int bubbleSize,
                         int min,
                         int max) {
    super(orientation, initialValue, bubbleSize,
          min, max);
    enableEvents(AWTEvent.ADJUSTMENT_EVENT_MASK);
    lastValue = initialValue;
  }

  /** Watch the events, adjusting by unit increment
   *  or block increment if appropriate.
   */
  public void processAdjustmentEvent(AdjustmentEvent e) {
    int type = e.getAdjustmentType();
    switch(type) {
      case e.UNIT_INCREMENT:
        setValue(lastValue + getUnitIncrement());
        break;
      case e.UNIT_DECREMENT:
        setValue(lastValue - getUnitIncrement());
        break;
      case e.BLOCK_INCREMENT:
        setValue(lastValue + getBlockIncrement());
        break;
      case e.BLOCK_DECREMENT:
        setValue(lastValue - getBlockIncrement());
        break;
    }
    lastValue = getValue();
    super.processAdjustmentEvent(e);
  }
}

