annotate agent/src/share/classes/sun/jvm/hotspot/ui/HighPrecisionJScrollBar.java @ 113:ba764ed4b6f2

6420645: Create a vm that uses compressed oops for up to 32gb heapsizes Summary: Compressed oops in instances, arrays, and headers. Code contributors are coleenp, phh, never, swamyv Reviewed-by: jmasa, kamg, acorn, tbell, kvn, rasbold
author coleenp
date Sun, 13 Apr 2008 17:43:42 -0400
parents a61af66fc99e
children c70a245cad3a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a61af66fc99e Initial load
duke
parents:
diff changeset
1 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
2 * Copyright 2000-2002 Sun Microsystems, Inc. All Rights Reserved.
a61af66fc99e Initial load
duke
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a61af66fc99e Initial load
duke
parents:
diff changeset
4 *
a61af66fc99e Initial load
duke
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
a61af66fc99e Initial load
duke
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
a61af66fc99e Initial load
duke
parents:
diff changeset
7 * published by the Free Software Foundation.
a61af66fc99e Initial load
duke
parents:
diff changeset
8 *
a61af66fc99e Initial load
duke
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
a61af66fc99e Initial load
duke
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a61af66fc99e Initial load
duke
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
a61af66fc99e Initial load
duke
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
a61af66fc99e Initial load
duke
parents:
diff changeset
13 * accompanied this code).
a61af66fc99e Initial load
duke
parents:
diff changeset
14 *
a61af66fc99e Initial load
duke
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
a61af66fc99e Initial load
duke
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
a61af66fc99e Initial load
duke
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a61af66fc99e Initial load
duke
parents:
diff changeset
18 *
a61af66fc99e Initial load
duke
parents:
diff changeset
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
a61af66fc99e Initial load
duke
parents:
diff changeset
20 * CA 95054 USA or visit www.sun.com if you need additional information or
a61af66fc99e Initial load
duke
parents:
diff changeset
21 * have any questions.
a61af66fc99e Initial load
duke
parents:
diff changeset
22 *
a61af66fc99e Initial load
duke
parents:
diff changeset
23 */
a61af66fc99e Initial load
duke
parents:
diff changeset
24
a61af66fc99e Initial load
duke
parents:
diff changeset
25 package sun.jvm.hotspot.ui;
a61af66fc99e Initial load
duke
parents:
diff changeset
26
a61af66fc99e Initial load
duke
parents:
diff changeset
27 import java.awt.event.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
28 import javax.swing.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
29 import javax.swing.event.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
30 import java.math.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
31 import java.util.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
32
a61af66fc99e Initial load
duke
parents:
diff changeset
33 /** A JScrollBar which uses BigIntegers as the representation for the
a61af66fc99e Initial load
duke
parents:
diff changeset
34 minimum, maximum, unit increment, etc. Interaction with the
a61af66fc99e Initial load
duke
parents:
diff changeset
35 buttons and track is accurate to unit and block increments;
a61af66fc99e Initial load
duke
parents:
diff changeset
36 however, if the scale of the scrollbar (defined by
a61af66fc99e Initial load
duke
parents:
diff changeset
37 getMaximumHP().subtract(getMinimumHP())) is very large, each
a61af66fc99e Initial load
duke
parents:
diff changeset
38 interaction with the thumb will necessarily cause extremely large
a61af66fc99e Initial load
duke
parents:
diff changeset
39 motion of the value. */
a61af66fc99e Initial load
duke
parents:
diff changeset
40
a61af66fc99e Initial load
duke
parents:
diff changeset
41 public class HighPrecisionJScrollBar extends JScrollBar {
a61af66fc99e Initial load
duke
parents:
diff changeset
42 private BigInteger valueHP;
a61af66fc99e Initial load
duke
parents:
diff changeset
43 private BigInteger visibleHP;
a61af66fc99e Initial load
duke
parents:
diff changeset
44 private BigInteger minimumHP;
a61af66fc99e Initial load
duke
parents:
diff changeset
45 private BigInteger maximumHP;
a61af66fc99e Initial load
duke
parents:
diff changeset
46 private BigInteger unitIncrementHP;
a61af66fc99e Initial load
duke
parents:
diff changeset
47 private BigInteger blockIncrementHP;
a61af66fc99e Initial load
duke
parents:
diff changeset
48 private BigDecimal scaleFactor;
a61af66fc99e Initial load
duke
parents:
diff changeset
49 private BigInteger rangeHP;
a61af66fc99e Initial load
duke
parents:
diff changeset
50 // The underlying scrollbar runs a range from 0..BIG_RANGE-1
a61af66fc99e Initial load
duke
parents:
diff changeset
51 private static final int BIG_RANGE = 10000;
a61af66fc99e Initial load
duke
parents:
diff changeset
52 // Do we need to scale HP values up/down to fit in 0..BIG_RANGE-1?
a61af66fc99e Initial load
duke
parents:
diff changeset
53 private boolean down;
a61af66fc99e Initial load
duke
parents:
diff changeset
54 private java.util.List changeListeners = new ArrayList();
a61af66fc99e Initial load
duke
parents:
diff changeset
55 // Number of digits after decimal point to use when scaling between
a61af66fc99e Initial load
duke
parents:
diff changeset
56 // high and low precision
a61af66fc99e Initial load
duke
parents:
diff changeset
57 private static final int SCALE = 20;
a61af66fc99e Initial load
duke
parents:
diff changeset
58
a61af66fc99e Initial load
duke
parents:
diff changeset
59
a61af66fc99e Initial load
duke
parents:
diff changeset
60 // This is a hack to allow us to differentiate between clicks on the
a61af66fc99e Initial load
duke
parents:
diff changeset
61 // arrow and track since we can't get useful information from
a61af66fc99e Initial load
duke
parents:
diff changeset
62 // JScrollBars' AdjustmentListener (bug in design of BasicUI
a61af66fc99e Initial load
duke
parents:
diff changeset
63 // classes; FIXME: file RFE.)
a61af66fc99e Initial load
duke
parents:
diff changeset
64 private static final int UNIT_INCREMENT = 1;
a61af66fc99e Initial load
duke
parents:
diff changeset
65 private static final int BLOCK_INCREMENT = 2;
a61af66fc99e Initial load
duke
parents:
diff changeset
66 private static final int MINIMUM = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
67 private static final int MAXIMUM = 65536;
a61af66fc99e Initial load
duke
parents:
diff changeset
68 private boolean updating = false;
a61af66fc99e Initial load
duke
parents:
diff changeset
69 private int lastValueSeen = -1;
a61af66fc99e Initial load
duke
parents:
diff changeset
70
a61af66fc99e Initial load
duke
parents:
diff changeset
71 public HighPrecisionJScrollBar() {
a61af66fc99e Initial load
duke
parents:
diff changeset
72 super();
a61af66fc99e Initial load
duke
parents:
diff changeset
73 initialize();
a61af66fc99e Initial load
duke
parents:
diff changeset
74 installListener();
a61af66fc99e Initial load
duke
parents:
diff changeset
75 }
a61af66fc99e Initial load
duke
parents:
diff changeset
76
a61af66fc99e Initial load
duke
parents:
diff changeset
77 public HighPrecisionJScrollBar(int orientation) {
a61af66fc99e Initial load
duke
parents:
diff changeset
78 super(orientation);
a61af66fc99e Initial load
duke
parents:
diff changeset
79 initialize();
a61af66fc99e Initial load
duke
parents:
diff changeset
80 installListener();
a61af66fc99e Initial load
duke
parents:
diff changeset
81 }
a61af66fc99e Initial load
duke
parents:
diff changeset
82
a61af66fc99e Initial load
duke
parents:
diff changeset
83 /** value, minimum and maximum should be positive */
a61af66fc99e Initial load
duke
parents:
diff changeset
84 public HighPrecisionJScrollBar(int orientation, BigInteger value, BigInteger minimum, BigInteger maximum) {
a61af66fc99e Initial load
duke
parents:
diff changeset
85 super(orientation);
a61af66fc99e Initial load
duke
parents:
diff changeset
86 initialize(value, minimum, maximum);
a61af66fc99e Initial load
duke
parents:
diff changeset
87 installListener();
a61af66fc99e Initial load
duke
parents:
diff changeset
88 }
a61af66fc99e Initial load
duke
parents:
diff changeset
89
a61af66fc99e Initial load
duke
parents:
diff changeset
90 public BigInteger getValueHP() {
a61af66fc99e Initial load
duke
parents:
diff changeset
91 return valueHP;
a61af66fc99e Initial load
duke
parents:
diff changeset
92 }
a61af66fc99e Initial load
duke
parents:
diff changeset
93
a61af66fc99e Initial load
duke
parents:
diff changeset
94
a61af66fc99e Initial load
duke
parents:
diff changeset
95 /** NOTE: the real value will always be set to be (value mod
a61af66fc99e Initial load
duke
parents:
diff changeset
96 unitIncrement) == 0, subtracting off the mod of the passed value
a61af66fc99e Initial load
duke
parents:
diff changeset
97 if necessary. */
a61af66fc99e Initial load
duke
parents:
diff changeset
98
a61af66fc99e Initial load
duke
parents:
diff changeset
99 public void setValueHP(BigInteger value) {
a61af66fc99e Initial load
duke
parents:
diff changeset
100 if (value.compareTo(getMaximumHP()) > 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
101 value = getMaximumHP();
a61af66fc99e Initial load
duke
parents:
diff changeset
102 } else if (value.compareTo(getMinimumHP()) < 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
103 value = getMinimumHP();
a61af66fc99e Initial load
duke
parents:
diff changeset
104 }
a61af66fc99e Initial load
duke
parents:
diff changeset
105 valueHP = value.subtract(value.mod(unitIncrementHP));
a61af66fc99e Initial load
duke
parents:
diff changeset
106 int lpValue = toUnderlyingRange(this.valueHP);
a61af66fc99e Initial load
duke
parents:
diff changeset
107 if (getValueHP().add(getVisibleAmountHP()).compareTo(getMaximumHP()) >= 0 ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
108 lpValue = BIG_RANGE - getVisibleAmount();
a61af66fc99e Initial load
duke
parents:
diff changeset
109 }
a61af66fc99e Initial load
duke
parents:
diff changeset
110 lastValueSeen = lpValue;
a61af66fc99e Initial load
duke
parents:
diff changeset
111 setValue(lpValue);
a61af66fc99e Initial load
duke
parents:
diff changeset
112 fireStateChanged();
a61af66fc99e Initial load
duke
parents:
diff changeset
113 }
a61af66fc99e Initial load
duke
parents:
diff changeset
114 public BigInteger getMinimumHP() {
a61af66fc99e Initial load
duke
parents:
diff changeset
115 return minimumHP;
a61af66fc99e Initial load
duke
parents:
diff changeset
116 }
a61af66fc99e Initial load
duke
parents:
diff changeset
117
a61af66fc99e Initial load
duke
parents:
diff changeset
118 public void setMinimumHP(BigInteger minimum) {
a61af66fc99e Initial load
duke
parents:
diff changeset
119 setRange(minimum, maximumHP);
a61af66fc99e Initial load
duke
parents:
diff changeset
120 updateScrollBarValues();
a61af66fc99e Initial load
duke
parents:
diff changeset
121 }
a61af66fc99e Initial load
duke
parents:
diff changeset
122
a61af66fc99e Initial load
duke
parents:
diff changeset
123 public BigInteger getMaximumHP() {
a61af66fc99e Initial load
duke
parents:
diff changeset
124 return maximumHP;
a61af66fc99e Initial load
duke
parents:
diff changeset
125 }
a61af66fc99e Initial load
duke
parents:
diff changeset
126
a61af66fc99e Initial load
duke
parents:
diff changeset
127 public void setMaximumHP(BigInteger maximum) {
a61af66fc99e Initial load
duke
parents:
diff changeset
128 setRange(minimumHP, maximum);
a61af66fc99e Initial load
duke
parents:
diff changeset
129 updateScrollBarValues();
a61af66fc99e Initial load
duke
parents:
diff changeset
130 }
a61af66fc99e Initial load
duke
parents:
diff changeset
131
a61af66fc99e Initial load
duke
parents:
diff changeset
132 public BigInteger getVisibleAmountHP() {
a61af66fc99e Initial load
duke
parents:
diff changeset
133 return visibleHP;
a61af66fc99e Initial load
duke
parents:
diff changeset
134 }
a61af66fc99e Initial load
duke
parents:
diff changeset
135
a61af66fc99e Initial load
duke
parents:
diff changeset
136 public void setVisibleAmountHP(BigInteger visibleAmount) {
a61af66fc99e Initial load
duke
parents:
diff changeset
137 this.visibleHP = visibleAmount;
a61af66fc99e Initial load
duke
parents:
diff changeset
138 // int lpVisAmt = toUnderlyingRange(visibleAmount);
a61af66fc99e Initial load
duke
parents:
diff changeset
139 // Make certain that visibleAmount value that are full range come out looking like full range
a61af66fc99e Initial load
duke
parents:
diff changeset
140 int lpVisAmt;
a61af66fc99e Initial load
duke
parents:
diff changeset
141 if (visibleAmount.compareTo(rangeHP) < 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
142 lpVisAmt = scaleToUnderlying(visibleAmount);
a61af66fc99e Initial load
duke
parents:
diff changeset
143 if (lpVisAmt == 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
144 lpVisAmt = 1;
a61af66fc99e Initial load
duke
parents:
diff changeset
145 }
a61af66fc99e Initial load
duke
parents:
diff changeset
146 setVisible(true);
a61af66fc99e Initial load
duke
parents:
diff changeset
147 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
148 lpVisAmt = BIG_RANGE;
a61af66fc99e Initial load
duke
parents:
diff changeset
149 setVisible(false);
a61af66fc99e Initial load
duke
parents:
diff changeset
150 }
a61af66fc99e Initial load
duke
parents:
diff changeset
151 setVisibleAmount(lpVisAmt);
a61af66fc99e Initial load
duke
parents:
diff changeset
152 }
a61af66fc99e Initial load
duke
parents:
diff changeset
153
a61af66fc99e Initial load
duke
parents:
diff changeset
154 public BigInteger getBlockIncrementHP() {
a61af66fc99e Initial load
duke
parents:
diff changeset
155 return blockIncrementHP;
a61af66fc99e Initial load
duke
parents:
diff changeset
156 }
a61af66fc99e Initial load
duke
parents:
diff changeset
157
a61af66fc99e Initial load
duke
parents:
diff changeset
158 public void setBlockIncrementHP(BigInteger blockIncrement) {
a61af66fc99e Initial load
duke
parents:
diff changeset
159 this.blockIncrementHP = blockIncrement;
a61af66fc99e Initial load
duke
parents:
diff changeset
160 // NOTE we do not forward this to the underlying scrollBar because of
a61af66fc99e Initial load
duke
parents:
diff changeset
161 // the earlier mentioned hack.
a61af66fc99e Initial load
duke
parents:
diff changeset
162 }
a61af66fc99e Initial load
duke
parents:
diff changeset
163
a61af66fc99e Initial load
duke
parents:
diff changeset
164 public BigInteger getUnitIncrementHP() {
a61af66fc99e Initial load
duke
parents:
diff changeset
165 return unitIncrementHP;
a61af66fc99e Initial load
duke
parents:
diff changeset
166 }
a61af66fc99e Initial load
duke
parents:
diff changeset
167
a61af66fc99e Initial load
duke
parents:
diff changeset
168 public void setUnitIncrementHP(BigInteger unitIncrement) {
a61af66fc99e Initial load
duke
parents:
diff changeset
169 this.unitIncrementHP = unitIncrement;
a61af66fc99e Initial load
duke
parents:
diff changeset
170 // NOTE we do not forward this to the underlying scrollBar because of
a61af66fc99e Initial load
duke
parents:
diff changeset
171 // the earlier mentioned hack.
a61af66fc99e Initial load
duke
parents:
diff changeset
172 }
a61af66fc99e Initial load
duke
parents:
diff changeset
173
a61af66fc99e Initial load
duke
parents:
diff changeset
174
a61af66fc99e Initial load
duke
parents:
diff changeset
175 public void addChangeListener(ChangeListener l) {
a61af66fc99e Initial load
duke
parents:
diff changeset
176 changeListeners.add(l);
a61af66fc99e Initial load
duke
parents:
diff changeset
177 }
a61af66fc99e Initial load
duke
parents:
diff changeset
178
a61af66fc99e Initial load
duke
parents:
diff changeset
179 public void removeChangeListener(ChangeListener l) {
a61af66fc99e Initial load
duke
parents:
diff changeset
180 changeListeners.remove(l);
a61af66fc99e Initial load
duke
parents:
diff changeset
181 }
a61af66fc99e Initial load
duke
parents:
diff changeset
182
a61af66fc99e Initial load
duke
parents:
diff changeset
183 //----------------------------------------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
184 // Programmatic access to scrollbar functionality
a61af66fc99e Initial load
duke
parents:
diff changeset
185 // (Causes change events to be sent)
a61af66fc99e Initial load
duke
parents:
diff changeset
186
a61af66fc99e Initial load
duke
parents:
diff changeset
187 public void scrollUpOrLeft() {
a61af66fc99e Initial load
duke
parents:
diff changeset
188 if (updating) return;
a61af66fc99e Initial load
duke
parents:
diff changeset
189 beginUpdate();
a61af66fc99e Initial load
duke
parents:
diff changeset
190 setValueHP(getValueHP().subtract(getUnitIncrementHP()));
a61af66fc99e Initial load
duke
parents:
diff changeset
191 endUpdate();
a61af66fc99e Initial load
duke
parents:
diff changeset
192 }
a61af66fc99e Initial load
duke
parents:
diff changeset
193
a61af66fc99e Initial load
duke
parents:
diff changeset
194 public void scrollDownOrRight() {
a61af66fc99e Initial load
duke
parents:
diff changeset
195 if (updating) return;
a61af66fc99e Initial load
duke
parents:
diff changeset
196 beginUpdate();
a61af66fc99e Initial load
duke
parents:
diff changeset
197 setValueHP(getValueHP().add(getUnitIncrementHP()));
a61af66fc99e Initial load
duke
parents:
diff changeset
198 endUpdate();
a61af66fc99e Initial load
duke
parents:
diff changeset
199 }
a61af66fc99e Initial load
duke
parents:
diff changeset
200
a61af66fc99e Initial load
duke
parents:
diff changeset
201 public void pageUpOrLeft() {
a61af66fc99e Initial load
duke
parents:
diff changeset
202 if (updating) return;
a61af66fc99e Initial load
duke
parents:
diff changeset
203 beginUpdate();
a61af66fc99e Initial load
duke
parents:
diff changeset
204 setValueHP(getValueHP().subtract(getBlockIncrementHP()));
a61af66fc99e Initial load
duke
parents:
diff changeset
205 endUpdate();
a61af66fc99e Initial load
duke
parents:
diff changeset
206 }
a61af66fc99e Initial load
duke
parents:
diff changeset
207
a61af66fc99e Initial load
duke
parents:
diff changeset
208 public void pageDownOrRight() {
a61af66fc99e Initial load
duke
parents:
diff changeset
209 if (updating) return;
a61af66fc99e Initial load
duke
parents:
diff changeset
210 beginUpdate();
a61af66fc99e Initial load
duke
parents:
diff changeset
211 setValueHP(getValueHP().add(getBlockIncrementHP()));
a61af66fc99e Initial load
duke
parents:
diff changeset
212 endUpdate();
a61af66fc99e Initial load
duke
parents:
diff changeset
213 }
a61af66fc99e Initial load
duke
parents:
diff changeset
214
a61af66fc99e Initial load
duke
parents:
diff changeset
215 //----------------------------------------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
216 // Internals only below this point
a61af66fc99e Initial load
duke
parents:
diff changeset
217 //
a61af66fc99e Initial load
duke
parents:
diff changeset
218
a61af66fc99e Initial load
duke
parents:
diff changeset
219 private void beginUpdate() {
a61af66fc99e Initial load
duke
parents:
diff changeset
220 updating = true;
a61af66fc99e Initial load
duke
parents:
diff changeset
221 }
a61af66fc99e Initial load
duke
parents:
diff changeset
222
a61af66fc99e Initial load
duke
parents:
diff changeset
223 private void endUpdate() {
a61af66fc99e Initial load
duke
parents:
diff changeset
224 updating = false;
a61af66fc99e Initial load
duke
parents:
diff changeset
225 }
a61af66fc99e Initial load
duke
parents:
diff changeset
226
a61af66fc99e Initial load
duke
parents:
diff changeset
227 private void initialize(BigInteger value, BigInteger minimum, BigInteger maximum) {
a61af66fc99e Initial load
duke
parents:
diff changeset
228 // Initialize the underlying scrollbar to the standard range values
a61af66fc99e Initial load
duke
parents:
diff changeset
229 // The increments are important and are how we differentiate arrow from track events
a61af66fc99e Initial load
duke
parents:
diff changeset
230 setMinimum(0);
a61af66fc99e Initial load
duke
parents:
diff changeset
231 setMaximum(BIG_RANGE - 1);
a61af66fc99e Initial load
duke
parents:
diff changeset
232 setValue(0);
a61af66fc99e Initial load
duke
parents:
diff changeset
233 setVisibleAmount(1);
a61af66fc99e Initial load
duke
parents:
diff changeset
234 setUnitIncrement(UNIT_INCREMENT);
a61af66fc99e Initial load
duke
parents:
diff changeset
235 setBlockIncrement(BLOCK_INCREMENT);
a61af66fc99e Initial load
duke
parents:
diff changeset
236
a61af66fc99e Initial load
duke
parents:
diff changeset
237 setUnitIncrementHP(new BigInteger(Integer.toString(getUnitIncrement())));
a61af66fc99e Initial load
duke
parents:
diff changeset
238 setBlockIncrementHP(new BigInteger(Integer.toString(getBlockIncrement())));
a61af66fc99e Initial load
duke
parents:
diff changeset
239
a61af66fc99e Initial load
duke
parents:
diff changeset
240 // Must set range and value first (it sets min/max)
a61af66fc99e Initial load
duke
parents:
diff changeset
241 setRange(minimum, maximum);
a61af66fc99e Initial load
duke
parents:
diff changeset
242
a61af66fc99e Initial load
duke
parents:
diff changeset
243 setVisibleAmountHP(new BigInteger(Integer.toString(getVisibleAmount())));
a61af66fc99e Initial load
duke
parents:
diff changeset
244 setValueHP(value);
a61af66fc99e Initial load
duke
parents:
diff changeset
245 }
a61af66fc99e Initial load
duke
parents:
diff changeset
246
a61af66fc99e Initial load
duke
parents:
diff changeset
247 private void initialize() {
a61af66fc99e Initial load
duke
parents:
diff changeset
248 BigInteger min = new BigInteger(Integer.toString(getMinimum()));
a61af66fc99e Initial load
duke
parents:
diff changeset
249 BigInteger max = new BigInteger(Integer.toString(getMaximum()));
a61af66fc99e Initial load
duke
parents:
diff changeset
250 initialize(min, min, max);
a61af66fc99e Initial load
duke
parents:
diff changeset
251 }
a61af66fc99e Initial load
duke
parents:
diff changeset
252
a61af66fc99e Initial load
duke
parents:
diff changeset
253 private void setRange(BigInteger minimum, BigInteger maximum) {
a61af66fc99e Initial load
duke
parents:
diff changeset
254 if (minimum.compareTo(maximum) > 0 ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
255 throw new RuntimeException("Bad scrollbar range " + minimum + " > " + maximum);
a61af66fc99e Initial load
duke
parents:
diff changeset
256 }
a61af66fc99e Initial load
duke
parents:
diff changeset
257 minimumHP = minimum;
a61af66fc99e Initial load
duke
parents:
diff changeset
258 maximumHP = maximum;
a61af66fc99e Initial load
duke
parents:
diff changeset
259 rangeHP = maximum.subtract(minimum).add(BigInteger.ONE);
a61af66fc99e Initial load
duke
parents:
diff changeset
260 BigInteger range2 = new BigInteger(Integer.toString(BIG_RANGE));
a61af66fc99e Initial load
duke
parents:
diff changeset
261 if (rangeHP.compareTo(range2) >= 0 ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
262 down = true;
a61af66fc99e Initial load
duke
parents:
diff changeset
263 scaleFactor = new BigDecimal(rangeHP, SCALE).divide(new BigDecimal(range2, SCALE), BigDecimal.ROUND_DOWN).max(new BigDecimal(BigInteger.ONE));
a61af66fc99e Initial load
duke
parents:
diff changeset
264 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
265 down = false;
a61af66fc99e Initial load
duke
parents:
diff changeset
266 scaleFactor = new BigDecimal(range2, SCALE).divide(new BigDecimal(rangeHP, SCALE), BigDecimal.ROUND_DOWN).max(new BigDecimal(BigInteger.ONE));
a61af66fc99e Initial load
duke
parents:
diff changeset
267 }
a61af66fc99e Initial load
duke
parents:
diff changeset
268 // FIXME: should put in original scaling algorithm (shifting by
a61af66fc99e Initial load
duke
parents:
diff changeset
269 // number of bits) as alternative when scale between low and high
a61af66fc99e Initial load
duke
parents:
diff changeset
270 // precision is very large
a61af66fc99e Initial load
duke
parents:
diff changeset
271 }
a61af66fc99e Initial load
duke
parents:
diff changeset
272
a61af66fc99e Initial load
duke
parents:
diff changeset
273 // A range update is complete. Rescale our computed values and
a61af66fc99e Initial load
duke
parents:
diff changeset
274 // inform the underlying scrollBar as needed.
a61af66fc99e Initial load
duke
parents:
diff changeset
275 private void updateScrollBarValues() {
a61af66fc99e Initial load
duke
parents:
diff changeset
276 setValueHP(getValueHP());
a61af66fc99e Initial load
duke
parents:
diff changeset
277 setVisibleAmountHP(getVisibleAmountHP());
a61af66fc99e Initial load
duke
parents:
diff changeset
278 setBlockIncrementHP(getBlockIncrementHP());
a61af66fc99e Initial load
duke
parents:
diff changeset
279 setUnitIncrementHP(getUnitIncrementHP());
a61af66fc99e Initial load
duke
parents:
diff changeset
280 }
a61af66fc99e Initial load
duke
parents:
diff changeset
281
a61af66fc99e Initial load
duke
parents:
diff changeset
282 private BigDecimal getScaleFactor() {
a61af66fc99e Initial load
duke
parents:
diff changeset
283 return scaleFactor;
a61af66fc99e Initial load
duke
parents:
diff changeset
284 }
a61af66fc99e Initial load
duke
parents:
diff changeset
285
a61af66fc99e Initial load
duke
parents:
diff changeset
286
a61af66fc99e Initial load
duke
parents:
diff changeset
287 // Value scaling routines
a61af66fc99e Initial load
duke
parents:
diff changeset
288 private BigInteger scaleToHP(int i) {
a61af66fc99e Initial load
duke
parents:
diff changeset
289 BigDecimal ib = new BigDecimal(Integer.toString(i));
a61af66fc99e Initial load
duke
parents:
diff changeset
290 if (down) return ib.multiply(getScaleFactor()).toBigInteger();
a61af66fc99e Initial load
duke
parents:
diff changeset
291 else return ib.divide(getScaleFactor(), BigDecimal.ROUND_DOWN).toBigInteger();
a61af66fc99e Initial load
duke
parents:
diff changeset
292 }
a61af66fc99e Initial load
duke
parents:
diff changeset
293
a61af66fc99e Initial load
duke
parents:
diff changeset
294 private int scaleToUnderlying(BigInteger i) {
a61af66fc99e Initial load
duke
parents:
diff changeset
295 BigDecimal d = new BigDecimal(i);
a61af66fc99e Initial load
duke
parents:
diff changeset
296 if (down) return d.divide(getScaleFactor(), BigDecimal.ROUND_DOWN).intValue();
a61af66fc99e Initial load
duke
parents:
diff changeset
297 else return d.multiply(getScaleFactor()).intValue();
a61af66fc99e Initial load
duke
parents:
diff changeset
298 }
a61af66fc99e Initial load
duke
parents:
diff changeset
299
a61af66fc99e Initial load
duke
parents:
diff changeset
300 // Range scaling routines
a61af66fc99e Initial load
duke
parents:
diff changeset
301 private BigInteger toHPRange(int i) {
a61af66fc99e Initial load
duke
parents:
diff changeset
302 return scaleToHP(i).add(minimumHP);
a61af66fc99e Initial load
duke
parents:
diff changeset
303 // return ib.shiftLeft(Math.max(2, maximumHP.bitLength() - 33));
a61af66fc99e Initial load
duke
parents:
diff changeset
304 }
a61af66fc99e Initial load
duke
parents:
diff changeset
305
a61af66fc99e Initial load
duke
parents:
diff changeset
306 private int toUnderlyingRange(BigInteger i) {
a61af66fc99e Initial load
duke
parents:
diff changeset
307 return scaleToUnderlying(i.subtract(minimumHP));
a61af66fc99e Initial load
duke
parents:
diff changeset
308 // return i.shiftRight(Math.max(2, maximumHP.bitLength() - 33)).intValue();
a61af66fc99e Initial load
duke
parents:
diff changeset
309 }
a61af66fc99e Initial load
duke
parents:
diff changeset
310
a61af66fc99e Initial load
duke
parents:
diff changeset
311 private void installListener() {
a61af66fc99e Initial load
duke
parents:
diff changeset
312 super.addAdjustmentListener(new AdjustmentListener() {
a61af66fc99e Initial load
duke
parents:
diff changeset
313 public void adjustmentValueChanged(AdjustmentEvent e) {
a61af66fc99e Initial load
duke
parents:
diff changeset
314 if (updating) {
a61af66fc99e Initial load
duke
parents:
diff changeset
315 return;
a61af66fc99e Initial load
duke
parents:
diff changeset
316 }
a61af66fc99e Initial load
duke
parents:
diff changeset
317 beginUpdate();
a61af66fc99e Initial load
duke
parents:
diff changeset
318 switch (e.getAdjustmentType()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
319 case AdjustmentEvent.TRACK:
a61af66fc99e Initial load
duke
parents:
diff changeset
320 int val = e.getValue();
a61af66fc99e Initial load
duke
parents:
diff changeset
321 int diff = val - lastValueSeen;
a61af66fc99e Initial load
duke
parents:
diff changeset
322 int absDiff = Math.abs(diff);
a61af66fc99e Initial load
duke
parents:
diff changeset
323 // System.err.println("diff: " + diff + " absDiff: " + absDiff);
a61af66fc99e Initial load
duke
parents:
diff changeset
324 if (absDiff == UNIT_INCREMENT) {
a61af66fc99e Initial load
duke
parents:
diff changeset
325 if (diff > 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
326 // System.err.println("case 1");
a61af66fc99e Initial load
duke
parents:
diff changeset
327 setValueHP(getValueHP().add(getUnitIncrementHP()));
a61af66fc99e Initial load
duke
parents:
diff changeset
328 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
329 // System.err.println("case 2");
a61af66fc99e Initial load
duke
parents:
diff changeset
330 setValueHP(getValueHP().subtract(getUnitIncrementHP()));
a61af66fc99e Initial load
duke
parents:
diff changeset
331 }
a61af66fc99e Initial load
duke
parents:
diff changeset
332 } else if (absDiff == BLOCK_INCREMENT) {
a61af66fc99e Initial load
duke
parents:
diff changeset
333 if (diff > 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
334 // System.err.println("case 3");
a61af66fc99e Initial load
duke
parents:
diff changeset
335 setValueHP(getValueHP().add(getBlockIncrementHP()));
a61af66fc99e Initial load
duke
parents:
diff changeset
336 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
337 // System.err.println("case 4");
a61af66fc99e Initial load
duke
parents:
diff changeset
338 setValueHP(getValueHP().subtract(getBlockIncrementHP()));
a61af66fc99e Initial load
duke
parents:
diff changeset
339 }
a61af66fc99e Initial load
duke
parents:
diff changeset
340 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
341 // System.err.println("case 5");
a61af66fc99e Initial load
duke
parents:
diff changeset
342 // FIXME: seem to be getting spurious update events,
a61af66fc99e Initial load
duke
parents:
diff changeset
343 // with diff = 0, upon mouse down/up on the track
a61af66fc99e Initial load
duke
parents:
diff changeset
344 if (absDiff != 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
345 // Convert low-precision value to high precision
a61af66fc99e Initial load
duke
parents:
diff changeset
346 // (note we lose the low bits)
a61af66fc99e Initial load
duke
parents:
diff changeset
347 BigInteger i = null;
a61af66fc99e Initial load
duke
parents:
diff changeset
348 if (e.getValue() == getMinimum()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
349 i = getMinimumHP();
a61af66fc99e Initial load
duke
parents:
diff changeset
350 } else if (e.getValue() >= getMaximum() - 1) {
a61af66fc99e Initial load
duke
parents:
diff changeset
351 i = getMaximumHP();
a61af66fc99e Initial load
duke
parents:
diff changeset
352 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
353 i = toHPRange(e.getValue());
a61af66fc99e Initial load
duke
parents:
diff changeset
354 }
a61af66fc99e Initial load
duke
parents:
diff changeset
355 setValueHP(i);
a61af66fc99e Initial load
duke
parents:
diff changeset
356 }
a61af66fc99e Initial load
duke
parents:
diff changeset
357 }
a61af66fc99e Initial load
duke
parents:
diff changeset
358 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
359 default:
a61af66fc99e Initial load
duke
parents:
diff changeset
360 // Should not reach here, but leaving it a no-op in case
a61af66fc99e Initial load
duke
parents:
diff changeset
361 // we later get the other events (should revisit code in
a61af66fc99e Initial load
duke
parents:
diff changeset
362 // that case)
a61af66fc99e Initial load
duke
parents:
diff changeset
363 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
364 }
a61af66fc99e Initial load
duke
parents:
diff changeset
365 endUpdate();
a61af66fc99e Initial load
duke
parents:
diff changeset
366 }
a61af66fc99e Initial load
duke
parents:
diff changeset
367 });
a61af66fc99e Initial load
duke
parents:
diff changeset
368 }
a61af66fc99e Initial load
duke
parents:
diff changeset
369
a61af66fc99e Initial load
duke
parents:
diff changeset
370 private void fireStateChanged() {
a61af66fc99e Initial load
duke
parents:
diff changeset
371 ChangeEvent e = null;
a61af66fc99e Initial load
duke
parents:
diff changeset
372 for (Iterator iter = changeListeners.iterator(); iter.hasNext(); ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
373 ChangeListener l = (ChangeListener) iter.next();
a61af66fc99e Initial load
duke
parents:
diff changeset
374 if (e == null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
375 e = new ChangeEvent(this);
a61af66fc99e Initial load
duke
parents:
diff changeset
376 }
a61af66fc99e Initial load
duke
parents:
diff changeset
377 l.stateChanged(e);
a61af66fc99e Initial load
duke
parents:
diff changeset
378 }
a61af66fc99e Initial load
duke
parents:
diff changeset
379 }
a61af66fc99e Initial load
duke
parents:
diff changeset
380
a61af66fc99e Initial load
duke
parents:
diff changeset
381 public static void main(String[] args) {
a61af66fc99e Initial load
duke
parents:
diff changeset
382 JFrame frame = new JFrame();
a61af66fc99e Initial load
duke
parents:
diff changeset
383 frame.setSize(300, 300);
a61af66fc99e Initial load
duke
parents:
diff changeset
384 // 32-bit version
a61af66fc99e Initial load
duke
parents:
diff changeset
385 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
386 HighPrecisionJScrollBar hpsb =
a61af66fc99e Initial load
duke
parents:
diff changeset
387 new HighPrecisionJScrollBar(
a61af66fc99e Initial load
duke
parents:
diff changeset
388 JScrollBar.VERTICAL,
a61af66fc99e Initial load
duke
parents:
diff changeset
389 new BigInteger(1, new byte[] {
a61af66fc99e Initial load
duke
parents:
diff changeset
390 (byte) 0x80, (byte) 0x00, (byte) 0x00, (byte) 0x00}),
a61af66fc99e Initial load
duke
parents:
diff changeset
391 new BigInteger(1, new byte[] {
a61af66fc99e Initial load
duke
parents:
diff changeset
392 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00}),
a61af66fc99e Initial load
duke
parents:
diff changeset
393 new BigInteger(1, new byte[] {
a61af66fc99e Initial load
duke
parents:
diff changeset
394 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF}));
a61af66fc99e Initial load
duke
parents:
diff changeset
395 hpsb.setUnitIncrementHP(new BigInteger(1, new byte[] {
a61af66fc99e Initial load
duke
parents:
diff changeset
396 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01}));
a61af66fc99e Initial load
duke
parents:
diff changeset
397 hpsb.setBlockIncrementHP(new BigInteger(1, new byte[] {
a61af66fc99e Initial load
duke
parents:
diff changeset
398 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x10}));
a61af66fc99e Initial load
duke
parents:
diff changeset
399 */
a61af66fc99e Initial load
duke
parents:
diff changeset
400
a61af66fc99e Initial load
duke
parents:
diff changeset
401 // 64-bit version
a61af66fc99e Initial load
duke
parents:
diff changeset
402 HighPrecisionJScrollBar hpsb =
a61af66fc99e Initial load
duke
parents:
diff changeset
403 new HighPrecisionJScrollBar(
a61af66fc99e Initial load
duke
parents:
diff changeset
404 JScrollBar.VERTICAL,
a61af66fc99e Initial load
duke
parents:
diff changeset
405 new BigInteger(1, new byte[] {
a61af66fc99e Initial load
duke
parents:
diff changeset
406 (byte) 0x80, (byte) 0x00, (byte) 0x00, (byte) 0x00,
a61af66fc99e Initial load
duke
parents:
diff changeset
407 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00}),
a61af66fc99e Initial load
duke
parents:
diff changeset
408 new BigInteger(1, new byte[] {
a61af66fc99e Initial load
duke
parents:
diff changeset
409 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
a61af66fc99e Initial load
duke
parents:
diff changeset
410 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00}),
a61af66fc99e Initial load
duke
parents:
diff changeset
411 new BigInteger(1, new byte[] {
a61af66fc99e Initial load
duke
parents:
diff changeset
412 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
a61af66fc99e Initial load
duke
parents:
diff changeset
413 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF}));
a61af66fc99e Initial load
duke
parents:
diff changeset
414 hpsb.setUnitIncrementHP(new BigInteger(1, new byte[] {
a61af66fc99e Initial load
duke
parents:
diff changeset
415 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
a61af66fc99e Initial load
duke
parents:
diff changeset
416 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01}));
a61af66fc99e Initial load
duke
parents:
diff changeset
417 hpsb.setBlockIncrementHP(new BigInteger(1, new byte[] {
a61af66fc99e Initial load
duke
parents:
diff changeset
418 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
a61af66fc99e Initial load
duke
parents:
diff changeset
419 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x10}));
a61af66fc99e Initial load
duke
parents:
diff changeset
420 hpsb.addChangeListener(new ChangeListener() {
a61af66fc99e Initial load
duke
parents:
diff changeset
421 public void stateChanged(ChangeEvent e) {
a61af66fc99e Initial load
duke
parents:
diff changeset
422 HighPrecisionJScrollBar h = (HighPrecisionJScrollBar) e.getSource();
a61af66fc99e Initial load
duke
parents:
diff changeset
423 System.out.println("New value = 0x" + h.getValueHP().toString(16));
a61af66fc99e Initial load
duke
parents:
diff changeset
424 }
a61af66fc99e Initial load
duke
parents:
diff changeset
425 });
a61af66fc99e Initial load
duke
parents:
diff changeset
426 frame.getContentPane().add(hpsb);
a61af66fc99e Initial load
duke
parents:
diff changeset
427 frame.show();
a61af66fc99e Initial load
duke
parents:
diff changeset
428 }
a61af66fc99e Initial load
duke
parents:
diff changeset
429
a61af66fc99e Initial load
duke
parents:
diff changeset
430 }