J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package javax.swing; |
| 27 | |
| 28 | import javax.swing.plaf.*; |
| 29 | import javax.swing.border.*; |
| 30 | import javax.swing.event.*; |
| 31 | import javax.accessibility.*; |
| 32 | |
| 33 | import java.awt.Component; |
| 34 | import java.awt.ComponentOrientation; |
| 35 | import java.awt.Graphics; |
| 36 | import java.awt.Rectangle; |
| 37 | import java.awt.Insets; |
| 38 | import java.awt.Color; |
| 39 | import java.awt.LayoutManager; |
| 40 | import java.awt.Point; |
| 41 | |
| 42 | import java.io.ObjectOutputStream; |
| 43 | import java.io.ObjectInputStream; |
| 44 | import java.io.IOException; |
| 45 | |
| 46 | import java.beans.*; |
| 47 | |
| 48 | |
| 49 | /** |
| 50 | * Provides a scrollable view of a lightweight component. |
| 51 | * A <code>JScrollPane</code> manages a viewport, optional |
| 52 | * vertical and horizontal scroll bars, and optional row and |
| 53 | * column heading viewports. |
| 54 | * You can find task-oriented documentation of <code>JScrollPane</code> in |
| 55 | * <a href="http://java.sun.com/docs/books/tutorial/uiswing/components/scrollpane.html">How to Use Scroll Panes</a>, |
| 56 | * a section in <em>The Java Tutorial</em>. Note that |
| 57 | * <code>JScrollPane</code> does not support heavyweight components. |
| 58 | * <p> |
| 59 | * <TABLE ALIGN="RIGHT" BORDER="0" SUMMARY="layout"> |
| 60 | * <TR> |
| 61 | * <TD ALIGN="CENTER"> |
| 62 | * <P ALIGN="CENTER"><IMG SRC="doc-files/JScrollPane-1.gif" |
| 63 | * alt="The following text describes this image." |
| 64 | * WIDTH="256" HEIGHT="248" ALIGN="BOTTOM" BORDER="0"> |
| 65 | * </TD> |
| 66 | * </TR> |
| 67 | * </TABLE> |
| 68 | * The <code>JViewport</code> provides a window, |
| 69 | * or "viewport" onto a data |
| 70 | * source -- for example, a text file. That data source is the |
| 71 | * "scrollable client" (aka data model) displayed by the |
| 72 | * <code>JViewport</code> view. |
| 73 | * A <code>JScrollPane</code> basically consists of <code>JScrollBar</code>s, |
| 74 | * a <code>JViewport</code>, and the wiring between them, |
| 75 | * as shown in the diagram at right. |
| 76 | * <p> |
| 77 | * In addition to the scroll bars and viewport, |
| 78 | * a <code>JScrollPane</code> can have a |
| 79 | * column header and a row header. Each of these is a |
| 80 | * <code>JViewport</code> object that |
| 81 | * you specify with <code>setRowHeaderView</code>, |
| 82 | * and <code>setColumnHeaderView</code>. |
| 83 | * The column header viewport automatically scrolls left and right, tracking |
| 84 | * the left-right scrolling of the main viewport. |
| 85 | * (It never scrolls vertically, however.) |
| 86 | * The row header acts in a similar fashion. |
| 87 | * <p> |
| 88 | * Where two scroll bars meet, the row header meets the column header, |
| 89 | * or a scroll bar meets one of the headers, both components stop short |
| 90 | * of the corner, leaving a rectangular space which is, by default, empty. |
| 91 | * These spaces can potentially exist in any number of the four corners. |
| 92 | * In the previous diagram, the top right space is present and identified |
| 93 | * by the label "corner component". |
| 94 | * <p> |
| 95 | * Any number of these empty spaces can be replaced by using the |
| 96 | * <code>setCorner</code> method to add a component to a particular corner. |
| 97 | * (Note: The same component cannot be added to multiple corners.) |
| 98 | * This is useful if there's |
| 99 | * some extra decoration or function you'd like to add to the scroll pane. |
| 100 | * The size of each corner component is entirely determined by the size of the |
| 101 | * headers and/or scroll bars that surround it. |
| 102 | * <p> |
| 103 | * A corner component will only be visible if there is an empty space in that |
| 104 | * corner for it to exist in. For example, consider a component set into the |
| 105 | * top right corner of a scroll pane with a column header. If the scroll pane's |
| 106 | * vertical scrollbar is not present, perhaps because the view component hasn't |
| 107 | * grown large enough to require it, then the corner component will not be |
| 108 | * shown (since there is no empty space in that corner created by the meeting |
| 109 | * of the header and vertical scroll bar). Forcing the scroll bar to always be |
| 110 | * shown, using |
| 111 | * <code>setVerticalScrollBarPolicy(VERTICAL_SCROLLBAR_ALWAYS)</code>, |
| 112 | * will ensure that the space for the corner component always exists. |
| 113 | * <p> |
| 114 | * To add a border around the main viewport, |
| 115 | * you can use <code>setViewportBorder</code>. |
| 116 | * (Of course, you can also add a border around the whole scroll pane using |
| 117 | * <code>setBorder</code>.) |
| 118 | * <p> |
| 119 | * A common operation to want to do is to set the background color that will |
| 120 | * be used if the main viewport view is smaller than the viewport, or is |
| 121 | * not opaque. This can be accomplished by setting the background color |
| 122 | * of the viewport, via <code>scrollPane.getViewport().setBackground()</code>. |
| 123 | * The reason for setting the color of the viewport and not the scrollpane |
| 124 | * is that by default <code>JViewport</code> is opaque |
| 125 | * which, among other things, means it will completely fill |
| 126 | * in its background using its background color. Therefore when |
| 127 | * <code>JScrollPane</code> draws its background the viewport will |
| 128 | * usually draw over it. |
| 129 | * <p> |
| 130 | * By default <code>JScrollPane</code> uses <code>ScrollPaneLayout</code> |
| 131 | * to handle the layout of its child Components. <code>ScrollPaneLayout</code> |
| 132 | * determines the size to make the viewport view in one of two ways: |
| 133 | * <ol> |
| 134 | * <li>If the view implements <code>Scrollable</code> |
| 135 | * a combination of <code>getPreferredScrollableViewportSize</code>, |
| 136 | * <code>getScrollableTracksViewportWidth</code> and |
| 137 | * <code>getScrollableTracksViewportHeight</code>is used, otherwise |
| 138 | * <li><code>getPreferredSize</code> is used. |
| 139 | * </ol> |
| 140 | * <p> |
| 141 | * <strong>Warning:</strong> Swing is not thread safe. For more |
| 142 | * information see <a |
| 143 | * href="package-summary.html#threading">Swing's Threading |
| 144 | * Policy</a>. |
| 145 | * <p> |
| 146 | * <strong>Warning:</strong> |
| 147 | * Serialized objects of this class will not be compatible with |
| 148 | * future Swing releases. The current serialization support is |
| 149 | * appropriate for short term storage or RMI between applications running |
| 150 | * the same version of Swing. As of 1.4, support for long term storage |
| 151 | * of all JavaBeans<sup><font size="-2">TM</font></sup> |
| 152 | * has been added to the <code>java.beans</code> package. |
| 153 | * Please see {@link java.beans.XMLEncoder}. |
| 154 | * |
| 155 | * @see JScrollBar |
| 156 | * @see JViewport |
| 157 | * @see ScrollPaneLayout |
| 158 | * @see Scrollable |
| 159 | * @see Component#getPreferredSize |
| 160 | * @see #setViewportView |
| 161 | * @see #setRowHeaderView |
| 162 | * @see #setColumnHeaderView |
| 163 | * @see #setCorner |
| 164 | * @see #setViewportBorder |
| 165 | * |
| 166 | * @beaninfo |
| 167 | * attribute: isContainer true |
| 168 | * attribute: containerDelegate getViewport |
| 169 | * description: A specialized container that manages a viewport, optional scrollbars and headers |
| 170 | * |
| 171 | * @author Hans Muller |
| 172 | */ |
| 173 | public class JScrollPane extends JComponent implements ScrollPaneConstants, Accessible |
| 174 | { |
| 175 | private Border viewportBorder; |
| 176 | |
| 177 | /** |
| 178 | * @see #getUIClassID |
| 179 | * @see #readObject |
| 180 | */ |
| 181 | private static final String uiClassID = "ScrollPaneUI"; |
| 182 | |
| 183 | /** |
| 184 | * The display policy for the vertical scrollbar. |
| 185 | * The default is |
| 186 | * <code>ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED</code>. |
| 187 | * @see #setVerticalScrollBarPolicy |
| 188 | */ |
| 189 | protected int verticalScrollBarPolicy = VERTICAL_SCROLLBAR_AS_NEEDED; |
| 190 | |
| 191 | |
| 192 | /** |
| 193 | * The display policy for the horizontal scrollbar. |
| 194 | * The default is |
| 195 | * <code>ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED</code>. |
| 196 | * @see #setHorizontalScrollBarPolicy |
| 197 | */ |
| 198 | protected int horizontalScrollBarPolicy = HORIZONTAL_SCROLLBAR_AS_NEEDED; |
| 199 | |
| 200 | |
| 201 | /** |
| 202 | * The scrollpane's viewport child. Default is an empty |
| 203 | * <code>JViewport</code>. |
| 204 | * @see #setViewport |
| 205 | */ |
| 206 | protected JViewport viewport; |
| 207 | |
| 208 | |
| 209 | /** |
| 210 | * The scrollpane's vertical scrollbar child. |
| 211 | * Default is a <code>JScrollBar</code>. |
| 212 | * @see #setVerticalScrollBar |
| 213 | */ |
| 214 | protected JScrollBar verticalScrollBar; |
| 215 | |
| 216 | |
| 217 | /** |
| 218 | * The scrollpane's horizontal scrollbar child. |
| 219 | * Default is a <code>JScrollBar</code>. |
| 220 | * @see #setHorizontalScrollBar |
| 221 | */ |
| 222 | protected JScrollBar horizontalScrollBar; |
| 223 | |
| 224 | |
| 225 | /** |
| 226 | * The row header child. Default is <code>null</code>. |
| 227 | * @see #setRowHeader |
| 228 | */ |
| 229 | protected JViewport rowHeader; |
| 230 | |
| 231 | |
| 232 | /** |
| 233 | * The column header child. Default is <code>null</code>. |
| 234 | * @see #setColumnHeader |
| 235 | */ |
| 236 | protected JViewport columnHeader; |
| 237 | |
| 238 | |
| 239 | /** |
| 240 | * The component to display in the lower left corner. |
| 241 | * Default is <code>null</code>. |
| 242 | * @see #setCorner |
| 243 | */ |
| 244 | protected Component lowerLeft; |
| 245 | |
| 246 | |
| 247 | /** |
| 248 | * The component to display in the lower right corner. |
| 249 | * Default is <code>null</code>. |
| 250 | * @see #setCorner |
| 251 | */ |
| 252 | protected Component lowerRight; |
| 253 | |
| 254 | |
| 255 | /** |
| 256 | * The component to display in the upper left corner. |
| 257 | * Default is <code>null</code>. |
| 258 | * @see #setCorner |
| 259 | */ |
| 260 | protected Component upperLeft; |
| 261 | |
| 262 | |
| 263 | /** |
| 264 | * The component to display in the upper right corner. |
| 265 | * Default is <code>null</code>. |
| 266 | * @see #setCorner |
| 267 | */ |
| 268 | protected Component upperRight; |
| 269 | |
| 270 | /* |
| 271 | * State flag for mouse wheel scrolling |
| 272 | */ |
| 273 | private boolean wheelScrollState = true; |
| 274 | |
| 275 | /** |
| 276 | * Creates a <code>JScrollPane</code> that displays the view |
| 277 | * component in a viewport |
| 278 | * whose view position can be controlled with a pair of scrollbars. |
| 279 | * The scrollbar policies specify when the scrollbars are displayed, |
| 280 | * For example, if <code>vsbPolicy</code> is |
| 281 | * <code>VERTICAL_SCROLLBAR_AS_NEEDED</code> |
| 282 | * then the vertical scrollbar only appears if the view doesn't fit |
| 283 | * vertically. The available policy settings are listed at |
| 284 | * {@link #setVerticalScrollBarPolicy} and |
| 285 | * {@link #setHorizontalScrollBarPolicy}. |
| 286 | * |
| 287 | * @see #setViewportView |
| 288 | * |
| 289 | * @param view the component to display in the scrollpanes viewport |
| 290 | * @param vsbPolicy an integer that specifies the vertical |
| 291 | * scrollbar policy |
| 292 | * @param hsbPolicy an integer that specifies the horizontal |
| 293 | * scrollbar policy |
| 294 | */ |
| 295 | public JScrollPane(Component view, int vsbPolicy, int hsbPolicy) |
| 296 | { |
| 297 | setLayout(new ScrollPaneLayout.UIResource()); |
| 298 | setVerticalScrollBarPolicy(vsbPolicy); |
| 299 | setHorizontalScrollBarPolicy(hsbPolicy); |
| 300 | setViewport(createViewport()); |
| 301 | setVerticalScrollBar(createVerticalScrollBar()); |
| 302 | setHorizontalScrollBar(createHorizontalScrollBar()); |
| 303 | if (view != null) { |
| 304 | setViewportView(view); |
| 305 | } |
| 306 | setOpaque(true); |
| 307 | updateUI(); |
| 308 | |
| 309 | if (!this.getComponentOrientation().isLeftToRight()) { |
| 310 | viewport.setViewPosition(new Point(Integer.MAX_VALUE, 0)); |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | |
| 315 | /** |
| 316 | * Creates a <code>JScrollPane</code> that displays the |
| 317 | * contents of the specified |
| 318 | * component, where both horizontal and vertical scrollbars appear |
| 319 | * whenever the component's contents are larger than the view. |
| 320 | * |
| 321 | * @see #setViewportView |
| 322 | * @param view the component to display in the scrollpane's viewport |
| 323 | */ |
| 324 | public JScrollPane(Component view) { |
| 325 | this(view, VERTICAL_SCROLLBAR_AS_NEEDED, HORIZONTAL_SCROLLBAR_AS_NEEDED); |
| 326 | } |
| 327 | |
| 328 | |
| 329 | /** |
| 330 | * Creates an empty (no viewport view) <code>JScrollPane</code> |
| 331 | * with specified |
| 332 | * scrollbar policies. The available policy settings are listed at |
| 333 | * {@link #setVerticalScrollBarPolicy} and |
| 334 | * {@link #setHorizontalScrollBarPolicy}. |
| 335 | * |
| 336 | * @see #setViewportView |
| 337 | * |
| 338 | * @param vsbPolicy an integer that specifies the vertical |
| 339 | * scrollbar policy |
| 340 | * @param hsbPolicy an integer that specifies the horizontal |
| 341 | * scrollbar policy |
| 342 | */ |
| 343 | public JScrollPane(int vsbPolicy, int hsbPolicy) { |
| 344 | this(null, vsbPolicy, hsbPolicy); |
| 345 | } |
| 346 | |
| 347 | |
| 348 | /** |
| 349 | * Creates an empty (no viewport view) <code>JScrollPane</code> |
| 350 | * where both horizontal and vertical scrollbars appear when needed. |
| 351 | */ |
| 352 | public JScrollPane() { |
| 353 | this(null, VERTICAL_SCROLLBAR_AS_NEEDED, HORIZONTAL_SCROLLBAR_AS_NEEDED); |
| 354 | } |
| 355 | |
| 356 | |
| 357 | /** |
| 358 | * Returns the look and feel (L&F) object that renders this component. |
| 359 | * |
| 360 | * @return the <code>ScrollPaneUI</code> object that renders this |
| 361 | * component |
| 362 | * @see #setUI |
| 363 | * @beaninfo |
| 364 | * bound: true |
| 365 | * hidden: true |
| 366 | * attribute: visualUpdate true |
| 367 | * description: The UI object that implements the Component's LookAndFeel. |
| 368 | */ |
| 369 | public ScrollPaneUI getUI() { |
| 370 | return (ScrollPaneUI)ui; |
| 371 | } |
| 372 | |
| 373 | |
| 374 | /** |
| 375 | * Sets the <code>ScrollPaneUI</code> object that provides the |
| 376 | * look and feel (L&F) for this component. |
| 377 | * |
| 378 | * @param ui the <code>ScrollPaneUI</code> L&F object |
| 379 | * @see #getUI |
| 380 | */ |
| 381 | public void setUI(ScrollPaneUI ui) { |
| 382 | super.setUI(ui); |
| 383 | } |
| 384 | |
| 385 | |
| 386 | /** |
| 387 | * Replaces the current <code>ScrollPaneUI</code> object with a version |
| 388 | * from the current default look and feel. |
| 389 | * To be called when the default look and feel changes. |
| 390 | * |
| 391 | * @see JComponent#updateUI |
| 392 | * @see UIManager#getUI |
| 393 | */ |
| 394 | public void updateUI() { |
| 395 | setUI((ScrollPaneUI)UIManager.getUI(this)); |
| 396 | } |
| 397 | |
| 398 | |
| 399 | /** |
| 400 | * Returns the suffix used to construct the name of the L&F class used to |
| 401 | * render this component. |
| 402 | * |
| 403 | * @return the string "ScrollPaneUI" |
| 404 | * @see JComponent#getUIClassID |
| 405 | * @see UIDefaults#getUI |
| 406 | * |
| 407 | * @beaninfo |
| 408 | * hidden: true |
| 409 | */ |
| 410 | public String getUIClassID() { |
| 411 | return uiClassID; |
| 412 | } |
| 413 | |
| 414 | |
| 415 | |
| 416 | /** |
| 417 | * Sets the layout manager for this <code>JScrollPane</code>. |
| 418 | * This method overrides <code>setLayout</code> in |
| 419 | * <code>java.awt.Container</code> to ensure that only |
| 420 | * <code>LayoutManager</code>s which |
| 421 | * are subclasses of <code>ScrollPaneLayout</code> can be used in a |
| 422 | * <code>JScrollPane</code>. If <code>layout</code> is non-null, this |
| 423 | * will invoke <code>syncWithScrollPane</code> on it. |
| 424 | * |
| 425 | * @param layout the specified layout manager |
| 426 | * @exception ClassCastException if layout is not a |
| 427 | * <code>ScrollPaneLayout</code> |
| 428 | * @see java.awt.Container#getLayout |
| 429 | * @see java.awt.Container#setLayout |
| 430 | * |
| 431 | * @beaninfo |
| 432 | * hidden: true |
| 433 | */ |
| 434 | public void setLayout(LayoutManager layout) { |
| 435 | if (layout instanceof ScrollPaneLayout) { |
| 436 | super.setLayout(layout); |
| 437 | ((ScrollPaneLayout)layout).syncWithScrollPane(this); |
| 438 | } |
| 439 | else if (layout == null) { |
| 440 | super.setLayout(layout); |
| 441 | } |
| 442 | else { |
| 443 | String s = "layout of JScrollPane must be a ScrollPaneLayout"; |
| 444 | throw new ClassCastException(s); |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | /** |
| 449 | * Overridden to return true so that any calls to <code>revalidate</code> |
| 450 | * on any descendants of this <code>JScrollPane</code> will cause the |
| 451 | * entire tree beginning with this <code>JScrollPane</code> to be |
| 452 | * validated. |
| 453 | * |
| 454 | * @return true |
| 455 | * @see java.awt.Container#validate |
| 456 | * @see JComponent#revalidate |
| 457 | * @see JComponent#isValidateRoot |
| 458 | * |
| 459 | * @beaninfo |
| 460 | * hidden: true |
| 461 | */ |
| 462 | public boolean isValidateRoot() { |
| 463 | return true; |
| 464 | } |
| 465 | |
| 466 | |
| 467 | /** |
| 468 | * Returns the vertical scroll bar policy value. |
| 469 | * @return the <code>verticalScrollBarPolicy</code> property |
| 470 | * @see #setVerticalScrollBarPolicy |
| 471 | */ |
| 472 | public int getVerticalScrollBarPolicy() { |
| 473 | return verticalScrollBarPolicy; |
| 474 | } |
| 475 | |
| 476 | |
| 477 | /** |
| 478 | * Determines when the vertical scrollbar appears in the scrollpane. |
| 479 | * Legal values are: |
| 480 | * <ul> |
| 481 | * <li><code>ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED</code> |
| 482 | * <li><code>ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER</code> |
| 483 | * <li><code>ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS</code> |
| 484 | * </ul> |
| 485 | * |
| 486 | * @param policy one of the three values listed above |
| 487 | * @exception IllegalArgumentException if <code>policy</code> |
| 488 | * is not one of the legal values shown above |
| 489 | * @see #getVerticalScrollBarPolicy |
| 490 | * |
| 491 | * @beaninfo |
| 492 | * preferred: true |
| 493 | * bound: true |
| 494 | * description: The scrollpane vertical scrollbar policy |
| 495 | * enum: VERTICAL_SCROLLBAR_AS_NEEDED ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED |
| 496 | * VERTICAL_SCROLLBAR_NEVER ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER |
| 497 | * VERTICAL_SCROLLBAR_ALWAYS ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS |
| 498 | */ |
| 499 | public void setVerticalScrollBarPolicy(int policy) { |
| 500 | switch (policy) { |
| 501 | case VERTICAL_SCROLLBAR_AS_NEEDED: |
| 502 | case VERTICAL_SCROLLBAR_NEVER: |
| 503 | case VERTICAL_SCROLLBAR_ALWAYS: |
| 504 | break; |
| 505 | default: |
| 506 | throw new IllegalArgumentException("invalid verticalScrollBarPolicy"); |
| 507 | } |
| 508 | int old = verticalScrollBarPolicy; |
| 509 | verticalScrollBarPolicy = policy; |
| 510 | firePropertyChange("verticalScrollBarPolicy", old, policy); |
| 511 | revalidate(); |
| 512 | repaint(); |
| 513 | } |
| 514 | |
| 515 | |
| 516 | /** |
| 517 | * Returns the horizontal scroll bar policy value. |
| 518 | * @return the <code>horizontalScrollBarPolicy</code> property |
| 519 | * @see #setHorizontalScrollBarPolicy |
| 520 | */ |
| 521 | public int getHorizontalScrollBarPolicy() { |
| 522 | return horizontalScrollBarPolicy; |
| 523 | } |
| 524 | |
| 525 | |
| 526 | /** |
| 527 | * Determines when the horizontal scrollbar appears in the scrollpane. |
| 528 | * The options are:<ul> |
| 529 | * <li><code>ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED</code> |
| 530 | * <li><code>ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER</code> |
| 531 | * <li><code>ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS</code> |
| 532 | * </ul> |
| 533 | * |
| 534 | * @param policy one of the three values listed above |
| 535 | * @exception IllegalArgumentException if <code>policy</code> |
| 536 | * is not one of the legal values shown above |
| 537 | * @see #getHorizontalScrollBarPolicy |
| 538 | * |
| 539 | * @beaninfo |
| 540 | * preferred: true |
| 541 | * bound: true |
| 542 | * description: The scrollpane scrollbar policy |
| 543 | * enum: HORIZONTAL_SCROLLBAR_AS_NEEDED ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED |
| 544 | * HORIZONTAL_SCROLLBAR_NEVER ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER |
| 545 | * HORIZONTAL_SCROLLBAR_ALWAYS ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS |
| 546 | */ |
| 547 | public void setHorizontalScrollBarPolicy(int policy) { |
| 548 | switch (policy) { |
| 549 | case HORIZONTAL_SCROLLBAR_AS_NEEDED: |
| 550 | case HORIZONTAL_SCROLLBAR_NEVER: |
| 551 | case HORIZONTAL_SCROLLBAR_ALWAYS: |
| 552 | break; |
| 553 | default: |
| 554 | throw new IllegalArgumentException("invalid horizontalScrollBarPolicy"); |
| 555 | } |
| 556 | int old = horizontalScrollBarPolicy; |
| 557 | horizontalScrollBarPolicy = policy; |
| 558 | firePropertyChange("horizontalScrollBarPolicy", old, policy); |
| 559 | revalidate(); |
| 560 | repaint(); |
| 561 | } |
| 562 | |
| 563 | |
| 564 | /** |
| 565 | * Returns the <code>Border</code> object that surrounds the viewport. |
| 566 | * |
| 567 | * @return the <code>viewportBorder</code> property |
| 568 | * @see #setViewportBorder |
| 569 | */ |
| 570 | public Border getViewportBorder() { |
| 571 | return viewportBorder; |
| 572 | } |
| 573 | |
| 574 | |
| 575 | /** |
| 576 | * Adds a border around the viewport. Note that the border isn't |
| 577 | * set on the viewport directly, <code>JViewport</code> doesn't support |
| 578 | * the <code>JComponent</code> border property. |
| 579 | * Similarly setting the <code>JScrollPane</code>s |
| 580 | * viewport doesn't affect the <code>viewportBorder</code> property. |
| 581 | * <p> |
| 582 | * The default value of this property is computed by the look |
| 583 | * and feel implementation. |
| 584 | * |
| 585 | * @param viewportBorder the border to be added |
| 586 | * @see #getViewportBorder |
| 587 | * @see #setViewport |
| 588 | * |
| 589 | * @beaninfo |
| 590 | * preferred: true |
| 591 | * bound: true |
| 592 | * description: The border around the viewport. |
| 593 | */ |
| 594 | public void setViewportBorder(Border viewportBorder) { |
| 595 | Border oldValue = this.viewportBorder; |
| 596 | this.viewportBorder = viewportBorder; |
| 597 | firePropertyChange("viewportBorder", oldValue, viewportBorder); |
| 598 | } |
| 599 | |
| 600 | |
| 601 | /** |
| 602 | * Returns the bounds of the viewport's border. |
| 603 | * |
| 604 | * @return a <code>Rectangle</code> object specifying the viewport border |
| 605 | */ |
| 606 | public Rectangle getViewportBorderBounds() |
| 607 | { |
| 608 | Rectangle borderR = new Rectangle(getSize()); |
| 609 | |
| 610 | Insets insets = getInsets(); |
| 611 | borderR.x = insets.left; |
| 612 | borderR.y = insets.top; |
| 613 | borderR.width -= insets.left + insets.right; |
| 614 | borderR.height -= insets.top + insets.bottom; |
| 615 | |
| 616 | boolean leftToRight = SwingUtilities.isLeftToRight(this); |
| 617 | |
| 618 | /* If there's a visible column header remove the space it |
| 619 | * needs from the top of borderR. |
| 620 | */ |
| 621 | |
| 622 | JViewport colHead = getColumnHeader(); |
| 623 | if ((colHead != null) && (colHead.isVisible())) { |
| 624 | int colHeadHeight = colHead.getHeight(); |
| 625 | borderR.y += colHeadHeight; |
| 626 | borderR.height -= colHeadHeight; |
| 627 | } |
| 628 | |
| 629 | /* If there's a visible row header remove the space it needs |
| 630 | * from the left of borderR. |
| 631 | */ |
| 632 | |
| 633 | JViewport rowHead = getRowHeader(); |
| 634 | if ((rowHead != null) && (rowHead.isVisible())) { |
| 635 | int rowHeadWidth = rowHead.getWidth(); |
| 636 | if ( leftToRight ) { |
| 637 | borderR.x += rowHeadWidth; |
| 638 | } |
| 639 | borderR.width -= rowHeadWidth; |
| 640 | } |
| 641 | |
| 642 | /* If there's a visible vertical scrollbar remove the space it needs |
| 643 | * from the width of borderR. |
| 644 | */ |
| 645 | JScrollBar vsb = getVerticalScrollBar(); |
| 646 | if ((vsb != null) && (vsb.isVisible())) { |
| 647 | int vsbWidth = vsb.getWidth(); |
| 648 | if ( !leftToRight ) { |
| 649 | borderR.x += vsbWidth; |
| 650 | } |
| 651 | borderR.width -= vsbWidth; |
| 652 | } |
| 653 | |
| 654 | /* If there's a visible horizontal scrollbar remove the space it needs |
| 655 | * from the height of borderR. |
| 656 | */ |
| 657 | JScrollBar hsb = getHorizontalScrollBar(); |
| 658 | if ((hsb != null) && (hsb.isVisible())) { |
| 659 | borderR.height -= hsb.getHeight(); |
| 660 | } |
| 661 | |
| 662 | return borderR; |
| 663 | } |
| 664 | |
| 665 | |
| 666 | /** |
| 667 | * By default <code>JScrollPane</code> creates scrollbars |
| 668 | * that are instances |
| 669 | * of this class. <code>Scrollbar</code> overrides the |
| 670 | * <code>getUnitIncrement</code> and <code>getBlockIncrement</code> |
| 671 | * methods so that, if the viewport's view is a <code>Scrollable</code>, |
| 672 | * the view is asked to compute these values. Unless |
| 673 | * the unit/block increment have been explicitly set. |
| 674 | * <p> |
| 675 | * <strong>Warning:</strong> |
| 676 | * Serialized objects of this class will not be compatible with |
| 677 | * future Swing releases. The current serialization support is |
| 678 | * appropriate for short term storage or RMI between applications running |
| 679 | * the same version of Swing. As of 1.4, support for long term storage |
| 680 | * of all JavaBeans<sup><font size="-2">TM</font></sup> |
| 681 | * has been added to the <code>java.beans</code> package. |
| 682 | * Please see {@link java.beans.XMLEncoder}. |
| 683 | * |
| 684 | * @see Scrollable |
| 685 | * @see JScrollPane#createVerticalScrollBar |
| 686 | * @see JScrollPane#createHorizontalScrollBar |
| 687 | */ |
| 688 | protected class ScrollBar extends JScrollBar implements UIResource |
| 689 | { |
| 690 | /** |
| 691 | * Set to true when the unit increment has been explicitly set. |
| 692 | * If this is false the viewport's view is obtained and if it |
| 693 | * is an instance of <code>Scrollable</code> the unit increment |
| 694 | * from it is used. |
| 695 | */ |
| 696 | private boolean unitIncrementSet; |
| 697 | /** |
| 698 | * Set to true when the block increment has been explicitly set. |
| 699 | * If this is false the viewport's view is obtained and if it |
| 700 | * is an instance of <code>Scrollable</code> the block increment |
| 701 | * from it is used. |
| 702 | */ |
| 703 | private boolean blockIncrementSet; |
| 704 | |
| 705 | /** |
| 706 | * Creates a scrollbar with the specified orientation. |
| 707 | * The options are: |
| 708 | * <ul> |
| 709 | * <li><code>ScrollPaneConstants.VERTICAL</code> |
| 710 | * <li><code>ScrollPaneConstants.HORIZONTAL</code> |
| 711 | * </ul> |
| 712 | * |
| 713 | * @param orientation an integer specifying one of the legal |
| 714 | * orientation values shown above |
| 715 | * @since 1.4 |
| 716 | */ |
| 717 | public ScrollBar(int orientation) { |
| 718 | super(orientation); |
| 719 | this.putClientProperty("JScrollBar.fastWheelScrolling", |
| 720 | Boolean.TRUE); |
| 721 | } |
| 722 | |
| 723 | /** |
| 724 | * Messages super to set the value, and resets the |
| 725 | * <code>unitIncrementSet</code> instance variable to true. |
| 726 | * |
| 727 | * @param unitIncrement the new unit increment value, in pixels |
| 728 | */ |
| 729 | public void setUnitIncrement(int unitIncrement) { |
| 730 | unitIncrementSet = true; |
| 731 | this.putClientProperty("JScrollBar.fastWheelScrolling", null); |
| 732 | super.setUnitIncrement(unitIncrement); |
| 733 | } |
| 734 | |
| 735 | /** |
| 736 | * Computes the unit increment for scrolling if the viewport's |
| 737 | * view is a <code>Scrollable</code> object. |
| 738 | * Otherwise return <code>super.getUnitIncrement</code>. |
| 739 | * |
| 740 | * @param direction less than zero to scroll up/left, |
| 741 | * greater than zero for down/right |
| 742 | * @return an integer, in pixels, containing the unit increment |
| 743 | * @see Scrollable#getScrollableUnitIncrement |
| 744 | */ |
| 745 | public int getUnitIncrement(int direction) { |
| 746 | JViewport vp = getViewport(); |
| 747 | if (!unitIncrementSet && (vp != null) && |
| 748 | (vp.getView() instanceof Scrollable)) { |
| 749 | Scrollable view = (Scrollable)(vp.getView()); |
| 750 | Rectangle vr = vp.getViewRect(); |
| 751 | return view.getScrollableUnitIncrement(vr, getOrientation(), direction); |
| 752 | } |
| 753 | else { |
| 754 | return super.getUnitIncrement(direction); |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | /** |
| 759 | * Messages super to set the value, and resets the |
| 760 | * <code>blockIncrementSet</code> instance variable to true. |
| 761 | * |
| 762 | * @param blockIncrement the new block increment value, in pixels |
| 763 | */ |
| 764 | public void setBlockIncrement(int blockIncrement) { |
| 765 | blockIncrementSet = true; |
| 766 | this.putClientProperty("JScrollBar.fastWheelScrolling", null); |
| 767 | super.setBlockIncrement(blockIncrement); |
| 768 | } |
| 769 | |
| 770 | /** |
| 771 | * Computes the block increment for scrolling if the viewport's |
| 772 | * view is a <code>Scrollable</code> object. Otherwise |
| 773 | * the <code>blockIncrement</code> equals the viewport's width |
| 774 | * or height. If there's no viewport return |
| 775 | * <code>super.getBlockIncrement</code>. |
| 776 | * |
| 777 | * @param direction less than zero to scroll up/left, |
| 778 | * greater than zero for down/right |
| 779 | * @return an integer, in pixels, containing the block increment |
| 780 | * @see Scrollable#getScrollableBlockIncrement |
| 781 | */ |
| 782 | public int getBlockIncrement(int direction) { |
| 783 | JViewport vp = getViewport(); |
| 784 | if (blockIncrementSet || vp == null) { |
| 785 | return super.getBlockIncrement(direction); |
| 786 | } |
| 787 | else if (vp.getView() instanceof Scrollable) { |
| 788 | Scrollable view = (Scrollable)(vp.getView()); |
| 789 | Rectangle vr = vp.getViewRect(); |
| 790 | return view.getScrollableBlockIncrement(vr, getOrientation(), direction); |
| 791 | } |
| 792 | else if (getOrientation() == VERTICAL) { |
| 793 | return vp.getExtentSize().height; |
| 794 | } |
| 795 | else { |
| 796 | return vp.getExtentSize().width; |
| 797 | } |
| 798 | } |
| 799 | |
| 800 | } |
| 801 | |
| 802 | |
| 803 | /** |
| 804 | * Returns a <code>JScrollPane.ScrollBar</code> by default. |
| 805 | * Subclasses may override this method to force <code>ScrollPaneUI</code> |
| 806 | * implementations to use a <code>JScrollBar</code> subclass. |
| 807 | * Used by <code>ScrollPaneUI</code> implementations to |
| 808 | * create the horizontal scrollbar. |
| 809 | * |
| 810 | * @return a <code>JScrollBar</code> with a horizontal orientation |
| 811 | * @see JScrollBar |
| 812 | */ |
| 813 | public JScrollBar createHorizontalScrollBar() { |
| 814 | return new ScrollBar(JScrollBar.HORIZONTAL); |
| 815 | } |
| 816 | |
| 817 | |
| 818 | /** |
| 819 | * Returns the horizontal scroll bar that controls the viewport's |
| 820 | * horizontal view position. |
| 821 | * |
| 822 | * @return the <code>horizontalScrollBar</code> property |
| 823 | * @see #setHorizontalScrollBar |
| 824 | */ |
| 825 | public JScrollBar getHorizontalScrollBar() { |
| 826 | return horizontalScrollBar; |
| 827 | } |
| 828 | |
| 829 | |
| 830 | /** |
| 831 | * Adds the scrollbar that controls the viewport's horizontal view |
| 832 | * position to the scrollpane. |
| 833 | * This is usually unnecessary, as <code>JScrollPane</code> creates |
| 834 | * horizontal and vertical scrollbars by default. |
| 835 | * |
| 836 | * @param horizontalScrollBar the horizontal scrollbar to be added |
| 837 | * @see #createHorizontalScrollBar |
| 838 | * @see #getHorizontalScrollBar |
| 839 | * |
| 840 | * @beaninfo |
| 841 | * expert: true |
| 842 | * bound: true |
| 843 | * description: The horizontal scrollbar. |
| 844 | */ |
| 845 | public void setHorizontalScrollBar(JScrollBar horizontalScrollBar) { |
| 846 | JScrollBar old = getHorizontalScrollBar(); |
| 847 | this.horizontalScrollBar = horizontalScrollBar; |
| 848 | if (horizontalScrollBar != null) { |
| 849 | add(horizontalScrollBar, HORIZONTAL_SCROLLBAR); |
| 850 | } |
| 851 | else if (old != null) { |
| 852 | remove(old); |
| 853 | } |
| 854 | firePropertyChange("horizontalScrollBar", old, horizontalScrollBar); |
| 855 | |
| 856 | revalidate(); |
| 857 | repaint(); |
| 858 | } |
| 859 | |
| 860 | |
| 861 | /** |
| 862 | * Returns a <code>JScrollPane.ScrollBar</code> by default. Subclasses |
| 863 | * may override this method to force <code>ScrollPaneUI</code> |
| 864 | * implementations to use a <code>JScrollBar</code> subclass. |
| 865 | * Used by <code>ScrollPaneUI</code> implementations to create the |
| 866 | * vertical scrollbar. |
| 867 | * |
| 868 | * @return a <code>JScrollBar</code> with a vertical orientation |
| 869 | * @see JScrollBar |
| 870 | */ |
| 871 | public JScrollBar createVerticalScrollBar() { |
| 872 | return new ScrollBar(JScrollBar.VERTICAL); |
| 873 | } |
| 874 | |
| 875 | |
| 876 | /** |
| 877 | * Returns the vertical scroll bar that controls the viewports |
| 878 | * vertical view position. |
| 879 | * |
| 880 | * @return the <code>verticalScrollBar</code> property |
| 881 | * @see #setVerticalScrollBar |
| 882 | */ |
| 883 | public JScrollBar getVerticalScrollBar() { |
| 884 | return verticalScrollBar; |
| 885 | } |
| 886 | |
| 887 | |
| 888 | /** |
| 889 | * Adds the scrollbar that controls the viewports vertical view position |
| 890 | * to the scrollpane. This is usually unnecessary, |
| 891 | * as <code>JScrollPane</code> creates vertical and |
| 892 | * horizontal scrollbars by default. |
| 893 | * |
| 894 | * @param verticalScrollBar the new vertical scrollbar to be added |
| 895 | * @see #createVerticalScrollBar |
| 896 | * @see #getVerticalScrollBar |
| 897 | * |
| 898 | * @beaninfo |
| 899 | * expert: true |
| 900 | * bound: true |
| 901 | * description: The vertical scrollbar. |
| 902 | */ |
| 903 | public void setVerticalScrollBar(JScrollBar verticalScrollBar) { |
| 904 | JScrollBar old = getVerticalScrollBar(); |
| 905 | this.verticalScrollBar = verticalScrollBar; |
| 906 | add(verticalScrollBar, VERTICAL_SCROLLBAR); |
| 907 | firePropertyChange("verticalScrollBar", old, verticalScrollBar); |
| 908 | |
| 909 | revalidate(); |
| 910 | repaint(); |
| 911 | } |
| 912 | |
| 913 | |
| 914 | /** |
| 915 | * Returns a new <code>JViewport</code> by default. |
| 916 | * Used to create the |
| 917 | * viewport (as needed) in <code>setViewportView</code>, |
| 918 | * <code>setRowHeaderView</code>, and <code>setColumnHeaderView</code>. |
| 919 | * Subclasses may override this method to return a subclass of |
| 920 | * <code>JViewport</code>. |
| 921 | * |
| 922 | * @return a new <code>JViewport</code> |
| 923 | */ |
| 924 | protected JViewport createViewport() { |
| 925 | return new JViewport(); |
| 926 | } |
| 927 | |
| 928 | |
| 929 | /** |
| 930 | * Returns the current <code>JViewport</code>. |
| 931 | * |
| 932 | * @see #setViewport |
| 933 | * @return the <code>viewport</code> property |
| 934 | */ |
| 935 | public JViewport getViewport() { |
| 936 | return viewport; |
| 937 | } |
| 938 | |
| 939 | |
| 940 | /** |
| 941 | * Removes the old viewport (if there is one); forces the |
| 942 | * viewPosition of the new viewport to be in the +x,+y quadrant; |
| 943 | * syncs up the row and column headers (if there are any) with the |
| 944 | * new viewport; and finally syncs the scrollbars and |
| 945 | * headers with the new viewport. |
| 946 | * <p> |
| 947 | * Most applications will find it more convenient to use |
| 948 | * <code>setViewportView</code> |
| 949 | * to add a viewport and a view to the scrollpane. |
| 950 | * |
| 951 | * @param viewport the new viewport to be used; if viewport is |
| 952 | * <code>null</code>, the old viewport is still removed |
| 953 | * and the new viewport is set to <code>null</code> |
| 954 | * @see #createViewport |
| 955 | * @see #getViewport |
| 956 | * @see #setViewportView |
| 957 | * |
| 958 | * @beaninfo |
| 959 | * expert: true |
| 960 | * bound: true |
| 961 | * attribute: visualUpdate true |
| 962 | * description: The viewport child for this scrollpane |
| 963 | * |
| 964 | */ |
| 965 | public void setViewport(JViewport viewport) { |
| 966 | JViewport old = getViewport(); |
| 967 | this.viewport = viewport; |
| 968 | if (viewport != null) { |
| 969 | add(viewport, VIEWPORT); |
| 970 | } |
| 971 | else if (old != null) { |
| 972 | remove(old); |
| 973 | } |
| 974 | firePropertyChange("viewport", old, viewport); |
| 975 | |
| 976 | if (accessibleContext != null) { |
| 977 | ((AccessibleJScrollPane)accessibleContext).resetViewPort(); |
| 978 | } |
| 979 | |
| 980 | revalidate(); |
| 981 | repaint(); |
| 982 | } |
| 983 | |
| 984 | |
| 985 | /** |
| 986 | * Creates a viewport if necessary and then sets its view. Applications |
| 987 | * that don't provide the view directly to the <code>JScrollPane</code> |
| 988 | * constructor |
| 989 | * should use this method to specify the scrollable child that's going |
| 990 | * to be displayed in the scrollpane. For example: |
| 991 | * <pre> |
| 992 | * JScrollPane scrollpane = new JScrollPane(); |
| 993 | * scrollpane.setViewportView(myBigComponentToScroll); |
| 994 | * </pre> |
| 995 | * Applications should not add children directly to the scrollpane. |
| 996 | * |
| 997 | * @param view the component to add to the viewport |
| 998 | * @see #setViewport |
| 999 | * @see JViewport#setView |
| 1000 | */ |
| 1001 | public void setViewportView(Component view) { |
| 1002 | if (getViewport() == null) { |
| 1003 | setViewport(createViewport()); |
| 1004 | } |
| 1005 | getViewport().setView(view); |
| 1006 | } |
| 1007 | |
| 1008 | |
| 1009 | |
| 1010 | /** |
| 1011 | * Returns the row header. |
| 1012 | * @return the <code>rowHeader</code> property |
| 1013 | * @see #setRowHeader |
| 1014 | */ |
| 1015 | public JViewport getRowHeader() { |
| 1016 | return rowHeader; |
| 1017 | } |
| 1018 | |
| 1019 | |
| 1020 | /** |
| 1021 | * Removes the old rowHeader, if it exists; if the new rowHeader |
| 1022 | * isn't <code>null</code>, syncs the y coordinate of its |
| 1023 | * viewPosition with |
| 1024 | * the viewport (if there is one) and then adds it to the scroll pane. |
| 1025 | * <p> |
| 1026 | * Most applications will find it more convenient to use |
| 1027 | * <code>setRowHeaderView</code> |
| 1028 | * to add a row header component and its viewport to the scroll pane. |
| 1029 | * |
| 1030 | * @param rowHeader the new row header to be used; if <code>null</code> |
| 1031 | * the old row header is still removed and the new rowHeader |
| 1032 | * is set to <code>null</code> |
| 1033 | * @see #getRowHeader |
| 1034 | * @see #setRowHeaderView |
| 1035 | * |
| 1036 | * @beaninfo |
| 1037 | * bound: true |
| 1038 | * expert: true |
| 1039 | * description: The row header child for this scrollpane |
| 1040 | */ |
| 1041 | public void setRowHeader(JViewport rowHeader) { |
| 1042 | JViewport old = getRowHeader(); |
| 1043 | this.rowHeader = rowHeader; |
| 1044 | if (rowHeader != null) { |
| 1045 | add(rowHeader, ROW_HEADER); |
| 1046 | } |
| 1047 | else if (old != null) { |
| 1048 | remove(old); |
| 1049 | } |
| 1050 | firePropertyChange("rowHeader", old, rowHeader); |
| 1051 | revalidate(); |
| 1052 | repaint(); |
| 1053 | } |
| 1054 | |
| 1055 | |
| 1056 | /** |
| 1057 | * Creates a row-header viewport if necessary, sets |
| 1058 | * its view and then adds the row-header viewport |
| 1059 | * to the scrollpane. For example: |
| 1060 | * <pre> |
| 1061 | * JScrollPane scrollpane = new JScrollPane(); |
| 1062 | * scrollpane.setViewportView(myBigComponentToScroll); |
| 1063 | * scrollpane.setRowHeaderView(myBigComponentsRowHeader); |
| 1064 | * </pre> |
| 1065 | * |
| 1066 | * @see #setRowHeader |
| 1067 | * @see JViewport#setView |
| 1068 | * @param view the component to display as the row header |
| 1069 | */ |
| 1070 | public void setRowHeaderView(Component view) { |
| 1071 | if (getRowHeader() == null) { |
| 1072 | setRowHeader(createViewport()); |
| 1073 | } |
| 1074 | getRowHeader().setView(view); |
| 1075 | } |
| 1076 | |
| 1077 | |
| 1078 | |
| 1079 | /** |
| 1080 | * Returns the column header. |
| 1081 | * @return the <code>columnHeader</code> property |
| 1082 | * @see #setColumnHeader |
| 1083 | */ |
| 1084 | public JViewport getColumnHeader() { |
| 1085 | return columnHeader; |
| 1086 | } |
| 1087 | |
| 1088 | |
| 1089 | /** |
| 1090 | * Removes the old columnHeader, if it exists; if the new columnHeader |
| 1091 | * isn't <code>null</code>, syncs the x coordinate of its viewPosition |
| 1092 | * with the viewport (if there is one) and then adds it to the scroll pane. |
| 1093 | * <p> |
| 1094 | * Most applications will find it more convenient to use |
| 1095 | * <code>setColumnHeaderView</code> |
| 1096 | * to add a column header component and its viewport to the scroll pane. |
| 1097 | * |
| 1098 | * @see #getColumnHeader |
| 1099 | * @see #setColumnHeaderView |
| 1100 | * |
| 1101 | * @beaninfo |
| 1102 | * bound: true |
| 1103 | * description: The column header child for this scrollpane |
| 1104 | * attribute: visualUpdate true |
| 1105 | */ |
| 1106 | public void setColumnHeader(JViewport columnHeader) { |
| 1107 | JViewport old = getColumnHeader(); |
| 1108 | this.columnHeader = columnHeader; |
| 1109 | if (columnHeader != null) { |
| 1110 | add(columnHeader, COLUMN_HEADER); |
| 1111 | } |
| 1112 | else if (old != null) { |
| 1113 | remove(old); |
| 1114 | } |
| 1115 | firePropertyChange("columnHeader", old, columnHeader); |
| 1116 | |
| 1117 | revalidate(); |
| 1118 | repaint(); |
| 1119 | } |
| 1120 | |
| 1121 | |
| 1122 | |
| 1123 | /** |
| 1124 | * Creates a column-header viewport if necessary, sets |
| 1125 | * its view, and then adds the column-header viewport |
| 1126 | * to the scrollpane. For example: |
| 1127 | * <pre> |
| 1128 | * JScrollPane scrollpane = new JScrollPane(); |
| 1129 | * scrollpane.setViewportView(myBigComponentToScroll); |
| 1130 | * scrollpane.setColumnHeaderView(myBigComponentsColumnHeader); |
| 1131 | * </pre> |
| 1132 | * |
| 1133 | * @see #setColumnHeader |
| 1134 | * @see JViewport#setView |
| 1135 | * |
| 1136 | * @param view the component to display as the column header |
| 1137 | */ |
| 1138 | public void setColumnHeaderView(Component view) { |
| 1139 | if (getColumnHeader() == null) { |
| 1140 | setColumnHeader(createViewport()); |
| 1141 | } |
| 1142 | getColumnHeader().setView(view); |
| 1143 | } |
| 1144 | |
| 1145 | |
| 1146 | /** |
| 1147 | * Returns the component at the specified corner. The |
| 1148 | * <code>key</code> value specifying the corner is one of: |
| 1149 | * <ul> |
| 1150 | * <li>ScrollPaneConstants.LOWER_LEFT_CORNER |
| 1151 | * <li>ScrollPaneConstants.LOWER_RIGHT_CORNER |
| 1152 | * <li>ScrollPaneConstants.UPPER_LEFT_CORNER |
| 1153 | * <li>ScrollPaneConstants.UPPER_RIGHT_CORNER |
| 1154 | * <li>ScrollPaneConstants.LOWER_LEADING_CORNER |
| 1155 | * <li>ScrollPaneConstants.LOWER_TRAILING_CORNER |
| 1156 | * <li>ScrollPaneConstants.UPPER_LEADING_CORNER |
| 1157 | * <li>ScrollPaneConstants.UPPER_TRAILING_CORNER |
| 1158 | * </ul> |
| 1159 | * |
| 1160 | * @param key one of the values as shown above |
| 1161 | * @return the corner component (which may be <code>null</code>) |
| 1162 | * identified by the given key, or <code>null</code> |
| 1163 | * if the key is invalid |
| 1164 | * @see #setCorner |
| 1165 | */ |
| 1166 | public Component getCorner(String key) { |
| 1167 | boolean isLeftToRight = getComponentOrientation().isLeftToRight(); |
| 1168 | if (key.equals(LOWER_LEADING_CORNER)) { |
| 1169 | key = isLeftToRight ? LOWER_LEFT_CORNER : LOWER_RIGHT_CORNER; |
| 1170 | } else if (key.equals(LOWER_TRAILING_CORNER)) { |
| 1171 | key = isLeftToRight ? LOWER_RIGHT_CORNER : LOWER_LEFT_CORNER; |
| 1172 | } else if (key.equals(UPPER_LEADING_CORNER)) { |
| 1173 | key = isLeftToRight ? UPPER_LEFT_CORNER : UPPER_RIGHT_CORNER; |
| 1174 | } else if (key.equals(UPPER_TRAILING_CORNER)) { |
| 1175 | key = isLeftToRight ? UPPER_RIGHT_CORNER : UPPER_LEFT_CORNER; |
| 1176 | } |
| 1177 | if (key.equals(LOWER_LEFT_CORNER)) { |
| 1178 | return lowerLeft; |
| 1179 | } |
| 1180 | else if (key.equals(LOWER_RIGHT_CORNER)) { |
| 1181 | return lowerRight; |
| 1182 | } |
| 1183 | else if (key.equals(UPPER_LEFT_CORNER)) { |
| 1184 | return upperLeft; |
| 1185 | } |
| 1186 | else if (key.equals(UPPER_RIGHT_CORNER)) { |
| 1187 | return upperRight; |
| 1188 | } |
| 1189 | else { |
| 1190 | return null; |
| 1191 | } |
| 1192 | } |
| 1193 | |
| 1194 | |
| 1195 | /** |
| 1196 | * Adds a child that will appear in one of the scroll panes |
| 1197 | * corners, if there's room. For example with both scrollbars |
| 1198 | * showing (on the right and bottom edges of the scrollpane) |
| 1199 | * the lower left corner component will be shown in the space |
| 1200 | * between ends of the two scrollbars. Legal values for |
| 1201 | * the <b>key</b> are: |
| 1202 | * <ul> |
| 1203 | * <li>ScrollPaneConstants.LOWER_LEFT_CORNER |
| 1204 | * <li>ScrollPaneConstants.LOWER_RIGHT_CORNER |
| 1205 | * <li>ScrollPaneConstants.UPPER_LEFT_CORNER |
| 1206 | * <li>ScrollPaneConstants.UPPER_RIGHT_CORNER |
| 1207 | * <li>ScrollPaneConstants.LOWER_LEADING_CORNER |
| 1208 | * <li>ScrollPaneConstants.LOWER_TRAILING_CORNER |
| 1209 | * <li>ScrollPaneConstants.UPPER_LEADING_CORNER |
| 1210 | * <li>ScrollPaneConstants.UPPER_TRAILING_CORNER |
| 1211 | * </ul> |
| 1212 | * <p> |
| 1213 | * Although "corner" doesn't match any beans property |
| 1214 | * signature, <code>PropertyChange</code> events are generated with the |
| 1215 | * property name set to the corner key. |
| 1216 | * |
| 1217 | * @param key identifies which corner the component will appear in |
| 1218 | * @param corner one of the following components: |
| 1219 | * <ul> |
| 1220 | * <li>lowerLeft |
| 1221 | * <li>lowerRight |
| 1222 | * <li>upperLeft |
| 1223 | * <li>upperRight |
| 1224 | * </ul> |
| 1225 | * @exception IllegalArgumentException if corner key is invalid |
| 1226 | */ |
| 1227 | public void setCorner(String key, Component corner) |
| 1228 | { |
| 1229 | Component old; |
| 1230 | boolean isLeftToRight = getComponentOrientation().isLeftToRight(); |
| 1231 | if (key.equals(LOWER_LEADING_CORNER)) { |
| 1232 | key = isLeftToRight ? LOWER_LEFT_CORNER : LOWER_RIGHT_CORNER; |
| 1233 | } else if (key.equals(LOWER_TRAILING_CORNER)) { |
| 1234 | key = isLeftToRight ? LOWER_RIGHT_CORNER : LOWER_LEFT_CORNER; |
| 1235 | } else if (key.equals(UPPER_LEADING_CORNER)) { |
| 1236 | key = isLeftToRight ? UPPER_LEFT_CORNER : UPPER_RIGHT_CORNER; |
| 1237 | } else if (key.equals(UPPER_TRAILING_CORNER)) { |
| 1238 | key = isLeftToRight ? UPPER_RIGHT_CORNER : UPPER_LEFT_CORNER; |
| 1239 | } |
| 1240 | if (key.equals(LOWER_LEFT_CORNER)) { |
| 1241 | old = lowerLeft; |
| 1242 | lowerLeft = corner; |
| 1243 | } |
| 1244 | else if (key.equals(LOWER_RIGHT_CORNER)) { |
| 1245 | old = lowerRight; |
| 1246 | lowerRight = corner; |
| 1247 | } |
| 1248 | else if (key.equals(UPPER_LEFT_CORNER)) { |
| 1249 | old = upperLeft; |
| 1250 | upperLeft = corner; |
| 1251 | } |
| 1252 | else if (key.equals(UPPER_RIGHT_CORNER)) { |
| 1253 | old = upperRight; |
| 1254 | upperRight = corner; |
| 1255 | } |
| 1256 | else { |
| 1257 | throw new IllegalArgumentException("invalid corner key"); |
| 1258 | } |
| 1259 | if (old != null) { |
| 1260 | remove(old); |
| 1261 | } |
| 1262 | if (corner != null) { |
| 1263 | add(corner, key); |
| 1264 | } |
| 1265 | firePropertyChange(key, old, corner); |
| 1266 | revalidate(); |
| 1267 | repaint(); |
| 1268 | } |
| 1269 | |
| 1270 | /** |
| 1271 | * Sets the orientation for the vertical and horizontal |
| 1272 | * scrollbars as determined by the |
| 1273 | * <code>ComponentOrientation</code> argument. |
| 1274 | * |
| 1275 | * @param co one of the following values: |
| 1276 | * <ul> |
| 1277 | * <li>java.awt.ComponentOrientation.LEFT_TO_RIGHT |
| 1278 | * <li>java.awt.ComponentOrientation.RIGHT_TO_LEFT |
| 1279 | * <li>java.awt.ComponentOrientation.UNKNOWN |
| 1280 | * </ul> |
| 1281 | * @see java.awt.ComponentOrientation |
| 1282 | */ |
| 1283 | public void setComponentOrientation( ComponentOrientation co ) { |
| 1284 | super.setComponentOrientation( co ); |
| 1285 | if( verticalScrollBar != null ) |
| 1286 | verticalScrollBar.setComponentOrientation( co ); |
| 1287 | if( horizontalScrollBar != null ) |
| 1288 | horizontalScrollBar.setComponentOrientation( co ); |
| 1289 | } |
| 1290 | |
| 1291 | /** |
| 1292 | * Indicates whether or not scrolling will take place in response to the |
| 1293 | * mouse wheel. Wheel scrolling is enabled by default. |
| 1294 | * |
| 1295 | * @see #setWheelScrollingEnabled |
| 1296 | * @since 1.4 |
| 1297 | * @beaninfo |
| 1298 | * bound: true |
| 1299 | * description: Flag for enabling/disabling mouse wheel scrolling |
| 1300 | */ |
| 1301 | public boolean isWheelScrollingEnabled() {return wheelScrollState;} |
| 1302 | |
| 1303 | /** |
| 1304 | * Enables/disables scrolling in response to movement of the mouse wheel. |
| 1305 | * Wheel scrolling is enabled by default. |
| 1306 | * |
| 1307 | * @param handleWheel <code>true</code> if scrolling should be done |
| 1308 | * automatically for a MouseWheelEvent, |
| 1309 | * <code>false</code> otherwise. |
| 1310 | * @see #isWheelScrollingEnabled |
| 1311 | * @see java.awt.event.MouseWheelEvent |
| 1312 | * @see java.awt.event.MouseWheelListener |
| 1313 | * @since 1.4 |
| 1314 | * @beaninfo |
| 1315 | * bound: true |
| 1316 | * description: Flag for enabling/disabling mouse wheel scrolling |
| 1317 | */ |
| 1318 | public void setWheelScrollingEnabled(boolean handleWheel) { |
| 1319 | boolean old = wheelScrollState; |
| 1320 | wheelScrollState = handleWheel; |
| 1321 | firePropertyChange("wheelScrollingEnabled", old, handleWheel); |
| 1322 | } |
| 1323 | |
| 1324 | /** |
| 1325 | * See <code>readObject</code> and <code>writeObject</code> in |
| 1326 | * <code>JComponent</code> for more |
| 1327 | * information about serialization in Swing. |
| 1328 | */ |
| 1329 | private void writeObject(ObjectOutputStream s) throws IOException { |
| 1330 | s.defaultWriteObject(); |
| 1331 | if (getUIClassID().equals(uiClassID)) { |
| 1332 | byte count = JComponent.getWriteObjCounter(this); |
| 1333 | JComponent.setWriteObjCounter(this, --count); |
| 1334 | if (count == 0 && ui != null) { |
| 1335 | ui.installUI(this); |
| 1336 | } |
| 1337 | } |
| 1338 | } |
| 1339 | |
| 1340 | |
| 1341 | /** |
| 1342 | * Returns a string representation of this <code>JScrollPane</code>. |
| 1343 | * This method |
| 1344 | * is intended to be used only for debugging purposes, and the |
| 1345 | * content and format of the returned string may vary between |
| 1346 | * implementations. The returned string may be empty but may not |
| 1347 | * be <code>null</code>. |
| 1348 | * |
| 1349 | * @return a string representation of this <code>JScrollPane</code>. |
| 1350 | */ |
| 1351 | protected String paramString() { |
| 1352 | String viewportBorderString = (viewportBorder != null ? |
| 1353 | viewportBorder.toString() : ""); |
| 1354 | String viewportString = (viewport != null ? |
| 1355 | viewport.toString() : ""); |
| 1356 | String verticalScrollBarPolicyString; |
| 1357 | if (verticalScrollBarPolicy == VERTICAL_SCROLLBAR_AS_NEEDED) { |
| 1358 | verticalScrollBarPolicyString = "VERTICAL_SCROLLBAR_AS_NEEDED"; |
| 1359 | } else if (verticalScrollBarPolicy == VERTICAL_SCROLLBAR_NEVER) { |
| 1360 | verticalScrollBarPolicyString = "VERTICAL_SCROLLBAR_NEVER"; |
| 1361 | } else if (verticalScrollBarPolicy == VERTICAL_SCROLLBAR_ALWAYS) { |
| 1362 | verticalScrollBarPolicyString = "VERTICAL_SCROLLBAR_ALWAYS"; |
| 1363 | } else verticalScrollBarPolicyString = ""; |
| 1364 | String horizontalScrollBarPolicyString; |
| 1365 | if (horizontalScrollBarPolicy == HORIZONTAL_SCROLLBAR_AS_NEEDED) { |
| 1366 | horizontalScrollBarPolicyString = "HORIZONTAL_SCROLLBAR_AS_NEEDED"; |
| 1367 | } else if (horizontalScrollBarPolicy == HORIZONTAL_SCROLLBAR_NEVER) { |
| 1368 | horizontalScrollBarPolicyString = "HORIZONTAL_SCROLLBAR_NEVER"; |
| 1369 | } else if (horizontalScrollBarPolicy == HORIZONTAL_SCROLLBAR_ALWAYS) { |
| 1370 | horizontalScrollBarPolicyString = "HORIZONTAL_SCROLLBAR_ALWAYS"; |
| 1371 | } else horizontalScrollBarPolicyString = ""; |
| 1372 | String horizontalScrollBarString = (horizontalScrollBar != null ? |
| 1373 | horizontalScrollBar.toString() |
| 1374 | : ""); |
| 1375 | String verticalScrollBarString = (verticalScrollBar != null ? |
| 1376 | verticalScrollBar.toString() : ""); |
| 1377 | String columnHeaderString = (columnHeader != null ? |
| 1378 | columnHeader.toString() : ""); |
| 1379 | String rowHeaderString = (rowHeader != null ? |
| 1380 | rowHeader.toString() : ""); |
| 1381 | String lowerLeftString = (lowerLeft != null ? |
| 1382 | lowerLeft.toString() : ""); |
| 1383 | String lowerRightString = (lowerRight != null ? |
| 1384 | lowerRight.toString() : ""); |
| 1385 | String upperLeftString = (upperLeft != null ? |
| 1386 | upperLeft.toString() : ""); |
| 1387 | String upperRightString = (upperRight != null ? |
| 1388 | upperRight.toString() : ""); |
| 1389 | |
| 1390 | return super.paramString() + |
| 1391 | ",columnHeader=" + columnHeaderString + |
| 1392 | ",horizontalScrollBar=" + horizontalScrollBarString + |
| 1393 | ",horizontalScrollBarPolicy=" + horizontalScrollBarPolicyString + |
| 1394 | ",lowerLeft=" + lowerLeftString + |
| 1395 | ",lowerRight=" + lowerRightString + |
| 1396 | ",rowHeader=" + rowHeaderString + |
| 1397 | ",upperLeft=" + upperLeftString + |
| 1398 | ",upperRight=" + upperRightString + |
| 1399 | ",verticalScrollBar=" + verticalScrollBarString + |
| 1400 | ",verticalScrollBarPolicy=" + verticalScrollBarPolicyString + |
| 1401 | ",viewport=" + viewportString + |
| 1402 | ",viewportBorder=" + viewportBorderString; |
| 1403 | } |
| 1404 | |
| 1405 | ///////////////// |
| 1406 | // Accessibility support |
| 1407 | //////////////// |
| 1408 | |
| 1409 | /** |
| 1410 | * Gets the AccessibleContext associated with this JScrollPane. |
| 1411 | * For scroll panes, the AccessibleContext takes the form of an |
| 1412 | * AccessibleJScrollPane. |
| 1413 | * A new AccessibleJScrollPane instance is created if necessary. |
| 1414 | * |
| 1415 | * @return an AccessibleJScrollPane that serves as the |
| 1416 | * AccessibleContext of this JScrollPane |
| 1417 | */ |
| 1418 | public AccessibleContext getAccessibleContext() { |
| 1419 | if (accessibleContext == null) { |
| 1420 | accessibleContext = new AccessibleJScrollPane(); |
| 1421 | } |
| 1422 | return accessibleContext; |
| 1423 | } |
| 1424 | |
| 1425 | /** |
| 1426 | * This class implements accessibility support for the |
| 1427 | * <code>JScrollPane</code> class. It provides an implementation of the |
| 1428 | * Java Accessibility API appropriate to scroll pane user-interface |
| 1429 | * elements. |
| 1430 | * <p> |
| 1431 | * <strong>Warning:</strong> |
| 1432 | * Serialized objects of this class will not be compatible with |
| 1433 | * future Swing releases. The current serialization support is |
| 1434 | * appropriate for short term storage or RMI between applications running |
| 1435 | * the same version of Swing. As of 1.4, support for long term storage |
| 1436 | * of all JavaBeans<sup><font size="-2">TM</font></sup> |
| 1437 | * has been added to the <code>java.beans</code> package. |
| 1438 | * Please see {@link java.beans.XMLEncoder}. |
| 1439 | */ |
| 1440 | protected class AccessibleJScrollPane extends AccessibleJComponent |
| 1441 | implements ChangeListener, PropertyChangeListener { |
| 1442 | |
| 1443 | protected JViewport viewPort = null; |
| 1444 | |
| 1445 | /* |
| 1446 | * Resets the viewport ChangeListener and PropertyChangeListener |
| 1447 | */ |
| 1448 | public void resetViewPort() { |
| 1449 | if (viewPort != null) { |
| 1450 | viewPort.removeChangeListener(this); |
| 1451 | viewPort.removePropertyChangeListener(this); |
| 1452 | } |
| 1453 | viewPort = JScrollPane.this.getViewport(); |
| 1454 | if (viewPort != null) { |
| 1455 | viewPort.addChangeListener(this); |
| 1456 | viewPort.addPropertyChangeListener(this); |
| 1457 | } |
| 1458 | } |
| 1459 | |
| 1460 | /** |
| 1461 | * AccessibleJScrollPane constructor |
| 1462 | */ |
| 1463 | public AccessibleJScrollPane() { |
| 1464 | super(); |
| 1465 | |
| 1466 | resetViewPort(); |
| 1467 | |
| 1468 | // initialize the AccessibleRelationSets for the JScrollPane |
| 1469 | // and JScrollBar(s) |
| 1470 | JScrollBar scrollBar = getHorizontalScrollBar(); |
| 1471 | if (scrollBar != null) { |
| 1472 | setScrollBarRelations(scrollBar); |
| 1473 | } |
| 1474 | scrollBar = getVerticalScrollBar(); |
| 1475 | if (scrollBar != null) { |
| 1476 | setScrollBarRelations(scrollBar); |
| 1477 | } |
| 1478 | } |
| 1479 | |
| 1480 | /** |
| 1481 | * Get the role of this object. |
| 1482 | * |
| 1483 | * @return an instance of AccessibleRole describing the role of the |
| 1484 | * object |
| 1485 | * @see AccessibleRole |
| 1486 | */ |
| 1487 | public AccessibleRole getAccessibleRole() { |
| 1488 | return AccessibleRole.SCROLL_PANE; |
| 1489 | } |
| 1490 | |
| 1491 | /** |
| 1492 | * Invoked when the target of the listener has changed its state. |
| 1493 | * |
| 1494 | * @param e a <code>ChangeEvent</code> object. Must not be null. |
| 1495 | * |
| 1496 | * @throws NullPointerException if the parameter is null. |
| 1497 | */ |
| 1498 | public void stateChanged(ChangeEvent e) { |
| 1499 | if (e == null) { |
| 1500 | throw new NullPointerException(); |
| 1501 | } |
| 1502 | firePropertyChange(ACCESSIBLE_VISIBLE_DATA_PROPERTY, |
| 1503 | Boolean.valueOf(false), |
| 1504 | Boolean.valueOf(true)); |
| 1505 | } |
| 1506 | |
| 1507 | /** |
| 1508 | * This method gets called when a bound property is changed. |
| 1509 | * @param e A <code>PropertyChangeEvent</code> object describing |
| 1510 | * the event source and the property that has changed. Must not be null. |
| 1511 | * |
| 1512 | * @throws NullPointerException if the parameter is null. |
| 1513 | * @since 1.5 |
| 1514 | */ |
| 1515 | public void propertyChange(PropertyChangeEvent e) { |
| 1516 | String propertyName = e.getPropertyName(); |
| 1517 | if (propertyName == "horizontalScrollBar" || |
| 1518 | propertyName == "verticalScrollBar") { |
| 1519 | |
| 1520 | if (e.getNewValue() instanceof JScrollBar) { |
| 1521 | setScrollBarRelations((JScrollBar)e.getNewValue()); |
| 1522 | } |
| 1523 | } |
| 1524 | } |
| 1525 | |
| 1526 | |
| 1527 | /* |
| 1528 | * Sets the CONTROLLER_FOR and CONTROLLED_BY AccessibleRelations for |
| 1529 | * the JScrollPane and JScrollBar. JScrollBar must not be null. |
| 1530 | */ |
| 1531 | void setScrollBarRelations(JScrollBar scrollBar) { |
| 1532 | /* |
| 1533 | * The JScrollBar is a CONTROLLER_FOR the JScrollPane. |
| 1534 | * The JScrollPane is CONTROLLED_BY the JScrollBar. |
| 1535 | */ |
| 1536 | AccessibleRelation controlledBy = |
| 1537 | new AccessibleRelation(AccessibleRelation.CONTROLLED_BY, |
| 1538 | scrollBar); |
| 1539 | AccessibleRelation controllerFor = |
| 1540 | new AccessibleRelation(AccessibleRelation.CONTROLLER_FOR, |
| 1541 | JScrollPane.this); |
| 1542 | |
| 1543 | // set the relation set for the scroll bar |
| 1544 | AccessibleContext ac = scrollBar.getAccessibleContext(); |
| 1545 | ac.getAccessibleRelationSet().add(controllerFor); |
| 1546 | |
| 1547 | // set the relation set for the scroll pane |
| 1548 | getAccessibleRelationSet().add(controlledBy); |
| 1549 | } |
| 1550 | } |
| 1551 | } |