/* * Thumb.java */ import java.awt.*; /* Thumb Object */ public class Thumb extends Canvas3D { static int THUMB = 0; /* Thumb Area */ static int PAGE_DOWN = -1; /* Page Down Constant */ static int PAGE_UP = -2; /* Page Up Constant */ int type; /* HORIZONAL/VERTICAL */ int value; /* Current Value */ int visible; /* Current Visible Size */ int minimum; /* Minimum Value */ int maximum; /* Maximum Value */ boolean mousePressed = false; /* Is Mouse Pressed */ int area = THUMB; /* Current Pressed Area */ int offset; /* Thumb Offset */ int length; /* Thumb Length */ int mofs; /* Mouse pressed Offset */ int range; /* Maximum-Minimum */ /** Thumb Constructor */ public Thumb(int _type) { type = _type; setValue(0, 1, 0, 1, false); } /** Internal Check Value */ private void checkValue() { if ( maximum <= minimum ) maximum = minimum+1; if ( visible < 1 ) visible = 1; if ( visible > maximum-minimum ) visible = maximum-minimum; if ( value > maximum-visible ) value = maximum-visible; if ( value < minimum ) value = minimum; } /** Internal Thumb Setup */ private synchronized boolean setThumb() { validate(); boolean changed; int _length, _offset; int wRange = (type == Scrollbar.HORIZONTAL) ? size().width : size().height; _length = Math.max(depth*4, visible*wRange/range); _offset = (value-minimum)*Math.max(1, wRange-_length)/ Math.max(1, range-visible); changed = ((offset != _offset) || (length != _length)); offset = _offset; length = _length; return changed; } /** Set Current Value */ public synchronized boolean setValue(int _value, int _visible, int _minimum, int _maximum, boolean frepaint) { visible = _visible; minimum = _minimum; maximum = _maximum; value = _value; checkValue(); range = maximum-minimum; boolean _active = (range > visible); if ( setThumb() && frepaint ) repaint(); return true; } /** Set Current Value */ public synchronized boolean setValue(int _value, boolean frepaint) { int oldvalue = value; value = _value; checkValue(); if ( setThumb() && frepaint ) repaint(); return (value != oldvalue); } /** Drawing Action Here */ public void update(Graphics g) { setThumb(); g.setColor(getBackground()); if ( type == Scrollbar.HORIZONTAL ) { if ( offset > 0 ) g.fillRect(0, 0, offset, size().height); Draw3DRect(g, offset, 0, length, size().height, depth, gray); int delta = size().width-offset-length; if ( delta > 0 ) g.fillRect(offset+length, 0, delta, size().height); } else { if ( offset > 0 ) g.fillRect(0, 0, size().width, offset); Draw3DRect(g, 0, offset, size().width, length, depth, gray); int delta = size().height-offset-length; if ( delta > 0 ) g.fillRect(0, offset+length, size().width, delta); } } /** Move Thumb */ public void moveThumb(int delta) { if ( delta == 0 ) return; if ( setValue(value+delta, true) ) Notify(null, this); } /** Respond to Delay Event Action */ public boolean keyDown(Event ev, int key) { if ( key < 0 ) { if ( mousePressed ) { action(ev, this); new Delay(100, key, this); } } return true; } /** Mouse Down Event */ public boolean mouseDown(Event ev, int x, int y) { if ( !isEnabled() ) return true; area = THUMB; if ( type == Scrollbar.HORIZONTAL ) { if ( x < offset ) area = PAGE_UP; else if ( x >= offset+length ) area = PAGE_DOWN; else mofs = x-offset; } else { if ( y < offset ) area = PAGE_UP; else if ( y >= offset+length ) area = PAGE_DOWN; else mofs = y-offset; } mousePressed = true; action(ev, this); new Delay(500, -1, this); return true; } /** Mouse Up Event */ public boolean mouseUp(Event ev, int x, int y) { mousePressed = false; return true; } /** Delay Mouse Drag Event */ public boolean delayDrag(Event ev, int x, int y) { if ( !mousePressed || (area != THUMB) ) return false; int wRange = (type == Scrollbar.HORIZONTAL) ? size().width : size().height; int kRange = Math.max(1, range-visible); wRange = Math.max(1, wRange-length); moveThumb((((type == Scrollbar.HORIZONTAL) ? x : y)- mofs-offset)*kRange/wRange); return true; } /** Component Action Event */ public boolean action(Event ev, Object arg) { if (arg == this) { if ( area == PAGE_DOWN ) moveThumb(visible); else if ( area == PAGE_UP ) moveThumb(-visible); return true; } return false; } }