KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > JScrollPane


1 /*
2  * @(#)JScrollPane.java 1.100 04/04/15
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

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 JavaDoc;
16 import java.awt.ComponentOrientation JavaDoc;
17 import java.awt.Graphics JavaDoc;
18 import java.awt.Rectangle JavaDoc;
19 import java.awt.Insets JavaDoc;
20 import java.awt.Color JavaDoc;
21 import java.awt.LayoutManager JavaDoc;
22 import java.awt.Point JavaDoc;
23
24 import java.io.ObjectOutputStream JavaDoc;
25 import java.io.ObjectInputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27
28 import java.beans.*;
29
30
31 /**
32  * Provides a scrollable view of a lightweight component.
33  * A <code>JScrollPane</code> manages a viewport, optional
34  * vertical and horizontal scroll bars, and optional row and
35  * column heading viewports.
36  * You can find task-oriented documentation of <code>JScrollPane</code> in
37  * <a HREF="http://java.sun.com/docs/books/tutorial/uiswing/components/scrollpane.html">How to Use Scroll Panes</a>,
38  * a section in <em>The Java Tutorial</em>. Note that
39  * <code>JScrollPane</code> does not support heavyweight components.
40  * <p>
41  * <TABLE ALIGN="RIGHT" BORDER="0" SUMMARY="layout">
42  * <TR>
43  * <TD ALIGN="CENTER">
44  * <P ALIGN="CENTER"><IMG SRC="doc-files/JScrollPane-1.gif"
45  * alt="The following text describes this image."
46  * WIDTH="256" HEIGHT="248" ALIGN="BOTTOM" BORDER="0">
47  * </TD>
48  * </TR>
49  * </TABLE>
50  * The <code>JViewport</code> provides a window,
51  * or &quot;viewport&quot; onto a data
52  * source -- for example, a text file. That data source is the
53  * &quot;scrollable client&quot; (aka data model) displayed by the
54  * <code>JViewport</code> view.
55  * A <code>JScrollPane</code> basically consists of <code>JScrollBar</code>s,
56  * a <code>JViewport</code>, and the wiring between them,
57  * as shown in the diagram at right.
58  * <p>
59  * In addition to the scroll bars and viewport,
60  * a <code>JScrollPane</code> can have a
61  * column header and a row header. Each of these is a
62  * <code>JViewport</code> object that
63  * you specify with <code>setRowHeaderView</code>,
64  * and <code>setColumnHeaderView</code>.
65  * The column header viewport automatically scrolls left and right, tracking
66  * the left-right scrolling of the main viewport.
67  * (It never scrolls vertically, however.)
68  * The row header acts in a similar fashion.
69  * <p>
70  * By default, the corners are empty.
71  * You can put a component into a corner using
72  * <code>setCorner</code>,
73  * in case you there is some function or decoration you
74  * would like to add to the scroll pane. The size of corner components is
75  * entirely determined by the size of the headers and scroll bars that
76  * surround them.
77  * <p>
78  * To add a border around the main viewport,
79  * you can use <code>setViewportBorder</code>.
80  * (Of course, you can also add a border around the whole scroll pane using
81  * <code>setBorder</code>.)
82  * <p>
83  * A common operation to want to do is to set the background color that will
84  * be used if the main viewport view is smaller than the viewport, or is
85  * not opaque. This can be accomplished by setting the background color
86  * of the viewport, via <code>scrollPane.getViewport().setBackground()</code>.
87  * The reason for setting the color of the viewport and not the scrollpane
88  * is that by default <code>JViewport</code> is opaque
89  * which, among other things, means it will completely fill
90  * in its background using its background color. Therefore when
91  * <code>JScrollPane</code> draws its background the viewport will
92  * usually draw over it.
93  * <p>
94  * By default <code>JScrollPane</code> uses <code>ScrollPaneLayout</code>
95  * to handle the layout of its child Components. <code>ScrollPaneLayout</code>
96  * determines the size to make the viewport view in one of two ways:
97  * <ol>
98  * <li>If the view implements <code>Scrollable</code>
99  * a combination of <code>getPreferredScrollableViewportSize</code>,
100  * <code>getScrollableTracksViewportWidth</code> and
101  * <code>getScrollableTracksViewportHeight</code>is used, otherwise
102  * <li><code>getPreferredSize</code> is used.
103  * </ol>
104  * <p>
105  * <strong>Warning:</strong>
106  * Serialized objects of this class will not be compatible with
107  * future Swing releases. The current serialization support is
108  * appropriate for short term storage or RMI between applications running
109  * the same version of Swing. As of 1.4, support for long term storage
110  * of all JavaBeans<sup><font size="-2">TM</font></sup>
111  * has been added to the <code>java.beans</code> package.
112  * Please see {@link java.beans.XMLEncoder}.
113  *
114  * @see JScrollBar
115  * @see JViewport
116  * @see ScrollPaneLayout
117  * @see Scrollable
118  * @see Component#getPreferredSize
119  * @see #setViewportView
120  * @see #setRowHeaderView
121  * @see #setColumnHeaderView
122  * @see #setCorner
123  * @see #setViewportBorder
124  *
125  * @beaninfo
126  * attribute: isContainer true
127  * attribute: containerDelegate getViewport
128  * description: A specialized container that manages a viewport, optional scrollbars and headers
129  *
130  * @version 1.100 @(#)JScrollPane.java 1.100
131  * @author Hans Muller
132  */

