| 1 7 package javax.swing.plaf.basic; 8 9 10 import sun.swing.DefaultLookup; 11 import sun.swing.UIAction; 12 13 import java.awt.*; 14 import java.awt.event.*; 15 16 import java.beans.*; 17 18 import javax.swing.*; 19 import javax.swing.event.*; 20 import javax.swing.plaf.*; 21 22 23 31 public class BasicScrollBarUI 32 extends ScrollBarUI implements LayoutManager, SwingConstants 33 { 34 private static final int POSITIVE_SCROLL = 1; 35 private static final int NEGATIVE_SCROLL = -1; 36 37 private static final int MIN_SCROLL = 2; 38 private static final int MAX_SCROLL = 3; 39 40 protected Dimension minimumThumbSize; 41 protected Dimension maximumThumbSize; 42 43 protected Color thumbHighlightColor; 44 protected Color thumbLightShadowColor; 45 protected Color thumbDarkShadowColor; 46 protected Color thumbColor; 47 protected Color trackColor; 48 protected Color trackHighlightColor; 49 50 protected JScrollBar scrollbar; 51 protected JButton incrButton; 52 protected JButton decrButton; 53 protected boolean isDragging; 54 protected TrackListener trackListener; 55 protected ArrowButtonListener buttonListener; 56 protected ModelListener modelListener; 57 58 protected Rectangle thumbRect; 59 protected Rectangle trackRect; 60 61 protected int trackHighlight; 62 63 protected static final int NO_HIGHLIGHT = 0; 64 protected static final int DECREASE_HIGHLIGHT = 1; 65 protected static final int INCREASE_HIGHLIGHT = 2; 66 67 protected ScrollListener scrollListener; 68 protected PropertyChangeListener propertyChangeListener; 69 protected Timer scrollTimer; 70 71 private final static int scrollSpeedThrottle = 60; 73 75 private boolean supportsAbsolutePositioning; 76 77 80 private int scrollBarWidth; 81 82 private Handler handler; 83 84 private boolean thumbActive; 85 86 90 private boolean useCachedValue = false; 91 94 private int scrollBarValue; 95 96 static void loadActionMap(LazyActionMap map) { 97 map.put(new Actions(Actions.POSITIVE_UNIT_INCREMENT)); 98 map.put(new Actions(Actions.POSITIVE_BLOCK_INCREMENT)); 99 map.put(new Actions(Actions.NEGATIVE_UNIT_INCREMENT)); 100 map.put(new Actions(Actions.NEGATIVE_BLOCK_INCREMENT)); 101 map.put(new Actions(Actions.MIN_SCROLL)); 102 map.put(new Actions(Actions.MAX_SCROLL)); 103 } 104 105 106 public static ComponentUI createUI(JComponent c) { 107 return new BasicScrollBarUI (); 108 } 109 110 111 protected void configureScrollBarColors() 112 { 113 LookAndFeel.installColors(scrollbar, "ScrollBar.background", 114 "ScrollBar.foreground"); 115 thumbHighlightColor = UIManager.getColor("ScrollBar.thumbHighlight"); 116 thumbLightShadowColor = UIManager.getColor("ScrollBar.thumbShadow"); 117 thumbDarkShadowColor = UIManager.getColor("ScrollBar.thumbDarkShadow"); 118 thumbColor = UIManager.getColor("ScrollBar.thumb"); 119 trackColor = UIManager.getColor("ScrollBar.track"); 120 trackHighlightColor = UIManager.getColor("ScrollBar.trackHighlight"); 121 } 122 123 124 public void installUI(JComponent c) { 125 scrollbar = (JScrollBar)c; 126 thumbRect = new Rectangle(0, 0, 0, 0); 127 trackRect = new Rectangle(0, 0, 0, 0); 128 installDefaults(); 129 installComponents(); 130 installListeners(); 131 installKeyboardActions(); 132 } 133 134 public void uninstallUI(JComponent c) { 135 scrollbar = (JScrollBar)c; 136 uninstallListeners(); 137 uninstallDefaults(); 138 uninstallComponents(); 139 uninstallKeyboardActions(); 140 thumbRect = null; 141 scrollbar = null; 142 incrButton = null; 143 decrButton = null; 144 } 145 146 147 protected void installDefaults() 148 { 149 scrollBarWidth = UIManager.getInt("ScrollBar.width"); 150 if (scrollBarWidth <= 0) { 151 scrollBarWidth = 16; 152 } 153 minimumThumbSize = (Dimension)UIManager.get("ScrollBar.minimumThumbSize"); 154 maximumThumbSize = (Dimension)UIManager.get("ScrollBar.maximumThumbSize"); 155 156 Boolean absB = (Boolean )UIManager.get("ScrollBar.allowsAbsolutePositioning"); 157 supportsAbsolutePositioning = (absB != null) ? absB.booleanValue() : 158 false; 159 160 trackHighlight = NO_HIGHLIGHT; 161 if (scrollbar.getLayout() == null || 162 (scrollbar.getLayout() instanceof UIResource)) { 163 scrollbar.setLayout(this); 164 } 165 configureScrollBarColors(); 166 LookAndFeel.installBorder(scrollbar, "ScrollBar.border"); 167 LookAndFeel.installProperty(scrollbar, "opaque", Boolean.TRUE); 168 169 scrollBarValue = scrollbar.getValue(); 170 } 171 172 173 protected void installComponents(){ 174 switch (scrollbar.getOrientation()) { 175 case JScrollBar.VERTICAL: 176 incrButton = createIncreaseButton(SOUTH); 177 decrButton = createDecreaseButton(NORTH); 178 break; 179 180 case JScrollBar.HORIZONTAL: 181 if (scrollbar.getComponentOrientation().isLeftToRight()) { 182 incrButton = createIncreaseButton(EAST); 183 decrButton = createDecreaseButton(WEST); 184 } else { 185 incrButton = createIncreaseButton(WEST); 186 decrButton = createDecreaseButton(EAST); 187 } 188 break; 189 } 190 scrollbar.add(incrButton); 191 scrollbar.add(decrButton); 192 scrollbar.setEnabled(scrollbar.isEnabled()); 194 } 195 196 protected void uninstallComponents(){ 197 scrollbar.remove(incrButton); 198 scrollbar.remove(decrButton); 199 } 200 201 202 protected void installListeners(){ 203 trackListener = createTrackListener(); 204 buttonListener = createArrowButtonListener(); 205 modelListener = createModelListener(); 206 propertyChangeListener = createPropertyChangeListener(); 207 208 scrollbar.addMouseListener(trackListener); 209 scrollbar.addMouseMotionListener(trackListener); 210 scrollbar.getModel().addChangeListener(modelListener); 211 scrollbar.addPropertyChangeListener(propertyChangeListener); 212 scrollbar.addFocusListener(getHandler()); 213 214 if (incrButton != null) { 215 incrButton.addMouseListener(buttonListener); 216 } 217 if (decrButton != null) { 218 decrButton.addMouseListener(buttonListener); 219 } 220 221 scrollListener = createScrollListener(); 222 scrollTimer = new Timer(scrollSpeedThrottle, scrollListener); 223 scrollTimer.setInitialDelay(300); } 225 226 227 protected void installKeyboardActions(){ 228 LazyActionMap.installLazyActionMap(scrollbar, BasicScrollBarUI .class, 229 "ScrollBar.actionMap"); 230 231 InputMap inputMap = getInputMap(JComponent.WHEN_FOCUSED); 232 SwingUtilities.replaceUIInputMap(scrollbar, JComponent.WHEN_FOCUSED, 233 inputMap); 234 inputMap = getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); 235 SwingUtilities.replaceUIInputMap(scrollbar, 236 JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, inputMap); 237 } 238 239 protected void uninstallKeyboardActions(){ 240 SwingUtilities.replaceUIInputMap(scrollbar, JComponent.WHEN_FOCUSED, 241 null); 242 SwingUtilities.replaceUIActionMap(scrollbar, null); 243 } 244 245 private InputMap getInputMap(int condition) { 246 if (condition == JComponent.WHEN_FOCUSED) { 247 InputMap keyMap = (InputMap)DefaultLookup.get( 248 scrollbar, this, "ScrollBar.focusInputMap"); 249 InputMap rtlKeyMap; 250 251 if (scrollbar.getComponentOrientation().isLeftToRight() || 252 ((rtlKeyMap = (InputMap)DefaultLookup.get(scrollbar, this, "ScrollBar.focusInputMap.RightToLeft")) == null)) { 253 return keyMap; 254 } else { 255 rtlKeyMap.setParent(keyMap); 256 return rtlKeyMap; 257 } 258 } 259 else if (condition == JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT) { 260 InputMap keyMap = (InputMap)DefaultLookup.get( 261 scrollbar, this, "ScrollBar.ancestorInputMap"); 262 InputMap rtlKeyMap; 263 264 if (scrollbar.getComponentOrientation().isLeftToRight() || 265 ((rtlKeyMap = (InputMap)DefaultLookup.get(scrollbar, this, "ScrollBar.ancestorInputMap.RightToLeft")) == null)) { 266 return keyMap; 267 } else { 268 rtlKeyMap.setParent(keyMap); 269 return rtlKeyMap; 270 } 271 } 272 return null; 273 } 274 275 276 protected void uninstallListeners() { 277 scrollTimer.stop(); 278 scrollTimer = null; 279 280 if (decrButton != null){ 281 decrButton.removeMouseListener(buttonListener); 282 } 283 if (incrButton != null){ 284 incrButton.removeMouseListener(buttonListener); 285 } 286 287 scrollbar.getModel().removeChangeListener(modelListener); 288 scrollbar.removeMouseListener(trackListener); 289 scrollbar.removeMouseMotionListener(trackListener); 290 scrollbar.removePropertyChangeListener(propertyChangeListener); 291 scrollbar.removeFocusListener(getHandler()); 292 handler = null; 293 } 294 295 296 protected void uninstallDefaults(){ 297 LookAndFeel.uninstallBorder(scrollbar); 298 if (scrollbar.getLayout() == this) { 299 scrollbar.setLayout(null); 300 } 301 } 302 303 304 private Handler getHandler() { 305 if (handler == null) { 306 handler = new Handler(); 307 } 308 return handler; 309 } 310 311 protected TrackListener createTrackListener(){ 312 return new TrackListener(); 313 } 314 315 protected ArrowButtonListener createArrowButtonListener(){ 316 return new ArrowButtonListener(); 317 } 318 319 protected ModelListener createModelListener(){ 320 return new ModelListener(); 321 } 322 323 protected ScrollListener createScrollListener(){ 324 return new ScrollListener(); 325 } 326 327 protected PropertyChangeListener createPropertyChangeListener() { 328 return getHandler(); 329 } 330 331 private void updateThumbState(int x, int y) { 332 Rectangle rect = getThumbBounds(); 333 334 setThumbRollover(rect.contains(x, y)); 335 } 336 337 343 protected void setThumbRollover(boolean active) { 344 if (thumbActive != active) { 345 thumbActive = active; 346 scrollbar.repaint(getThumbBounds()); 347 } 348 } 349 350 356 public boolean isThumbRollover() { 357 return thumbActive; 358 } 359 360 public void paint(Graphics g, JComponent c) { 361 paintTrack(g, c, getTrackBounds()); 362 Rectangle thumbBounds = getThumbBounds(); 363 if (thumbBounds.intersects(g.getClipBounds())) { 364 paintThumb(g, c, thumbBounds); 365 } 366 } 367 368 369 385 public Dimension getPreferredSize(JComponent c) { 386 return (scrollbar.getOrientation() == JScrollBar.VERTICAL) 387 ? new Dimension(scrollBarWidth, 48) 388 : new Dimension(48, scrollBarWidth); 389 } 390 391 392 398 public Dimension getMaximumSize(JComponent c) { 399 return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE); 400 } 401 402 protected JButton createDecreaseButton(int orientation) { 403 return new BasicArrowButton (orientation, 404 UIManager.getColor("ScrollBar.thumb"), 405 UIManager.getColor("ScrollBar.thumbShadow"), 406 UIManager.getColor("ScrollBar.thumbDarkShadow"), 407 UIManager.getColor("ScrollBar.thumbHighlight")); 408 } 409 410 protected JButton createIncreaseButton(int orientation) { 411 return new BasicArrowButton (orientation, 412 UIManager.getColor("ScrollBar.thumb"), 413 UIManager.getColor("ScrollBar.thumbShadow"), 414 UIManager.getColor("ScrollBar.thumbDarkShadow"), 415 UIManager.getColor("ScrollBar.thumbHighlight")); 416 } 417 418 419 protected void paintDecreaseHighlight(Graphics g) 420 { 421 Insets insets = scrollbar.getInsets(); 422 Rectangle thumbR = getThumbBounds(); 423 g.setColor(trackHighlightColor); 424 425 if (scrollbar.getOrientation() == JScrollBar.VERTICAL) { 426 int x = insets.left; 427 int y = decrButton.getY() + decrButton.getHeight(); 428 int w = scrollbar.getWidth() - (insets.left + insets.right); 429 int h = thumbR.y - y; 430 g.fillRect(x, y, w, h); 431 } 432 else { 433 int x, w; 434 if (scrollbar.getComponentOrientation().isLeftToRight()) { 435 x = decrButton.getX() + decrButton.getWidth(); 436 w = thumbR.x - x; 437 } else { 438 x = thumbR.x + thumbR.width; 439 w = decrButton.getX() - x; 440 } 441 int y = insets.top; 442 int h = scrollbar.getHeight() - (insets.top + insets.bottom); 443 g.fillRect(x, y, w, h); 444 } 445 } 446 447 448 protected void paintIncreaseHighlight(Graphics g) 449 { 450 Insets insets = scrollbar.getInsets(); 451 Rectangle thumbR = getThumbBounds(); 452 g.setColor(trackHighlightColor); 453 454 if (scrollbar.getOrientation() == JScrollBar.VERTICAL) { 455 int x = insets.left; 456 int y = thumbR.y + thumbR.height; 457 int w = scrollbar.getWidth() - (insets.left + insets.right); 458 int h = incrButton.getY() - y; 459 g.fillRect(x, y, w, h); 460 } 461 else { 462 int x, w; 463 if (scrollbar.getComponentOrientation().isLeftToRight()) { 464 x = thumbR.x + thumbR.width; 465 w = incrButton.getX() - x; 466 } else { 467 x = incrButton.getX() + incrButton.getWidth(); 468 w = thumbR.x - x; 469 } 470 int y = insets.top; 471 int h = scrollbar.getHeight() - (insets.top + insets.bottom); 472 g.fillRect(x, y, w, h); 473 } 474 } 475 476 477 protected void paintTrack(Graphics g, JComponent c, Rectangle trackBounds) 478 { 479 g.setColor(trackColor); 480 g.fillRect(trackBounds.x, trackBounds.y, trackBounds.width, trackBounds.height); 481 482 if(trackHighlight == DECREASE_HIGHLIGHT) { 483 paintDecreaseHighlight(g); 484 } 485 else if(trackHighlight == INCREASE_HIGHLIGHT) { 486 paintIncreaseHighlight(g); 487 } 488 } 489 490 491 protected void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds) 492 { 493 if(thumbBounds.isEmpty() || !scrollbar.isEnabled()) { 494 return; 495 } 496 497 int w = thumbBounds.width; 498 int h = thumbBounds.height; 499 500 g.translate(thumbBounds.x, thumbBounds.y); 501 502 g.setColor(thumbDarkShadowColor); 503 g.drawRect(0, 0, w-1, h-1); 504 g.setColor(thumbColor); 505 g.fillRect(0, 0, w-1, h-1); 506 507 g.setColor(thumbHighlightColor); 508 g.drawLine(1, 1, 1, h-2); 509 g.drawLine(2, 1, w-3, 1); 510 511 g.setColor(thumbLightShadowColor); 512 g.drawLine(2, h-2, w-2, h-2); 513 g.drawLine(w-2, 1, w-2, h-3); 514 515 g.translate(-thumbBounds.x, -thumbBounds.y); 516 } 517 518 519 530 protected Dimension getMinimumThumbSize() { 531 return minimumThumbSize; 532 } 533 534 545 protected Dimension getMaximumThumbSize() { 546 return maximumThumbSize; 547 } 548 549 550 553 554 public void addLayoutComponent(String name, Component child) {} 555 public void removeLayoutComponent(Component child) {} 556 557 public Dimension preferredLayoutSize(Container scrollbarContainer) { 558 return getPreferredSize((JComponent)scrollbarContainer); 559 } 560 561 public Dimension minimumLayoutSize(Container scrollbarContainer) { 562 return getMinimumSize((JComponent)scrollbarContainer); 563 } 564 565 private int getValue(JScrollBar sb) { 566 return (useCachedValue) ? scrollBarValue : sb.getValue(); 567 } 568 569 protected void layoutVScrollbar(JScrollBar sb) 570 { 571 Dimension sbSize = sb.getSize(); 572 Insets sbInsets = sb.getInsets(); 573 574 577 int itemW = sbSize.width - (sbInsets.left + sbInsets.right); 578 int itemX = sbInsets.left; 579 580 583 boolean squareButtons = DefaultLookup.getBoolean( 584 scrollbar, this, "ScrollBar.squareButtons", false); 585 int decrButtonH = squareButtons ? itemW : 586 decrButton.getPreferredSize().height; 587 int decrButtonY = sbInsets.top; 588 589 int incrButtonH = squareButtons ? itemW : 590 incrButton.getPreferredSize().height; 591 int incrButtonY = sbSize.height - (sbInsets.bottom + incrButtonH); 592 593 596 int sbInsetsH = sbInsets.top + sbInsets.bottom; 597 int sbButtonsH = decrButtonH + incrButtonH; 598 float trackH = sbSize.height - (sbInsetsH + sbButtonsH); 599 600 606 float min = sb.getMinimum(); 607 float extent = sb.getVisibleAmount(); 608 float range = sb.getMaximum() - min; 609 float value = getValue(sb); 610 611 int thumbH = (range <= 0) 612 ? getMaximumThumbSize().height : (int)(trackH * (extent / range)); 613 thumbH = Math.max(thumbH, getMinimumThumbSize().height); 614 thumbH = Math.min(thumbH, getMaximumThumbSize().height); 615 616 int thumbY = incrButtonY - thumbH; 617 if (value < (sb.getMaximum() - sb.getVisibleAmount())) { 618 float thumbRange = trackH - thumbH; 619 thumbY = (int)(0.5f + (thumbRange * ((value - min) / (range - extent)))); 620 thumbY += decrButtonY + decrButtonH; 621 } 622 623 626 int sbAvailButtonH = (sbSize.height - sbInsetsH); 627 if (sbAvailButtonH < sbButtonsH) { 628 incrButtonH = decrButtonH = sbAvailButtonH / 2; 629 incrButtonY = sbSize.height - (sbInsets.bottom + incrButtonH); 630 } 631 decrButton.setBounds(itemX, decrButtonY, itemW, decrButtonH); 632 incrButton.setBounds(itemX, incrButtonY, itemW, incrButtonH); 633 634 636 int itrackY = decrButtonY + decrButtonH; 637 int itrackH = incrButtonY - itrackY; 638 trackRect.setBounds(itemX, itrackY, itemW, itrackH); 639 640 644 if(thumbH >= (int)trackH) { 645 setThumbBounds(0, 0, 0, 0); 646 } 647 else { 648 if ((thumbY + thumbH) > incrButtonY) { 649 thumbY = incrButtonY - thumbH; 650 } 651 if (thumbY < (decrButtonY + decrButtonH)) { 652 thumbY = decrButtonY + decrButtonH + 1; 653 } 654 setThumbBounds(itemX, thumbY, itemW, thumbH); 655 } 656 } 657 658 659 protected void layoutHScrollbar(JScrollBar sb) 660 { 661 Dimension sbSize = sb.getSize(); 662 Insets sbInsets = sb.getInsets(); 663 664 666 int itemH = sbSize.height - (sbInsets.top + sbInsets.bottom); 667 int itemY = sbInsets.top; 668 669 boolean ltr = sb.getComponentOrientation().isLeftToRight(); 670 671 674 boolean squareButtons = DefaultLookup.getBoolean( 675 scrollbar, this, "ScrollBar.squareButtons", false); 676 int leftButtonW = squareButtons ? itemH : 677 decrButton.getPreferredSize().width; 678 int rightButtonW = squareButtons ? itemH : 679 incrButton.getPreferredSize().width; 680 if (!ltr) { 681 int temp = leftButtonW; 682 leftButtonW = rightButtonW; 683 rightButtonW = temp; 684 } 685 int leftButtonX = sbInsets.left; 686 int rightButtonX = sbSize.width - (sbInsets.right + rightButtonW); 687 688 691 int sbInsetsW = sbInsets.left + sbInsets.right; 692 int sbButtonsW = leftButtonW + rightButtonW; 693 float trackW = sbSize.width - (sbInsetsW + sbButtonsW); 694 695 701 float min = sb.getMinimum(); 702 float max = sb.getMaximum(); 703 float extent = sb.getVisibleAmount(); 704 float range = max - min; 705 float value = getValue(sb); 706 707 int thumbW = (range <= 0) 708 ? getMaximumThumbSize().width : (int)(trackW * (extent / range)); 709 thumbW = Math.max(thumbW, getMinimumThumbSize().width); 710 thumbW = Math.min(thumbW, getMaximumThumbSize().width); 711 712 int thumbX = ltr ? rightButtonX - thumbW : leftButtonX + leftButtonW; 713 if (value < (max - sb.getVisibleAmount())) { 714 float thumbRange = trackW - thumbW; 715 if( ltr ) { 716 thumbX = (int)(0.5f + (thumbRange * ((value - min) / (range - extent)))); 717 } else { 718 thumbX = (int)(0.5f + (thumbRange * ((max - extent - value) / (range - extent)))); 719 } 720 thumbX += leftButtonX + leftButtonW; 721 } 722 723 726 int sbAvailButtonW = (sbSize.width - sbInsetsW); 727 if (sbAvailButtonW < sbButtonsW) { 728 rightButtonW = leftButtonW = sbAvailButtonW / 2; 729 rightButtonX = sbSize.width - (sbInsets.right + rightButtonW); 730 } 731 732 (ltr ? decrButton : incrButton).setBounds(leftButtonX, itemY, leftButtonW, itemH); 733 (ltr ? incrButton : decrButton).setBounds(rightButtonX, itemY, rightButtonW, itemH); 734 735 737 int itrackX = leftButtonX + leftButtonW; 738 int itrackW = rightButtonX - itrackX; 739 trackRect.setBounds(itrackX, itemY, itrackW, itemH); 740 741 744 if (thumbW >= (int)trackW) { 745 setThumbBounds(0, 0, 0, 0); 746 } 747 else { 748 if (thumbX + thumbW > rightButtonX) { 749 thumbX = rightButtonX - thumbW; 750 } 751 if (thumbX < leftButtonX + leftButtonW) { 752 thumbX = leftButtonX + leftButtonW + 1; 753 } 754 setThumbBounds(thumbX, itemY, thumbW, itemH); 755 } 756 } 757 758 public void layoutContainer(Container scrollbarContainer) 759 { 760 764 if (isDragging) { 765 return; 766 } 767 768 JScrollBar scrollbar = (JScrollBar)scrollbarContainer; 769 switch (scrollbar.getOrientation()) { 770 case JScrollBar.VERTICAL: 771 layoutVScrollbar(scrollbar); 772 break; 773 774 case JScrollBar.HORIZONTAL: 775 layoutHScrollbar(scrollbar); 776 break; 777 } 778 } 779 780 781 787 protected void setThumbBounds(int x, int y, int width, int height) 788 { 789 791 if ((thumbRect.x == x) && 792 (thumbRect.y == y) && 793 (thumbRect.width == width) && 794 (thumbRect.height == height)) { 795 return; 796 } 797 798 801 int minX = Math.min(x, thumbRect.x); 802 int minY = Math.min(y, thumbRect.y); 803 int maxX = Math.max(x + width, thumbRect.x + thumbRect.width); 804 int maxY = Math.max(y + height, thumbRect.y + thumbRect.height); 805 806 thumbRect.setBounds(x, y, width, height); 807 scrollbar.repaint(minX, minY, maxX - minX, maxY - minY); 808 809 setThumbRollover(false); 812 } 813 814 815 824 protected Rectangle getThumbBounds() { 825 return thumbRect; 826 } 827 828 829 841 protected Rectangle getTrackBounds() { 842 return trackRect; 843 } 844 845 849 static void scrollByBlock(JScrollBar scrollbar, int direction) { 850 int oldValue = scrollbar.getValue(); 853 int blockIncrement = scrollbar.getBlockIncrement(direction); 854 int delta = blockIncrement * ((direction > 0) ? +1 : -1); 855 int newValue = oldValue + delta; 856 857 if (delta > 0 && newValue < oldValue) { 859 newValue = scrollbar.getMaximum(); 860 } 861 else if (delta < 0 && newValue > oldValue) { 862 newValue = scrollbar.getMinimum(); 863 } 864 865 scrollbar.setValue(newValue); 866 } 867 868 protected void scrollByBlock(int direction) 869 { 870 scrollByBlock(scrollbar, direction); 871 trackHighlight = direction > 0 ? INCREASE_HIGHLIGHT : DECREASE_HIGHLIGHT; 872 Rectangle dirtyRect = getTrackBounds(); 873 scrollbar.repaint(dirtyRect.x, dirtyRect.y, dirtyRect.width, dirtyRect.height); 874 } 875 876 880 static void scrollByUnits(JScrollBar scrollbar, int direction, 881 int units) { 882 int delta; 885 886 for (int i=0; i<units; i++) { 887 if (direction > 0) { 888 delta = scrollbar.getUnitIncrement(direction); 889 } 890 else { 891 delta = -scrollbar.getUnitIncrement(direction); 892 } 893 894 int oldValue = scrollbar.getValue(); 895 int newValue = oldValue + delta; 896 897 if (delta > 0 && newValue < oldValue) { 899 newValue = scrollbar.getMaximum(); 900 } 901 else if (delta < 0 && newValue > oldValue) { 902 newValue = scrollbar.getMinimum(); 903 } 904 if (oldValue == newValue) { 905 break; 906 } 907 scrollbar.setValue(newValue); 908 } 909 } 910 911 protected void scrollByUnit(int direction) { 912 scrollByUnits(scrollbar, direction, 1); 913 } 914 915 922 public boolean getSupportsAbsolutePositioning() { 923 return supportsAbsolutePositioning; 924 } 925 926 930 protected class ModelListener implements ChangeListener { 931 public void stateChanged(ChangeEvent e) { 932 if (!useCachedValue) { 933 scrollBarValue = scrollbar.getValue(); 934 } 935 layoutContainer(scrollbar); 936 useCachedValue = false; 937 } 938 } 939 940 941 944 protected class TrackListener 945 extends MouseAdapter implements MouseMotionListener 946 { 947 protected transient int offset; 948 protected transient int currentMouseX, currentMouseY; 949 private transient int direction = +1; 950 951 public void mouseReleased(MouseEvent e) 952 { 953 if (isDragging) { 954 updateThumbState(e.getX(), e.getY()); 955 } 956 if (SwingUtilities.isRightMouseButton(e) || 957 (!getSupportsAbsolutePositioning() && 958 SwingUtilities.isMiddleMouseButton(e))) 959 return; 960 if(!scrollbar.isEnabled()) 961 return; 962 963 Rectangle r = getTrackBounds(); 964 scrollbar.repaint(r.x, r.y, r.width, r.height); 965 966 trackHighlight = NO_HIGHLIGHT; 967 isDragging = false; 968 offset = 0; 969 scrollTimer.stop(); 970 useCachedValue = true; 971 scrollbar.setValueIsAdjusting(false); 972 } 973 974 975 982 public void mousePressed(MouseEvent e) 983 { 984 if (SwingUtilities.isRightMouseButton(e) || 985 (!getSupportsAbsolutePositioning() && 986 SwingUtilities.isMiddleMouseButton(e))) 987 return; 988 if(!scrollbar.isEnabled()) 989 return; 990 991 if (!scrollbar.hasFocus() && scrollbar.isRequestFocusEnabled()) { 992 scrollbar.requestFocus(); 993 } 994 995 useCachedValue = true; 996 scrollbar.setValueIsAdjusting(true); 997 998 currentMouseX = e.getX(); 999 currentMouseY = e.getY(); 1000 1001 if(getThumbBounds().contains(currentMouseX, currentMouseY)) { 1003 switch (scrollbar.getOrientation()) { 1004 case JScrollBar.VERTICAL: 1005 offset = currentMouseY - getThumbBounds().y; 1006 break; 1007 case JScrollBar.HORIZONTAL: 1008 offset = currentMouseX - getThumbBounds().x; 1009 break; 1010 } 1011 isDragging = true; 1012 &n
|