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
|
/* ScrollPaneLayout.java --
Copyright (C) 2002, 2004 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath 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 for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
package javax.swing;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.LayoutManager;
import java.awt.Point;
import java.awt.Rectangle;
import java.io.Serializable;
import javax.swing.border.Border;
/**
* ScrollPaneLayout
* @author Andrew Selkirk
* @version 1.0
*/
public class ScrollPaneLayout
implements LayoutManager, ScrollPaneConstants, Serializable
{
private static final long serialVersionUID = -4480022884523193743L;
public static class UIResource extends ScrollPaneLayout
implements javax.swing.plaf.UIResource {
public UIResource() {
}
}
protected JViewport viewport;
protected JScrollBar vsb;
protected JScrollBar hsb;
protected JViewport rowHead;
protected JViewport colHead;
protected Component lowerLeft;
protected Component lowerRight;
protected Component upperLeft;
protected Component upperRight;
protected int vsbPolicy;
protected int hsbPolicy;
public ScrollPaneLayout() {
}
public void syncWithScrollPane(JScrollPane scrollPane) {
viewport = scrollPane.getViewport();
rowHead = scrollPane.getRowHeader();
colHead = scrollPane.getColumnHeader();
vsb = scrollPane.getVerticalScrollBar();
hsb = scrollPane.getHorizontalScrollBar();
vsbPolicy = scrollPane.getVerticalScrollBarPolicy();
hsbPolicy = scrollPane.getHorizontalScrollBarPolicy();
lowerLeft = scrollPane.getCorner(LOWER_LEFT_CORNER);
lowerRight = scrollPane.getCorner(LOWER_RIGHT_CORNER);
upperLeft = scrollPane.getCorner(UPPER_LEFT_CORNER);
upperRight = scrollPane.getCorner(UPPER_RIGHT_CORNER);
}
protected Component addSingletonComponent(Component oldComponent,
Component newComponent) {
return null;
}
public void addLayoutComponent(String key, Component component)
{
if (key == VIEWPORT)
viewport = (JViewport) component;
else if (key == VERTICAL_SCROLLBAR)
vsb = (JScrollBar) component;
else if (key == HORIZONTAL_SCROLLBAR)
hsb = (JScrollBar) component;
else if (key == ROW_HEADER)
rowHead = (JViewport) component;
else if (key == COLUMN_HEADER)
colHead = (JViewport) component;
else if (key == LOWER_RIGHT_CORNER)
lowerRight = component;
else if (key == UPPER_RIGHT_CORNER)
upperRight = component;
else if (key == LOWER_LEFT_CORNER)
lowerLeft = component;
else if (key == UPPER_LEFT_CORNER)
upperLeft = component;
}
public void removeLayoutComponent(Component component) {
if (component == viewport)
viewport = null;
else if (component == vsb)
vsb = null;
else if (component == hsb)
hsb = null;
else if (component == rowHead)
rowHead = null;
else if (component == colHead)
colHead = null;
else if (component == lowerRight)
lowerRight = null;
else if (component == upperRight)
upperRight = null;
else if (component == lowerLeft)
lowerLeft = null;
else if (component == upperLeft)
upperLeft = null;
}
public int getVerticalScrollBarPolicy()
{
return vsbPolicy;
}
public void setVerticalScrollBarPolicy(int policy)
{
vsbPolicy = policy;
}
public int getHorizontalScrollBarPolicy()
{
return hsbPolicy;
}
public void setHorizontalScrollBarPolicy(int policy)
{
hsbPolicy = policy;
}
public JViewport getViewport()
{
return viewport;
}
public JScrollBar getHorizontalScrollBar()
{
return hsb;
}
public JScrollBar getVerticalScrollBar()
{
return vsb;
}
public JViewport getRowHeader()
{
return rowHead;
}
public JViewport getColumnHeader()
{
return colHead;
}
public Component getCorner(String key)
{
if (key == LOWER_RIGHT_CORNER)
return lowerRight;
else if (key == UPPER_RIGHT_CORNER)
return upperRight;
else if (key == LOWER_LEFT_CORNER)
return lowerLeft;
else if (key == UPPER_LEFT_CORNER)
return upperLeft;
return null;
}
private static void maybeSetPreferredSize(JComponent src, Dimension dim)
{
Dimension tmp = null;
if (src != null)
tmp = src.getPreferredSize();
if (tmp != null)
dim.setSize(tmp);
}
private static void maybeSetMinimumSize(JComponent src, Dimension dim)
{
Dimension tmp = null;
if (src != null)
tmp = src.getMinimumSize();
if (tmp != null)
dim.setSize(tmp);
}
public Dimension preferredLayoutSize(Container parent)
{
if (parent != null && parent instanceof JScrollPane)
{
JScrollPane sc = (JScrollPane) parent;
synchronized (sc.getTreeLock ())
{
Dimension insetsSize = new Dimension(0,0);
Dimension viewportSize = new Dimension(0,0);
Dimension viewportInsetsSize = new Dimension(0,0);
Dimension columnHeaderSize = new Dimension(0,0);
Dimension rowHeaderSize = new Dimension(0,0);
Dimension verticalScrollBarSize = new Dimension(0,0);
Dimension horizontalScrollBarSize = new Dimension(0,0);
Insets insets = sc.getInsets();
Border viewportBorder = sc.getViewportBorder();
Insets viewportInsets = null;
if (viewportBorder != null)
{
viewportInsets = viewportBorder.getBorderInsets(parent);
if (viewportInsets != null)
viewportInsetsSize.setSize(viewportInsets.left + viewportInsets.right,
viewportInsets.top + viewportInsets.bottom);
}
if (insets != null)
insetsSize.setSize(insets.left + insets.right,
insets.top + insets.bottom);
if (viewport != null)
{
Component view = null;
Scrollable scr = null;
Dimension pref = null;
view = viewport.getView();
if (view != null && view instanceof Scrollable)
scr = (Scrollable) view;
if (scr != null)
pref = scr.getPreferredScrollableViewportSize();
if (pref == null)
pref = viewport.getPreferredSize();
if (pref != null)
viewportSize.setSize(pref);
}
maybeSetPreferredSize(colHead, columnHeaderSize);
maybeSetPreferredSize(rowHead, rowHeaderSize);
maybeSetPreferredSize(vsb, verticalScrollBarSize);
maybeSetPreferredSize(hsb, horizontalScrollBarSize);
return new Dimension(insetsSize.width
+ viewportSize.width
+ viewportInsetsSize.width
+ rowHeaderSize.width
+ verticalScrollBarSize.width,
insetsSize.height
+ viewportSize.height
+ viewportInsetsSize.height
+ columnHeaderSize.height
+ horizontalScrollBarSize.height);
}
}
else
{
return new Dimension(0,0);
}
}
public Dimension minimumLayoutSize(Container parent)
{
if (parent instanceof JScrollPane)
{
JScrollPane sc = (JScrollPane) parent;
synchronized (sc.getTreeLock ())
{
Dimension insetsSize = new Dimension(0,0);
Dimension viewportSize = new Dimension(0,0);
Dimension viewportInsetsSize = new Dimension(0,0);
Dimension columnHeaderSize = new Dimension(0,0);
Dimension rowHeaderSize = new Dimension(0,0);
Dimension verticalScrollBarSize = new Dimension(0,0);
Dimension horizontalScrollBarSize = new Dimension(0,0);
Insets insets = sc.getInsets();
Border viewportBorder = sc.getViewportBorder();
Insets viewportInsets = null;
if (viewportBorder != null)
{
viewportInsets = viewportBorder.getBorderInsets(parent);
if (viewportInsets != null)
viewportInsetsSize.setSize(viewportInsets.left + viewportInsets.right,
viewportInsets.top + viewportInsets.bottom);
}
if (insets != null)
insetsSize.setSize(insets.left + insets.right,
insets.top + insets.bottom);
maybeSetMinimumSize(colHead, columnHeaderSize);
maybeSetMinimumSize(rowHead, rowHeaderSize);
if (vsbPolicy != VERTICAL_SCROLLBAR_NEVER)
maybeSetMinimumSize(vsb, verticalScrollBarSize);
if (hsbPolicy != HORIZONTAL_SCROLLBAR_NEVER)
maybeSetMinimumSize(hsb, horizontalScrollBarSize);
return new Dimension(insetsSize.width
+ viewportSize.width
+ viewportInsetsSize.width
+ rowHeaderSize.width
+ verticalScrollBarSize.width,
insetsSize.height
+ viewportSize.height
+ viewportInsetsSize.height
+ columnHeaderSize.height
+ horizontalScrollBarSize.height);
}
}
else
{
return new Dimension(0,0);
}
}
/**
*
* +----+--------------------+----+ y1
* | c1 | column header | c2 |
* +----+--------------------+----+ y2
* | r | | v |
* | o | | |
* | w | | s |
* | | | r |
* | h | | o |
* | e | viewport | l |
* | a | | l |
* | d | | b |
* | e | | a |
* | r | | r |
* +----+--------------------+----+ y3
* | c3 | h scrollbar | c4 |
* +----+--------------------+----+ y4
* x1 x2 x3 x4
*
*/
public void layoutContainer(Container parent)
{
if (parent instanceof JScrollPane)
{
JScrollPane sc = (JScrollPane) parent;
synchronized (sc.getTreeLock ())
{
JViewport viewport = sc.getViewport();
Dimension viewSize = viewport.getViewSize();
Point viewPos = viewport.getViewPosition();
int x1 = 0, x2 = 0, x3 = 0, x4 = 0;
int y1 = 0, y2 = 0, y3 = 0, y4 = 0;
Rectangle scrollPaneBounds = SwingUtilities.calculateInnerArea(sc, null);
x1 = scrollPaneBounds.x;
y1 = scrollPaneBounds.y;
x4 = scrollPaneBounds.x + scrollPaneBounds.width;
y4 = scrollPaneBounds.y + scrollPaneBounds.height;
if (colHead != null)
y2 = y1 + colHead.getPreferredSize().height;
else
y2 = y1;
if (rowHead != null)
x2 = x1 + rowHead.getPreferredSize().width;
else
x2 = x1;
int vsbPolicy = sc.getVerticalScrollBarPolicy();
int hsbPolicy = sc.getHorizontalScrollBarPolicy();
x3 = x4 - vsb.getPreferredSize().width;
y3 = y4 - hsb.getPreferredSize().height;
boolean showVsb =
(vsb != null)
&& ((vsbPolicy == VERTICAL_SCROLLBAR_ALWAYS)
|| (vsbPolicy == VERTICAL_SCROLLBAR_AS_NEEDED
&& viewSize.height > (y3 - y2)));
boolean showHsb =
(hsb != null)
&& ((hsbPolicy == HORIZONTAL_SCROLLBAR_ALWAYS)
|| (hsbPolicy == HORIZONTAL_SCROLLBAR_AS_NEEDED
&& viewSize.width > (x3 - x2)));
if (!showVsb)
x3 = x4;
if (!showHsb)
y3 = y4;
// now set the layout
if (viewport != null)
viewport.setBounds(new Rectangle(x2, y2, x3-x2, y3-y2));
if (colHead != null)
colHead.setBounds(new Rectangle(x2, y1, x3-x2, y2-y1));
if (rowHead != null)
rowHead.setBounds(new Rectangle(x1, y2, x2-x1, y3-y2));
if (showVsb)
{
vsb.setVisible(true);
vsb.setBounds(new Rectangle(x3, y2, x4-x3, y3-y2));
}
else if (vsb != null)
vsb.setVisible(false);
if (showHsb)
{
hsb.setVisible(true);
hsb.setBounds(new Rectangle(x2, y3, x3-x2, y4-y3));
}
else if (hsb != null)
hsb.setVisible(false);
if (upperLeft != null)
upperLeft.setBounds(new Rectangle(x1, y1, x2-x1, y2-y1));
if (upperRight != null)
upperRight.setBounds(new Rectangle(x3, y1, x4-x3, y2-y1));
if (lowerLeft != null)
lowerLeft.setBounds(new Rectangle(x1, y3, x2-x1, y4-y3));
if (lowerRight != null)
lowerRight.setBounds(new Rectangle(x3, y3, x4-x3, y4-y3));
}
}
}
/**
* Returns the bounds of the border around a ScrollPane's viewport.
*
* @param scrollPane the ScrollPane for which's viewport the border
* is requested
*
* @deprecated As of Swing 1.1 replaced by
* {@link javax.swing.JScrollPane#getViewportBorderBounds}.
*/
public Rectangle getViewportBorderBounds(JScrollPane scrollPane) {
return null;
}
}
|