133 public class JScrollPane extends JComponent JavaDoc implements ScrollPaneConstants JavaDoc, Accessible
134 {
135     private Border viewportBorder;
136
137     /**
138      * @see #getUIClassID
139      * @see #readObject
140      */

141     private static final String JavaDoc uiClassID = "ScrollPaneUI";
142
143     /**
144      * The display policy for the vertical scrollbar.
145      * The default is
146      * <code>ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED</code>.
147      * @see #setVerticalScrollBarPolicy
148      */

149     protected int verticalScrollBarPolicy = VERTICAL_SCROLLBAR_AS_NEEDED;
150
151
152     /**
153      * The display policy for the horizontal scrollbar.
154      * The default is
155      * <code>ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED</code>.
156      * @see #setHorizontalScrollBarPolicy
157      */

158     protected int horizontalScrollBarPolicy = HORIZONTAL_SCROLLBAR_AS_NEEDED;
159
160
161     /**
162      * The scrollpane's viewport child. Default is an empty
163      * <code>JViewport</code>.
164      * @see #setViewport
165      */

166     protected JViewport JavaDoc viewport;
167
168
169     /**
170      * The scrollpane's vertical scrollbar child.
171      * Default is a <code>JScrollBar</code>.
172      * @see #setVerticalScrollBar
173      */

174     protected JScrollBar JavaDoc verticalScrollBar;
175
176
177     /**
178      * The scrollpane's horizontal scrollbar child.
179      * Default is a <code>JScrollBar</code>.
180      * @see #setHorizontalScrollBar
181      */

182     protected JScrollBar JavaDoc horizontalScrollBar;
183
184
185     /**
186      * The row header child. Default is <code>null</code>.
187      * @see #setRowHeader
188      */

189     protected JViewport JavaDoc rowHeader;
190
191
192     /**
193      * The column header child. Default is <code>null</code>.
194      * @see #setColumnHeader
195      */

196     protected JViewport JavaDoc columnHeader;
197
198
199     /**
200      * The component to display in the lower left corner.
201      * Default is <code>null</code>.
202      * @see #setCorner
203      */

204     protected Component JavaDoc lowerLeft;
205
206
207     /**
208      * The component to display in the lower right corner.
209      * Default is <code>null</code>.
210      * @see #setCorner
211      */

212     protected Component JavaDoc lowerRight;
213
214
215     /**
216      * The component to display in the upper left corner.
217      * Default is <code>null</code>.
218      * @see #setCorner
219      */

220     protected Component JavaDoc upperLeft;
221
222
223     /**
224      * The component to display in the upper right corner.
225      * Default is <code>null</code>.
226      * @see #setCorner
227      */

228     protected Component JavaDoc upperRight;
229
230     /*
231      * State flag for mouse wheel scrolling
232      */

233     private boolean wheelScrollState = true;
234
235     /**
236      * Creates a <code>JScrollPane</code> that displays the view
237      * component in a viewport
238      * whose view position can be controlled with a pair of scrollbars.
239      * The scrollbar policies specify when the scrollbars are displayed,
240      * For example, if <code>vsbPolicy</code> is
241      * <code>VERTICAL_SCROLLBAR_AS_NEEDED</code>
242      * then the vertical scrollbar only appears if the view doesn't fit
243      * vertically. The available policy settings are listed at
244      * {@link #setVerticalScrollBarPolicy} and
245      * {@link #setHorizontalScrollBarPolicy}.
246      *
247      * @see #setViewportView
248      *
249      * @param view the component to display in the scrollpanes viewport
250      * @param vsbPolicy an integer that specifies the vertical
251      * scrollbar policy
252      * @param hsbPolicy an integer that specifies the horizontal
253      * scrollbar policy
254      */

255     public JScrollPane(Component JavaDoc view, int vsbPolicy, int hsbPolicy)
256     {
257     setLayout(new ScrollPaneLayout.UIResource JavaDoc());
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 JavaDoc(Integer.MAX_VALUE, 0));
271     }
272     }
273
274
275     /**
276      * Creates a <code>JScrollPane</code> that displays the
277      * contents of the specified
278      * component, where both horizontal and vertical scrollbars appear
279      * whenever the component's contents are larger than the view.
280      *
281      * @see #setViewportView
282      * @param view the component to display in the scrollpane's viewport
283      */

284     public JScrollPane(Component JavaDoc view) {
285         this(view, VERTICAL_SCROLLBAR_AS_NEEDED, HORIZONTAL_SCROLLBAR_AS_NEEDED);
286     }
287
288
289     /**
290      * Creates an empty (no viewport view) <code>JScrollPane</code>
291      * with specified
292      * scrollbar policies. The available policy settings are listed at
293      * {@link #setVerticalScrollBarPolicy} and
294      * {@link #setHorizontalScrollBarPolicy}.
295      *
296      * @see #setViewportView
297      *
298      * @param vsbPolicy an integer that specifies the vertical
299      * scrollbar policy
300      * @param hsbPolicy an integer that specifies the horizontal
301      * scrollbar policy
302      */

303     public JScrollPane(int vsbPolicy, int hsbPolicy) {
304         this(null, vsbPolicy, hsbPolicy);
305     }
306
307
308     /**
309      * Creates an empty (no viewport view) <code>JScrollPane</code>
310      * where both horizontal and vertical scrollbars appear when needed.
311      */

312     public JScrollPane() {
313         this(null, VERTICAL_SCROLLBAR_AS_NEEDED, HORIZONTAL_SCROLLBAR_AS_NEEDED);
314     }
315
316
317     /**
318      * Returns the look and feel (L&F) object that renders this component.
319      *
320      * @return the <code>ScrollPaneUI</code> object that renders this
321      * component
322      * @see #setUI
323      * @beaninfo
324      * bound: true
325      * hidden: true
326      * attribute: visualUpdate true
327      * description: The UI object that implements the Component's LookAndFeel.
328      */

