summaryrefslogtreecommitdiff
path: root/java/gjt/Scroller.java
blob: 61d6e5f55e52baff5129f9592a3f7454b23c9132 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package gjt;

import java.awt.*;

/**
 * Each Scroller contains a Panel (viewport) and two Scrollbars 
 * (horizontal and vertical).  Works in conjunction with a 
 * ScrollerLayout, that lays out the viewport and two 
 * scrollbars.<p>
 * 
 * Subclasses must override:<p>
 * <dl>
 * <dd> abstract public void      scrollTo(int x, int y)
 * <dd> abstract public Dimension getScrollAreaSize()
 * </dl>
 * 
 * @version 1.0, Apr 1 1996
 * @author  David Geary
 * @see     ComponentScroller
 * @see     ImageScroller
 * @see     ScrollerLayout
 * @see     gjt.test.ComponentScrollerTest
 * @see     gjt.test.ImageScrollerTest
 */
public abstract class Scroller extends Panel {
    protected Panel     viewport;
    protected Scrollbar hbar, vbar;

    abstract public void      scrollTo(int x, int y);
    abstract public Dimension getScrollAreaSize();

    public Scroller() {
        setLayout(new ScrollerLayout(this));
        add("Scroll", viewport = new Panel());
        add("East", vbar = new Scrollbar(Scrollbar.VERTICAL));
        add("South",hbar = new Scrollbar(Scrollbar.HORIZONTAL));
    }
    public Scrollbar getHorizontalScrollbar() {return hbar;    }
    public Scrollbar getVerticalScrollbar  () {return vbar;    }
    public Panel     getViewport           () {return viewport;}

    public boolean handleEvent(Event event) {
        boolean handledEvent;

        switch(event.id) {
            case Event.SCROLL_LINE_UP:   scrollLineUp(event); 
            break;
            case Event.SCROLL_LINE_DOWN: scrollLineDown(event); 
            break;
            case Event.SCROLL_PAGE_UP:   scrollPageUp  (event); 
            break;
            case Event.SCROLL_PAGE_DOWN: scrollPageDown(event); 
            break;
            case Event.SCROLL_ABSOLUTE:  scrollAbsolute(event); 
            break;
        }
        handledEvent = event.id == Event.SCROLL_LINE_UP   ||
                       event.id == Event.SCROLL_LINE_DOWN ||
                       event.id == Event.SCROLL_PAGE_UP   ||
                       event.id == Event.SCROLL_PAGE_DOWN ||
                       event.id == Event.SCROLL_ABSOLUTE;

        if(handledEvent) return true;
        else             return super.handleEvent(event);
    }
    public void paint (Graphics g) { scroll(); }
    public void update(Graphics g) { paint(g); }

    public void manageScrollbars() { 
        manageHorizontalScrollbar();
        manageVerticalScrollbar  ();
    }
    protected void manageHorizontalScrollbar() {
        Dimension size           = size();
        Dimension scrollAreaSize = getScrollAreaSize();

        if(vbar.isVisible())
            size.width -= vbar.size().width;

        if(scrollAreaSize.width > size.width) {
            if( ! hbar.isVisible())
                hbar.show();
        }
        else if(hbar.isVisible()) {
            hbar.hide();
            hbar.setValue(0);
            repaint();
        }
    }
    protected void manageVerticalScrollbar() {
        Dimension size           = size();
        Dimension scrollAreaSize = getScrollAreaSize();

        if(hbar.isVisible())
            size.height -= hbar.size().height;

        if(scrollAreaSize.height > size.height) {
            if( ! vbar.isVisible())
                vbar.show();
        }
        else if(vbar.isVisible()) {
            vbar.hide();
            vbar.setValue(0);
            repaint();
        }
    }
    public void setScrollbarValues() {
        if(hbar.isVisible()) setHorizontalScrollbarValues();
        if(vbar.isVisible()) setVerticalScrollbarValues();
    }
    protected void setHorizontalScrollbarValues() {
        Dimension vsize          = viewport.size();
        Dimension scrollAreaSize = getScrollAreaSize(); 
        int max = scrollAreaSize.width - vsize.width;

        hbar.setValues(hbar.getValue(), // value
                       vsize.width,     // amt visible/page
                       0,               // minimum
                       max);            // maximum

        setHorizontalLineAndPageIncrements();
    }
    protected void setVerticalScrollbarValues() {
        Dimension vsize          = viewport.size();
        Dimension scrollAreaSize = getScrollAreaSize(); 
        int max = scrollAreaSize.height - vsize.height;

        vbar.setValues(vbar.getValue(), // value
                       vsize.height,    // amt visible/page
                       0,               // minimum
                       max);            // maximum

        setVerticalLineAndPageIncrements();
    }
    protected void scrollLineUp  (Event event) { scroll(); }
    protected void scrollLineDown(Event event) { scroll(); }
    protected void scrollPageUp  (Event event) { scroll(); }
    protected void scrollPageDown(Event event) { scroll(); }
    protected void scrollAbsolute(Event event) { scroll(); }

    protected void setHorizontalLineAndPageIncrements() {
        Dimension size = getScrollAreaSize();
        hbar.setLineIncrement(size.width/10);
        hbar.setPageIncrement(size.width/5);
    }
    protected void setVerticalLineAndPageIncrements() {
        Dimension size = getScrollAreaSize();
        vbar.setLineIncrement(size.height/10);
        vbar.setPageIncrement(size.height/5);
    }
    protected void scroll() {
        scrollTo(hbar.getValue(), vbar.getValue());
    }
}