jdk/src/java.desktop/share/classes/javax/swing/BoxLayout.java
author aghaisas
Mon, 10 Jul 2017 14:55:29 +0530
changeset 47151 362dcbee0613
parent 45648 87c997b74bb8
permissions -rw-r--r--
6919529: NPE from MultiUIDefaults.getUIError Reviewed-by: aghaisas, psadhukhan, serb Contributed-by: shashidhara.veerabhadraiah@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
45025
9ad3afa82b5e 8179596: Update java.desktop to be HTML-5 friendly
serb
parents: 32283
diff changeset
     2
 * Copyright (c) 1997, 2017, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
package javax.swing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.beans.ConstructorProperties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.PrintStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * A layout manager that allows multiple components to be laid out either
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * vertically or horizontally. The components will not wrap so, for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * example, a vertical arrangement of components will stay vertically
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * arranged when the frame is resized.
45648
87c997b74bb8 8180326: Update the tables in java.desktop to be HTML-5 friendly
serb
parents: 45025
diff changeset
    39
 * <table class="borderless" style="float:right">
87c997b74bb8 8180326: Update the tables in java.desktop to be HTML-5 friendly
serb
parents: 45025
diff changeset
    40
 * <caption>Example:</caption>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *    <TR>
45025
9ad3afa82b5e 8179596: Update java.desktop to be HTML-5 friendly
serb
parents: 32283
diff changeset
    42
 *      <TD style="text-align:center">
20458
f2423fb3fd19 8025840: Fix all the doclint warnings about trademark
cl
parents: 20455
diff changeset
    43
 *         <P STYLE="TEXT-ALIGN:CENTER"><IMG SRC="doc-files/BoxLayout-1.gif"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *          alt="The following text describes this graphic."
20458
f2423fb3fd19 8025840: Fix all the doclint warnings about trademark
cl
parents: 20455
diff changeset
    45
 *          WIDTH="191" HEIGHT="201" STYLE="FLOAT:BOTTOM; BORDER:0">
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *      </TD>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *    </TR>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * </TABLE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * Nesting multiple panels with different combinations of horizontal and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * vertical gives an effect similar to GridBagLayout, without the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * complexity. The diagram shows two panels arranged horizontally, each
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * of which contains 3 components arranged vertically.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <p> The BoxLayout manager is constructed with an axis parameter that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * specifies the type of layout that will be done. There are four choices:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
32283
1a96ab120a48 8133134: docs: replace <tt> tags (obsolete in html5) for java.desktop
avstepan
parents: 25859
diff changeset
    58
 * <blockquote><b>{@code X_AXIS}</b> - Components are laid out horizontally
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * from left to right.</blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
32283
1a96ab120a48 8133134: docs: replace <tt> tags (obsolete in html5) for java.desktop
avstepan
parents: 25859
diff changeset
    61
 * <blockquote><b>{@code Y_AXIS}</b> - Components are laid out vertically
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * from top to bottom.</blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *
32283
1a96ab120a48 8133134: docs: replace <tt> tags (obsolete in html5) for java.desktop
avstepan
parents: 25859
diff changeset
    64
 * <blockquote><b>{@code LINE_AXIS}</b> - Components are laid out the way
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * words are laid out in a line, based on the container's
32283
1a96ab120a48 8133134: docs: replace <tt> tags (obsolete in html5) for java.desktop
avstepan
parents: 25859
diff changeset
    66
 * {@code ComponentOrientation} property. If the container's
1a96ab120a48 8133134: docs: replace <tt> tags (obsolete in html5) for java.desktop
avstepan
parents: 25859
diff changeset
    67
 * {@code ComponentOrientation} is horizontal then components are laid out
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * horizontally, otherwise they are laid out vertically.  For horizontal
32283
1a96ab120a48 8133134: docs: replace <tt> tags (obsolete in html5) for java.desktop
avstepan
parents: 25859
diff changeset
    69
 * orientations, if the container's {@code ComponentOrientation} is left to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * right then components are laid out left to right, otherwise they are laid
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * out right to left. For vertical orientations components are always laid out
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * from top to bottom.</blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *
32283
1a96ab120a48 8133134: docs: replace <tt> tags (obsolete in html5) for java.desktop
avstepan
parents: 25859
diff changeset
    74
 * <blockquote><b>{@code PAGE_AXIS}</b> - Components are laid out the way
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * text lines are laid out on a page, based on the container's
32283
1a96ab120a48 8133134: docs: replace <tt> tags (obsolete in html5) for java.desktop
avstepan
parents: 25859
diff changeset
    76
 * {@code ComponentOrientation} property. If the container's