329     public ScrollPaneUI getUI() {
330         return (ScrollPaneUI)ui;
331     }
332
333
334     /**
335      * Sets the <code>ScrollPaneUI</code> object that provides the
336      * look and feel (L&F) for this component.
337      *
338      * @param ui the <code>ScrollPaneUI</code> L&F object
339      * @see #getUI
340      */

341     public void setUI(ScrollPaneUI ui) {
342         super.setUI(ui);
343     }
344
345
346     /**
347      * Replaces the current <code>ScrollPaneUI</code> object with a version
348      * from the current default look and feel.
349      * To be called when the default look and feel changes.
350      *
351      * @see JComponent#updateUI
352      * @see UIManager#getUI
353      */

354     public void updateUI() {
355         setUI((ScrollPaneUI)UIManager.getUI(this));
356     }
357
358
359     /**
360      * Returns the suffix used to construct the name of the L&F class used to
361      * render this component.
362      *
363      * @return the string "ScrollPaneUI"
364      * @see JComponent#getUIClassID
365      * @see UIDefaults#getUI
366      *
367      * @beaninfo
368      * hidden: true
369      */

370     public String JavaDoc getUIClassID() {
371         return uiClassID;
372     }
373
374
375
376     /**
377      * Sets the layout manager for this <code>JScrollPane</code>.
378      * This method overrides <code>setLayout</code> in
379      * <code>java.awt.Container</code> to ensure that only
380      * <code>LayoutManager</code>s which
381      * are subclasses of <code>ScrollPaneLayout</code> can be used in a
382      * <code>JScrollPane</code>. If <code>layout</code> is non-null, this
383      * will invoke <code>syncWithScrollPane</code> on it.
384      *
385      * @param layout the specified layout manager
386      * @exception ClassCastException if layout is not a
387      * <code>ScrollPaneLayout</code>
388      * @see java.awt.Container#getLayout
389      * @see java.awt.Container#setLayout
390      *
391      * @beaninfo
392      * hidden: true
393      */

394     public void setLayout(LayoutManager JavaDoc layout) {
395         if (layout instanceof ScrollPaneLayout JavaDoc) {
396             super.setLayout(layout);
397             ((ScrollPaneLayout JavaDoc)layout).syncWithScrollPane(this);
398         }
399         else if (layout == null) {
400             super.setLayout(layout);
401         }
402     else {
403         String JavaDoc s = "layout of JScrollPane must be a ScrollPaneLayout";
404         throw new ClassCastException JavaDoc(s);
405     }
406     }
407
408     /**
409      * Overridden to return true so that any calls to <code>revalidate</code>
410      * on any descendants of this <code>JScrollPane</code> will cause the
411      * entire tree beginning with this <code>JScrollPane</code> to be
412      * validated.
413      *
414      * @return true
415      * @see java.awt.Container#validate
416      * @see JComponent#revalidate
417      * @see JComponent#isValidateRoot
418      *
419      * @beaninfo
420      * hidden: true
421      */

422     public boolean isValidateRoot() {
423         return true;
424     }
425
426
427     /**
428      * Returns the vertical scroll bar policy value.
429      * @return the <code>verticalScrollBarPolicy</code> property
430      * @see #setVerticalScrollBarPolicy
431      */

432     public int getVerticalScrollBarPolicy() {
433         return verticalScrollBarPolicy;
434     }
435
436
437     /**
438      * Determines when the vertical scrollbar appears in the scrollpane.
439      * Legal values are:
440      * <ul>
441      * <li><code>ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED</code>
442      * <li><code>ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER</code>
443      * <li><code>ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS</code>
444      * </ul>
445      *
446      * @param policy one of the three values listed above
447      * @exception IllegalArgumentException if <code>policy</code>
448      * is not one of the legal values shown above
449      * @see #getVerticalScrollBarPolicy
450      *
451      * @beaninfo
452      * preferred: true
453      * bound: true
454      * description: The scrollpane vertical scrollbar policy
455      * enum: VERTICAL_SCROLLBAR_AS_NEEDED ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED
456      * VERTICAL_SCROLLBAR_NEVER ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER
457      * VERTICAL_SCROLLBAR_ALWAYS ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS
458      */

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 JavaDoc("invalid verticalScrollBarPolicy");
467     }
468     int old = verticalScrollBarPolicy;
469     verticalScrollBarPolicy = policy;
470     firePropertyChange("verticalScrollBarPolicy", old, policy);
471     revalidate();
472     repaint();
473     }
474
475
476     /**
477      * Returns the horizontal scroll bar policy value.
478      * @return the <code>horizontalScrollBarPolicy</code> property
479      * @see #setHorizontalScrollBarPolicy
480      */

481     public int getHorizontalScrollBarPolicy() {
482     return horizontalScrollBarPolicy;
483     }
484
485
486     /**
487      * Determines when the horizontal scrollbar appears in the scrollpane.
488      * The options are:<ul>
489      * <li><code>ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED</code>
490      * <li><code>ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER</code>
491      * <li><code>ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS</code>
492      * </ul>
493      *
494      * @param policy one of the three values listed above
495      * @exception IllegalArgumentException if <code>policy</code>
496      * is not one of the legal values shown above
497      * @see #getHorizontalScrollBarPolicy
498      *
499      * @beaninfo
500      * preferred: true
501      * bound: true
502      * description: The scrollpane scrollbar policy
503      * enum: HORIZONTAL_SCROLLBAR_AS_NEEDED ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED
504      * HORIZONTAL_SCROLLBAR_NEVER ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER
505      * HORIZONTAL_SCROLLBAR_ALWAYS ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS
506      */

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 JavaDoc("invalid horizontalScrollBarPolicy");
515     }
516     int old = horizontalScrollBarPolicy;
517     horizontalScrollBarPolicy = policy;
518     firePropertyChange("horizontalScrollBarPolicy", old, policy);
519     revalidate();
520     repaint();
521     }
522
523
524     /**
525      * Returns the <code>Border</code> object that surrounds the viewport.
526      *
527      * @return the <code>viewportBorder</code> property
528      * @see #setViewportBorder
529      */

