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 1061 public void setColumnHeader(JViewport columnHeader) { 1062 JViewport old = getColumnHeader(); 1063 this.columnHeader = columnHeader; 1064 if (columnHeader != null) { 1065 add(columnHeader, COLUMN_HEADER); 1066 } 1067 else if (old != null) { 1068 remove(old); 1069 } 1070 firePropertyChange("columnHeader", old, columnHeader); 1071 1072 revalidate(); 1073 repaint(); 1074 } 1075 1076 1077 1078 1093 public void setColumnHeaderView(Component view) { 1094 if (getColumnHeader() == null) { 1095 setColumnHeader(createViewport()); 1096 } 1097 getColumnHeader().setView(view); 1098 } 1099 1100 1101 1126 public Component getCorner(String key) { 1127 boolean isLeftToRight = getComponentOrientation().isLeftToRight(); 1128 if (key.equals(LOWER_LEADING_CORNER)) { 1129 key = isLeftToRight ? LOWER_LEFT_CORNER : LOWER_RIGHT_CORNER; 1130 } else if (key.equals(LOWER_TRAILING_CORNER)) { 1131 key = isLeftToRight ? LOWER_RIGHT_CORNER : LOWER_LEFT_CORNER; 1132 } else if (key.equals(UPPER_LEADING_CORNER)) { 1133 key = isLeftToRight ? UPPER_LEFT_CORNER : UPPER_RIGHT_CORNER; 1134 } else if (key.equals(UPPER_TRAILING_CORNER)) { 1135 key = isLeftToRight ? UPPER_RIGHT_CORNER : UPPER_LEFT_CORNER; 1136 } 1137 if (key.equals(LOWER_LEFT_CORNER)) { 1138 return lowerLeft; 1139 } 1140 else if (key.equals(LOWER_RIGHT_CORNER)) { 1141 return lowerRight; 1142 } 1143 else if (key.equals(UPPER_LEFT_CORNER)) { 1144 return upperLeft; 1145 } 1146 else if (key.equals(UPPER_RIGHT_CORNER)) { 1147 return upperRight; 1148 } 1149 else { 1150 return null; 1151 } 1152 } 1153 1154 1155 1187 public void setCorner(String key, Component corner) 1188 { 1189 Component old; 1190 boolean isLeftToRight = getComponentOrientation().isLeftToRight(); 1191 if (key.equals(LOWER_LEADING_CORNER)) { 1192 key = isLeftToRight ? LOWER_LEFT_CORNER : LOWER_RIGHT_CORNER; 1193 } else if (key.equals(LOWER_TRAILING_CORNER)) { 1194 key = isLeftToRight ? LOWER_RIGHT_CORNER : LOWER_LEFT_CORNER; 1195 } else if (key.equals(UPPER_LEADING_CORNER)) { 1196 key = isLeftToRight ? UPPER_LEFT_CORNER : UPPER_RIGHT_CORNER; 1197 } else if (key.equals(UPPER_TRAILING_CORNER)) { 1198 key = isLeftToRight ? UPPER_RIGHT_CORNER : UPPER_LEFT_CORNER; 1199 } 1200 if (key.equals(LOWER_LEFT_CORNER)) { 1201 old = lowerLeft; 1202 lowerLeft = corner; 1203 } 1204 else if (key.equals(LOWER_RIGHT_CORNER)) { 1205 old = lowerRight; 1206 lowerRight = corner; 1207 } 1208 else if (key.equals(UPPER_LEFT_CORNER)) { 1209 old = upperLeft; 1210 upperLeft = corner; 1211 } 1212 else if (key.equals(UPPER_RIGHT_CORNER)) { 1213 old = upperRight; 1214 upperRight = corner; 1215 } 1216 else { 1217 throw new IllegalArgumentException ("invalid corner key"); 1218 } 1219 if (old != null) { 1220 remove(old); 1221 } 1222 if (corner != null) { 1223 add(corner, key); 1224 } 1225 firePropertyChange(key, old, corner); 1226 revalidate(); 1227 repaint(); 1228 } 1229 1230 1243 public void setComponentOrientation( ComponentOrientation co ) { 1244 super.setComponentOrientation( co ); 1245 if( verticalScrollBar != null ) 1246 verticalScrollBar.setComponentOrientation( co ); 1247 if( horizontalScrollBar != null ) 1248 horizontalScrollBar.setComponentOrientation( co ); 1249 } 1250 1251 1261 public boolean isWheelScrollingEnabled() {return wheelScrollState;} 1262 1263 1278 public void setWheelScrollingEnabled(boolean handleWheel) { 1279 boolean old = wheelScrollState; 1280 wheelScrollState = handleWheel; 1281 firePropertyChange("wheelScrollingEnabled", old, handleWheel); 1282 } 1283 1284 1289 private void writeObject(ObjectOutputStream s) throws IOException { 1290 s.defaultWriteObject(); 1291 if (getUIClassID().equals(uiClassID)) { 1292 byte count = JComponent.getWriteObjCounter(this); 1293 JComponent.setWriteObjCounter(this, --count); 1294 if (count == 0 && ui != null) { 1295 ui.installUI(this); 1296 } 1297 } 1298 } 1299 1300 1301 1311 protected String paramString() { 1312 String viewportBorderString = (viewportBorder != null ? 1313 viewportBorder.toString() : ""); 1314 String viewportString = (viewport != null ? 1315 viewport.toString() : ""); 1316 String verticalScrollBarPolicyString; 1317 if (verticalScrollBarPolicy == VERTICAL_SCROLLBAR_AS_NEEDED) { 1318 verticalScrollBarPolicyString = "VERTICAL_SCROLLBAR_AS_NEEDED"; 1319 } else if (verticalScrollBarPolicy == VERTICAL_SCROLLBAR_NEVER) { 1320 verticalScrollBarPolicyString = "VERTICAL_SCROLLBAR_NEVER"; 1321 } else if (verticalScrollBarPolicy == VERTICAL_SCROLLBAR_ALWAYS) { 1322 verticalScrollBarPolicyString = "VERTICAL_SCROLLBAR_ALWAYS"; 1323 } else verticalScrollBarPolicyString = ""; 1324 String horizontalScrollBarPolicyString; 1325 if (horizontalScrollBarPolicy == HORIZONTAL_SCROLLBAR_AS_NEEDED) { 1326 horizontalScrollBarPolicyString = "HORIZONTAL_SCROLLBAR_AS_NEEDED"; 1327 } else if (horizontalScrollBarPolicy == HORIZONTAL_SCROLLBAR_NEVER) { 1328 horizontalScrollBarPolicyString = "HORIZONTAL_SCROLLBAR_NEVER"; 1329 } else if (horizontalScrollBarPolicy == HORIZONTAL_SCROLLBAR_ALWAYS) { 1330 horizontalScrollBarPolicyString = "HORIZONTAL_SCROLLBAR_ALWAYS"; 1331 } else horizontalScrollBarPolicyString = ""; 1332 String horizontalScrollBarString = (horizontalScrollBar != null ? 1333 horizontalScrollBar.toString() 1334 : ""); 1335 String verticalScrollBarString = (verticalScrollBar != null ? 1336 verticalScrollBar.toString() : ""); 1337 String columnHeaderString = (columnHeader != null ? 1338 columnHeader.toString() : ""); 1339 String rowHeaderString = (rowHeader != null ? 1340 rowHeader.toString() : ""); 1341 String lowerLeftString = (lowerLeft != null ? 1342 lowerLeft.toString() : ""); 1343 String lowerRightString = (lowerRight != null ? 1344 lowerRight.toString() : ""); 1345 String upperLeftString = (upperLeft != null ? 1346 upperLeft.toString() : ""); 1347 String upperRightString = (upperRight != null ? 1348 upperRight.toString() : ""); 1349 1350 return super.paramString() + 1351 ",columnHeader=" + columnHeaderString + 1352 ",horizontalScrollBar=" + horizontalScrollBarString + 1353 ",horizontalScrollBarPolicy=" + horizontalScrollBarPolicyString + 1354 ",lowerLeft=" + lowerLeftString + 1355 ",lowerRight=" + lowerRightString + 1356 ",rowHeader=" + rowHeaderString + 1357 ",upperLeft=" + upperLeftString + 1358 ",upperRight=" + upperRightString + 1359 ",verticalScrollBar=" + verticalScrollBarString + 1360 ",verticalScrollBarPolicy=" + verticalScrollBarPolicyString + 1361 ",viewport=" + viewportString + 1362 ",viewportBorder=" + viewportBorderString; 1363 } 1364 1365 1369 1378 public AccessibleContext getAccessibleContext() { 1379 if (accessibleContext == null) { 1380 accessibleContext = new AccessibleJScrollPane(); 1381 } 1382 return accessibleContext; 1383 } 1384 1385 1400 protected class AccessibleJScrollPane extends AccessibleJComponent 1401 implements ChangeListener, PropertyChangeListener { 1402 1403 protected JViewport viewPort = null; 1404 1405 1408 public void resetViewPort() { 1409 if (viewPort != null) { 1410 viewPort.removeChangeListener(this); 1411 viewPort.removePropertyChangeListener(this); 1412 } 1413 viewPort = JScrollPane.this.getViewport(); 1414 if (viewPort != null) { 1415 viewPort.addChangeListener(this); 1416 viewPort.addPropertyChangeListener(this); 1417 } 1418 } 1419 1420 1423 public AccessibleJScrollPane() { 1424 super(); 1425 1426 resetViewPort(); 1427 1428 JScrollBar scrollBar = getHorizontalScrollBar(); 1431 if (scrollBar != null) { 1432 setScrollBarRelations(scrollBar); 1433 } 1434 scrollBar = getVerticalScrollBar(); 1435 if (scrollBar != null) { 1436 setScrollBarRelations(scrollBar); 1437 } 1438 } 1439 1440 1447 public AccessibleRole getAccessibleRole() { 1448 return AccessibleRole.SCROLL_PANE; 1449 } 1450 1451 1458 public void stateChanged(ChangeEvent e) { 1459 if (e == null) { 1460 throw new NullPointerException (); 1461 } 1462 firePropertyChange(ACCESSIBLE_VISIBLE_DATA_PROPERTY, 1463 Boolean.valueOf(false), 1464 Boolean.valueOf(true)); 1465 } 1466 1467 1475 public void propertyChange(PropertyChangeEvent e) { 1476 String propertyName = e.getPropertyName(); 1477 if (propertyName == "horizontalScrollBar" || 1478 propertyName == "verticalScrollBar") { 1479 1480 if (e.getNewValue() instanceof JScrollBar ) { 1481 setScrollBarRelations((JScrollBar )e.getNewValue()); 1482 } 1483 } 1484 } 1485 1486 1487 1491 void setScrollBarRelations(JScrollBar scrollBar) { 1492 1496 AccessibleRelation controlledBy = 1497 new AccessibleRelation(AccessibleRelation.CONTROLLED_BY, 1498 scrollBar); 1499 AccessibleRelation controllerFor = 1500 new AccessibleRelation(AccessibleRelation.CONTROLLER_FOR, 1501 JScrollPane.this); 1502 1503 AccessibleContext ac = scrollBar.getAccessibleContext(); 1505 ac.getAccessibleRelationSet().add(controllerFor); 1506 1507 getAccessibleRelationSet().add(controlledBy); 1509 } 1510 } 1511} 1512 1513 | Popular Tags |