- Notifications
You must be signed in to change notification settings - Fork 5.8k
/
Copy pathJScrollPane.java
1529 lines (1384 loc) · 54.2 KB
/
JScrollPane.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
packagejavax.swing;
importjava.awt.Component;
importjava.awt.ComponentOrientation;
importjava.awt.Insets;
importjava.awt.LayoutManager;
importjava.awt.Point;
importjava.awt.Rectangle;
importjava.beans.BeanProperty;
importjava.beans.JavaBean;
importjava.beans.PropertyChangeEvent;
importjava.beans.PropertyChangeListener;
importjava.beans.Transient;
importjava.io.IOException;
importjava.io.ObjectOutputStream;
importjava.io.Serial;
importjavax.accessibility.Accessible;
importjavax.accessibility.AccessibleContext;
importjavax.accessibility.AccessibleRelation;
importjavax.accessibility.AccessibleRole;
importjavax.swing.border.Border;
importjavax.swing.event.ChangeEvent;
importjavax.swing.event.ChangeListener;
importjavax.swing.plaf.ScrollPaneUI;
importjavax.swing.plaf.UIResource;
/**
* Provides a scrollable view of a lightweight component.
* A <code>JScrollPane</code> manages a viewport, optional
* vertical and horizontal scroll bars, and optional row and
* column heading viewports.
* You can find task-oriented documentation of <code>JScrollPane</code> in
* <a href="https://docs.oracle.com/javase/tutorial/uiswing/components/scrollpane.html">How to Use Scroll Panes</a>,
* a section in <em>The Java Tutorial</em>. Note that
* <code>JScrollPane</code> does not support heavyweight components.
*
* <div style="float:right;text-align:center">
* <p><b>Example:</b>
* <p><img src="doc-files/JScrollPane-1.gif"
* alt="The following text describes this image."
* width="256" height="248">
* </div>
* The <code>JViewport</code> provides a window,
* or "viewport" onto a data
* source -- for example, a text file. That data source is the
* "scrollable client" (aka data model) displayed by the
* <code>JViewport</code> view.
* A <code>JScrollPane</code> basically consists of <code>JScrollBar</code>s,
* a <code>JViewport</code>, and the wiring between them,
* as shown in the diagram at right.
* <p>
* In addition to the scroll bars and viewport,
* a <code>JScrollPane</code> can have a
* column header and a row header. Each of these is a
* <code>JViewport</code> object that
* you specify with <code>setRowHeaderView</code>,
* and <code>setColumnHeaderView</code>.
* The column header viewport automatically scrolls left and right, tracking
* the left-right scrolling of the main viewport.
* (It never scrolls vertically, however.)
* The row header acts in a similar fashion.
* <p>
* Where two scroll bars meet, the row header meets the column header,
* or a scroll bar meets one of the headers, both components stop short
* of the corner, leaving a rectangular space which is, by default, empty.
* These spaces can potentially exist in any number of the four corners.
* In the previous diagram, the top right space is present and identified
* by the label "corner component".
* <p>
* Any number of these empty spaces can be replaced by using the
* <code>setCorner</code> method to add a component to a particular corner.
* (Note: The same component cannot be added to multiple corners.)
* This is useful if there's
* some extra decoration or function you'd like to add to the scroll pane.
* The size of each corner component is entirely determined by the size of the
* headers and/or scroll bars that surround it.
* <p>
* A corner component will only be visible if there is an empty space in that
* corner for it to exist in. For example, consider a component set into the
* top right corner of a scroll pane with a column header. If the scroll pane's
* vertical scrollbar is not present, perhaps because the view component hasn't
* grown large enough to require it, then the corner component will not be
* shown (since there is no empty space in that corner created by the meeting
* of the header and vertical scroll bar). Forcing the scroll bar to always be
* shown, using
* <code>setVerticalScrollBarPolicy(VERTICAL_SCROLLBAR_ALWAYS)</code>,
* will ensure that the space for the corner component always exists.
* <p>
* To add a border around the main viewport,
* you can use <code>setViewportBorder</code>.
* (Of course, you can also add a border around the whole scroll pane using
* <code>setBorder</code>.)
* <p>
* A common operation to want to do is to set the background color that will
* be used if the main viewport view is smaller than the viewport, or is
* not opaque. This can be accomplished by setting the background color
* of the viewport, via <code>scrollPane.getViewport().setBackground()</code>.
* The reason for setting the color of the viewport and not the scrollpane
* is that by default <code>JViewport</code> is opaque
* which, among other things, means it will completely fill
* in its background using its background color. Therefore when
* <code>JScrollPane</code> draws its background the viewport will
* usually draw over it.
* <p>
* By default <code>JScrollPane</code> uses <code>ScrollPaneLayout</code>
* to handle the layout of its child Components. <code>ScrollPaneLayout</code>
* determines the size to make the viewport view in one of two ways:
* <ol>
* <li>If the view implements <code>Scrollable</code>
* a combination of <code>getPreferredScrollableViewportSize</code>,
* <code>getScrollableTracksViewportWidth</code> and
* <code>getScrollableTracksViewportHeight</code>is used, otherwise
* <li><code>getPreferredSize</code> is used.
* </ol>
* <p>
* <strong>Warning:</strong> Swing is not thread safe. For more
* information see <a
* href="package-summary.html#threading">Swing's Threading
* Policy</a>.
* <p>
* <strong>Warning:</strong>
* Serialized objects of this class will not be compatible with
* future Swing releases. The current serialization support is
* appropriate for short term storage or RMI between applications running
* the same version of Swing. As of 1.4, support for long term storage
* of all JavaBeans
* has been added to the <code>java.beans</code> package.
* Please see {@link java.beans.XMLEncoder}.
*
* @see JScrollBar
* @see JViewport
* @see ScrollPaneLayout
* @see Scrollable
* @see Component#getPreferredSize
* @see #setViewportView
* @see #setRowHeaderView
* @see #setColumnHeaderView
* @see #setCorner
* @see #setViewportBorder
*
* @author Hans Muller
* @since 1.2
*/
@JavaBean(defaultProperty = "UI", description = "A specialized container that manages a viewport, optional scrollbars and headers")
@SwingContainer(delegate = "getViewport")
@SuppressWarnings("serial") // Same-version serialization only
publicclassJScrollPaneextendsJComponentimplementsScrollPaneConstants, Accessible
{
privateBorderviewportBorder;
/**
* @see #getUIClassID
* @see #readObject
*/
privatestaticfinalStringuiClassID = "ScrollPaneUI";
/**
* The display policy for the vertical scrollbar.
* The default is
* <code>ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED</code>.
* @see #setVerticalScrollBarPolicy
*/
protectedintverticalScrollBarPolicy = VERTICAL_SCROLLBAR_AS_NEEDED;
/**
* The display policy for the horizontal scrollbar.
* The default is
* <code>ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED</code>.
* @see #setHorizontalScrollBarPolicy
*/
protectedinthorizontalScrollBarPolicy = HORIZONTAL_SCROLLBAR_AS_NEEDED;
/**
* The scrollpane's viewport child. Default is an empty
* <code>JViewport</code>.
* @see #setViewport
*/
protectedJViewportviewport;
/**
* The scrollpane's vertical scrollbar child.
* Default is a <code>JScrollBar</code>.
* @see #setVerticalScrollBar
*/
protectedJScrollBarverticalScrollBar;
/**
* The scrollpane's horizontal scrollbar child.
* Default is a <code>JScrollBar</code>.
* @see #setHorizontalScrollBar
*/
protectedJScrollBarhorizontalScrollBar;
/**
* The row header child. Default is <code>null</code>.
* @see #setRowHeader
*/
protectedJViewportrowHeader;
/**
* The column header child. Default is <code>null</code>.
* @see #setColumnHeader
*/
protectedJViewportcolumnHeader;
/**
* The component to display in the lower left corner.
* Default is <code>null</code>.
* @see #setCorner
*/
protectedComponentlowerLeft;
/**
* The component to display in the lower right corner.
* Default is <code>null</code>.
* @see #setCorner
*/
protectedComponentlowerRight;
/**
* The component to display in the upper left corner.
* Default is <code>null</code>.
* @see #setCorner
*/
protectedComponentupperLeft;
/**
* The component to display in the upper right corner.
* Default is <code>null</code>.
* @see #setCorner
*/
protectedComponentupperRight;
/*
* State flag for mouse wheel scrolling
*/
privatebooleanwheelScrollState = true;
/**
* Creates a <code>JScrollPane</code> that displays the view
* component in a viewport
* whose view position can be controlled with a pair of scrollbars.
* The scrollbar policies specify when the scrollbars are displayed,
* For example, if <code>vsbPolicy</code> is
* <code>VERTICAL_SCROLLBAR_AS_NEEDED</code>
* then the vertical scrollbar only appears if the view doesn't fit
* vertically. The available policy settings are listed at
* {@link #setVerticalScrollBarPolicy} and
* {@link #setHorizontalScrollBarPolicy}.
*
* @see #setViewportView
*
* @param view the component to display in the scrollpanes viewport
* @param vsbPolicy an integer that specifies the vertical
* scrollbar policy
* @param hsbPolicy an integer that specifies the horizontal
* scrollbar policy
*/
publicJScrollPane(Componentview, intvsbPolicy, inthsbPolicy)
{
setLayout(newScrollPaneLayout.UIResource());
setVerticalScrollBarPolicy(vsbPolicy);
setHorizontalScrollBarPolicy(hsbPolicy);
setViewport(createViewport());
setVerticalScrollBar(createVerticalScrollBar());
setHorizontalScrollBar(createHorizontalScrollBar());
if (view != null) {
setViewportView(view);
}
setUIProperty("opaque",true);
updateUI();
if (!this.getComponentOrientation().isLeftToRight()) {
viewport.setViewPosition(newPoint(Integer.MAX_VALUE, 0));
}
}
/**
* Creates a <code>JScrollPane</code> that displays the
* contents of the specified
* component, where both horizontal and vertical scrollbars appear
* whenever the component's contents are larger than the view.
*
* @see #setViewportView
* @param view the component to display in the scrollpane's viewport
*/
publicJScrollPane(Componentview) {
this(view, VERTICAL_SCROLLBAR_AS_NEEDED, HORIZONTAL_SCROLLBAR_AS_NEEDED);
}
/**
* Creates an empty (no viewport view) <code>JScrollPane</code>
* with specified
* scrollbar policies. The available policy settings are listed at
* {@link #setVerticalScrollBarPolicy} and
* {@link #setHorizontalScrollBarPolicy}.
*
* @see #setViewportView
*
* @param vsbPolicy an integer that specifies the vertical
* scrollbar policy
* @param hsbPolicy an integer that specifies the horizontal
* scrollbar policy
*/
publicJScrollPane(intvsbPolicy, inthsbPolicy) {
this(null, vsbPolicy, hsbPolicy);
}
/**
* Creates an empty (no viewport view) <code>JScrollPane</code>
* where both horizontal and vertical scrollbars appear when needed.
*/
publicJScrollPane() {
this(null, VERTICAL_SCROLLBAR_AS_NEEDED, HORIZONTAL_SCROLLBAR_AS_NEEDED);
}
/**
* Returns the look and feel (L&F) object that renders this component.
*
* @return the <code>ScrollPaneUI</code> object that renders this
* component
* @see #setUI
*/
@BeanProperty(hidden = true, visualUpdate = true, description
= "The UI object that implements the Component's LookAndFeel.")
publicScrollPaneUIgetUI() {
return (ScrollPaneUI)ui;
}
/**
* Sets the <code>ScrollPaneUI</code> object that provides the
* look and feel (L&F) for this component.
*
* @param ui the <code>ScrollPaneUI</code> L&F object
* @see #getUI
*/
publicvoidsetUI(ScrollPaneUIui) {
super.setUI(ui);
}
/**
* Replaces the current <code>ScrollPaneUI</code> object with a version
* from the current default look and feel.
* To be called when the default look and feel changes.
*
* @see JComponent#updateUI
* @see UIManager#getUI
*/
publicvoidupdateUI() {
setUI((ScrollPaneUI)UIManager.getUI(this));
}
/**
* Returns the suffix used to construct the name of the L&F class used to
* render this component.
*
* @return the string "ScrollPaneUI"
* @see JComponent#getUIClassID
* @see UIDefaults#getUI
*/
@BeanProperty(bound = false, hidden = true)
publicStringgetUIClassID() {
returnuiClassID;
}
/**
* Sets the layout manager for this <code>JScrollPane</code>.
* This method overrides <code>setLayout</code> in
* <code>java.awt.Container</code> to ensure that only
* <code>LayoutManager</code>s which
* are subclasses of <code>ScrollPaneLayout</code> can be used in a
* <code>JScrollPane</code>. If <code>layout</code> is non-null, this
* will invoke <code>syncWithScrollPane</code> on it.
*
* @param layout the specified layout manager
* @throws ClassCastException if layout is not a
* <code>ScrollPaneLayout</code>
* @see java.awt.Container#getLayout
* @see java.awt.Container#setLayout
*/
publicvoidsetLayout(LayoutManagerlayout) {
if (layoutinstanceofScrollPaneLayout) {
super.setLayout(layout);
((ScrollPaneLayout)layout).syncWithScrollPane(this);
}
elseif (layout == null) {
super.setLayout(layout);
}
else {
Strings = "layout of JScrollPane must be a ScrollPaneLayout";
thrownewClassCastException(s);
}
}
/**
* Overridden to return true so that any calls to <code>revalidate</code>
* on any descendants of this <code>JScrollPane</code> will cause the
* entire tree beginning with this <code>JScrollPane</code> to be
* validated.
*
* @return true
* @see java.awt.Container#validate
* @see JComponent#revalidate
* @see JComponent#isValidateRoot
* @see java.awt.Container#isValidateRoot
*/
@Override
@BeanProperty(hidden = true)
publicbooleanisValidateRoot() {
returntrue;
}
/**
* Returns the vertical scroll bar policy value.
* @return the <code>verticalScrollBarPolicy</code> property
* @see #setVerticalScrollBarPolicy
*/
publicintgetVerticalScrollBarPolicy() {
returnverticalScrollBarPolicy;
}
/**
* Determines when the vertical scrollbar appears in the scrollpane.
* Legal values are:
* <ul>
* <li><code>ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED</code>
* <li><code>ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER</code>
* <li><code>ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS</code>
* </ul>
*
* @param policy one of the three values listed above
* @throws IllegalArgumentException if <code>policy</code>
* is not one of the legal values shown above
* @see #getVerticalScrollBarPolicy
*/
@BeanProperty(preferred = true, enumerationValues = {
"ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED",
"ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER",
"ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS"}, description
= "The scrollpane vertical scrollbar policy")
publicvoidsetVerticalScrollBarPolicy(intpolicy) {
switch (policy) {
caseVERTICAL_SCROLLBAR_AS_NEEDED:
caseVERTICAL_SCROLLBAR_NEVER:
caseVERTICAL_SCROLLBAR_ALWAYS:
break;
default:
thrownewIllegalArgumentException("invalid verticalScrollBarPolicy");
}
intold = verticalScrollBarPolicy;
verticalScrollBarPolicy = policy;
firePropertyChange("verticalScrollBarPolicy", old, policy);
revalidate();
repaint();
}
/**
* Returns the horizontal scroll bar policy value.
* @return the <code>horizontalScrollBarPolicy</code> property
* @see #setHorizontalScrollBarPolicy
*/
publicintgetHorizontalScrollBarPolicy() {
returnhorizontalScrollBarPolicy;
}
/**
* Determines when the horizontal scrollbar appears in the scrollpane.
* The options are:<ul>
* <li><code>ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED</code>
* <li><code>ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER</code>
* <li><code>ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS</code>
* </ul>
*
* @param policy one of the three values listed above
* @throws IllegalArgumentException if <code>policy</code>
* is not one of the legal values shown above
* @see #getHorizontalScrollBarPolicy
*/
@BeanProperty(preferred = true, enumerationValues = {
"ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED",
"ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER",
"ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS"}, description
= "The scrollpane scrollbar policy")
publicvoidsetHorizontalScrollBarPolicy(intpolicy) {
switch (policy) {
caseHORIZONTAL_SCROLLBAR_AS_NEEDED:
caseHORIZONTAL_SCROLLBAR_NEVER:
caseHORIZONTAL_SCROLLBAR_ALWAYS:
break;
default:
thrownewIllegalArgumentException("invalid horizontalScrollBarPolicy");
}
intold = horizontalScrollBarPolicy;
horizontalScrollBarPolicy = policy;
firePropertyChange("horizontalScrollBarPolicy", old, policy);
revalidate();
repaint();
}
/**
* Returns the <code>Border</code> object that surrounds the viewport.
*
* @return the <code>viewportBorder</code> property
* @see #setViewportBorder
*/
publicBordergetViewportBorder() {
returnviewportBorder;
}
/**
* Adds a border around the viewport. Note that the border isn't
* set on the viewport directly, <code>JViewport</code> doesn't support
* the <code>JComponent</code> border property.
* Similarly setting the <code>JScrollPane</code>s
* viewport doesn't affect the <code>viewportBorder</code> property.
* <p>
* The default value of this property is computed by the look
* and feel implementation.
*
* @param viewportBorder the border to be added
* @see #getViewportBorder
* @see #setViewport
*/
@BeanProperty(preferred = true, description
= "The border around the viewport.")
publicvoidsetViewportBorder(BorderviewportBorder) {
BorderoldValue = this.viewportBorder;
this.viewportBorder = viewportBorder;
firePropertyChange("viewportBorder", oldValue, viewportBorder);
}
/**
* Returns the bounds of the viewport's border.
*
* @return a <code>Rectangle</code> object specifying the viewport border
*/
@BeanProperty(bound = false)
publicRectanglegetViewportBorderBounds()
{
RectangleborderR = newRectangle(getSize());
Insetsinsets = getInsets();
borderR.x = insets.left;
borderR.y = insets.top;
borderR.width -= insets.left + insets.right;
borderR.height -= insets.top + insets.bottom;
booleanleftToRight = SwingUtilities.isLeftToRight(this);
/* If there's a visible column header remove the space it
* needs from the top of borderR.
*/
JViewportcolHead = getColumnHeader();
if ((colHead != null) && (colHead.isVisible())) {
intcolHeadHeight = colHead.getHeight();
borderR.y += colHeadHeight;
borderR.height -= colHeadHeight;
}
/* If there's a visible row header remove the space it needs
* from the left of borderR.
*/
JViewportrowHead = getRowHeader();
if ((rowHead != null) && (rowHead.isVisible())) {
introwHeadWidth = rowHead.getWidth();
if ( leftToRight ) {
borderR.x += rowHeadWidth;
}
borderR.width -= rowHeadWidth;
}
/* If there's a visible vertical scrollbar remove the space it needs
* from the width of borderR.
*/
JScrollBarvsb = getVerticalScrollBar();
if ((vsb != null) && (vsb.isVisible())) {
intvsbWidth = vsb.getWidth();
if ( !leftToRight ) {
borderR.x += vsbWidth;
}
borderR.width -= vsbWidth;
}
/* If there's a visible horizontal scrollbar remove the space it needs
* from the height of borderR.
*/
JScrollBarhsb = getHorizontalScrollBar();
if ((hsb != null) && (hsb.isVisible())) {
borderR.height -= hsb.getHeight();
}
returnborderR;
}
/**
* By default <code>JScrollPane</code> creates scrollbars
* that are instances
* of this class. <code>Scrollbar</code> overrides the
* <code>getUnitIncrement</code> and <code>getBlockIncrement</code>
* methods so that, if the viewport's view is a <code>Scrollable</code>,
* the view is asked to compute these values. Unless
* the unit/block increment have been explicitly set.
* <p>
* <strong>Warning:</strong>
* Serialized objects of this class will not be compatible with
* future Swing releases. The current serialization support is
* appropriate for short term storage or RMI between applications running
* the same version of Swing. As of 1.4, support for long term storage
* of all JavaBeans
* has been added to the <code>java.beans</code> package.
* Please see {@link java.beans.XMLEncoder}.
*
* @see Scrollable
* @see JScrollPane#createVerticalScrollBar
* @see JScrollPane#createHorizontalScrollBar
*/
@SuppressWarnings("serial") // Same-version serialization only
protectedclassScrollBarextendsJScrollBarimplementsUIResource
{
/**
* Set to true when the unit increment has been explicitly set.
* If this is false the viewport's view is obtained and if it
* is an instance of <code>Scrollable</code> the unit increment
* from it is used.
*/
privatebooleanunitIncrementSet;
/**
* Set to true when the block increment has been explicitly set.
* If this is false the viewport's view is obtained and if it
* is an instance of <code>Scrollable</code> the block increment
* from it is used.
*/
privatebooleanblockIncrementSet;
/**
* Creates a scrollbar with the specified orientation.
* The options are:
* <ul>
* <li><code>ScrollPaneConstants.VERTICAL</code>
* <li><code>ScrollPaneConstants.HORIZONTAL</code>
* </ul>
*
* @param orientation an integer specifying one of the legal
* orientation values shown above
* @since 1.4
*/
publicScrollBar(intorientation) {
super(orientation);
this.putClientProperty("JScrollBar.fastWheelScrolling",
Boolean.TRUE);
}
/**
* Messages super to set the value, and resets the
* <code>unitIncrementSet</code> instance variable to true.
*
* @param unitIncrement the new unit increment value, in pixels
*/
publicvoidsetUnitIncrement(intunitIncrement) {
unitIncrementSet = true;
this.putClientProperty("JScrollBar.fastWheelScrolling", null);
super.setUnitIncrement(unitIncrement);
}
/**
* Computes the unit increment for scrolling if the viewport's
* view is a <code>Scrollable</code> object.
* Otherwise return <code>super.getUnitIncrement</code>.
*
* @param direction less than zero to scroll up/left,
* greater than zero for down/right
* @return an integer, in pixels, containing the unit increment
* @see Scrollable#getScrollableUnitIncrement
*/
publicintgetUnitIncrement(intdirection) {
JViewportvp = getViewport();
if (!unitIncrementSet && (vp != null) &&
(vp.getView() instanceofScrollable)) {
Scrollableview = (Scrollable)(vp.getView());
Rectanglevr = vp.getViewRect();
returnview.getScrollableUnitIncrement(vr, getOrientation(), direction);
}
else {
returnsuper.getUnitIncrement(direction);
}
}
/**
* Messages super to set the value, and resets the
* <code>blockIncrementSet</code> instance variable to true.
*
* @param blockIncrement the new block increment value, in pixels
*/
publicvoidsetBlockIncrement(intblockIncrement) {
blockIncrementSet = true;
this.putClientProperty("JScrollBar.fastWheelScrolling", null);
super.setBlockIncrement(blockIncrement);
}
/**
* Computes the block increment for scrolling if the viewport's
* view is a <code>Scrollable</code> object. Otherwise
* the <code>blockIncrement</code> equals the viewport's width
* or height. If there's no viewport return
* <code>super.getBlockIncrement</code>.
*
* @param direction less than zero to scroll up/left,
* greater than zero for down/right
* @return an integer, in pixels, containing the block increment
* @see Scrollable#getScrollableBlockIncrement
*/
publicintgetBlockIncrement(intdirection) {
JViewportvp = getViewport();
if (blockIncrementSet || vp == null) {
returnsuper.getBlockIncrement(direction);
}
elseif (vp.getView() instanceofScrollable) {
Scrollableview = (Scrollable)(vp.getView());
Rectanglevr = vp.getViewRect();
returnview.getScrollableBlockIncrement(vr, getOrientation(), direction);
}
elseif (getOrientation() == VERTICAL) {
returnvp.getExtentSize().height;
}
else {
returnvp.getExtentSize().width;
}
}
}
/**
* Returns a <code>JScrollPane.ScrollBar</code> by default.
* Subclasses may override this method to force <code>ScrollPaneUI</code>
* implementations to use a <code>JScrollBar</code> subclass.
* Used by <code>ScrollPaneUI</code> implementations to
* create the horizontal scrollbar.
*
* @return a <code>JScrollBar</code> with a horizontal orientation
* @see JScrollBar
*/
publicJScrollBarcreateHorizontalScrollBar() {
returnnewScrollBar(JScrollBar.HORIZONTAL);
}
/**
* Returns the horizontal scroll bar that controls the viewport's
* horizontal view position.
*
* @return the <code>horizontalScrollBar</code> property
* @see #setHorizontalScrollBar
*/
@Transient
publicJScrollBargetHorizontalScrollBar() {
returnhorizontalScrollBar;
}
/**
* Adds the scrollbar that controls the viewport's horizontal view
* position to the scrollpane.
* This is usually unnecessary, as <code>JScrollPane</code> creates
* horizontal and vertical scrollbars by default.
*
* @param horizontalScrollBar the horizontal scrollbar to be added
* @see #createHorizontalScrollBar
* @see #getHorizontalScrollBar
*/
@BeanProperty(expert = true, description
= "The horizontal scrollbar.")
publicvoidsetHorizontalScrollBar(JScrollBarhorizontalScrollBar) {
JScrollBarold = getHorizontalScrollBar();
this.horizontalScrollBar = horizontalScrollBar;
if (horizontalScrollBar != null) {
add(horizontalScrollBar, HORIZONTAL_SCROLLBAR);
}
elseif (old != null) {
remove(old);
}
firePropertyChange("horizontalScrollBar", old, horizontalScrollBar);
revalidate();
repaint();
}
/**
* Returns a <code>JScrollPane.ScrollBar</code> by default. Subclasses
* may override this method to force <code>ScrollPaneUI</code>
* implementations to use a <code>JScrollBar</code> subclass.
* Used by <code>ScrollPaneUI</code> implementations to create the
* vertical scrollbar.
*
* @return a <code>JScrollBar</code> with a vertical orientation
* @see JScrollBar
*/
publicJScrollBarcreateVerticalScrollBar() {
returnnewScrollBar(JScrollBar.VERTICAL);
}
/**
* Returns the vertical scroll bar that controls the viewports
* vertical view position.
*
* @return the <code>verticalScrollBar</code> property
* @see #setVerticalScrollBar
*/
@Transient
publicJScrollBargetVerticalScrollBar() {
returnverticalScrollBar;
}
/**
* Adds the scrollbar that controls the viewports vertical view position
* to the scrollpane. This is usually unnecessary,
* as <code>JScrollPane</code> creates vertical and
* horizontal scrollbars by default.
*
* @param verticalScrollBar the new vertical scrollbar to be added
* @see #createVerticalScrollBar
* @see #getVerticalScrollBar
*/
@BeanProperty(expert = true, description
= "The vertical scrollbar.")
publicvoidsetVerticalScrollBar(JScrollBarverticalScrollBar) {
JScrollBarold = getVerticalScrollBar();
this.verticalScrollBar = verticalScrollBar;
add(verticalScrollBar, VERTICAL_SCROLLBAR);
firePropertyChange("verticalScrollBar", old, verticalScrollBar);
revalidate();
repaint();
}
/**
* Returns a new <code>JViewport</code> by default.
* Used to create the
* viewport (as needed) in <code>setViewportView</code>,
* <code>setRowHeaderView</code>, and <code>setColumnHeaderView</code>.
* Subclasses may override this method to return a subclass of
* <code>JViewport</code>.
*
* @return a new <code>JViewport</code>
*/
protectedJViewportcreateViewport() {
returnnewJViewport();
}
/**
* Returns the current <code>JViewport</code>.
*
* @see #setViewport
* @return the <code>viewport</code> property
*/
publicJViewportgetViewport() {
returnviewport;
}
/**
* Removes the old viewport (if there is one); forces the
* viewPosition of the new viewport to be in the +x,+y quadrant;
* syncs up the row and column headers (if there are any) with the
* new viewport; and finally syncs the scrollbars and
* headers with the new viewport.
* <p>
* Most applications will find it more convenient to use
* <code>setViewportView</code>
* to add a viewport and a view to the scrollpane.
*
* @param viewport the new viewport to be used; if viewport is
* <code>null</code>, the old viewport is still removed
* and the new viewport is set to <code>null</code>
* @see #createViewport
* @see #getViewport
* @see #setViewportView
*/
@BeanProperty(expert = true, visualUpdate = true, description
= "The viewport child for this scrollpane")
publicvoidsetViewport(JViewportviewport) {
JViewportold = getViewport();
this.viewport = viewport;
if (viewport != null) {
add(viewport, VIEWPORT);
}
elseif (old != null) {
remove(old);
}
firePropertyChange("viewport", old, viewport);
if (accessibleContext != null) {
((AccessibleJScrollPane)accessibleContext).resetViewPort();
}
revalidate();
repaint();
}
/**
* Creates a viewport if necessary and then sets its view. Applications
* that don't provide the view directly to the <code>JScrollPane</code>
* constructor
* should use this method to specify the scrollable child that's going
* to be displayed in the scrollpane. For example:
* <pre>
* JScrollPane scrollpane = new JScrollPane();
* scrollpane.setViewportView(myBigComponentToScroll);
* </pre>
* Applications should not add children directly to the scrollpane.
*
* @param view the component to add to the viewport
* @see #setViewport
* @see JViewport#setView
*/
publicvoidsetViewportView(Componentview) {
if (getViewport() == null) {
setViewport(createViewport());
}
getViewport().setView(view);
}
/**
* Returns the row header.
* @return the <code>rowHeader</code> property
* @see #setRowHeader
*/
@Transient
publicJViewportgetRowHeader() {
returnrowHeader;
}
/**
* Removes the old rowHeader, if it exists; if the new rowHeader
* isn't <code>null</code>, syncs the y coordinate of its
* viewPosition with