530     public Border getViewportBorder() {
531         return viewportBorder;
532     }
533
534
535     /**
536      * Adds a border around the viewport. Note that the border isn't
537      * set on the viewport directly, <code>JViewport</code> doesn't support
538      * the <code>JComponent</code> border property.
539      * Similarly setting the <code>JScrollPane</code>s
540      * viewport doesn't affect the <code>viewportBorder</code> property.
541      * <p>
542      * The default value of this property is computed by the look
543      * and feel implementation.
544      *
545      * @param viewportBorder the border to be added
546      * @see #getViewportBorder
547      * @see #setViewport
548      *
549      * @beaninfo
550      * preferred: true
551      * bound: true
552      * description: The border around the viewport.
553      */

554     public void setViewportBorder(Border viewportBorder) {
555         Border oldValue = this.viewportBorder;
556         this.viewportBorder = viewportBorder;
557         firePropertyChange("viewportBorder", oldValue, viewportBorder);
558     }
559
560
561     /**
562      * Returns the bounds of the viewport's border.
563      *
564      * @return a <code>Rectangle</code> object specifying the viewport border
565      */

566     public Rectangle JavaDoc getViewportBorderBounds()
567     {
568     Rectangle JavaDoc borderR = new Rectangle JavaDoc(getSize());
569
570     Insets JavaDoc 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     /* If there's a visible column header remove the space it
579      * needs from the top of borderR.
580      */

581
582     JViewport JavaDoc colHead = getColumnHeader();
583     if ((colHead != null) && (colHead.isVisible())) {
584         int colHeadHeight = colHead.getHeight();
585         borderR.y += colHeadHeight;
586         borderR.height -= colHeadHeight;
587     }
588
589     /* If there's a visible row header remove the space it needs
590      * from the left of borderR.
591      */

592
593     JViewport JavaDoc 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     /* If there's a visible vertical scrollbar remove the space it needs
603      * from the width of borderR.
604      */

605     JScrollBar JavaDoc 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     /* If there's a visible horizontal scrollbar remove the space it needs
615      * from the height of borderR.
616      */

617     JScrollBar JavaDoc hsb = getHorizontalScrollBar();
618     if ((hsb != null) && (hsb.isVisible())) {
619         borderR.height -= hsb.getHeight();
620     }
621
622     return borderR;
623     }
624
625
626     /**
627      * By default <code>JScrollPane</code> creates scrollbars
628      * that are instances
629      * of this class. <code>Scrollbar</code> overrides the
630      * <code>getUnitIncrement</code> and <code>getBlockIncrement</code>
631      * methods so that, if the viewport's view is a <code>Scrollable</code>,
632      * the view is asked to compute these values. Unless
633      * the unit/block increment have been explicitly set.
634      * <p>
635      * <strong>Warning:</strong>
636      * Serialized objects of this class will not be compatible with
637      * future Swing releases. The current serialization support is
638      * appropriate for short term storage or RMI between applications running
639      * the same version of Swing. As of 1.4, support for long term storage
640      * of all JavaBeans<sup><font size="-2">TM</font></sup>
641      * has been added to the <code>java.beans</code> package.
642      * Please see {@link java.beans.XMLEncoder}.
643      *
644      * @see Scrollable
645      * @see JScrollPane#createVerticalScrollBar
646      * @see JScrollPane#createHorizontalScrollBar
647      */