1a96ab120a48 8133134: docs: replace <tt> tags (obsolete in html5) for java.desktop
avstepan
parents: 25859
diff changeset
    77
 * {@code ComponentOrientation} is horizontal then components are laid out
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * vertically, otherwise they are laid out horizontally.  For horizontal
32283
1a96ab120a48 8133134: docs: replace <tt> tags (obsolete in html5) for java.desktop
avstepan
parents: 25859
diff changeset
    79
 * orientations, if the container's {@code ComponentOrientation} is left to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * right then components are laid out left to right, otherwise they are laid
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * out right to left.&nbsp; For vertical orientations components are always
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * laid out from top to bottom.</blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * For all directions, components are arranged in the same order as they were
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * added to the container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * BoxLayout attempts to arrange components
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * at their preferred widths (for horizontal layout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * or heights (for vertical layout).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * For a horizontal layout,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * if not all the components are the same height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * BoxLayout attempts to make all the components
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * as high as the highest component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * If that's not possible for a particular component,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * then BoxLayout aligns that component vertically,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * according to the component's Y alignment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * By default, a component has a Y alignment of 0.5,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * which means that the vertical center of the component
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * should have the same Y coordinate as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * the vertical centers of other components with 0.5 Y alignment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * Similarly, for a vertical layout,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * BoxLayout attempts to make all components in the column
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * as wide as the widest component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * If that fails, it aligns them horizontally
32283
1a96ab120a48 8133134: docs: replace <tt> tags (obsolete in html5) for java.desktop
avstepan
parents: 25859
diff changeset
   106
 * according to their X alignments.  For {@code PAGE_AXIS} layout,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * horizontal alignment is done based on the leading edge of the component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * In other words, an X alignment value of 0.0 means the left edge of a
32283
1a96ab120a48 8133134: docs: replace <tt> tags (obsolete in html5) for java.desktop
avstepan
parents: 25859
diff changeset
   109
 * component if the container's {@code ComponentOrientation} is left to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * right and it means the right edge of the component otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * Instead of using BoxLayout directly, many programs use the Box class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * The Box class is a lightweight container that uses a BoxLayout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * It also provides handy methods to help you use BoxLayout well.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * Adding components to multiple nested boxes is a powerful way to get
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * the arrangement you want.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * For further information and examples see
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * <a
20455
f6f9a0c2796b 8020688: Broken links in documentation at http://docs.oracle.com/javase/6/docs/api/index.
mcherkas
parents: 11268
diff changeset
   120
 href="http://docs.oracle.com/javase/tutorial/uiswing/layout/box.html">How to Use BoxLayout</a>,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * a section in <em>The Java Tutorial.</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * the same version of Swing.  As of 1.4, support for long term storage
20458
f2423fb3fd19 8025840: Fix all the doclint warnings about trademark
cl
parents: 20455
diff changeset
   128
 * of all JavaBeans&trade;
32283
1a96ab120a48 8133134: docs: replace <tt> tags (obsolete in html5) for java.desktop
avstepan
parents: 25859
diff changeset
   129
 * has been added to the {@code java.beans} package.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * @see Box
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * @see java.awt.ComponentOrientation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * @see JComponent#getAlignmentX
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * @see JComponent#getAlignmentY
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 * @author   Timothy Prinzing
25201
4adc75e0c4e5 8046485: Add missing @since tag under javax.swing.*
henryjen
parents: 23010
diff changeset
   138
 * @since 1.2
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 */
11268
f0e59c4852de 7116950: Reduce number of warnings in swing
alexsch
parents: 5506
diff changeset
   140
@SuppressWarnings("serial")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
public class BoxLayout implements LayoutManager2, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * Specifies that components should be laid out left to right.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    public static final int X_AXIS = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * Specifies that components should be laid out top to bottom.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    public static final int Y_AXIS = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * Specifies that components should be laid out in the direction of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * a line of text as determined by the target container's
32283
1a96ab120a48 8133134: docs: replace <tt> tags (obsolete in html5) for java.desktop
avstepan
parents: 25859
diff changeset
   156
     * {@code ComponentOrientation} property.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    public static final int LINE_AXIS = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * Specifies that components should be laid out in the direction that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * lines flow across a page as determined by the target container's
32283
1a96ab120a48 8133134: docs: replace <tt> tags (obsolete in html5) for java.desktop
avstepan
parents: 25859
diff changeset
   163
     * {@code ComponentOrientation} property.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    public static final int PAGE_AXIS = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * Creates a layout manager that will lay out components along the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * given axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @param target  the container that needs to be laid out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @param axis  the axis to lay out components along. Can be one of:
32283
1a96ab120a48 8133134: docs: replace <tt> tags (obsolete in html5) for java.desktop
avstepan
parents: 25859
diff changeset
   173
     *              {@code BoxLayout.X_AXIS, BoxLayout.Y_AXIS,
1a96ab120a48 8133134: docs: replace <tt> tags (obsolete in html5) for java.desktop
avstepan
parents: 25859
diff changeset
   174
     *              BoxLayout.LINE_AXIS} or {@code BoxLayout.PAGE_AXIS}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *
32283
1a96ab120a48 8133134: docs: replace <tt> tags (obsolete in html5) for java.desktop
avstepan
parents: 25859
diff changeset
   176
     * @exception AWTError  if the value of {@code axis} is invalid
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    @ConstructorProperties({"target", "axis"})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    public BoxLayout(Container target, int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        if (axis != X_AXIS && axis != Y_AXIS &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            axis != LINE_AXIS && axis != PAGE_AXIS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            throw new AWTError("Invalid axis");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        this.axis = axis;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        this.target = target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * Constructs a BoxLayout that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * produces debugging messages.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @param target  the container that needs to be laid out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * @param axis  the axis to lay out components along. Can be one of:
32283
1a96ab120a48 8133134: docs: replace <tt> tags (obsolete in html5) for java.desktop
avstepan
parents: 25859
diff changeset
   194
     *              {@code BoxLayout.X_AXIS, BoxLayout.Y_AXIS,
1a96ab120a48 8133134: docs: replace <tt> tags (obsolete in html5) for java.desktop
avstepan
parents: 25859
diff changeset
   195
     *              BoxLayout.LINE_AXIS} or {@code BoxLayout.PAGE_AXIS}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @param dbg  the stream to which debugging messages should be sent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     *   null if none
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    BoxLayout(Container target, int axis, PrintStream dbg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        this(target, axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        this.dbg = dbg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * Returns the container that uses this layout manager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * @return the container that uses this layout manager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    public final Container getTarget() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        return this.target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * Returns the axis that was used to lay out components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * Returns one of:
32283
1a96ab120a48 8133134: docs: replace <tt> tags (obsolete in html5) for java.desktop
avstepan
parents: 25859
diff changeset
   219
     * {@code BoxLayout.X_AXIS, BoxLayout.Y_AXIS,
1a96ab120a48 8133134: docs: replace <tt> tags (obsolete in html5) for java.desktop
avstepan
parents: 25859
diff changeset
   220
     * BoxLayout.LINE_AXIS} or {@code BoxLayout.PAGE_AXIS}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * @return the axis that was used to lay out components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    public final int getAxis() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        return this.axis;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * Indicates that a child has changed its layout related information,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * and thus any cached calculations should be flushed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * This method is called by AWT when the invalidate method is called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * on the Container.  Since the invalidate method may be called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * asynchronously to the event thread, this method may be called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * asynchronously.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * @param target  the affected container
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * @exception AWTError  if the target isn't the container specified to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     *                      BoxLayout constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    public synchronized void invalidateLayout(Container target) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        checkContainer(target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        xChildren = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        yChildren = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        xTotal = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        yTotal = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * Not used by this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * @param name the name of the component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * @param comp the component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    public void addLayoutComponent(String name, Component comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        invalidateLayout(comp.getParent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * Not used by this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * @param comp the component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    public void removeLayoutComponent(Component comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        invalidateLayout(comp.getParent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * Not used by this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * @param comp the component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * @param constraints constraints
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    public void addLayoutComponent(Component comp, Object constraints) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        invalidateLayout(comp.getParent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * Returns the preferred dimensions for this layout, given the components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * in the specified target container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * @param target  the container that needs to be laid out
21592
da6abe91602a 8025234: [javadoc] fix some errors in javax.swing.**
yan
parents: 21278
diff changeset
   286
     * @return the dimensions &gt;= 0 &amp;&amp; &lt;= Integer.MAX_VALUE
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * @exception AWTError  if the target isn't the container specified to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     *                      BoxLayout constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * @see Container
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * @see #minimumLayoutSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * @see #maximumLayoutSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    public Dimension preferredLayoutSize(Container target) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        Dimension size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            checkContainer(target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            checkRequests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            size = new Dimension(xTotal.preferred, yTotal.preferred);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        Insets insets = target.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        size.width = (int) Math.min((long) size.width + (long) insets.left + (long) insets.right, Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        size.height = (int) Math.min((long) size.height + (long) insets.top + (long) insets.bottom, Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * Returns the minimum dimensions needed to lay out the components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * contained in the specified target container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * @param target  the container that needs to be laid out
21592
da6abe91602a 8025234: [javadoc] fix some errors in javax.swing.**
yan
parents: 21278
diff changeset
   312
     * @return the dimensions &gt;= 0 &amp;&amp; &lt;= Integer.MAX_VALUE
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * @exception AWTError  if the target isn't the container specified to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     *                      BoxLayout constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * @see #preferredLayoutSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * @see #maximumLayoutSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    public Dimension minimumLayoutSize(Container target) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        Dimension size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            checkContainer(target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            checkRequests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            size = new Dimension(xTotal.minimum, yTotal.minimum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        Insets insets = target.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        size.width = (int) Math.min((long) size.width + (long) insets.left + (long) insets.right, Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        size.height = (int) Math.min((long) size.height + (long) insets.top + (long) insets.bottom, Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * Returns the maximum dimensions the target container can use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * to lay out the components it contains.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * @param target  the container that needs to be laid out
21592
da6abe91602a 8025234: [javadoc] fix some errors in javax.swing.**
yan
parents: 21278
diff changeset
   337
     * @return the dimensions &gt;= 0 &amp;&amp; &lt;= Integer.MAX_VALUE
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * @exception AWTError  if the target isn't the container specified to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     *                      BoxLayout constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * @see #preferredLayoutSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * @see #minimumLayoutSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    public Dimension maximumLayoutSize(Container target) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        Dimension size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            checkContainer(target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            checkRequests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            size = new Dimension(xTotal.maximum, yTotal.maximum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        Insets insets = target.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        size.width = (int) Math.min((long) size.width + (long) insets.left + (long) insets.right, Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        size.height = (int) Math.min((long) size.height + (long) insets.top + (long) insets.bottom, Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * Returns the alignment along the X axis for the container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * If the box is horizontal, the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * alignment will be returned. Otherwise, the alignment needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * to place the children along the X axis will be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * @param target  the container
21592
da6abe91602a 8025234: [javadoc] fix some errors in javax.swing.**
yan
parents: 21278
diff changeset
   364
     * @return the alignment &gt;= 0.0f &amp;&amp; &lt;= 1.0f
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * @exception AWTError  if the target isn't the container specified to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     *                      BoxLayout constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    public synchronized float getLayoutAlignmentX(Container target) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        checkContainer(target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        checkRequests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        return xTotal.alignment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * Returns the alignment along the Y axis for the container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * If the box is vertical, the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * alignment will be returned. Otherwise, the alignment needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * to place the children along the Y axis will be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * @param target  the container
21592
da6abe91602a 8025234: [javadoc] fix some errors in javax.swing.**
yan
parents: 21278
diff changeset
   381
     * @return the alignment &gt;= 0.0f &amp;&amp; &lt;= 1.0f
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * @exception AWTError  if the target isn't the container specified to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     *                      BoxLayout constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    public synchronized float getLayoutAlignmentY(Container target) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        checkContainer(target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        checkRequests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        return yTotal.alignment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * Called by the AWT <!-- XXX CHECK! --> when the specified container
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * needs to be laid out.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * @param target  the container to lay out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * @exception AWTError  if the target isn't the container specified to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     *                      BoxLayout constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    public void layoutContainer(Container target) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        checkContainer(target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        int nChildren = target.getComponentCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        int[] xOffsets = new int[nChildren];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        int[] xSpans = new int[nChildren];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        int[] yOffsets = new int[nChildren];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        int[] ySpans = new int[nChildren];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        Dimension alloc = target.getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        Insets in = target.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        alloc.width -= in.left + in.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        alloc.height -= in.top + in.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        // Resolve axis to an absolute value (either X_AXIS or Y_AXIS)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        ComponentOrientation o = target.getComponentOrientation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        int absoluteAxis = resolveAxis( axis, o );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        boolean ltr = (absoluteAxis != axis) ? o.isLeftToRight() : true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        // determine the child placements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            checkRequests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            if (absoluteAxis == X_AXIS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                SizeRequirements.calculateTiledPositions(alloc.width, xTotal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                                                         xChildren, xOffsets,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                                                         xSpans, ltr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                SizeRequirements.calculateAlignedPositions(alloc.height, yTotal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                                                           yChildren, yOffsets,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                                                           ySpans);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                SizeRequirements.calculateAlignedPositions(alloc.width, xTotal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                                                           xChildren, xOffsets,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                                                           xSpans, ltr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                SizeRequirements.calculateTiledPositions(alloc.height, yTotal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                                                         yChildren, yOffsets,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                                                         ySpans);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        // flush changes to the container
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        for (int i = 0; i < nChildren; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            Component c = target.getComponent(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            c.setBounds((int) Math.min((long) in.left + (long) xOffsets[i], Integer.MAX_VALUE),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                        (int) Math.min((long) in.top + (long) yOffsets[i], Integer.MAX_VALUE),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                        xSpans[i], ySpans[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        if (dbg != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            for (int i = 0; i < nChildren; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                Component c = target.getComponent(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                dbg.println(c.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                dbg.println("X: " + xChildren[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                dbg.println("Y: " + yChildren[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    void checkContainer(Container target) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        if (this.target != target) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            throw new AWTError("BoxLayout can't be shared");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    void checkRequests() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        if (xChildren == null || yChildren == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            // The requests have been invalidated... recalculate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            // the request information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            int n = target.getComponentCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            xChildren = new SizeRequirements[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            yChildren = new SizeRequirements[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                Component c = target.getComponent(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                if (!c.isVisible()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                    xChildren[i] = new SizeRequirements(0,0,0, c.getAlignmentX());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                    yChildren[i] = new SizeRequirements(0,0,0, c.getAlignmentY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                Dimension min = c.getMinimumSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                Dimension typ = c.getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                Dimension max = c.getMaximumSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                xChildren[i] = new SizeRequirements(min.width, typ.width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                                                    max.width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                                                    c.getAlignmentX());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                yChildren[i] = new SizeRequirements(min.height, typ.height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                                                    max.height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                                                    c.getAlignmentY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            // Resolve axis to an absolute value (either X_AXIS or Y_AXIS)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            int absoluteAxis = resolveAxis(axis,target.getComponentOrientation());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            if (absoluteAxis == X_AXIS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                xTotal = SizeRequirements.getTiledSizeRequirements(xChildren);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                yTotal = SizeRequirements.getAlignedSizeRequirements(yChildren);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                xTotal = SizeRequirements.getAlignedSizeRequirements(xChildren);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                yTotal = SizeRequirements.getTiledSizeRequirements(yChildren);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * Given one of the 4 axis values, resolve it to an absolute axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * The relative axis values, PAGE_AXIS and LINE_AXIS are converted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * to their absolute couterpart given the target's ComponentOrientation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * value.  The absolute axes, X_AXIS and Y_AXIS are returned unmodified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * @param axis the axis to resolve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * @param o the ComponentOrientation to resolve against
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * @return the resolved axis
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    private int resolveAxis( int axis, ComponentOrientation o ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        int absoluteAxis;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        if( axis == LINE_AXIS ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            absoluteAxis = o.isHorizontal() ? X_AXIS : Y_AXIS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        } else if( axis == PAGE_AXIS ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            absoluteAxis = o.isHorizontal() ? Y_AXIS : X_AXIS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            absoluteAxis = axis;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        return absoluteAxis;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    private int axis;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    private Container target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    private transient SizeRequirements[] xChildren;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    private transient SizeRequirements[] yChildren;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    private transient SizeRequirements xTotal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    private transient SizeRequirements yTotal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    private transient PrintStream dbg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
}