| 1 7 8 package javax.swing; 9 10 import javax.swing.plaf.*; 11 import javax.swing.border.*; 12 import javax.swing.event.*; 13 import javax.accessibility.*; 14 15 import java.awt.Component ; 16 import java.awt.ComponentOrientation ; 17 import java.awt.Graphics ; 18 import java.awt.Rectangle ; 19 import java.awt.Insets ; 20 import java.awt.Color ; 21 import java.awt.LayoutManager ; 22 import java.awt.Point ; 23 24 import java.io.ObjectOutputStream ; 25 import java.io.ObjectInputStream ; 26 import java.io.IOException ; 27 28 import java.beans.*; 29 30 31 133 public class JScrollPane extends JComponent implements ScrollPaneConstants , Accessible 134 { 135 private Border viewportBorder; 136 137 141 private static final String uiClassID = "ScrollPaneUI"; 142 143 149 protected int verticalScrollBarPolicy = VERTICAL_SCROLLBAR_AS_NEEDED; 150 151 152 158 protected int horizontalScrollBarPolicy = HORIZONTAL_SCROLLBAR_AS_NEEDED; 159 160 161 166 protected JViewport viewport; 167 168 169 174 protected JScrollBar verticalScrollBar; 175 176 177 182 protected JScrollBar horizontalScrollBar; 183 184 185 189 protected JViewport rowHeader; 190 191 192 196 protected JViewport columnHeader; 197 198 199 204 protected Component lowerLeft; 205 206 207 212 protected Component lowerRight; 213 214 215 220 protected Component upperLeft; 221 222 223 228 protected Component upperRight; 229 230 233 private boolean wheelScrollState = true; 234 235 255 public JScrollPane(Component view, int vsbPolicy, int hsbPolicy) 256 { 257 setLayout(new ScrollPaneLayout.UIResource ()); 258 setVerticalScrollBarPolicy(vsbPolicy); 259 setHorizontalScrollBarPolicy(hsbPolicy); 260 setViewport(createViewport()); 261 setVerticalScrollBar(createVerticalScrollBar()); 262 setHorizontalScrollBar(createHorizontalScrollBar()); 263 if (view != null) { 264 setViewportView(view); 265 } 266 setOpaque(true); 267 updateUI(); 268 269 if (!this.getComponentOrientation().isLeftToRight()) { 270 viewport.setViewPosition(new Point (Integer.MAX_VALUE, 0)); 271 } 272 } 273 274 275 284 public JScrollPane(Component view) { 285 this(view, VERTICAL_SCROLLBAR_AS_NEEDED, HORIZONTAL_SCROLLBAR_AS_NEEDED); 286 } 287 288 289 303 public JScrollPane(int vsbPolicy, int hsbPolicy) { 304 this(null, vsbPolicy, hsbPolicy); 305 } 306 307 308 312 public JScrollPane() { 313 this(null, VERTICAL_SCROLLBAR_AS_NEEDED, HORIZONTAL_SCROLLBAR_AS_NEEDED); 314 } 315 316 317 329 public ScrollPaneUI getUI() { 330 return (ScrollPaneUI)ui; 331 } 332 333 334 341 public void setUI(ScrollPaneUI ui) { 342 super.setUI(ui); 343 } 344 345 346 354 public void updateUI() { 355 setUI((ScrollPaneUI)UIManager.getUI(this)); 356 } 357 358 359 370 public String getUIClassID() { 371 return uiClassID; 372 } 373 374 375 376 394 public void setLayout(LayoutManager layout) { 395 if (layout instanceof ScrollPaneLayout ) { 396 super.setLayout(layout); 397 ((ScrollPaneLayout )layout).syncWithScrollPane(this); 398 } 399 else if (layout == null) { 400 super.setLayout(layout); 401 } 402 else { 403 String s = "layout of JScrollPane must be a ScrollPaneLayout"; 404 throw new ClassCastException (s); 405 } 406 } 407 408 422 public boolean isValidateRoot() { 423 return true; 424 } 425 426 427 432 public int getVerticalScrollBarPolicy() { 433 return verticalScrollBarPolicy; 434 } 435 436 437 459 public void setVerticalScrollBarPolicy(int policy) { 460 switch (policy) { 461 case VERTICAL_SCROLLBAR_AS_NEEDED: 462 case VERTICAL_SCROLLBAR_NEVER: 463 case VERTICAL_SCROLLBAR_ALWAYS: 464 break; 465 default: 466 throw new IllegalArgumentException ("invalid verticalScrollBarPolicy"); 467 } 468 int old = verticalScrollBarPolicy; 469 verticalScrollBarPolicy = policy; 470 firePropertyChange("verticalScrollBarPolicy", old, policy); 471 revalidate(); 472 repaint(); 473 } 474 475 476 481 public int getHorizontalScrollBarPolicy() { 482 return horizontalScrollBarPolicy; 483 } 484 485 486 507 public void setHorizontalScrollBarPolicy(int policy) { 508 switch (policy) { 509 case HORIZONTAL_SCROLLBAR_AS_NEEDED: 510 case HORIZONTAL_SCROLLBAR_NEVER: 511 case HORIZONTAL_SCROLLBAR_ALWAYS: 512 break; 513 default: 514 throw new IllegalArgumentException ("invalid horizontalScrollBarPolicy"); 515 } 516 int old = horizontalScrollBarPolicy; 517 horizontalScrollBarPolicy = policy; 518 firePropertyChange("horizontalScrollBarPolicy", old, policy); 519 revalidate(); 520 repaint(); 521 } 522 523 524 530 public Border getViewportBorder() { 531 return viewportBorder; 532 } 533 534 535 554 public void setViewportBorder(Border viewportBorder) { 555 Border oldValue = this.viewportBorder; 556 this.viewportBorder = viewportBorder; 557 firePropertyChange("viewportBorder", oldValue, viewportBorder); 558 } 559 560 561 566 public Rectangle getViewportBorderBounds() 567 { 568 Rectangle borderR = new Rectangle (getSize()); 569 570 Insets insets = getInsets(); 571 borderR.x = insets.left; 572 borderR.y = insets.top; 573 borderR.width -= insets.left + insets.right; 574 borderR.height -= insets.top + insets.bottom; 575 576 boolean leftToRight = SwingUtilities.isLeftToRight(this); 577 578 581 582 JViewport colHead = getColumnHeader(); 583 if ((colHead != null) && (colHead.isVisible())) { 584 int colHeadHeight = colHead.getHeight(); 585 borderR.y += colHeadHeight; 586 borderR.height -= colHeadHeight; 587 } 588 589 592 593 JViewport rowHead = getRowHeader(); 594 if ((rowHead != null) && (rowHead.isVisible())) { 595 int rowHeadWidth = rowHead.getWidth(); 596 if ( leftToRight ) { 597 borderR.x += rowHeadWidth; 598 } 599 borderR.width -= rowHeadWidth; 600 } 601 602 605 JScrollBar vsb = getVerticalScrollBar(); 606 if ((vsb != null) && (vsb.isVisible())) { 607 int vsbWidth = vsb.getWidth(); 608 if ( !leftToRight ) { 609 borderR.x += vsbWidth; 610 } 611 borderR.width -= vsbWidth; 612 } 613 614 617 JScrollBar hsb = getHorizontalScrollBar(); 618 if ((hsb != null) && (hsb.isVisible())) { 619 borderR.height -= hsb.getHeight(); 620 } 621 622 return borderR; 623 } 624 625 626 648 protected class ScrollBar extends JScrollBar implements UIResource 649 { 650 656 private boolean unitIncrementSet; 657 663 private boolean blockIncrementSet; 664 665 676 public ScrollBar(int orientation) { 677 super(orientation); 678 } 679 680 686 public void setUnitIncrement(int unitIncrement) { 687 unitIncrementSet = true; 688 super.setUnitIncrement(unitIncrement); 689 } 690 691 701 public int getUnitIncrement(int direction) { 702 JViewport vp = getViewport(); 703 if (!unitIncrementSet && (vp != null) && 704 (vp.getView() instanceof Scrollable )) { 705 Scrollable view = (Scrollable )(vp.getView()); 706 Rectangle vr = vp.getViewRect(); 707 return view.getScrollableUnitIncrement(vr, getOrientation(), direction); 708 } 709 else { 710 return super.getUnitIncrement(direction); 711 } 712 } 713 714 720 public void setBlockIncrement(int blockIncrement) { 721 blockIncrementSet = true; 722 super.setBlockIncrement(blockIncrement); 723 } 724 725 737 public int getBlockIncrement(int direction) { 738 JViewport vp = getViewport(); 739 if (blockIncrementSet || vp == null) { 740 return super.getBlockIncrement(direction); 741 } 742 else if (vp.getView() instanceof Scrollable ) { 743 Scrollable view = (Scrollable )(vp.getView()); 744 Rectangle vr = vp.getViewRect(); 745 return view.getScrollableBlockIncrement(vr, getOrientation(), direction); 746 } 747 else if (getOrientation() == VERTICAL) { 748 return vp.getExtentSize().height; 749 } 750 else { 751 return vp.getExtentSize().width; 752 } 753 } 754 755 } 756 757 758 768 public JScrollBar createHorizontalScrollBar() { 769 return new ScrollBar(JScrollBar.HORIZONTAL); 770 } 771 772 773 780 public JScrollBar getHorizontalScrollBar() { 781 return horizontalScrollBar; 782 } 783 784 785 800 public void setHorizontalScrollBar(JScrollBar horizontalScrollBar) { 801 JScrollBar old = getHorizontalScrollBar(); 802 this.horizontalScrollBar = horizontalScrollBar; 803 if (horizontalScrollBar != null) { 804 add(horizontalScrollBar, HORIZONTAL_SCROLLBAR); 805 } 806 else if (old != null) { 807 remove(old); 808 } 809 firePropertyChange("horizontalScrollBar", old, horizontalScrollBar); 810 811 revalidate(); 812 repaint(); 813 } 814 815 816 826 public JScrollBar createVerticalScrollBar() { 827 return new ScrollBar(JScrollBar.VERTICAL); 828 } 829 830 831 838 public JScrollBar getVerticalScrollBar() { 839 return verticalScrollBar; 840 } 841 842 843 858 public void setVerticalScrollBar(JScrollBar verticalScrollBar) { 859 JScrollBar old = getVerticalScrollBar(); 860 this.verticalScrollBar = verticalScrollBar; 861 add(verticalScrollBar, VERTICAL_SCROLLBAR); 862 firePropertyChange("verticalScrollBar", old, verticalScrollBar); 863 864 revalidate(); 865 repaint(); 866 } 867 868 869 879 protected JViewport createViewport() { 880 return new JViewport (); 881 } 882 883 884 890 public JViewport getViewport() { 891 return viewport; 892 } 893 894 895 920 public void setViewport(JViewport viewport) { 921 JViewport old = getViewport(); 922 this.viewport = viewport; 923 if (viewport != null) { 924 add(viewport, VIEWPORT); 925 } 926 else if (old != null) { 927 remove(old); 928 } 929 firePropertyChange("viewport", old, viewport); 930 931 if (accessibleContext != null) { 932 ((AccessibleJScrollPane)accessibleContext).resetViewPort(); 933 } 934 935 revalidate(); 936 repaint(); 937 } 938 939 940 956 public void setViewportView(Component view) { 957 if (getViewport() == null) { 958 setViewport(createViewport()); 959 } 960 getViewport().setView(view); 961 } 962 963 964 965 970 public JViewport getRowHeader() { 971 return rowHeader; 972 } 973 974 975 996 public void setRowHeader(JViewport rowHeader) { 997 JViewport old = getRowHeader(); 998 this.rowHeader = rowHeader; 999 if (rowHeader != null) { 1000 add(rowHeader, ROW_HEADER); 1001 } 1002 else if (old != null) { 1003 remove(old); 1004 } 1005 firePropertyChange("rowHeader", old, rowHeader); 1006 revalidate(); 1007 repaint(); 1008 } 1009 1010 1011 1025 public void setRowHeaderView(Component view) { 1026 if (getRowHeader() == null) { 1027 setRowHeader(createViewport()); 1028 } 1029 getRowHeader().setView(view); 1030 } 1031 1032 1033 1034 1039 public JViewport getColumnHeader() { 1040 return columnHeader; 1041 } 1042 1043 1044 |