648     protected class ScrollBar extends JScrollBar JavaDoc implements UIResource
649     {
650     /**
651          * Set to true when the unit increment has been explicitly set.
652      * If this is false the viewport's view is obtained and if it
653      * is an instance of <code>Scrollable</code> the unit increment
654          * from it is used.
655      */

656     private boolean unitIncrementSet;
657     /**
658          * Set to true when the block increment has been explicitly set.
659      * If this is false the viewport's view is obtained and if it
660      * is an instance of <code>Scrollable</code> the block increment
661          * from it is used.
662      */

663     private boolean blockIncrementSet;
664
665         /**
666          * Creates a scrollbar with the specified orientation.
667          * The options are:
668          * <ul>
669          * <li><code>ScrollPaneConstants.VERTICAL</code>
670          * <li><code>ScrollPaneConstants.HORIZONTAL</code>
671          * </ul>
672          *
673          * @param orientation an integer specifying one of the legal
674      * orientation values shown above
675          */

676         public ScrollBar(int orientation) {
677             super(orientation);
678         }
679
680     /**
681      * Messages super to set the value, and resets the
682      * <code>unitIncrementSet</code> instance variable to true.
683      *
684      * @param unitIncrement the new unit increment value, in pixels
685      */

686     public void setUnitIncrement(int unitIncrement) {
687         unitIncrementSet = true;
688         super.setUnitIncrement(unitIncrement);
689     }
690
691         /**
692          * Computes the unit increment for scrolling if the viewport's
693          * view is a <code>Scrollable</code> object.
694          * Otherwise return <code>super.getUnitIncrement</code>.
695          *
696          * @param direction less than zero to scroll up/left,
697          * greater than zero for down/right
698          * @return an integer, in pixels, containing the unit increment
699          * @see Scrollable#getScrollableUnitIncrement
700          */

701         public int getUnitIncrement(int direction) {
702             JViewport JavaDoc vp = getViewport();
703             if (!unitIncrementSet && (vp != null) &&
704         (vp.getView() instanceof Scrollable JavaDoc)) {
705                 Scrollable JavaDoc view = (Scrollable JavaDoc)(vp.getView());
706                 Rectangle JavaDoc vr = vp.getViewRect();
707                 return view.getScrollableUnitIncrement(vr, getOrientation(), direction);
708             }
709             else {
710                 return super.getUnitIncrement(direction);
711             }
712         }
713
714     /**
715      * Messages super to set the value, and resets the
716      * <code>blockIncrementSet</code> instance variable to true.
717      *
718      * @param blockIncrement the new block increment value, in pixels
719      */

720     public void setBlockIncrement(int blockIncrement) {
721         blockIncrementSet = true;
722         super.setBlockIncrement(blockIncrement);
723     }
724
725         /**
726      * Computes the block increment for scrolling if the viewport's
727      * view is a <code>Scrollable</code> object. Otherwise
728          * the <code>blockIncrement</code> equals the viewport's width
729          * or height. If there's no viewport return
730          * <code>super.getBlockIncrement</code>.
731          *
732          * @param direction less than zero to scroll up/left,
733          * greater than zero for down/right
734      * @return an integer, in pixels, containing the block increment
735          * @see Scrollable#getScrollableBlockIncrement
736          */

737         public int getBlockIncrement(int direction) {
738             JViewport JavaDoc vp = getViewport();
739             if (blockIncrementSet || vp == null) {
740                 return super.getBlockIncrement(direction);
741             }
742             else if (vp.getView() instanceof Scrollable JavaDoc) {
743                 Scrollable JavaDoc view = (Scrollable JavaDoc)(vp.getView());
744                 Rectangle JavaDoc 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     /**
759      * Returns a <code>JScrollPane.ScrollBar</code> by default.
760      * Subclasses may override this method to force <code>ScrollPaneUI</code>
761      * implementations to use a <code>JScrollBar</code> subclass.
762      * Used by <code>ScrollPaneUI</code> implementations to
763      * create the horizontal scrollbar.
764      *
765      * @return a <code>JScrollBar</code> with a horizontal orientation
766      * @see JScrollBar
767      */

768     public JScrollBar JavaDoc createHorizontalScrollBar() {
769         return new ScrollBar(JScrollBar.HORIZONTAL);
770     }
771
772
773     /**
774      * Returns the horizontal scroll bar that controls the viewport's
775      * horizontal view position.
776      *
777      * @return the <code>horizontalScrollBar</code> property
778      * @see #setHorizontalScrollBar
779      */

780     public JScrollBar JavaDoc getHorizontalScrollBar() {
781         return horizontalScrollBar;
782     }
783
784     
785     /**
786      * Adds the scrollbar that controls the viewport's horizontal view
787      * position to the scrollpane.
788      * This is usually unnecessary, as <code>JScrollPane</code> creates
789      * horizontal and vertical scrollbars by default.
790      *
791      * @param horizontalScrollBar the horizontal scrollbar to be added
792      * @see #createHorizontalScrollBar
793      * @see #getHorizontalScrollBar
794      *
795      * @beaninfo
796      * expert: true
797      * bound: true
798      * description: The horizontal scrollbar.
799      */

800     public void setHorizontalScrollBar(JScrollBar JavaDoc horizontalScrollBar) {
801     JScrollBar JavaDoc 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     /**
817      * Returns a <code>JScrollPane.ScrollBar</code> by default. Subclasses
818      * may override this method to force <code>ScrollPaneUI</code>
819      * implementations to use a <code>JScrollBar</code> subclass.
820      * Used by <code>ScrollPaneUI</code> implementations to create the
821      * vertical scrollbar.
822      *
823      * @return a <code>JScrollBar</code> with a vertical orientation
824      * @see JScrollBar
825      */

826     public JScrollBar JavaDoc createVerticalScrollBar() {
827         return new ScrollBar(JScrollBar.VERTICAL);
828     }
829
830
831     /**
832      * Returns the vertical scroll bar that controls the viewports
833      * vertical view position.
834      *
835      * @return the <code>verticalScrollBar</code> property
836      * @see #setVerticalScrollBar
837      */

838     public JScrollBar JavaDoc getVerticalScrollBar() {
839     return verticalScrollBar;
840     }
841
842
843     /**
844      * Adds the scrollbar that controls the viewports vertical view position
845      * to the scrollpane. This is usually unnecessary,
846      * as <code>JScrollPane</code> creates vertical and
847      * horizontal scrollbars by default.
848      *
849      * @param verticalScrollBar the new vertical scrollbar to be added
850      * @see #createVerticalScrollBar
851      * @see #getVerticalScrollBar
852      *
853      * @beaninfo
854      * expert: true
855      * bound: true
856      * description: The vertical scrollbar.
857      */

858     public void setVerticalScrollBar(JScrollBar JavaDoc verticalScrollBar) {
859     JScrollBar JavaDoc 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     /**
870      * Returns a new <code>JViewport</code> by default.
871      * Used to create the
872      * viewport (as needed) in <code>setViewportView</code>,
873      * <code>setRowHeaderView</code>, and <code>setColumnHeaderView</code>.
874      * Subclasses may override this method to return a subclass of
875      * <code>JViewport</code>.
876      *
877      * @return a new <code>JViewport</code>
878      */

879     protected JViewport JavaDoc createViewport() {
880         return new JViewport JavaDoc();
881     }
882
883
884     /**
885      * Returns the current <code>JViewport</code>.
886      *
887      * @see #setViewport
888      * @return the <code>viewport</code> property
889      */

890     public JViewport JavaDoc getViewport() {
891         return viewport;
892     }
893
894     
895     /**
896      * Removes the old viewport (if there is one); forces the
897      * viewPosition of the new viewport to be in the +x,+y quadrant;
898      * syncs up the row and column headers (if there are any) with the
899      * new viewport; and finally syncs the scrollbars and
900      * headers with the new viewport.
901      * <p>
902      * Most applications will find it more convenient to use
903      * <code>setViewportView</code>
904      * to add a viewport and a view to the scrollpane.
905      *
906      * @param viewport the new viewport to be used; if viewport is
907      * <code>null</code>, the old viewport is still removed
908      * and the new viewport is set to <code>null</code>
909      * @see #createViewport
910      * @see #getViewport
911      * @see #setViewportView
912      *
913      * @beaninfo
914      * expert: true
915      * bound: true
916      * attribute: visualUpdate true
917      * description: The viewport child for this scrollpane
918      *
919      */

920     public void setViewport(JViewport JavaDoc viewport) {
921     JViewport JavaDoc 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     /**
941      * Creates a viewport if necessary and then sets its view. Applications
942      * that don't provide the view directly to the <code>JScrollPane</code>
943      * constructor
944      * should use this method to specify the scrollable child that's going
945      * to be displayed in the scrollpane. For example:
946      * <pre>
947      * JScrollPane scrollpane = new JScrollPane();
948      * scrollpane.setViewportView(myBigComponentToScroll);
949      * </pre>
950      * Applications should not add children directly to the scrollpane.
951      *
952      * @param view the component to add to the viewport
953      * @see #setViewport
954      * @see JViewport#setView
955      */

956     public void setViewportView(Component JavaDoc view) {
957         if (getViewport() == null) {
958             setViewport(createViewport());
959         }
960         getViewport().setView(view);
961     }
962
963
964
965     /**
966      * Returns the row header.
967      * @return the <code>rowHeader</code> property
968      * @see #setRowHeader
969      */

970     public JViewport JavaDoc getRowHeader() {
971         return rowHeader;
972     }
973
974
975     /**
976      * Removes the old rowHeader, if it exists. If the new rowHeader
977      * isn't <code>null</code>, syncs the y coordinate of its
978      * viewPosition with
979      * the viewport (if there is one) and then adds it to the scrollpane.
980      * <p>
981      * Most applications will find it more convenient to use
982      * <code>setRowHeaderView</code>
983      * to add a row header component and its viewport to the scrollpane.
984      *
985      * @param rowHeader the new row header to be used; if <code>null</code>
986      * the old row header is still removed and the new rowHeader
987      * is set to <code>null</code>
988      * @see #getRowHeader
989      * @see #setRowHeaderView
990      *
991      * @beaninfo
992      * bound: true
993      * expert: true
994      * description: The row header child for this scrollpane
995      */

996     public void setRowHeader(JViewport JavaDoc rowHeader) {
997     JViewport JavaDoc 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    /**
1012     * Creates a row-header viewport if necessary, sets
1013     * its view and then adds the row-header viewport
1014     * to the scrollpane. For example:
1015     * <pre>
1016     * JScrollPane scrollpane = new JScrollPane();
1017     * scrollpane.setViewportView(myBigComponentToScroll);
1018     * scrollpane.setRowHeaderView(myBigComponentsRowHeader);
1019     * </pre>
1020     *
1021     * @see #setRowHeader
1022     * @see JViewport#setView
1023     * @param view the component to display as the row header
1024     */

1025    public void setRowHeaderView(Component JavaDoc view) {
1026        if (getRowHeader() == null) {
1027            setRowHeader(createViewport());
1028        }
1029        getRowHeader().setView(view);
1030    }
1031
1032
1033
1034    /**
1035     * Returns the column header.
1036     * @return the <code>columnHeader</code> property
1037     * @see #setColumnHeader
1038     */

1039    public JViewport JavaDoc getColumnHeader() {
1040        return columnHeader;
1041    }
1042
1043
1044    /**
1045     * Removes the old columnHeader, if it exists. If the new columnHeader
1046     * isn't <code>null</code>, sync the x coordinate of the its viewPosition
1047     * with the viewport (if there is one) and then add it to the scrollpane.
1048     * <p>
1049     * Most applications will find it more convenient to use
1050     * <code>setRowHeaderView</code>
1051     * to add a row header component and its viewport to the scrollpane.
1052     *
1053     * @see #getColumnHeader
1054     * @see #setColumnHeaderView
1055     *
1056     * @beaninfo
1057     * bound: true
1058     * description: The column header child for this scrollpane
1059     * attribute: visualUpdate true
1060     */

1061    public void setColumnHeader(JViewport JavaDoc columnHeader) {
1062    JViewport JavaDoc 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    /**
1079     * Creates a column-header viewport if necessary, sets
1080     * its view, and then adds the column-header viewport
1081     * to the scrollpane. For example:
1082     * <pre>
1083     * JScrollPane scrollpane = new JScrollPane();
1084     * scrollpane.setViewportView(myBigComponentToScroll);
1085     * scrollpane.setColumnHeaderView(myBigComponentsColumnHeader);
1086     * </pre>
1087     *
1088     * @see #setColumnHeader
1089     * @see JViewport#setView
1090     *
1091     * @param view the component to display as the column header
1092     */

1093    public void setColumnHeaderView(Component JavaDoc view) {
1094        if (getColumnHeader() == null) {
1095            setColumnHeader(createViewport());
1096        }
1097        getColumnHeader().setView(view);
1098    }
1099
1100
1101    /**
1102     * Returns the component at the specified corner. The
1103     * <code>key</code> value specifying the corner is one of:
1104     * <ul>
1105     * <li>JScrollPane.LOWER_LEFT_CORNER
1106     * <li>JScrollPane.LOWER_RIGHT_CORNER
1107     * <li>JScrollPane.UPPER_LEFT_CORNER
1108     * <li>JScrollPane.UPPER_RIGHT_CORNER
1109     * <li>JScrollPane.LOWER_LEADING_CORNER
1110     * <li>JScrollPane.LOWER_TRAILING_CORNER
1111     * <li>JScrollPane.UPPER_LEADING_CORNER
1112     * <li>JScrollPane.UPPER_TRAILING_CORNER
1113     * </ul>
1114     *
1115     * @param key one of the values as shown above
1116     * @return one of the components listed below or <code>null</code>
1117     * if <code>key</code> is invalid:
1118     * <ul>
1119     * <li>lowerLeft
1120     * <li>lowerRight
1121     * <li>upperLeft
1122     * <li>upperRight
1123     * </ul>
1124     * @see #setCorner
1125     */

1126    public Component JavaDoc getCorner(String JavaDoc 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    /**
1156     * Adds a child that will appear in one of the scroll panes
1157     * corners, if there's room. For example with both scrollbars
1158     * showing (on the right and bottom edges of the scrollpane)
1159     * the lower left corner component will be shown in the space
1160     * between ends of the two scrollbars. Legal values for
1161     * the <b>key</b> are:
1162     * <ul>
1163     * <li>JScrollPane.LOWER_LEFT_CORNER
1164     * <li>JScrollPane.LOWER_RIGHT_CORNER
1165     * <li>JScrollPane.UPPER_LEFT_CORNER
1166     * <li>JScrollPane.UPPER_RIGHT_CORNER
1167     * <li>JScrollPane.LOWER_LEADING_CORNER
1168     * <li>JScrollPane.LOWER_TRAILING_CORNER
1169     * <li>JScrollPane.UPPER_LEADING_CORNER
1170     * <li>JScrollPane.UPPER_TRAILING_CORNER
1171     * </ul>
1172     * <p>
1173     * Although "corner" doesn't match any beans property
1174     * signature, <code>PropertyChange</code> events are generated with the
1175     * property name set to the corner key.
1176     *
1177     * @param key identifies which corner the component will appear in
1178     * @param corner one of the following components:
1179     * <ul>
1180     * <li>lowerLeft
1181     * <li>lowerRight
1182     * <li>upperLeft
1183     * <li>upperRight
1184     * </ul>
1185     * @exception IllegalArgumentException if corner key is invalid
1186     */

1187    public void setCorner(String JavaDoc key, Component JavaDoc corner)
1188    {
1189    Component JavaDoc 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 JavaDoc("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    /**
1231     * Sets the orientation for the vertical and horizontal
1232     * scrollbars as determined by the
1233     * <code>ComponentOrientation</code> argument.
1234     *
1235     * @param co one of the following values:
1236     * <ul>
1237     * <li>java.awt.ComponentOrientation.LEFT_TO_RIGHT
1238     * <li>java.awt.ComponentOrientation.RIGHT_TO_LEFT
1239     * <li>java.awt.ComponentOrientation.UNKNOWN
1240     * </ul>
1241     * @see java.awt.ComponentOrientation
1242     */

1243    public void setComponentOrientation( ComponentOrientation JavaDoc co ) {
1244        super.setComponentOrientation( co );
1245        if( verticalScrollBar != null )
1246            verticalScrollBar.setComponentOrientation( co );
1247        if( horizontalScrollBar != null )
1248            horizontalScrollBar.setComponentOrientation( co );
1249    }
1250
1251    /**
1252     * Indicates whether or not scrolling will take place in response to the
1253     * mouse wheel. Wheel scrolling is enabled by default.
1254     *
1255     * @see #setWheelScrollingEnabled
1256     * @since 1.4
1257     * @beaninfo
1258     * bound: true
1259     * description: Flag for enabling/disabling mouse wheel scrolling
1260     */

1261    public boolean isWheelScrollingEnabled() {return wheelScrollState;}
1262
1263    /**
1264     * Enables/disables scrolling in response to movement of the mouse wheel.
1265     * Wheel scrolling is enabled by default.
1266     *
1267     * @param handleWheel <code>true</code> if scrolling should be done
1268     * automatically for a MouseWheelEvent,
1269     * <code>false</code> otherwise.
1270     * @see #isWheelScrollingEnabled
1271     * @see java.awt.event.MouseWheelEvent
1272     * @see java.awt.event.MouseWheelListener
1273     * @since 1.4
1274     * @beaninfo
1275     * bound: true
1276     * description: Flag for enabling/disabling mouse wheel scrolling
1277     */

1278    public void setWheelScrollingEnabled(boolean handleWheel) {
1279        boolean old = wheelScrollState;
1280        wheelScrollState = handleWheel;
1281        firePropertyChange("wheelScrollingEnabled", old, handleWheel);
1282    }
1283
1284    /**
1285     * See <code>readObject</code> and <code>writeObject</code> in
1286     * <code>JComponent</code> for more
1287     * information about serialization in Swing.
1288     */

1289    private void writeObject(ObjectOutputStream JavaDoc s) throws IOException JavaDoc {
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    /**
1302     * Returns a string representation of this <code>JScrollPane</code>.
1303     * This method
1304     * is intended to be used only for debugging purposes, and the
1305     * content and format of the returned string may vary between
1306     * implementations. The returned string may be empty but may not
1307     * be <code>null</code>.
1308     *
1309     * @return a string representation of this <code>JScrollPane</code>.
1310     */

1311    protected String JavaDoc paramString() {
1312        String JavaDoc viewportBorderString = (viewportBorder != null ?
1313                       viewportBorder.toString() : "");
1314        String JavaDoc viewportString = (viewport != null ?
1315                 viewport.toString() : "");
1316        String JavaDoc 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 JavaDoc 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 JavaDoc horizontalScrollBarString = (horizontalScrollBar != null ?
1333                        horizontalScrollBar.toString()
1334                        : "");
1335        String JavaDoc verticalScrollBarString = (verticalScrollBar != null ?
1336                      verticalScrollBar.toString() : "");
1337        String JavaDoc columnHeaderString = (columnHeader != null ?
1338                     columnHeader.toString() : "");
1339        String JavaDoc rowHeaderString = (rowHeader != null ?
1340                  rowHeader.toString() : "");
1341        String JavaDoc lowerLeftString = (lowerLeft != null ?
1342                  lowerLeft.toString() : "");
1343        String JavaDoc lowerRightString = (lowerRight != null ?
1344                  lowerRight.toString() : "");
1345        String JavaDoc upperLeftString = (upperLeft != null ?
1346                  upperLeft.toString() : "");
1347        String JavaDoc 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/////////////////
1366
// Accessibility support
1367
////////////////
1368

1369    /**
1370     * Gets the AccessibleContext associated with this JScrollPane.
1371     * For scroll panes, the AccessibleContext takes the form of an
1372     * AccessibleJScrollPane.
1373     * A new AccessibleJScrollPane instance is created if necessary.
1374     *
1375     * @return an AccessibleJScrollPane that serves as the
1376     * AccessibleContext of this JScrollPane
1377     */

1378    public AccessibleContext getAccessibleContext() {
1379        if (accessibleContext == null) {
1380            accessibleContext = new AccessibleJScrollPane();
1381        }
1382        return accessibleContext;
1383    }
1384
1385    /**
1386     * This class implements accessibility support for the
1387     * <code>JScrollPane</code> class. It provides an implementation of the
1388     * Java Accessibility API appropriate to scroll pane user-interface
1389     * elements.
1390     * <p>
1391     * <strong>Warning:</strong>
1392     * Serialized objects of this class will not be compatible with
1393     * future Swing releases. The current serialization support is
1394     * appropriate for short term storage or RMI between applications running
1395     * the same version of Swing. As of 1.4, support for long term storage
1396     * of all JavaBeans<sup><font size="-2">TM</font></sup>
1397     * has been added to the <code>java.beans</code> package.
1398     * Please see {@link java.beans.XMLEncoder}.
1399     */

1400    protected class AccessibleJScrollPane extends AccessibleJComponent
1401        implements ChangeListener, PropertyChangeListener {
1402
1403        protected JViewport JavaDoc viewPort = null;
1404
1405    /*
1406     * Resets the viewport ChangeListener and PropertyChangeListener
1407     */

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        /**
1421         * AccessibleJScrollPane constructor
1422         */

1423        public AccessibleJScrollPane() {
1424            super();
1425
1426        resetViewPort();
1427
1428        // initialize the AccessibleRelationSets for the JScrollPane
1429
// and JScrollBar(s)
1430
JScrollBar JavaDoc scrollBar = getHorizontalScrollBar();
1431        if (scrollBar != null) {
1432        setScrollBarRelations(scrollBar);
1433        }
1434        scrollBar = getVerticalScrollBar();
1435        if (scrollBar != null) {
1436        setScrollBarRelations(scrollBar);
1437        }
1438        }
1439
1440        /**
1441         * Get the role of this object.
1442         *
1443         * @return an instance of AccessibleRole describing the role of the
1444         * object
1445         * @see AccessibleRole
1446         */

1447        public AccessibleRole getAccessibleRole() {
1448            return AccessibleRole.SCROLL_PANE;
1449        }
1450
1451    /**
1452         * Invoked when the target of the listener has changed its state.
1453         *
1454         * @param e a <code>ChangeEvent</code> object. Must not be null.
1455     *
1456     * @throws NullPointerException if the parameter is null.
1457     */

1458        public void stateChanged(ChangeEvent e) {
1459        if (e == null) {
1460        throw new NullPointerException JavaDoc();
1461        }
1462        firePropertyChange(ACCESSIBLE_VISIBLE_DATA_PROPERTY,
1463                   Boolean.valueOf(false),
1464                   Boolean.valueOf(true));
1465        }
1466
1467    /**
1468     * This method gets called when a bound property is changed.
1469     * @param evt A <code>PropertyChangeEvent</code> object describing
1470     * the event source and the property that has changed. Must not be null.
1471     *
1472     * @throws NullPointerException if the parameter is null.
1473     * @since 1.5
1474     */

1475    public void propertyChange(PropertyChangeEvent e) {
1476        String JavaDoc propertyName = e.getPropertyName();
1477        if (propertyName == "horizontalScrollBar" ||
1478        propertyName == "verticalScrollBar") {
1479        
1480        if (e.getNewValue() instanceof JScrollBar JavaDoc) {
1481            setScrollBarRelations((JScrollBar JavaDoc)e.getNewValue());
1482        }
1483        }
1484    }
1485
1486
1487    /*
1488     * Sets the CONTROLLER_FOR and CONTROLLED_BY AccessibleRelations for
1489     * the JScrollPane and JScrollBar. JScrollBar must not be null.
1490     */

1491    void setScrollBarRelations(JScrollBar JavaDoc scrollBar) {
1492        /*
1493         * The JScrollBar is a CONTROLLER_FOR the JScrollPane.
1494         * The JScrollPane is CONTROLLED_BY the JScrollBar.
1495         */

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        // set the relation set for the scroll bar
1504
AccessibleContext ac = scrollBar.getAccessibleContext();
1505        ac.getAccessibleRelationSet().add(controllerFor);
1506
1507        // set the relation set for the scroll pane
1508
getAccessibleRelationSet().add(controlledBy);
1509    }
1510    }
1511}
1512
1513
Popular Tags