summaryrefslogtreecommitdiff
path: root/java/gjt/ScrollerLayout.java
blob: 21012fd5688edc4e3ff0df2711372d53a5553f9d (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
155
156
157
158
159
160
package gjt;

import java.awt.*;

/**
 * Layout manager for a Scroller.<p>
 *
 * Lays out 3 Components:  a horizontal scrollbar, a vertical 
 * scrollbar and a viewport (Panel).<p>
 *
 * Valid names/Component pairs that can be added via 
 * addLayoutComponent(String, Component):<p>
 * <dl>
 * <dd> "East"   Scrollbar (vertical)
 * <dd> "West"   Scrollbar (vertical)
 * <dd> "North"  Scrollbar (horizontal)
 * <dd> "South"  Scrollbar (horizontal)
 * <dd> "Scroll" Panel (viewport)
 * </dl>
 *
 * @version 1.0, Apr 1 1996
 * @author  David Geary
 * @see     Scroller
 */
public class ScrollerLayout implements LayoutManager {
    private Scroller  scroller;
    private Scrollbar hbar, vbar;
    private String    hbarPosition, vbarPosition;
    private Component viewport;
    private int       top, bottom, right, left;

    public ScrollerLayout(Scroller scroller) {
        this.scroller = scroller;
    }

    public void addLayoutComponent(String name, 
                                   Component comp) {
        Assert.notFalse(comp != null);

        if(comp instanceof Scrollbar) {
            Scrollbar sbar = (Scrollbar)comp;

            if(sbar.getOrientation() == Scrollbar.VERTICAL) {
                Assert.notFalse("East".equals(name) == true ||
                                "West".equals(name) == true);
                vbar         = sbar;
                vbarPosition = name;
            }
            else {
                Assert.notFalse("North".equals(name) == true ||
                                "South".equals(name) == true);
                hbar         = sbar;
                hbarPosition = name;
            }
        }
        else {
            Assert.notFalse("Scroll".equals(name) == true);
            viewport = comp;
        }
    }
    public void removeLayoutComponent(Component comp) {
        if(comp == vbar)     vbar     = null;
        if(comp == hbar)     hbar     = null;
        if(comp == viewport) viewport = null;
    }
    public Dimension preferredLayoutSize(Container parent) {
        Dimension dim = new Dimension(0,0);

        if(vbar != null && vbar.isVisible()) {
            Dimension d = vbar.preferredSize();
            dim.width += d.width;
            dim.height = d.height;
        }
        if(hbar != null && hbar.isVisible()) {
            Dimension d = hbar.preferredSize();
            dim.width += d.width;
            dim.height = Math.max(d.height, dim.height);
        }
        if(viewport != null && viewport.isVisible()) {
            Dimension d = viewport.preferredSize();
            dim.width += d.width;
            dim.height = Math.max(d.height, dim.height);
        }
        return dim;
    }
    public Dimension minimumLayoutSize(Container parent) {
        Dimension dim = new Dimension(0,0);

        if(vbar != null && vbar.isVisible()) {
            Dimension d = vbar.minimumSize();
            dim.width += d.width;
            dim.height = d.height;
        }
        if(hbar != null && hbar.isVisible()) {
            Dimension d = hbar.minimumSize();
            dim.width += d.width;
            dim.height = Math.max(d.height, dim.height);
        }
        if(viewport != null && viewport.isVisible()) {
            Dimension d = viewport.minimumSize();
            dim.width += d.width;
            dim.height = Math.max(d.height, dim.height);
        }
        return dim;
    }
    public void layoutContainer(Container target) {
        Insets insets        = target.insets();
        Dimension targetSize = target.size();

        top    = insets.top;
        bottom = targetSize.height - insets.bottom;
        left   = insets.left;
        right  = targetSize.width - insets.right;

        scroller.manageScrollbars();

        reshapeHorizontalScrollbar();
        reshapeVerticalScrollbar  ();
        reshapeViewport           ();

        scroller.setScrollbarValues();
    }
    private void reshapeHorizontalScrollbar() {
        if(hbar != null && hbar.isVisible()) {
            if("North".equals(hbarPosition)) {
                Dimension d = hbar.preferredSize();
                hbar.reshape(left, top, right - left, d.height);
                top += d.height;
            }
            else {  // South
                Dimension d = hbar.preferredSize();
                hbar.reshape(left, bottom - d.height,
                            right - left,d.height);
                bottom -= d.height;
            }
        }
    }
    private void reshapeVerticalScrollbar() {
        if(hbar != null && vbar.isVisible()) {
            if("East".equals(vbarPosition)) {
                Dimension d = vbar.preferredSize();
                vbar.reshape(right - d.width, top, 
                             d.width, bottom - top);
                right -= d.width;
            }
            else { // West
                Dimension d = vbar.preferredSize();
                vbar.reshape(left, top, 
                             d.width, bottom - top);
                left += d.width;
            }
        }
    }
    private void reshapeViewport() {
        if(viewport != null && viewport.isVisible()) {
            viewport.reshape(left, top, 
                             right - left, bottom - top);
        }
    }
}