1 7 package java.awt; 8 9 import java.awt.peer.LightweightPeer; 10 import java.awt.peer.ScrollPanePeer; 11 import java.awt.event.*; 12 import javax.accessibility.*; 13 import sun.awt.ScrollPaneWheelScroller; 14 import sun.awt.SunToolkit; 15 16 import java.io.Serializable ; 17 import java.io.ObjectInputStream ; 18 import java.io.ObjectOutputStream ; 19 import java.io.IOException ; 20 21 74 public class ScrollPane extends Container implements Accessible { 75 76 77 80 private static native void initIDs(); 81 82 static { 83 84 Toolkit.loadLibraries(); 85 if (!GraphicsEnvironment.isHeadless()) { 86 initIDs(); 87 } 88 } 89 90 95 public static final int SCROLLBARS_AS_NEEDED = 0; 96 97 101 public static final int SCROLLBARS_ALWAYS = 1; 102 103 107 public static final int SCROLLBARS_NEVER = 2; 108 109 117 private int scrollbarDisplayPolicy; 118 119 129 private ScrollPaneAdjustable vAdjustable; 130 131 141 private ScrollPaneAdjustable hAdjustable; 142 143 private static final String base = "scrollpane"; 144 private static int nameCounter = 0; 145 146 private static final boolean defaultWheelScroll = true; 147 148 155 private boolean wheelScrollingEnabled = defaultWheelScroll; 156 157 160 private static final long serialVersionUID = 7956609840827222915L; 161 162 169 public ScrollPane() throws HeadlessException { 170 this(SCROLLBARS_AS_NEEDED); 171 } 172 173 182 public ScrollPane(int scrollbarDisplayPolicy) throws HeadlessException { 183 GraphicsEnvironment.checkHeadless(); 184 this.layoutMgr = null; 185 this.width = 100; 186 this.height = 100; 187 switch (scrollbarDisplayPolicy) { 188 case SCROLLBARS_NEVER: 189 case SCROLLBARS_AS_NEEDED: 190 case SCROLLBARS_ALWAYS: 191 this.scrollbarDisplayPolicy = scrollbarDisplayPolicy; 192 break; 193 default: 194 throw new IllegalArgumentException ("illegal scrollbar display policy"); 195 } 196 197 vAdjustable = new ScrollPaneAdjustable (this, new PeerFixer(this), 198 Adjustable.VERTICAL); 199 hAdjustable = new ScrollPaneAdjustable (this, new PeerFixer(this), 200 Adjustable.HORIZONTAL); 201 setWheelScrollingEnabled(defaultWheelScroll); 202 } 203 204 208 String constructComponentName() { 209 synchronized (getClass()) { 210 return base + nameCounter++; 211 } 212 } 213 214 private void addToPanel(Component comp, Object constraints, int index) { 218 Panel child = new Panel (); 219 child.setLayout(new BorderLayout ()); 220 child.add(comp); 221 super.addImpl(child, constraints, index); 222 validate(); 223 } 224 225 233 protected final void addImpl(Component comp, Object constraints, int index) { 234 synchronized (getTreeLock()) { 235 if (getComponentCount() > 0) { 236 remove(0); 237 } 238 if (index > 0) { 239 throw new IllegalArgumentException ("position greater than 0"); 240 } 241 242 if (!SunToolkit.isLightweightOrUnknown(comp)) { 243 super.addImpl(comp, constraints, index); 244 } else { 245 addToPanel(comp, constraints, index); 246 } 247 } 248 } 249 250 254 public int getScrollbarDisplayPolicy() { 255 return scrollbarDisplayPolicy; 256 } 257 258 262 public Dimension getViewportSize() { 263 Insets i = getInsets(); 264 return new Dimension (width - i.right - i.left, 265 height - i.top - i.bottom); 266 } 267 268 274 public int getHScrollbarHeight() { 275 int h = 0; 276 if (scrollbarDisplayPolicy != SCROLLBARS_NEVER) { 277 ScrollPanePeer peer = (ScrollPanePeer)this.peer; 278 if (peer != null) { 279 h = peer.getHScrollbarHeight(); 280 } 281 } 282 return h; 283 } 284 285 291 public int getVScrollbarWidth() { 292 int w = 0; 293 if (scrollbarDisplayPolicy != SCROLLBARS_NEVER) { 294 ScrollPanePeer peer = (ScrollPanePeer)this.peer; 295 if (peer != null) { 296 w = peer.getVScrollbarWidth(); 297 } 298 } 299 return w; 300 } 301 302 309 public Adjustable getVAdjustable() { 310 return vAdjustable; 311 } 312 313 320 public Adjustable getHAdjustable() { 321 return hAdjustable; 322 } 323 324 339 public void setScrollPosition(int x, int y) { 340 synchronized (getTreeLock()) { 341 if (ncomponents <= 0) { 342 throw new NullPointerException ("child is null"); 343 } 344 hAdjustable.setValue(x); 345 vAdjustable.setValue(y); 346 } 347 } 348 349 362 public void setScrollPosition(Point p) { 363 setScrollPosition(p.x, p.y); 364 } 365 366 375 public Point getScrollPosition() { 376 if (ncomponents <= 0) { 377 throw new NullPointerException ("child is null"); 378 } 379 return new Point (hAdjustable.getValue(), vAdjustable.getValue()); 380 } 381 382 387 public final void setLayout(LayoutManager mgr) { 388 throw new AWTError ("ScrollPane controls layout"); 389 } 390 391 399 public void doLayout() { 400 layout(); 401 } 402 403 410 Dimension calculateChildSize() { 411 Dimension size = getSize(); 417 Insets insets = getInsets(); 418 int viewWidth = size.width - insets.left*2; 419 int viewHeight = size.height - insets.top*2; 420 421 boolean vbarOn; 425 boolean hbarOn; 426 Component child = getComponent(0); 427 Dimension childSize = new Dimension (child.getPreferredSize()); 428 429 if (scrollbarDisplayPolicy == SCROLLBARS_AS_NEEDED) { 430 vbarOn = childSize.height > viewHeight; 431 hbarOn = childSize.width > viewWidth; 432 } else if (scrollbarDisplayPolicy == SCROLLBARS_ALWAYS) { 433 vbarOn = hbarOn = true; 434 } else { vbarOn = hbarOn = false; 436 } 437 438 int vbarWidth = getVScrollbarWidth(); 442 int hbarHeight = getHScrollbarHeight(); 443 if (vbarOn) { 444 viewWidth -= vbarWidth; 445 } 446 if(hbarOn) { 447 viewHeight -= hbarHeight; 448 } 449 450 if (childSize.width < viewWidth) { 454 childSize.width = viewWidth; 455 } 456 if (childSize.height < viewHeight) { 457 childSize.height = viewHeight; 458 } 459 460 return childSize; 461 } 462 463 467 @Deprecated 468 public void layout() { 469 if (ncomponents > 0) { 470 Component c = getComponent(0); 471 Point p = getScrollPosition(); 472 Dimension cs = calculateChildSize(); 473 Dimension vs = getViewportSize(); 474 Insets i = getInsets(); 475 476 c.reshape(i.left - p.x, i.top - p.y, cs.width, cs.height); 477 ScrollPanePeer peer = (ScrollPanePeer)this.peer; 478 if (peer != null) { 479 peer.childResized(cs.width, cs.height); 480 } 481 482 vs = getViewportSize(); 486 hAdjustable.setSpan(0, cs.width, vs.width); 487 vAdjustable.setSpan(0, cs.height, vs.height); 488 } 489 } 490 491 497 public void printComponents(Graphics g) { 498 if (ncomponents > 0) { 499 Component c = component[0]; 500 Point p = c.getLocation(); 501 Dimension vs = getViewportSize(); 502 Insets i = getInsets(); 503 504 Graphics cg = g.create(); 505 try { 506 cg.clipRect(i.left, i.top, vs.width, vs.height); 507 cg.translate(p.x, p.y); 508 c.printAll(cg); 509 } finally { 510 cg.dispose(); 511 } 512 } 513 } 514 515 518 public void addNotify() { 519 synchronized (getTreeLock()) { 520 521 int vAdjustableValue = 0; 522 int hAdjustableValue = 0; 523 524 if (getComponentCount() > 0) { 529 vAdjustableValue = vAdjustable.getValue(); 530 hAdjustableValue = hAdjustable.getValue(); 531 vAdjustable.setValue(0); 532 hAdjustable.setValue(0); 533 } 534 535 if (peer == null) 536 peer = getToolkit().createScrollPane(this); 537 super.addNotify(); 538 539 if (getComponentCount() > 0) { 541 vAdjustable.setValue(vAdjustableValue); 542 hAdjustable.setValue(hAdjustableValue); 543 } 544 } 545 } 546 547 557 public String paramString() { 558 String sdpStr; 559 switch (scrollbarDisplayPolicy) { 560 case SCROLLBARS_AS_NEEDED: 561 sdpStr = "as-needed"; 562 break; 563 case SCROLLBARS_ALWAYS: 564 sdpStr = "always"; 565 break; 566 case SCROLLBARS_NEVER: 567 sdpStr = "never"; 568 break; 569 default: 570 sdpStr = "invalid display policy"; 571 } 572 Point p = ncomponents > 0? getScrollPosition() : new Point (0,0); 573 Insets i = getInsets(); 574 return super.paramString()+",ScrollPosition=("+p.x+","+p.y+")"+ 575 ",Insets=("+i.top+","+i.left+","+i.bottom+","+i.right+")"+ 576 ",ScrollbarDisplayPolicy="+sdpStr+ 577 ",wheelScrollingEnabled="+isWheelScrollingEnabled(); 578 } 579 580 void autoProcessMouseWheel(MouseWheelEvent e) { 581 processMouseWheelEvent(e); 582 } 583 584 594 protected void processMouseWheelEvent(MouseWheelEvent e) { 595 if (isWheelScrollingEnabled()) { 596 ScrollPaneWheelScroller.handleWheelScrolling(this, e); 597 e.consume(); 598 } 599 super.processMouseWheelEvent(e); 600 } 601 602 606 protected boolean eventTypeEnabled(int type) { 607 if (type == MouseEvent.MOUSE_WHEEL && isWheelScrollingEnabled()) { 608 return true; 609 } 610 else { 611 return super.eventTypeEnabled(type); 612 } 613 } 614 615 627 public void setWheelScrollingEnabled(boolean handleWheel) { 628 wheelScrollingEnabled = handleWheel; 629 } 630 631 638 public boolean isWheelScrollingEnabled() { 639 return wheelScrollingEnabled; 640 } 641 642 643 646 private void writeObject(ObjectOutputStream s) throws IOException { 647 s.defaultWriteObject(); 651 } 652 653 660 private void readObject(ObjectInputStream s) 661 throws ClassNotFoundException , IOException , HeadlessException 662 { 663 GraphicsEnvironment.checkHeadless(); 664 ObjectInputStream.GetField f = s.readFields(); 667 668 scrollbarDisplayPolicy = f.get("scrollbarDisplayPolicy", 670 SCROLLBARS_AS_NEEDED); 671 hAdjustable = (ScrollPaneAdjustable )f.get("hAdjustable", null); 672 vAdjustable = (ScrollPaneAdjustable )f.get("vAdjustable", null); 673 674 wheelScrollingEnabled = f.get("wheelScrollingEnabled", 676 defaultWheelScroll); 677 678 } 691 692 class PeerFixer implements AdjustmentListener, java.io.Serializable 693 { 694 private static final long serialVersionUID = 1043664721353696630L; 695 696 PeerFixer(ScrollPane scroller) { 697 this.scroller = scroller; 698 } 699 700 703 public void adjustmentValueChanged(AdjustmentEvent e) { 704 Adjustable adj = e.getAdjustable(); 705 int value = e.getValue(); 706 ScrollPanePeer peer = (ScrollPanePeer) scroller.peer; 707 if (peer != null) { 708 peer.setValue(adj, value); 709 } 710 711 Component c = scroller.getComponent(0); 712 switch(adj.getOrientation()) { 713 case Adjustable.VERTICAL: 714 c.move(c.getLocation().x, -(value)); 715 break; 716 case Adjustable.HORIZONTAL: 717 c.move(-(value), c.getLocation().y); 718 break; 719 default: 720 throw new IllegalArgumentException ("Illegal adjustable orientation"); 721 } 722 } 723 724 private ScrollPane scroller; 725 } 726 727 728 732 741 public AccessibleContext getAccessibleContext() { 742 if (accessibleContext == null) { 743 accessibleContext = new AccessibleAWTScrollPane(); 744 } 745 return accessibleContext; 746 } 747 748 754 protected class AccessibleAWTScrollPane extends AccessibleAWTContainer 755 { 756 759 private static final long serialVersionUID = 6100703663886637L; 760 761 768 public AccessibleRole getAccessibleRole() { 769 return AccessibleRole.SCROLL_PANE; 770 } 771 772 } 774 } 775 776 787 class PeerFixer implements AdjustmentListener, java.io.Serializable { 788 789 PeerFixer(ScrollPane scroller) { 790 this.scroller = scroller; 791 } 792 793 796 public void adjustmentValueChanged(AdjustmentEvent e) { 797 Adjustable adj = e.getAdjustable(); 798 int value = e.getValue(); 799 ScrollPanePeer peer = (ScrollPanePeer) scroller.peer; 800 if (peer != null) { 801 peer.setValue(adj, value); 802 } 803 804 Component c = scroller.getComponent(0); 805 switch(adj.getOrientation()) { 806 case Adjustable.VERTICAL: 807 c.move(c.getLocation().x, -(value)); 808 break; 809 case Adjustable.HORIZONTAL: 810 c.move(-(value), c.getLocation().y); 811 break; 812 default: 813 throw new IllegalArgumentException ("Illegal adjustable orientation"); 814 } 815 } 816 817 private ScrollPane scroller; 818 } 819 | Popular Tags |