001: /*
002: * (C) Copyright IBM Corp. 1998-2005. All Rights Reserved.
003: *
004: * The program is provided "as is" without any warranty express or
005: * implied, including the warranty of non-infringement and the implied
006: * warranties of merchantibility and fitness for a particular purpose.
007: * IBM will not be liable for any damages suffered by you as a result
008: * of using the Program. In no event will IBM be liable for any
009: * special, indirect or consequential damages or lost profits even if
010: * IBM has been advised of the possibility of their occurrence. IBM
011: * will not be liable for any third party claims against you.
012: */
013: package com.ibm.richtext.textpanel;
014:
015: /*
016: 7/9/97 - changed some deprecated methods in Scrollbar
017: Also setting Unit and Block increment values. Maybe
018: it matters...
019: 6/29/98 - reimplemented this class. Now this class talks to
020: any component which implements Scroller.Client.
021: ScrollHolder is gone, too.
022: 2/4/99 - No longer a Panel. Also, doesn't create Scrollbars,
023: and in fact doesn't even use the Scrollbar class
024: directly.
025: */
026:
027: import java.awt.Component;
028: import java.awt.Rectangle;
029:
030: import java.awt.event.AdjustmentListener;
031: import java.awt.event.AdjustmentEvent;
032: import java.awt.Adjustable;
033:
034: /**
035: * This class manages the interaction between a scrollable client
036: * and vertical and horizontal scrollbars. It calls the client's
037: * scrollTo method in response to manipulation of the scroll bars.
038: *
039: * This class used to be a Panel containing the scrollbars and
040: * the client panel. As part of the migration away from direct
041: * AWT dependencies, this class is no longer part of the view
042: * hierarchy. Instead it simply keeps a reference to its
043: * client and scroll bars. It is the responsibility of higher-
044: * level classes to set up the view hierarchy.
045: */
046: final class Scroller implements AdjustmentListener {
047: static final String COPYRIGHT = "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";
048:
049: static interface Client {
050: void scrollTo(int x, int y);
051:
052: Rectangle getScrollSize();
053:
054: Rectangle getBounds();
055: }
056:
057: private Adjustable fHorizScrollBar = null;
058: private Adjustable fVertScrollBar = null;
059: private Client fClient = null;
060:
061: /**
062: * These are used if the respective Scrollbar is not present.
063: */
064: private int fHorizValue, fVertValue;
065:
066: private static final int DEFAULT_UNIT_INC = 10;
067:
068: /**
069: * Construct a new Scroller with the given Adjustables,
070: * which really should be scrollbars of some ilk.
071: * Also, the Adjustables are required to be AWT Components,
072: * so the Scroller can enable and disable them.
073: * However, a Scroller can work with either AWT Scrollbars
074: * or JFC JScrollbars.
075: * @param horizScrollBar the horizontal scrollbar. null if
076: * there is no horizontal scrollbar.
077: * @param vertScrollBar the vertical scrollbar. null if
078: * there is no vertical scrollbar.
079: */
080: public Scroller(Adjustable horizScrollBar, Adjustable vertScrollBar) {
081:
082: //setLayout(new ScrollBarLayout());
083:
084: fHorizScrollBar = horizScrollBar;
085: fVertScrollBar = vertScrollBar;
086:
087: if (fVertScrollBar != null) {
088: fVertScrollBar.setUnitIncrement(DEFAULT_UNIT_INC);
089: fVertScrollBar.addAdjustmentListener(this );
090: }
091: if (fHorizScrollBar != null) {
092: fHorizScrollBar.setUnitIncrement(DEFAULT_UNIT_INC);
093: fHorizScrollBar.addAdjustmentListener(this );
094: }
095: }
096:
097: public void setClient(Client client) {
098:
099: fClient = client;
100: clientScrollSizeChanged();
101: }
102:
103: public void adjustmentValueChanged(AdjustmentEvent event) {
104:
105: // variable not used boolean horizontal;
106: if (event.getAdjustable() == fHorizScrollBar) {
107: int vertVal = fVertScrollBar == null ? fVertValue
108: : fVertScrollBar.getValue();
109: scrollTo(event.getValue(), vertVal);
110: } else {
111: int horizVal = fHorizScrollBar == null ? fHorizValue
112: : fHorizScrollBar.getValue();
113: scrollTo(horizVal, event.getValue());
114: }
115: }
116:
117: private void setValues(Adjustable scrollbar, int visible,
118: int minimum, int height) {
119:
120: int maximum = minimum + height;
121:
122: if (scrollbar != null) {
123:
124: Component scrollbarToo = (Component) scrollbar;
125:
126: if (maximum <= visible) {
127: scrollbarToo.setEnabled(false);
128: } else {
129: scrollbarToo.setEnabled(true);
130: }
131:
132: scrollbar.setMinimum(minimum);
133: scrollbar.setMaximum(maximum);
134: scrollbar.setVisibleAmount(visible);
135: // workaround setBlockIncrement warnings for increments < 1
136: scrollbar.setBlockIncrement(Math.max(1, visible
137: - DEFAULT_UNIT_INC));
138: }
139: }
140:
141: public void clientScrollSizeChanged() {
142: Rectangle bounds = fClient.getBounds();
143: Rectangle preferredSize = fClient.getScrollSize();
144:
145: setValues(fHorizScrollBar, bounds.width, preferredSize.x,
146: preferredSize.width);
147: setValues(fVertScrollBar, bounds.height, preferredSize.y,
148: preferredSize.height);
149: }
150:
151: public void setPosition(int x, int y) {
152:
153: if (fHorizScrollBar != null) {
154: fHorizScrollBar.setValue(x);
155: } else {
156: fHorizValue = x;
157: }
158: if (fVertScrollBar != null) {
159: fVertScrollBar.setValue(y);
160: } else {
161: fVertValue = y;
162: }
163: }
164:
165: private void scrollTo(int x, int y) {
166: fClient.scrollTo(x, y);
167: }
168:
169: public void setHorizLineDistance(int newDistance) {
170: if (fHorizScrollBar != null) {
171: fHorizScrollBar.setUnitIncrement(newDistance);
172: }
173: }
174:
175: public void setHorizPageOverlap(int newOverlap) {
176: if (fHorizScrollBar != null) {
177: fHorizScrollBar.setBlockIncrement( // workaround warnings for values < 1 on unix
178: Math.max(1, fHorizScrollBar.getVisibleAmount()
179: - newOverlap));
180: }
181: }
182:
183: public void setVertLineDistance(int newDistance) {
184: if (fVertScrollBar != null) {
185: fVertScrollBar.setUnitIncrement(newDistance);
186: }
187: }
188:
189: public void setVertPageOverlap(int newOverlap) {
190: if (fVertScrollBar != null) {
191: fVertScrollBar.setBlockIncrement( // workaround warnings for values < 1 on unix
192: Math.max(1, fVertScrollBar.getVisibleAmount()
193: - newOverlap));
194: }
195: }
196: }
|