summaryrefslogtreecommitdiff
path: root/libjava/classpath/javax/swing/ScrollPaneLayout.java
blob: fa1743bedf2459c9af50f4c793f2dd54e1cde7e1 (plain)
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
/* 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.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()
    {
      super();
    }
  }

  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()
  {
        // Nothing to do here.
  }

  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);
  }

  /**
   * Removes an existing component.  If oldComponent is not null
   * and is not equal to newComponent, oldComponent must be removed
   * from its parent.
   * @param oldComponent the old Component that may need to be removed.
   * @param newComponent the Component to add.
   * @return the newComponent
   */
  protected Component addSingletonComponent(Component oldComponent,
                                            Component newComponent)
  {
    if (oldComponent != null && oldComponent != newComponent)
      oldComponent.getParent().remove(oldComponent);
    return newComponent;
  }

  /**
   * Add the specified component to the layout.
   * @param key must be one of VIEWPORT, VERTICAL_SCROLLBAR,
   * HORIZONTAL_SCROLLBAR, ROW_HEADER, COLUMN_HEADER,
   * LOWER_RIGHT_CORNER, LOWER_LEFT_CORNER, UPPER_RIGHT_CORNER,
   * UPPER_LEFT_CORNER.
   * @param component the Component to add
   * @throws IllegalArgumentException if key is not as above
   */
  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;
    else
      throw new IllegalArgumentException();
  }

  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;
  }

  /**
   * Sets the vertical scrollbar policy.
   * @param policy must be one of VERTICAL_SCROLLBAR_AS_NEEDED,
   * VERTICAL_SCROLLBAR_NEVER, VERTICAL_SCROLLBAR_ALWAYS.
   * @throws IllegalArgumentException if policy is not one of the valid
   * JScrollBar policies.
   */
  public void setVerticalScrollBarPolicy(int policy)
  {
    if (policy != VERTICAL_SCROLLBAR_AS_NEEDED &&
        policy != VERTICAL_SCROLLBAR_NEVER &&
        policy != VERTICAL_SCROLLBAR_ALWAYS)
      throw new IllegalArgumentException("Illegal Scrollbar Policy");
    vsbPolicy = policy;
  }

  public int getHorizontalScrollBarPolicy()
  {
    return hsbPolicy;
  }

  /**
   * Sets the horizontal scrollbar policy.
   * @param policy must be one of HORIZONTAL_SCROLLBAR_AS_NEEDED,
   * HORIZONTAL_SCROLLBAR_NEVER, HORIZONTAL_SCROLLBAR_ALWAYS.
   * @throws IllegalArgumentException if policy is not one of the valid
   * JScrollbar policies.
   */
  public void setHorizontalScrollBarPolicy(int policy)
  {
    if (policy != HORIZONTAL_SCROLLBAR_AS_NEEDED &&
        policy != HORIZONTAL_SCROLLBAR_NEVER &&
        policy != HORIZONTAL_SCROLLBAR_ALWAYS)
      throw new IllegalArgumentException("Illegal Scrollbar 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;
  }

  /**
   * Returns the Component at the specified corner.
   * @param key the corner.
   * @return the Component at the specified corner, or null if
   * key is not one of the four valid corners.
   */
  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;
  }

  public Dimension preferredLayoutSize(Container parent)
  {
    // Sun's implementation simply throws a ClassCastException if
    // parent is no JScrollPane, so do we.
    JScrollPane sc = (JScrollPane) parent;
    Dimension viewportSize = viewport.getPreferredSize();
    Dimension viewSize = viewport.getViewSize();
    int width = viewportSize.width;
    int height = viewportSize.height;

    // horizontal scrollbar needed if the view's preferred width
    // is larger than the viewport's preferred width
    if (hsb != null && viewSize.width > viewportSize.width)
      height += hsb.getPreferredSize().height;

    // vertical scrollbar needed if the view's preferred height
    // is larger than the viewport's preferred height
    if (vsb != null && viewSize.height > viewportSize.height)
      width += vsb.getPreferredSize().width;
    if (rowHead != null && rowHead.isVisible())
      width += rowHead.getPreferredSize().width;
    if (colHead != null && colHead.isVisible())
      height += colHead.getPreferredSize().height;

    // Add insets of viewportBorder if present.
    Border vpBorder = sc.getViewportBorder();
    if (vpBorder != null)
      {
        Insets i = vpBorder.getBorderInsets(sc);
        width += i.left + i.right;
        height += i.top + i.bottom;
      }

    Insets i = sc.getInsets();
    return new Dimension(width + i.left + i.right,
                         height + i.left + i.right);
  }

  public Dimension minimumLayoutSize(Container parent)
  {
    // Sun's implementation simply throws a ClassCastException if
    // parent is no JScrollPane, so do we.
    JScrollPane sc = (JScrollPane) parent;
    Insets i = sc.getInsets();
    Dimension viewportMinSize = sc.getViewport().getMinimumSize();

    int width = i.left + i.right + viewportMinSize.width;
    if (sc.getVerticalScrollBarPolicy()
        != JScrollPane.VERTICAL_SCROLLBAR_NEVER)
      width += sc.getVerticalScrollBar().getMinimumSize().width;

    int height = i.top + i.bottom + viewportMinSize.height;
    if (sc.getHorizontalScrollBarPolicy()
        != JScrollPane.HORIZONTAL_SCROLLBAR_NEVER)
      height += sc.getHorizontalScrollBar().getMinimumSize().height;

    // Add insets of viewportBorder if present.
    Border vpBorder = sc.getViewportBorder();
    if (vpBorder != null)
      {
        i = vpBorder.getBorderInsets(sc);
        width += i.left + i.right;
        height += i.top + i.bottom;
      }

    return new Dimension(width, height);
  }

  /**
   *
   *     +----+--------------------+----+ 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)
  {
    // Sun's implementation simply throws a ClassCastException if
    // parent is no JScrollPane, so do we.
    JScrollPane sc = (JScrollPane) parent;
    JViewport viewport = sc.getViewport();
    Component view = viewport.getView();

    // If there is no view in the viewport, there is no work to be done.
    if (view == null)
      return;

    Dimension viewSize = viewport.getView().getPreferredSize();

    int x1 = 0, x2 = 0, x3 = 0, x4 = 0;
    int y1 = 0, y2 = 0, y3 = 0, y4 = 0;
    Rectangle scrollPaneBounds = SwingUtilities.calculateInnerArea(sc, null);

    // If there is a viewportBorder, remove its insets from the available
    // space.
    Border vpBorder = sc.getViewportBorder();
    Insets vpi;
    if (vpBorder != null)
      vpi = vpBorder.getBorderInsets(sc);
    else
      vpi = new Insets(0, 0, 0, 0);

    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();

    int vsWidth = 0;
    int hsHeight = 0;

    boolean showVsb =
      (vsb != null)
      && ((vsbPolicy == VERTICAL_SCROLLBAR_ALWAYS)
          || (vsbPolicy == VERTICAL_SCROLLBAR_AS_NEEDED
              && viewSize.height > (y4 - y2)));

    if (showVsb)
      vsWidth = vsb.getPreferredSize().width;

    // The horizontal scroll bar may become necessary if the vertical scroll
    // bar appears, reducing the space, left for the component.

    boolean showHsb =
      (hsb != null)
      && ((hsbPolicy == HORIZONTAL_SCROLLBAR_ALWAYS)
          || (hsbPolicy == HORIZONTAL_SCROLLBAR_AS_NEEDED
              && viewSize.width > (x4 - x2 - vsWidth)));

    if (showHsb)
      hsHeight = hsb.getPreferredSize().height;

    // If the horizontal scroll bar appears, and the vertical scroll bar
    // was not necessary assuming that there is no horizontal scroll bar,
    // the vertical scroll bar may become necessary because the horizontal
    // scroll bar reduces the vertical space for the component.
    if (!showVsb)
      {
        showVsb =
          (vsb != null)
          && ((vsbPolicy == VERTICAL_SCROLLBAR_ALWAYS)
              || (vsbPolicy == VERTICAL_SCROLLBAR_AS_NEEDED
                  && viewSize.height > (y4 - y2)));

        if (showVsb)
          vsWidth = vsb.getPreferredSize().width;
      }

    x3 = x4 - vsWidth;
    y3 = y4 - hsHeight;

    // now set the layout
    if (viewport != null)
      viewport.setBounds(new Rectangle(x2 + vpi.left, y2 + vpi.top,
                                       x3 - x2 - vpi.left - vpi.right,
                                       y3 - y2 - vpi.top - vpi.bottom));

    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;
  }


}