]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/richtext/textpanel/Scroller.java
icu4jsrc
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / richtext / textpanel / Scroller.java
1 /*\r
2  * (C) Copyright IBM Corp. 1998-2005.  All Rights Reserved.\r
3  *\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
12  */\r
13 package com.ibm.richtext.textpanel;\r
14 \r
15 /*\r
16     7/9/97 - changed some deprecated methods in Scrollbar\r
17             Also setting Unit and Block increment values.  Maybe\r
18             it matters...\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
24              directly.\r
25 */\r
26 \r
27 import java.awt.Component;\r
28 import java.awt.Rectangle;\r
29 \r
30 import java.awt.event.AdjustmentListener;\r
31 import java.awt.event.AdjustmentEvent;\r
32 import java.awt.Adjustable;\r
33 \r
34 /**\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
38 *\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
45 */\r
46 final class Scroller implements AdjustmentListener\r
47 {\r
48     static final String COPYRIGHT =\r
49                 "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";\r
50 \r
51     static interface Client {\r
52         void scrollTo(int x, int y);\r
53         Rectangle getScrollSize();\r
54         Rectangle getBounds();\r
55     }\r
56 \r
57     private Adjustable fHorizScrollBar = null;\r
58     private Adjustable fVertScrollBar = null;\r
59     private Client fClient = null;\r
60 \r
61     /**\r
62      * These are used if the respective Scrollbar is not present.\r
63      */\r
64     private int fHorizValue, fVertValue;\r
65 \r
66     private static final int DEFAULT_UNIT_INC = 10;\r
67 \r
68     /**\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
79      */\r
80     public Scroller(Adjustable horizScrollBar,\r
81                     Adjustable vertScrollBar) {\r
82 \r
83         //setLayout(new ScrollBarLayout());\r
84 \r
85         fHorizScrollBar = horizScrollBar;\r
86         fVertScrollBar = vertScrollBar;\r
87 \r
88         if (fVertScrollBar != null) {\r
89             fVertScrollBar.setUnitIncrement(DEFAULT_UNIT_INC);\r
90             fVertScrollBar.addAdjustmentListener(this);\r
91         }\r
92         if (fHorizScrollBar != null) {\r
93             fHorizScrollBar.setUnitIncrement(DEFAULT_UNIT_INC);\r
94             fHorizScrollBar.addAdjustmentListener(this);\r
95         }\r
96     }\r
97 \r
98     public void setClient(Client client) {\r
99 \r
100         fClient = client;\r
101         clientScrollSizeChanged();\r
102     }\r
103 \r
104     public void adjustmentValueChanged(AdjustmentEvent event) {\r
105 \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
111         }\r
112         else {\r
113             int horizVal = fHorizScrollBar == null? fHorizValue :\r
114                                         fHorizScrollBar.getValue();\r
115             scrollTo(horizVal, event.getValue());\r
116         }\r
117     }\r
118 \r
119     private void setValues(Adjustable scrollbar,\r
120                            int visible,\r
121                            int minimum,\r
122                            int height) {\r
123 \r
124         int maximum = minimum+height;\r
125         \r
126         if (scrollbar != null) {\r
127 \r
128             Component scrollbarToo = (Component) scrollbar;\r
129 \r
130             if (maximum <= visible) {\r
131                 scrollbarToo.setEnabled(false);\r
132             }\r
133             else {\r
134                 scrollbarToo.setEnabled(true);\r
135             }\r
136             \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
142         }\r
143     }\r
144 \r
145 \r
146     public void clientScrollSizeChanged()\r
147     {\r
148         Rectangle bounds = fClient.getBounds();\r
149         Rectangle preferredSize = fClient.getScrollSize();\r
150 \r
151         setValues(fHorizScrollBar, bounds.width, preferredSize.x, preferredSize.width);\r
152         setValues(fVertScrollBar, bounds.height, preferredSize.y, preferredSize.height);\r
153     }\r
154 \r
155     public void setPosition(int x, int y) {\r
156 \r
157         if (fHorizScrollBar != null) {\r
158             fHorizScrollBar.setValue(x);\r
159         }\r
160         else {\r
161             fHorizValue = x;\r
162         }\r
163         if (fVertScrollBar != null) {\r
164             fVertScrollBar.setValue(y);\r
165         }\r
166         else {\r
167             fVertValue = y;\r
168         }\r
169     }\r
170 \r
171     private void scrollTo(int x, int y)\r
172     {\r
173         fClient.scrollTo(x, y);\r
174     }\r
175 \r
176     public void setHorizLineDistance(int newDistance)\r
177     {\r
178         if (fHorizScrollBar != null) {\r
179             fHorizScrollBar.setUnitIncrement(newDistance);\r
180         }\r
181     }\r
182 \r
183     public void setHorizPageOverlap(int newOverlap)\r
184     {\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
188         }\r
189     }\r
190 \r
191     public void setVertLineDistance(int newDistance)\r
192     {\r
193         if (fVertScrollBar != null) {\r
194             fVertScrollBar.setUnitIncrement(newDistance);\r
195         }\r
196     }\r
197 \r
198     public void setVertPageOverlap(int newOverlap)\r
199     {\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
203         }\r
204     }\r
205 }\r