jdk/src/share/classes/sun/awt/VerticalBagLayout.java
author never
Mon, 12 Jul 2010 22:27:18 -0700
changeset 5926 a36f90d986b6
parent 5506 202f599c92aa
permissions -rw-r--r--
6968385: malformed xml in sweeper logging Reviewed-by: kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1995, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.awt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * A vertical 'bag' of Components.  Allocates space for each Component from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * top to bottom.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * @author      Herb Jellinek
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
public class VerticalBagLayout implements LayoutManager {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    int vgap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
     * Constructs a new VerticalBagLayout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    public VerticalBagLayout() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        this(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * Constructs a VerticalBagLayout with the specified gaps.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * @param vgap the vertical gap
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    public VerticalBagLayout(int vgap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        this.vgap = vgap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * Adds the specified named component to the layout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * @param name the String name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * @param comp the component to be added
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    public void addLayoutComponent(String name, Component comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * Removes the specified component from the layout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * @param comp the component to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    public void removeLayoutComponent(Component comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * Returns the minimum dimensions needed to lay out the components
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * contained in the specified target container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * @param target the Container on which to do the layout
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * @see Container
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * @see #preferredLayoutSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public Dimension minimumLayoutSize(Container target) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        Dimension dim = new Dimension();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        int nmembers = target.countComponents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        for (int i = 0; i < nmembers; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            Component comp = target.getComponent(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            if (comp.isVisible()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                Dimension d = comp.minimumSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                dim.width = Math.max(d.width, dim.width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                dim.height += d.height + vgap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        Insets insets = target.insets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        dim.width += insets.left + insets.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        dim.height += insets.top + insets.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        return dim;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * Returns the preferred dimensions for this layout given the components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * in the specified target container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @param target the component which needs to be laid out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * @see Container
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @see #minimumLayoutSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    public Dimension preferredLayoutSize(Container target) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        Dimension dim = new Dimension();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        int nmembers = target.countComponents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        for (int i = 0; i < nmembers; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            Component comp = target.getComponent(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            if (true || comp.isVisible()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                Dimension d = comp.preferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                dim.width = Math.max(d.width, dim.width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                dim.height += d.height + vgap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        Insets insets = target.insets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        dim.width += insets.left + insets.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        dim.height += insets.top + insets.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        return dim;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * Lays out the specified container. This method will actually reshape the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * components in the specified target container in order to satisfy the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * constraints of the VerticalBagLayout object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @param target the component being laid out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @see Container
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    public void layoutContainer(Container target) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        Insets insets = target.insets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        int top = insets.top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        int bottom = target.size().height - insets.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        int left = insets.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        int right = target.size().width - insets.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        int nmembers = target.countComponents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        for (int i = 0; i < nmembers; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            Component comp = target.getComponent(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            if (comp.isVisible()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                int compHeight = comp.size().height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                comp.resize(right - left, compHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                Dimension d = comp.preferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                comp.reshape(left, top, right - left, d.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                top += d.height + vgap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * Returns the String representation of this VerticalBagLayout's values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        return getClass().getName() + "[vgap=" + vgap + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
}