2 * (C) Copyright IBM Corp. 1998-2005. All Rights Reserved.
\r
4 * The program is provided "as is" without any warranty express or
\r
5 * implied, including the warranty of non-infringement and the implied
\r
6 * warranties of merchantibility and fitness for a particular purpose.
\r
7 * IBM will not be liable for any damages suffered by you as a result
\r
8 * of using the Program. In no event will IBM be liable for any
\r
9 * special, indirect or consequential damages or lost profits even if
\r
10 * IBM has been advised of the possibility of their occurrence. IBM
\r
11 * will not be liable for any third party claims against you.
\r
13 package com.ibm.richtext.textpanel;
\r
16 7/9/97 - changed some deprecated methods in Scrollbar
\r
17 Also setting Unit and Block increment values. Maybe
\r
19 6/29/98 - reimplemented this class. Now this class talks to
\r
20 any component which implements Scroller.Client.
\r
21 ScrollHolder is gone, too.
\r
22 2/4/99 - No longer a Panel. Also, doesn't create Scrollbars,
\r
23 and in fact doesn't even use the Scrollbar class
\r
27 import java.awt.Component;
\r
28 import java.awt.Rectangle;
\r
30 import java.awt.event.AdjustmentListener;
\r
31 import java.awt.event.AdjustmentEvent;
\r
32 import java.awt.Adjustable;
\r
35 * This class manages the interaction between a scrollable client
\r
36 * and vertical and horizontal scrollbars. It calls the client's
\r
37 * scrollTo method in response to manipulation of the scroll bars.
\r
39 * This class used to be a Panel containing the scrollbars and
\r
40 * the client panel. As part of the migration away from direct
\r
41 * AWT dependencies, this class is no longer part of the view
\r
42 * hierarchy. Instead it simply keeps a reference to its
\r
43 * client and scroll bars. It is the responsibility of higher-
\r
44 * level classes to set up the view hierarchy.
\r
46 final class Scroller implements AdjustmentListener
\r
48 static final String COPYRIGHT =
\r
49 "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";
\r
51 static interface Client {
\r
52 void scrollTo(int x, int y);
\r
53 Rectangle getScrollSize();
\r
54 Rectangle getBounds();
\r
57 private Adjustable fHorizScrollBar = null;
\r
58 private Adjustable fVertScrollBar = null;
\r
59 private Client fClient = null;
\r
62 * These are used if the respective Scrollbar is not present.
\r
64 private int fHorizValue, fVertValue;
\r
66 private static final int DEFAULT_UNIT_INC = 10;
\r
69 * Construct a new Scroller with the given Adjustables,
\r
70 * which really should be scrollbars of some ilk.
\r
71 * Also, the Adjustables are required to be AWT Components,
\r
72 * so the Scroller can enable and disable them.
\r
73 * However, a Scroller can work with either AWT Scrollbars
\r
74 * or JFC JScrollbars.
\r
75 * @param horizScrollBar the horizontal scrollbar. null if
\r
76 * there is no horizontal scrollbar.
\r
77 * @param vertScrollBar the vertical scrollbar. null if
\r
78 * there is no vertical scrollbar.
\r
80 public Scroller(Adjustable horizScrollBar,
\r
81 Adjustable vertScrollBar) {
\r
83 //setLayout(new ScrollBarLayout());
\r
85 fHorizScrollBar = horizScrollBar;
\r
86 fVertScrollBar = vertScrollBar;
\r
88 if (fVertScrollBar != null) {
\r
89 fVertScrollBar.setUnitIncrement(DEFAULT_UNIT_INC);
\r
90 fVertScrollBar.addAdjustmentListener(this);
\r
92 if (fHorizScrollBar != null) {
\r
93 fHorizScrollBar.setUnitIncrement(DEFAULT_UNIT_INC);
\r
94 fHorizScrollBar.addAdjustmentListener(this);
\r
98 public void setClient(Client client) {
\r
101 clientScrollSizeChanged();
\r
104 public void adjustmentValueChanged(AdjustmentEvent event) {
\r
106 // variable not used boolean horizontal;
\r
107 if (event.getAdjustable() == fHorizScrollBar) {
\r
108 int vertVal = fVertScrollBar == null? fVertValue :
\r
109 fVertScrollBar.getValue();
\r
110 scrollTo(event.getValue(), vertVal);
\r
113 int horizVal = fHorizScrollBar == null? fHorizValue :
\r
114 fHorizScrollBar.getValue();
\r
115 scrollTo(horizVal, event.getValue());
\r
119 private void setValues(Adjustable scrollbar,
\r
124 int maximum = minimum+height;
\r
126 if (scrollbar != null) {
\r
128 Component scrollbarToo = (Component) scrollbar;
\r
130 if (maximum <= visible) {
\r
131 scrollbarToo.setEnabled(false);
\r
134 scrollbarToo.setEnabled(true);
\r
137 scrollbar.setMinimum(minimum);
\r
138 scrollbar.setMaximum(maximum);
\r
139 scrollbar.setVisibleAmount(visible);
\r
140 // workaround setBlockIncrement warnings for increments < 1
\r
141 scrollbar.setBlockIncrement(Math.max(1, visible - DEFAULT_UNIT_INC));
\r
146 public void clientScrollSizeChanged()
\r
148 Rectangle bounds = fClient.getBounds();
\r
149 Rectangle preferredSize = fClient.getScrollSize();
\r
151 setValues(fHorizScrollBar, bounds.width, preferredSize.x, preferredSize.width);
\r
152 setValues(fVertScrollBar, bounds.height, preferredSize.y, preferredSize.height);
\r
155 public void setPosition(int x, int y) {
\r
157 if (fHorizScrollBar != null) {
\r
158 fHorizScrollBar.setValue(x);
\r
163 if (fVertScrollBar != null) {
\r
164 fVertScrollBar.setValue(y);
\r
171 private void scrollTo(int x, int y)
\r
173 fClient.scrollTo(x, y);
\r
176 public void setHorizLineDistance(int newDistance)
\r
178 if (fHorizScrollBar != null) {
\r
179 fHorizScrollBar.setUnitIncrement(newDistance);
\r
183 public void setHorizPageOverlap(int newOverlap)
\r
185 if (fHorizScrollBar != null) {
\r
186 fHorizScrollBar.setBlockIncrement( // workaround warnings for values < 1 on unix
\r
187 Math.max(1, fHorizScrollBar.getVisibleAmount()-newOverlap));
\r
191 public void setVertLineDistance(int newDistance)
\r
193 if (fVertScrollBar != null) {
\r
194 fVertScrollBar.setUnitIncrement(newDistance);
\r
198 public void setVertPageOverlap(int newOverlap)
\r
200 if (fVertScrollBar != null) {
\r
201 fVertScrollBar.setBlockIncrement( // workaround warnings for values < 1 on unix
\r
202 Math.max(1, fVertScrollBar.getVisibleAmount()-newOverlap));
\r