1 7 package java.awt; 8 9 import java.awt.event.AdjustmentEvent ; 10 import java.awt.event.AdjustmentListener ; 11 import java.awt.peer.ScrollPanePeer; 12 import java.io.Serializable ; 13 14 15 23 public class ScrollPaneAdjustable implements Adjustable , Serializable { 24 25 29 private ScrollPane sp; 30 31 39 private int orientation; 40 41 50 private int value; 51 52 67 private int minimum; 68 69 77 private int maximum; 78 79 87 private int visibleAmount; 88 89 98 private transient boolean isAdjusting; 99 100 109 private int unitIncrement = 1; 110 111 120 private int blockIncrement = 1; 121 122 private AdjustmentListener adjustmentListener; 123 124 128 private static final String SCROLLPANE_ONLY = 129 "Can be set by scrollpane only"; 130 131 132 135 private static native void initIDs(); 136 137 static { 138 Toolkit.loadLibraries(); 139 if (!GraphicsEnvironment.isHeadless()) { 140 initIDs(); 141 } 142 } 143 144 147 private static final long serialVersionUID = -3359745691033257079L; 148 149 150 160 ScrollPaneAdjustable(ScrollPane sp, AdjustmentListener l, int orientation) { 161 this.sp = sp; 162 this.orientation = orientation; 163 addAdjustmentListener(l); 164 } 165 166 173 void setSpan(int min, int max, int visible) { 174 minimum = min; 176 maximum = Math.max(max, minimum + 1); 177 visibleAmount = Math.min(visible, maximum - minimum); 178 visibleAmount = Math.max(visibleAmount, 1); 179 blockIncrement = Math.max((int)(visible * .90), 1); 180 setValue(value); 181 } 182 183 189 public int getOrientation() { 190 return orientation; 191 } 192 193 200 public void setMinimum(int min) { 201 throw new AWTError (SCROLLPANE_ONLY); 202 } 203 204 public int getMinimum() { 205 return 0; 208 } 209 210 217 public void setMaximum(int max) { 218 throw new AWTError (SCROLLPANE_ONLY); 219 } 220 221 public int getMaximum() { 222 return maximum; 223 } 224 225 public synchronized void setUnitIncrement(int u) { 226 if (u != unitIncrement) { 227 unitIncrement = u; 228 if (sp.peer != null) { 229 ScrollPanePeer peer = (ScrollPanePeer) sp.peer; 230 peer.setUnitIncrement(this, u); 231 } 232 } 233 } 234 235 public int getUnitIncrement() { 236 return unitIncrement; 237 } 238 239 public synchronized void setBlockIncrement(int b) { 240 blockIncrement = b; 241 } 242 243 public int getBlockIncrement() { 244 return blockIncrement; 245 } 246 247 254 public void setVisibleAmount(int v) { 255 throw new AWTError (SCROLLPANE_ONLY); 256 } 257 258 public int getVisibleAmount() { 259 return visibleAmount; 260 } 261 262 263 270 public void setValueIsAdjusting(boolean b) { 271 if (isAdjusting != b) { 272 isAdjusting = b; 273 AdjustmentEvent e = 274 new AdjustmentEvent (this, 275 AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED, 276 AdjustmentEvent.TRACK, value, b); 277 adjustmentListener.adjustmentValueChanged(e); 278 } 279 } 280 281 288 public boolean getValueIsAdjusting() { 289 return isAdjusting; 290 } 291 292 301 public void setValue(int v) { 302 setTypedValue(v, AdjustmentEvent.TRACK); 303 } 304 305 316 private void setTypedValue(int v, int type) { 317 v = Math.max(v, minimum); 318 v = Math.min(v, maximum - visibleAmount); 319 320 if (v != value) { 321 value = v; 322 AdjustmentEvent e = 326 new AdjustmentEvent (this, 327 AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED, 328 type, value, isAdjusting); 329 adjustmentListener.adjustmentValueChanged(e); 330 } 331 } 332 333 public int getValue() { 334 return value; 335 } 336 337 349 public synchronized void addAdjustmentListener(AdjustmentListener l) { 350 if (l == null) { 351 return; 352 } 353 adjustmentListener = AWTEventMulticaster.add(adjustmentListener, l); 354 } 355 356 369 public synchronized void removeAdjustmentListener(AdjustmentListener l){ 370 if (l == null) { 371 return; 372 } 373 adjustmentListener = AWTEventMulticaster.remove(adjustmentListener, l); 374 } 375 376 391 public synchronized AdjustmentListener [] getAdjustmentListeners() { 392 return (AdjustmentListener [])(AWTEventMulticaster.getListeners( 393 adjustmentListener, 394 AdjustmentListener .class)); 395 } 396 397 401 public String toString() { 402 return getClass().getName() + "[" + paramString() + "]"; 403 } 404 405 414 public String paramString() { 415 return ((orientation == Adjustable.VERTICAL ? "vertical," 416 :"horizontal,") 417 + "[0.."+maximum+"]" 418 + ",val=" + value 419 + ",vis=" + visibleAmount 420 + ",unit=" + unitIncrement 421 + ",block=" + blockIncrement 422 + ",isAdjusting=" + isAdjusting); 423 } 424 } 425 | Popular Tags |