jdk/src/share/classes/javax/swing/GroupLayout.java
author malenkov
Tue, 29 Oct 2013 17:01:06 +0400
changeset 21278 ef8a3a2a72f2
parent 21244 7c2ac5ca05a2
child 21592 da6abe91602a
permissions -rw-r--r--
8022746: List of spelling errors in API doc Reviewed-by: alexsch, smarks
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 7160
diff changeset
     2
 * Copyright (c) 2006, 2010, 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: 1639
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: 1639
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: 1639
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.awt.Component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.Container;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.Dimension;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.Insets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.LayoutManager2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import static java.awt.Component.BaselineResizeBehavior;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import static javax.swing.LayoutStyle.ComponentPlacement;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import static javax.swing.SwingConstants.HORIZONTAL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import static javax.swing.SwingConstants.VERTICAL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * {@code GroupLayout} is a {@code LayoutManager} that hierarchically
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * groups components in order to position them in a {@code Container}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * {@code GroupLayout} is intended for use by builders, but may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * hand-coded as well.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * Grouping is done by instances of the {@link Group Group} class. {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * GroupLayout} supports two types of groups. A sequential group
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * positions its child elements sequentially, one after another. A
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * parallel group aligns its child elements in one of four ways.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * Each group may contain any number of elements, where an element is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * a {@code Group}, {@code Component}, or gap. A gap can be thought
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * of as an invisible component with a minimum, preferred and maximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * size. In addition {@code GroupLayout} supports a preferred gap,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * whose value comes from {@code LayoutStyle}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * Elements are similar to a spring. Each element has a range as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * specified by a minimum, preferred and maximum.  Gaps have either a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * developer-specified range, or a range determined by {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * LayoutStyle}. The range for {@code Component}s is determined from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * the {@code Component}'s {@code getMinimumSize}, {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * getPreferredSize} and {@code getMaximumSize} methods. In addition,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * when adding {@code Component}s you may specify a particular range
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * to use instead of that from the component. The range for a {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * Group} is determined by the type of group. A {@code ParallelGroup}'s
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * range is the maximum of the ranges of its elements. A {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * SequentialGroup}'s range is the sum of the ranges of its elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * {@code GroupLayout} treats each axis independently.  That is, there
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * is a group representing the horizontal axis, and a group
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * representing the vertical axis.  The horizontal group is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * responsible for determining the minimum, preferred and maximum size
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * along the horizontal axis as well as setting the x and width of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * components contained in it. The vertical group is responsible for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * determining the minimum, preferred and maximum size along the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * vertical axis as well as setting the y and height of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * components contained in it. Each {@code Component} must exist in both
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * a horizontal and vertical group, otherwise an {@code IllegalStateException}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * is thrown during layout, or when the minimum, preferred or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * maximum size is requested.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * The following diagram shows a sequential group along the horizontal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * axis. The sequential group contains three components. A parallel group
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * was used along the vertical axis.
21244
7c2ac5ca05a2 8026021: more fix of javadoc errors and warnings reported by doclint, see the description
cl
parents: 7668
diff changeset
    82
 * <p style="text-align:center">
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * <img src="doc-files/groupLayout.1.gif">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * To reinforce that each axis is treated independently the diagram shows
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * the range of each group and element along each axis. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * range of each component has been projected onto the axes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * and the groups are rendered in blue (horizontal) and red (vertical).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * For readability there is a gap between each of the elements in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * sequential group.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * The sequential group along the horizontal axis is rendered as a solid
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * blue line. Notice the sequential group is the sum of the children elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * it contains.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * Along the vertical axis the parallel group is the maximum of the height
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * of each of the components. As all three components have the same height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * the parallel group has the same height.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * The following diagram shows the same three components, but with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * parallel group along the horizontal axis and the sequential group along
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * the vertical axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * <p>
21244
7c2ac5ca05a2 8026021: more fix of javadoc errors and warnings reported by doclint, see the description
cl
parents: 7668
diff changeset
   104
 * <p style="text-align:center">
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * <img src="doc-files/groupLayout.2.gif">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * As {@code c1} is the largest of the three components, the parallel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * group is sized to {@code c1}. As {@code c2} and {@code c3} are smaller
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * than {@code c1} they are aligned based on the alignment specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * for the component (if specified) or the default alignment of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * parallel group. In the diagram {@code c2} and {@code c3} were created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * with an alignment of {@code LEADING}. If the component orientation were
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * right-to-left then {@code c2} and {@code c3} would be positioned on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * the opposite side.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * The following diagram shows a sequential group along both the horizontal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * and vertical axis.
21244
7c2ac5ca05a2 8026021: more fix of javadoc errors and warnings reported by doclint, see the description
cl
parents: 7668
diff changeset
   118
 * <p style="text-align:center">
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * <img src="doc-files/groupLayout.3.gif">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * {@code GroupLayout} provides the ability to insert gaps between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * {@code Component}s. The size of the gap is determined by an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * instance of {@code LayoutStyle}. This may be turned on using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * {@code setAutoCreateGaps} method.  Similarly, you may use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * the {@code setAutoCreateContainerGaps} method to insert gaps
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * between components that touch the edge of the parent container and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * The following builds a panel consisting of two labels in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * one column, followed by two textfields in the next column:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 *   JComponent panel = ...;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 *   GroupLayout layout = new GroupLayout(panel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 *   panel.setLayout(layout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 *   // Turn on automatically adding gaps between components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 *   layout.setAutoCreateGaps(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 *   // Turn on automatically creating gaps between components that touch
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 *   // the edge of the container and the container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 *   layout.setAutoCreateContainerGaps(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 *   // Create a sequential group for the horizontal axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 *   GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 *   // The sequential group in turn contains two parallel groups.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 *   // One parallel group contains the labels, the other the text fields.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 *   // Putting the labels in a parallel group along the horizontal axis
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 *   // positions them at the same x location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 *   //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 *   // Variable indentation is used to reinforce the level of grouping.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 *   hGroup.addGroup(layout.createParallelGroup().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 *            addComponent(label1).addComponent(label2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 *   hGroup.addGroup(layout.createParallelGroup().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 *            addComponent(tf1).addComponent(tf2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 *   layout.setHorizontalGroup(hGroup);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 *   // Create a sequential group for the vertical axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 *   GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 *   // The sequential group contains two parallel groups that align
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
 *   // the contents along the baseline. The first parallel group contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
 *   // the first label and text field, and the second parallel group contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
 *   // the second label and text field. By using a sequential group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
 *   // the labels and text fields are positioned vertically after one another.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
 *   vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
 *            addComponent(label1).addComponent(tf1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
 *   vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
 *            addComponent(label2).addComponent(tf2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
 *   layout.setVerticalGroup(vGroup);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
 * When run the following is produced.
21244
7c2ac5ca05a2 8026021: more fix of javadoc errors and warnings reported by doclint, see the description
cl
parents: 7668
diff changeset
   175
 * <p style="text-align:center">
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
 * <img src="doc-files/groupLayout.example.png">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
 * This layout consists of the following.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
 * <ul><li>The horizontal axis consists of a sequential group containing two
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
 *         parallel groups.  The first parallel group contains the labels,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
 *         and the second parallel group contains the text fields.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
 *     <li>The vertical axis consists of a sequential group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
 *         containing two parallel groups.  The parallel groups are configured
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
 *         to align their components along the baseline. The first parallel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
 *         group contains the first label and first text field, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
 *         the second group consists of the second label and second
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
 *         text field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
 * There are a couple of things to notice in this code:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
 *   <li>You need not explicitly add the components to the container; this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
 *       is indirectly done by using one of the {@code add} methods of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
 *       {@code Group}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
 *   <li>The various {@code add} methods return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
 *       the caller.  This allows for easy chaining of invocations.  For
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
 *       example, {@code group.addComponent(label1).addComponent(label2);} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
 *       equivalent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
 *       {@code group.addComponent(label1); group.addComponent(label2);}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
 *   <li>There are no public constructors for {@code Group}s; instead
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
 *       use the create methods of {@code GroupLayout}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
 * @author Tomas Pavek
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
 * @author Jan Stola
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
 * @author Scott Violet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
public class GroupLayout implements LayoutManager2 {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    // Used in size calculations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    private static final int MIN_SIZE = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    private static final int PREF_SIZE = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    private static final int MAX_SIZE = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    // Used by prepare, indicates min, pref or max isn't going to be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    private static final int SPECIFIC_SIZE = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    private static final int UNSET = Integer.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * Indicates the size from the component or gap should be used for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * particular range value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @see Group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    public static final int DEFAULT_SIZE = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * Indicates the preferred size from the component or gap should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * be used for a particular range value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * @see Group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    public static final int PREFERRED_SIZE = -2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    // Whether or not we automatically try and create the preferred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    // padding between components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    private boolean autocreatePadding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    // Whether or not we automatically try and create the preferred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    // padding between components the touch the edge of the container and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    // the container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    private boolean autocreateContainerPadding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * Group responsible for layout along the horizontal axis.  This is NOT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * the user specified group, use getHorizontalGroup to dig that out.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    private Group horizontalGroup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * Group responsible for layout along the vertical axis.  This is NOT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * the user specified group, use getVerticalGroup to dig that out.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    private Group verticalGroup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    // Maps from Component to ComponentInfo.  This is used for tracking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    // information specific to a Component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    private Map<Component,ComponentInfo> componentInfos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    // Container we're doing layout for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    private Container host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    // Used by areParallelSiblings, cached to avoid excessive garbage.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    private Set<Spring> tmpParallelSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    // Indicates Springs have changed in some way since last change.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    private boolean springsChanged;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    // Indicates invalidateLayout has been invoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    private boolean isValid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    // Whether or not any preferred padding (or container padding) springs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    // exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    private boolean hasPreferredPaddingSprings;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * The LayoutStyle instance to use, if null the sharedInstance is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    private LayoutStyle layoutStyle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * If true, components that are not visible are treated as though they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * aren't there.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    private boolean honorsVisibility;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * Enumeration of the possible ways {@code ParallelGroup} can align
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * its children.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * @see #createParallelGroup(Alignment)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    public enum Alignment {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
         * Indicates the elements should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
         * aligned to the origin.  For the horizontal axis with a left to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
         * right orientation this means aligned to the left edge. For the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
         * vertical axis leading means aligned to the top edge.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
         * @see #createParallelGroup(Alignment)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        LEADING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
         * Indicates the elements should be aligned to the end of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
         * region.  For the horizontal axis with a left to right
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
         * orientation this means aligned to the right edge. For the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
         * vertical axis trailing means aligned to the bottom edge.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
         * @see #createParallelGroup(Alignment)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        TRAILING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
         * Indicates the elements should be centered in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
         * the region.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
         * @see #createParallelGroup(Alignment)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        CENTER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
         * Indicates the elements should be aligned along
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
         * their baseline.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
         * @see #createParallelGroup(Alignment)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
         * @see #createBaselineGroup(boolean,boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        BASELINE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    private static void checkSize(int min, int pref, int max,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            boolean isComponentSpring) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        checkResizeType(min, isComponentSpring);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        if (!isComponentSpring && pref < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            throw new IllegalArgumentException("Pref must be >= 0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        } else if (isComponentSpring) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            checkResizeType(pref, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        checkResizeType(max, isComponentSpring);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        checkLessThan(min, pref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        checkLessThan(pref, max);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    private static void checkResizeType(int type, boolean isComponentSpring) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        if (type < 0 && ((isComponentSpring && type != DEFAULT_SIZE &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                type != PREFERRED_SIZE) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                (!isComponentSpring && type != PREFERRED_SIZE))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            throw new IllegalArgumentException("Invalid 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
    private static void checkLessThan(int min, int max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        if (min >= 0 && max >= 0 && min > max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                    "Following is not met: min<=pref<=max");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * Creates a {@code GroupLayout} for the specified {@code Container}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * @param host the {@code Container} the {@code GroupLayout} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     *        the {@code LayoutManager} for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * @throws IllegalArgumentException if host is {@code null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    public GroupLayout(Container host) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        if (host == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            throw new IllegalArgumentException("Container must be non-null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        honorsVisibility = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        this.host = host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        setHorizontalGroup(createParallelGroup(Alignment.LEADING, true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        setVerticalGroup(createParallelGroup(Alignment.LEADING, true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        componentInfos = new HashMap<Component,ComponentInfo>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        tmpParallelSet = new HashSet<Spring>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    /**
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 21244
diff changeset
   385
     * Sets whether component visibility is considered when sizing and
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * positioning components. A value of {@code true} indicates that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * non-visible components should not be treated as part of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * layout. A value of {@code false} indicates that components should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * positioned and sized regardless of visibility.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * A value of {@code false} is useful when the visibility of components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * is dynamically adjusted and you don't want surrounding components and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * the sizing to change.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * The specified value is used for components that do not have an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * explicit visibility specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * The default is {@code true}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     *
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 21244
diff changeset
   400
     * @param honorsVisibility whether component visibility is considered when
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     *                         sizing and positioning components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * @see #setHonorsVisibility(Component,Boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    public void setHonorsVisibility(boolean honorsVisibility) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        if (this.honorsVisibility != honorsVisibility) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            this.honorsVisibility = honorsVisibility;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            springsChanged = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            isValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            invalidateHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    /**
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 21244
diff changeset
   414
     * Returns whether component visibility is considered when sizing and
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * positioning components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     *
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 21244
diff changeset
   417
     * @return whether component visibility is considered when sizing and
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     *         positioning components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    public boolean getHonorsVisibility() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        return honorsVisibility;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    /**
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 21244
diff changeset
   425
     * Sets whether the component's visibility is considered for
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * sizing and positioning. A value of {@code Boolean.TRUE}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * indicates that if {@code component} is not visible it should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * not be treated as part of the layout. A value of {@code false}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * indicates that {@code component} is positioned and sized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * regardless of it's visibility.  A value of {@code null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * indicates the value specified by the single argument method {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * setHonorsVisibility} should be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * If {@code component} is not a child of the {@code Container} this
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 21244
diff changeset
   435
     * {@code GroupLayout} is managing, it will be added to the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * {@code Container}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * @param component the component
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 21244
diff changeset
   439
     * @param honorsVisibility whether visibility of this {@code component} should be
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     *              considered for sizing and positioning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * @throws IllegalArgumentException if {@code component} is {@code null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * @see #setHonorsVisibility(Component,Boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    public void setHonorsVisibility(Component component,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            Boolean honorsVisibility) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        if (component == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            throw new IllegalArgumentException("Component must be non-null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        getComponentInfo(component).setHonorsVisibility(honorsVisibility);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        springsChanged = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        isValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        invalidateHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * Sets whether a gap between components should automatically be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * created.  For example, if this is {@code true} and you add two
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * components to a {@code SequentialGroup} a gap between the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * two components is automatically be created.  The default is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * {@code false}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * @param autoCreatePadding whether a gap between components is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     *        automatically created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    public void setAutoCreateGaps(boolean autoCreatePadding) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        if (this.autocreatePadding != autoCreatePadding) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            this.autocreatePadding = autoCreatePadding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            invalidateHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * Returns {@code true} if gaps between components are automatically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * @return {@code true} if gaps between components are automatically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     *         created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    public boolean getAutoCreateGaps() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        return autocreatePadding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * Sets whether a gap between the container and components that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * touch the border of the container should automatically be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * created. The default is {@code false}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * @param autoCreateContainerPadding whether a gap between the container and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     *        components that touch the border of the container should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     *        automatically be created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    public void setAutoCreateContainerGaps(boolean autoCreateContainerPadding){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        if (this.autocreateContainerPadding != autoCreateContainerPadding) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            this.autocreateContainerPadding = autoCreateContainerPadding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            horizontalGroup = createTopLevelGroup(getHorizontalGroup());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            verticalGroup = createTopLevelGroup(getVerticalGroup());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            invalidateHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        }
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
     * Returns {@code true} if gaps between the container and components that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * border the container are automatically created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * @return {@code true} if gaps between the container and components that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     *         border the container are automatically created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    public boolean getAutoCreateContainerGaps() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        return autocreateContainerPadding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * Sets the {@code Group} that positions and sizes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * components along the horizontal axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * @param group the {@code Group} that positions and sizes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     *        components along the horizontal axis
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * @throws IllegalArgumentException if group is {@code null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    public void setHorizontalGroup(Group group) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        if (group == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            throw new IllegalArgumentException("Group must be non-null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        horizontalGroup = createTopLevelGroup(group);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        invalidateHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * Returns the {@code Group} that positions and sizes components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * along the horizontal axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * @return the {@code Group} responsible for positioning and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     *         sizing component along the horizontal axis
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    private Group getHorizontalGroup() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        int index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        if (horizontalGroup.springs.size() > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            index = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        return (Group)horizontalGroup.springs.get(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * Sets the {@code Group} that positions and sizes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     * components along the vertical axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * @param group the {@code Group} that positions and sizes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     *        components along the vertical axis
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * @throws IllegalArgumentException if group is {@code null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    public void setVerticalGroup(Group group) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        if (group == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            throw new IllegalArgumentException("Group must be non-null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        verticalGroup = createTopLevelGroup(group);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        invalidateHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * Returns the {@code Group} that positions and sizes components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * along the vertical axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * @return the {@code Group} responsible for positioning and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     *         sizing component along the vertical axis
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    private Group getVerticalGroup() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        int index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        if (verticalGroup.springs.size() > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            index = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        return (Group)verticalGroup.springs.get(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * Wraps the user specified group in a sequential group.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * container gaps should be generated the necessary springs are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * added.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    private Group createTopLevelGroup(Group specifiedGroup) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        SequentialGroup group = createSequentialGroup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        if (getAutoCreateContainerGaps()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
            group.addSpring(new ContainerAutoPreferredGapSpring());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
            group.addGroup(specifiedGroup);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
            group.addSpring(new ContainerAutoPreferredGapSpring());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            group.addGroup(specifiedGroup);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        return group;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * Creates and returns a {@code SequentialGroup}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * @return a new {@code SequentialGroup}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    public SequentialGroup createSequentialGroup() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        return new SequentialGroup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * Creates and returns a {@code ParallelGroup} with an alignment of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * {@code Alignment.LEADING}.  This is a cover method for the more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * general {@code createParallelGroup(Alignment)} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * @return a new {@code ParallelGroup}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * @see #createParallelGroup(Alignment)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
    public ParallelGroup createParallelGroup() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        return createParallelGroup(Alignment.LEADING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * Creates and returns a {@code ParallelGroup} with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * alignment.  This is a cover method for the more general {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * createParallelGroup(Alignment,boolean)} method with {@code true}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * supplied for the second argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * @param alignment the alignment for the elements of the group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * @throws IllegalArgumentException if {@code alignment} is {@code null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * @return a new {@code ParallelGroup}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * @see #createBaselineGroup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * @see ParallelGroup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    public ParallelGroup createParallelGroup(Alignment alignment) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        return createParallelGroup(alignment, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * Creates and returns a {@code ParallelGroup} with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * alignment and resize behavior. The {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * alignment} argument specifies how children elements are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * positioned that do not fill the group. For example, if a {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * ParallelGroup} with an alignment of {@code TRAILING} is given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * 100 and a child only needs 50, the child is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * positioned at the position 50 (with a component orientation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * left-to-right).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * Baseline alignment is only useful when used along the vertical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * axis. A {@code ParallelGroup} created with a baseline alignment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * along the horizontal axis is treated as {@code LEADING}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * Refer to {@link GroupLayout.ParallelGroup ParallelGroup} for details on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * the behavior of baseline groups.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * @param alignment the alignment for the elements of the group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * @param resizable {@code true} if the group is resizable; if the group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     *        is not resizable the preferred size is used for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     *        minimum and maximum size of the group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * @throws IllegalArgumentException if {@code alignment} is {@code null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * @return a new {@code ParallelGroup}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * @see #createBaselineGroup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * @see GroupLayout.ParallelGroup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    public ParallelGroup createParallelGroup(Alignment alignment,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            boolean resizable){
7160
e610223887fb 6613904: javax.swing.GroupLayout.createParallelGroup(..) doesn't throw IllegalArgumentException for null arg
rupashka
parents: 6648
diff changeset
   656
        if (alignment == null) {
e610223887fb 6613904: javax.swing.GroupLayout.createParallelGroup(..) doesn't throw IllegalArgumentException for null arg
rupashka
parents: 6648
diff changeset
   657
            throw new IllegalArgumentException("alignment must be non null");
e610223887fb 6613904: javax.swing.GroupLayout.createParallelGroup(..) doesn't throw IllegalArgumentException for null arg
rupashka
parents: 6648
diff changeset
   658
        }
e610223887fb 6613904: javax.swing.GroupLayout.createParallelGroup(..) doesn't throw IllegalArgumentException for null arg
rupashka
parents: 6648
diff changeset
   659
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        if (alignment == Alignment.BASELINE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            return new BaselineGroup(resizable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        return new ParallelGroup(alignment, resizable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * Creates and returns a {@code ParallelGroup} that aligns it's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * elements along the baseline.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * @param resizable whether the group is resizable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * @param anchorBaselineToTop whether the baseline is anchored to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     *        the top or bottom of the group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * @see #createBaselineGroup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * @see ParallelGroup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    public ParallelGroup createBaselineGroup(boolean resizable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            boolean anchorBaselineToTop) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        return new BaselineGroup(resizable, anchorBaselineToTop);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * Forces the specified components to have the same size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * regardless of their preferred, minimum or maximum sizes. Components that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * are linked are given the maximum of the preferred size of each of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * the linked components. For example, if you link two components with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * a preferred width of 10 and 20, both components are given a width of 20.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * This can be used multiple times to force any number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * components to share the same size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     * Linked Components are not be resizable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     * @param components the {@code Component}s that are to have the same size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * @throws IllegalArgumentException if {@code components} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     *         {@code null}, or contains {@code null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     * @see #linkSize(int,Component[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
    public void linkSize(Component... components) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        linkSize(SwingConstants.HORIZONTAL, components);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        linkSize(SwingConstants.VERTICAL, components);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     * Forces the specified components to have the same size along the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     * specified axis regardless of their preferred, minimum or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     * maximum sizes. Components that are linked are given the maximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     * of the preferred size of each of the linked components. For
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     * example, if you link two components along the horizontal axis
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     * and the preferred width is 10 and 20, both components are given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     * a width of 20.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     * This can be used multiple times to force any number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     * components to share the same size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * Linked {@code Component}s are not be resizable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * @param components the {@code Component}s that are to have the same size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * @param axis the axis to link the size along; one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     *             {@code SwingConstants.HORIZONTAL} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     *             {@code SwingConstans.VERTICAL}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     * @throws IllegalArgumentException if {@code components} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     *         {@code null}, or contains {@code null}; or {@code axis}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     *          is not {@code SwingConstants.HORIZONTAL} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     *          {@code SwingConstants.VERTICAL}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
    public void linkSize(int axis, Component... components) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        if (components == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
            throw new IllegalArgumentException("Components must be non-null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        for (int counter = components.length - 1; counter >= 0; counter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            Component c = components[counter];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            if (components[counter] == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                        "Components must be non-null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            // Force the component to be added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            getComponentInfo(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        int glAxis;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        if (axis == SwingConstants.HORIZONTAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            glAxis = HORIZONTAL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        } else if (axis == SwingConstants.VERTICAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
            glAxis = VERTICAL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
            throw new IllegalArgumentException("Axis must be one of " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                    "SwingConstants.HORIZONTAL or SwingConstants.VERTICAL");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        LinkInfo master = getComponentInfo(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                components[components.length - 1]).getLinkInfo(glAxis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        for (int counter = components.length - 2; counter >= 0; counter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
            master.add(getComponentInfo(components[counter]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        invalidateHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     * Replaces an existing component with a new one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     * @param existingComponent the component that should be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     *        and replaced with {@code newComponent}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     * @param newComponent the component to put in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     *        {@code existingComponent}'s place
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     * @throws IllegalArgumentException if either of the components are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     *         {@code null} or {@code existingComponent} is not being managed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     *         by this layout manager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
    public void replace(Component existingComponent, Component newComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        if (existingComponent == null || newComponent == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            throw new IllegalArgumentException("Components must be non-null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        // Make sure all the components have been registered, otherwise we may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        // not update the correct Springs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        if (springsChanged) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            registerComponents(horizontalGroup, HORIZONTAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
            registerComponents(verticalGroup, VERTICAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        ComponentInfo info = componentInfos.remove(existingComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        if (info == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            throw new IllegalArgumentException("Component must already exist");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        host.remove(existingComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        if (newComponent.getParent() != host) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
            host.add(newComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        info.setComponent(newComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        componentInfos.put(newComponent, info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        invalidateHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     * Sets the {@code LayoutStyle} used to calculate the preferred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     * gaps between components. A value of {@code null} indicates the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     * shared instance of {@code LayoutStyle} should be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     * @param layoutStyle the {@code LayoutStyle} to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     * @see LayoutStyle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
    public void setLayoutStyle(LayoutStyle layoutStyle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        this.layoutStyle = layoutStyle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        invalidateHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     * Returns the {@code LayoutStyle} used for calculating the preferred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     * gap between components. This returns the value specified to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     * {@code setLayoutStyle}, which may be {@code null}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     * @return the {@code LayoutStyle} used for calculating the preferred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     *         gap between components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
    public LayoutStyle getLayoutStyle() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        return layoutStyle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
    private LayoutStyle getLayoutStyle0() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        LayoutStyle layoutStyle = getLayoutStyle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        if (layoutStyle == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
            layoutStyle = LayoutStyle.getInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        return layoutStyle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
    private void invalidateHost() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        if (host instanceof JComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
            ((JComponent)host).revalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
            host.invalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        host.repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
    // LayoutManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     * Notification that a {@code Component} has been added to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     * the parent container.  You should not invoke this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     * directly, instead you should use one of the {@code Group}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     * methods to add a {@code Component}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     * @param name the string to be associated with the component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     * @param component the {@code Component} to be added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
    public void addLayoutComponent(String name, Component component) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
     * Notification that a {@code Component} has been removed from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     * the parent container.  You should not invoke this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     * directly, instead invoke {@code remove} on the parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     * {@code Container}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     * @param component the component to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     * @see java.awt.Component#remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
    public void removeLayoutComponent(Component component) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        ComponentInfo info = componentInfos.remove(component);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        if (info != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
            info.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
            springsChanged = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
            isValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     * Returns the preferred size for the specified container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     * @param parent the container to return the preferred size for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     * @return the preferred size for {@code parent}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     * @throws IllegalArgumentException if {@code parent} is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     *         the same {@code Container} this was created with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     * @throws IllegalStateException if any of the components added to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     *         this layout are not in both a horizontal and vertical group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     * @see java.awt.Container#getPreferredSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
    public Dimension preferredLayoutSize(Container parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
        checkParent(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
        prepare(PREF_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        return adjustSize(horizontalGroup.getPreferredSize(HORIZONTAL),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
                verticalGroup.getPreferredSize(VERTICAL));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     * Returns the minimum size for the specified container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * @param parent the container to return the size for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     * @return the minimum size for {@code parent}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     * @throws IllegalArgumentException if {@code parent} is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     *         the same {@code Container} that this was created with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     * @throws IllegalStateException if any of the components added to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     *         this layout are not in both a horizontal and vertical group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     * @see java.awt.Container#getMinimumSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
    public Dimension minimumLayoutSize(Container parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
        checkParent(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
        prepare(MIN_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
        return adjustSize(horizontalGroup.getMinimumSize(HORIZONTAL),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
                verticalGroup.getMinimumSize(VERTICAL));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
     * Lays out the specified container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
     * @param parent the container to be laid out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     * @throws IllegalStateException if any of the components added to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
     *         this layout are not in both a horizontal and vertical group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
    public void layoutContainer(Container parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        // Step 1: Prepare for layout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        prepare(SPECIFIC_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        Insets insets = parent.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        int width = parent.getWidth() - insets.left - insets.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        int height = parent.getHeight() - insets.top - insets.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        boolean ltr = isLeftToRight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
        if (getAutoCreateGaps() || getAutoCreateContainerGaps() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
                hasPreferredPaddingSprings) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
            // Step 2: Calculate autopadding springs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
            calculateAutopadding(horizontalGroup, HORIZONTAL, SPECIFIC_SIZE, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
                    width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
            calculateAutopadding(verticalGroup, VERTICAL, SPECIFIC_SIZE, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
                    height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        // Step 3: set the size of the groups.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
        horizontalGroup.setSize(HORIZONTAL, 0, width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
        verticalGroup.setSize(VERTICAL, 0, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        // Step 4: apply the size to the components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
        for (ComponentInfo info : componentInfos.values()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
            info.setBounds(insets, width, ltr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
    // LayoutManager2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
     * Notification that a {@code Component} has been added to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
     * the parent container.  You should not invoke this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
     * directly, instead you should use one of the {@code Group}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
     * methods to add a {@code Component}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     * @param component the component added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
     * @param constraints description of where to place the component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
    public void addLayoutComponent(Component component, Object constraints) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     * Returns the maximum size for the specified container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     * @param parent the container to return the size for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
     * @return the maximum size for {@code parent}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
     * @throws IllegalArgumentException if {@code parent} is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
     *         the same {@code Container} that this was created with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
     * @throws IllegalStateException if any of the components added to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
     *         this layout are not in both a horizontal and vertical group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
     * @see java.awt.Container#getMaximumSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
    public Dimension maximumLayoutSize(Container parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
        checkParent(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
        prepare(MAX_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
        return adjustSize(horizontalGroup.getMaximumSize(HORIZONTAL),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
                verticalGroup.getMaximumSize(VERTICAL));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     * Returns the alignment along the x axis.  This specifies how
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     * the component would like to be aligned relative to other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
     * components.  The value should be a number between 0 and 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
     * where 0 represents alignment along the origin, 1 is aligned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
     * the furthest away from the origin, 0.5 is centered, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
     * @param parent the {@code Container} hosting this {@code LayoutManager}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
     * @throws IllegalArgumentException if {@code parent} is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
     *         the same {@code Container} that this was created with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
     * @return the alignment; this implementation returns {@code .5}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
    public float getLayoutAlignmentX(Container parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
        checkParent(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
        return .5f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
     * Returns the alignment along the y axis.  This specifies how
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     * the component would like to be aligned relative to other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
     * components.  The value should be a number between 0 and 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
     * where 0 represents alignment along the origin, 1 is aligned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
     * the furthest away from the origin, 0.5 is centered, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
     * @param parent the {@code Container} hosting this {@code LayoutManager}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
     * @throws IllegalArgumentException if {@code parent} is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
     *         the same {@code Container} that this was created with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     * @return alignment; this implementation returns {@code .5}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
    public float getLayoutAlignmentY(Container parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
        checkParent(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
        return .5f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
     * Invalidates the layout, indicating that if the layout manager
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
     * has cached information it should be discarded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
     * @param parent the {@code Container} hosting this LayoutManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     * @throws IllegalArgumentException if {@code parent} is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     *         the same {@code Container} that this was created with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
    public void invalidateLayout(Container parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
        checkParent(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
        // invalidateLayout is called from Container.invalidate, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
        // does NOT grab the treelock.  All other methods do.  To make sure
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
        // there aren't any possible threading problems we grab the tree lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
        // here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
        synchronized(parent.getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
            isValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
    private void prepare(int sizeType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
        boolean visChanged = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
        // Step 1: If not-valid, clear springs and update visibility.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
        if (!isValid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
            isValid = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
            horizontalGroup.setSize(HORIZONTAL, UNSET, UNSET);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
            verticalGroup.setSize(VERTICAL, UNSET, UNSET);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
            for (ComponentInfo ci : componentInfos.values()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
                if (ci.updateVisibility()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
                    visChanged = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
                ci.clearCachedSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
        // Step 2: Make sure components are bound to ComponentInfos
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
        if (springsChanged) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
            registerComponents(horizontalGroup, HORIZONTAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
            registerComponents(verticalGroup, VERTICAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
        // Step 3: Adjust the autopadding. This removes existing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
        // autopadding, then recalculates where it should go.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
        if (springsChanged || visChanged) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
            checkComponents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
            horizontalGroup.removeAutopadding();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
            verticalGroup.removeAutopadding();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
            if (getAutoCreateGaps()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
                insertAutopadding(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
            } else if (hasPreferredPaddingSprings ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
                    getAutoCreateContainerGaps()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
                insertAutopadding(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
            springsChanged = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
        // Step 4: (for min/pref/max size calculations only) calculate the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
        // autopadding. This invokes for unsetting the calculated values, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
        // recalculating them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
        // If sizeType == SPECIFIC_SIZE, it indicates we're doing layout, this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
        // step will be done later on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
        if (sizeType != SPECIFIC_SIZE && (getAutoCreateGaps() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
                getAutoCreateContainerGaps() || hasPreferredPaddingSprings)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
            calculateAutopadding(horizontalGroup, HORIZONTAL, sizeType, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
            calculateAutopadding(verticalGroup, VERTICAL, sizeType, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
    private void calculateAutopadding(Group group, int axis, int sizeType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
            int origin, int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
        group.unsetAutopadding();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
        switch(sizeType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
            case MIN_SIZE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
                size = group.getMinimumSize(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
            case PREF_SIZE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
                size = group.getPreferredSize(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
            case MAX_SIZE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
                size = group.getMaximumSize(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
        group.setSize(axis, origin, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
        group.calculateAutopadding(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
    private void checkComponents() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
        for (ComponentInfo info : componentInfos.values()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
            if (info.horizontalSpring == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
                throw new IllegalStateException(info.component +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
                        " is not attached to a horizontal group");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
            if (info.verticalSpring == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
                throw new IllegalStateException(info.component +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
                        " is not attached to a vertical group");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
    private void registerComponents(Group group, int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
        List<Spring> springs = group.springs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
        for (int counter = springs.size() - 1; counter >= 0; counter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
            Spring spring = springs.get(counter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
            if (spring instanceof ComponentSpring) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
                ((ComponentSpring)spring).installIfNecessary(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
            } else if (spring instanceof Group) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
                registerComponents((Group)spring, axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
    private Dimension adjustSize(int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
        Insets insets = host.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
        return new Dimension(width + insets.left + insets.right,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
                height + insets.top + insets.bottom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
    private void checkParent(Container parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
        if (parent != host) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
                    "GroupLayout can only be used with one Container at a time");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
     * Returns the {@code ComponentInfo} for the specified Component,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
     * creating one if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
    private ComponentInfo getComponentInfo(Component component) {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
  1126
        ComponentInfo info = componentInfos.get(component);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
        if (info == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
            info = new ComponentInfo(component);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
            componentInfos.put(component, info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
            if (component.getParent() != host) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
                host.add(component);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
        return info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
     * Adjusts the autopadding springs for the horizontal and vertical
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
     * groups.  If {@code insert} is {@code true} this will insert auto padding
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
     * springs, otherwise this will only adjust the springs that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
     * comprise auto preferred padding springs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
    private void insertAutopadding(boolean insert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
        horizontalGroup.insertAutopadding(HORIZONTAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
                new ArrayList<AutoPreferredGapSpring>(1),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
                new ArrayList<AutoPreferredGapSpring>(1),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
                new ArrayList<ComponentSpring>(1),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
                new ArrayList<ComponentSpring>(1), insert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
        verticalGroup.insertAutopadding(VERTICAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
                new ArrayList<AutoPreferredGapSpring>(1),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
                new ArrayList<AutoPreferredGapSpring>(1),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
                new ArrayList<ComponentSpring>(1),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
                new ArrayList<ComponentSpring>(1), insert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
     * Returns {@code true} if the two Components have a common ParallelGroup
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
     * ancestor along the particular axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
    private boolean areParallelSiblings(Component source, Component target,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
            int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
        ComponentInfo sourceInfo = getComponentInfo(source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
        ComponentInfo targetInfo = getComponentInfo(target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
        Spring sourceSpring;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
        Spring targetSpring;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
        if (axis == HORIZONTAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
            sourceSpring = sourceInfo.horizontalSpring;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
            targetSpring = targetInfo.horizontalSpring;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
            sourceSpring = sourceInfo.verticalSpring;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
            targetSpring = targetInfo.verticalSpring;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
        Set<Spring> sourcePath = tmpParallelSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
        sourcePath.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
        Spring spring = sourceSpring.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
        while (spring != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
            sourcePath.add(spring);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
            spring = spring.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
        spring = targetSpring.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
        while (spring != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
            if (sourcePath.contains(spring)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
                sourcePath.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
                while (spring != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
                    if (spring instanceof ParallelGroup) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
                        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
                    spring = spring.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
            spring = spring.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
        sourcePath.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
    private boolean isLeftToRight() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
        return host.getComponentOrientation().isLeftToRight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
     * Returns a string representation of this {@code GroupLayout}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
     * This method is intended to be used for debugging purposes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
     * and the content and format of the returned string may vary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
     * between implementations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
     * @return a string representation of this {@code GroupLayout}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
        if (springsChanged) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
            registerComponents(horizontalGroup, HORIZONTAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
            registerComponents(verticalGroup, VERTICAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
        StringBuffer buffer = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
        buffer.append("HORIZONTAL\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
        createSpringDescription(buffer, horizontalGroup, "  ", HORIZONTAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
        buffer.append("\nVERTICAL\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
        createSpringDescription(buffer, verticalGroup, "  ", VERTICAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
        return buffer.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
    private void createSpringDescription(StringBuffer buffer, Spring spring,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
            String indent, int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
        String origin = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
        String padding = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
        if (spring instanceof ComponentSpring) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
            ComponentSpring cSpring = (ComponentSpring)spring;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
            origin = Integer.toString(cSpring.getOrigin()) + " ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
            String name = cSpring.getComponent().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
            if (name != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
                origin = "name=" + name + ", ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
        if (spring instanceof AutoPreferredGapSpring) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
            AutoPreferredGapSpring paddingSpring =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
                    (AutoPreferredGapSpring)spring;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
            padding = ", userCreated=" + paddingSpring.getUserCreated() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
                    ", matches=" + paddingSpring.getMatchDescription();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
        buffer.append(indent + spring.getClass().getName() + " " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
                Integer.toHexString(spring.hashCode()) + " " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
                origin +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
                ", size=" + spring.getSize() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
                ", alignment=" + spring.getAlignment() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
                " prefs=[" + spring.getMinimumSize(axis) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
                " " + spring.getPreferredSize(axis) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
                " " + spring.getMaximumSize(axis) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
                padding + "]\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
        if (spring instanceof Group) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
            List<Spring> springs = ((Group)spring).springs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
            indent += "  ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
            for (int counter = 0; counter < springs.size(); counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
                createSpringDescription(buffer, springs.get(counter), indent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
                        axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
     * Spring consists of a range: min, pref and max, a value some where in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
     * the middle of that, and a location. Spring caches the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
     * min/max/pref.  If the min/pref/max has internally changes, or needs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
     * to be updated you must invoke clear.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
    private abstract class Spring {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
        private int size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
        private int min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
        private int max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
        private int pref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
        private Spring parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
        private Alignment alignment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
        Spring() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
            min = pref = max = UNSET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
         * Calculates and returns the minimum size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
         * @param axis the axis of layout; one of HORIZONTAL or VERTICAL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
         * @return the minimum size
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
        abstract int calculateMinimumSize(int axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
         * Calculates and returns the preferred size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
         * @param axis the axis of layout; one of HORIZONTAL or VERTICAL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
         * @return the preferred size
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
        abstract int calculatePreferredSize(int axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
         * Calculates and returns the minimum size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
         * @param axis the axis of layout; one of HORIZONTAL or VERTICAL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
         * @return the minimum size
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
        abstract int calculateMaximumSize(int axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
         * Sets the parent of this Spring.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
        void setParent(Spring parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
            this.parent = parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
         * Returns the parent of this spring.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
        Spring getParent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
            return parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 21244
diff changeset
  1318
        // This is here purely as a convenience for ParallelGroup to avoid
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
        // having to track alignment separately.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
        void setAlignment(Alignment alignment) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
            this.alignment = alignment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
         * Alignment for this Spring, this may be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
        Alignment getAlignment() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
            return alignment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
         * Returns the minimum size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
        final int getMinimumSize(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
            if (min == UNSET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
                min = constrain(calculateMinimumSize(axis));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
            return min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
         * Returns the preferred size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
        final int getPreferredSize(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
            if (pref == UNSET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
                pref = constrain(calculatePreferredSize(axis));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
            return pref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
         * Returns the maximum size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
        final int getMaximumSize(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
            if (max == UNSET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
                max = constrain(calculateMaximumSize(axis));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
            return max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
         * Sets the value and location of the spring.  Subclasses
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
         * will want to invoke super, then do any additional sizing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
         * @param axis HORIZONTAL or VERTICAL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
         * @param origin of this Spring
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
         * @param size of the Spring.  If size is UNSET, this invokes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
         *        clear.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
        void setSize(int axis, int origin, int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
            this.size = size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
            if (size == UNSET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
                unset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
         * Resets the cached min/max/pref.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
        void unset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
            size = min = pref = max = UNSET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
         * Returns the current size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
        int getSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
            return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
        int constrain(int value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
            return Math.min(value, Short.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
        int getBaseline() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
        BaselineResizeBehavior getBaselineResizeBehavior() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
            return BaselineResizeBehavior.OTHER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
        final boolean isResizable(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
            int min = getMinimumSize(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
            int pref = getPreferredSize(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
            return (min != pref || pref != getMaximumSize(axis));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
         * Returns {@code true} if this spring will ALWAYS have a zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
         * size. This should NOT check the current size, rather it's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
         * meant to quickly test if this Spring will always have a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
         * zero size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
         * @param treatAutopaddingAsZeroSized if {@code true}, auto padding
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
         *        springs should be treated as having a size of {@code 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
         * @return {@code true} if this spring will have a zero size,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
         *         {@code false} otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
        abstract boolean willHaveZeroSize(boolean treatAutopaddingAsZeroSized);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
     * {@code Group} provides the basis for the two types of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
     * operations supported by {@code GroupLayout}: laying out
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
     * components one after another ({@link SequentialGroup SequentialGroup})
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
     * or aligned ({@link ParallelGroup ParallelGroup}). {@code Group} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
     * its subclasses have no public constructor; to create one use
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
     * one of {@code createSequentialGroup} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
     * {@code createParallelGroup}. Additionally, taking a {@code Group}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
     * created from one {@code GroupLayout} and using it with another
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
     * will produce undefined results.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
     * Various methods in {@code Group} and its subclasses allow you
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
     * to explicitly specify the range. The arguments to these methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
     * can take two forms, either a value greater than or equal to 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
     * or one of {@code DEFAULT_SIZE} or {@code PREFERRED_SIZE}. A
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
     * value greater than or equal to {@code 0} indicates a specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
     * size. {@code DEFAULT_SIZE} indicates the corresponding size
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
     * from the component should be used.  For example, if {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
     * DEFAULT_SIZE} is passed as the minimum size argument, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
     * minimum size is obtained from invoking {@code getMinimumSize}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
     * on the component. Likewise, {@code PREFERRED_SIZE} indicates
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
     * the value from {@code getPreferredSize} should be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
     * The following example adds {@code myComponent} to {@code group}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
     * with specific values for the range. That is, the minimum is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
     * explicitly specified as 100, preferred as 200, and maximum as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
     * 300.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
     *   group.addComponent(myComponent, 100, 200, 300);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
     * The following example adds {@code myComponent} to {@code group} using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
     * a combination of the forms. The minimum size is forced to be the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
     * same as the preferred size, the preferred size is determined by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
     * using {@code myComponent.getPreferredSize} and the maximum is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
     * determined by invoking {@code getMaximumSize} on the component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
     *   group.addComponent(myComponent, GroupLayout.PREFERRED_SIZE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
     *             GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
     * Unless otherwise specified all the methods of {@code Group} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
     * its subclasses that allow you to specify a range throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
     * {@code IllegalArgumentException} if passed an invalid range. An
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
     * invalid range is one in which any of the values are &lt; 0 and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
     * not one of {@code PREFERRED_SIZE} or {@code DEFAULT_SIZE}, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
     * the following is not met (for specific values): {@code min}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
     * &lt;= {@code pref} &lt;= {@code max}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
     * Similarly any methods that take a {@code Component} throw a
6648
6b796dace707 6606019: NPE and IAE are interchanged in specification for GroupLayout.Group class
alexp
parents: 5506
diff changeset
  1471
     * {@code IllegalArgumentException} if passed {@code null} and any methods
6b796dace707 6606019: NPE and IAE are interchanged in specification for GroupLayout.Group class
alexp
parents: 5506
diff changeset
  1472
     * that take a {@code Group} throw an {@code NullPointerException} if
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
     * passed {@code null}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
     * @see #createSequentialGroup
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
     * @see #createParallelGroup
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
    public abstract class Group extends Spring {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
        // private int origin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
        // private int size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
        List<Spring> springs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
        Group() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
            springs = new ArrayList<Spring>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
         * Adds a {@code Group} to this {@code Group}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
         * @param group the {@code Group} to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
         * @return this {@code Group}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
        public Group addGroup(Group group) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
            return addSpring(group);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
         * Adds a {@code Component} to this {@code Group}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
         * @param component the {@code Component} to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
         * @return this {@code Group}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
        public Group addComponent(Component component) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
            return addComponent(component, DEFAULT_SIZE, DEFAULT_SIZE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
                    DEFAULT_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
         * Adds a {@code Component} to this {@code Group}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
         * with the specified size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
         * @param component the {@code Component} to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
         * @param min the minimum size or one of {@code DEFAULT_SIZE} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
         *            {@code PREFERRED_SIZE}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
         * @param pref the preferred size or one of {@code DEFAULT_SIZE} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
         *            {@code PREFERRED_SIZE}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
         * @param max the maximum size or one of {@code DEFAULT_SIZE} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
         *            {@code PREFERRED_SIZE}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
         * @return this {@code Group}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
        public Group addComponent(Component component, int min, int pref,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
                int max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
            return addSpring(new ComponentSpring(component, min, pref, max));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
         * Adds a rigid gap to this {@code Group}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
         * @param size the size of the gap
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
         * @return this {@code Group}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
         * @throws IllegalArgumentException if {@code size} is less than
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
         *         {@code 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
        public Group addGap(int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
            return addGap(size, size, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
         * Adds a gap to this {@code Group} with the specified size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
         * @param min the minimum size of the gap
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
         * @param pref the preferred size of the gap
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
         * @param max the maximum size of the gap
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
         * @throws IllegalArgumentException if any of the values are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
         *         less than {@code 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
         * @return this {@code Group}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
        public Group addGap(int min, int pref, int max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
            return addSpring(new GapSpring(min, pref, max));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
        Spring getSpring(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
            return springs.get(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
        int indexOf(Spring spring) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
            return springs.indexOf(spring);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
         * Adds the Spring to the list of {@code Spring}s and returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
         * the receiver.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
        Group addSpring(Spring spring) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
            springs.add(spring);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
            spring.setParent(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
            if (!(spring instanceof AutoPreferredGapSpring) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
                    !((AutoPreferredGapSpring)spring).getUserCreated()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
                springsChanged = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
        // Spring methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
        void setSize(int axis, int origin, int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
            super.setSize(axis, origin, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
            if (size == UNSET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
                for (int counter = springs.size() - 1; counter >= 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
                counter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
                    getSpring(counter).setSize(axis, origin, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
                setValidSize(axis, origin, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
         * This is invoked from {@code setSize} if passed a value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
         * other than UNSET.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
        abstract void setValidSize(int axis, int origin, int size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
        int calculateMinimumSize(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
            return calculateSize(axis, MIN_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
        int calculatePreferredSize(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
            return calculateSize(axis, PREF_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
        int calculateMaximumSize(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
            return calculateSize(axis, MAX_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
         * Calculates the specified size.  This is called from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
         * one of the {@code getMinimumSize0},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
         * {@code getPreferredSize0} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
         * {@code getMaximumSize0} methods.  This will invoke
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
         * to {@code operator} to combine the values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
        int calculateSize(int axis, int type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
            int count = springs.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
            if (count == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
            if (count == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
                return getSpringSize(getSpring(0), axis, type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
            int size = constrain(operator(getSpringSize(getSpring(0), axis,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
                    type), getSpringSize(getSpring(1), axis, type)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
            for (int counter = 2; counter < count; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
                size = constrain(operator(size, getSpringSize(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
                        getSpring(counter), axis, type)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
            return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
        int getSpringSize(Spring spring, int axis, int type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
            switch(type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
                case MIN_SIZE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
                    return spring.getMinimumSize(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
                case PREF_SIZE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
                    return spring.getPreferredSize(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
                case MAX_SIZE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
                    return spring.getMaximumSize(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
            assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
         * Used to compute how the two values representing two springs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
         * will be combined.  For example, a group that layed things out
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
         * one after the next would return {@code a + b}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
        abstract int operator(int a, int b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
        // Padding
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
         * Adjusts the autopadding springs in this group and its children.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
         * If {@code insert} is true this will insert auto padding
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
         * springs, otherwise this will only adjust the springs that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
         * comprise auto preferred padding springs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
         * @param axis the axis of the springs; HORIZONTAL or VERTICAL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
         * @param leadingPadding List of AutopaddingSprings that occur before
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
         *                       this Group
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
         * @param trailingPadding any trailing autopadding springs are added
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
         *                        to this on exit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
         * @param leading List of ComponentSprings that occur before this Group
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
         * @param trailing any trailing ComponentSpring are added to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
         *                 List
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
         * @param insert Whether or not to insert AutopaddingSprings or just
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
         *               adjust any existing AutopaddingSprings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
        abstract void insertAutopadding(int axis,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
                List<AutoPreferredGapSpring> leadingPadding,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
                List<AutoPreferredGapSpring> trailingPadding,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
                List<ComponentSpring> leading, List<ComponentSpring> trailing,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
                boolean insert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
         * Removes any AutopaddingSprings for this Group and its children.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
        void removeAutopadding() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
            unset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
            for (int counter = springs.size() - 1; counter >= 0; counter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
                Spring spring = springs.get(counter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
                if (spring instanceof AutoPreferredGapSpring) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
                    if (((AutoPreferredGapSpring)spring).getUserCreated()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
                        ((AutoPreferredGapSpring)spring).reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
                        springs.remove(counter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
                } else if (spring instanceof Group) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
                    ((Group)spring).removeAutopadding();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
        void unsetAutopadding() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
            // Clear cached pref/min/max.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
            unset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
            for (int counter = springs.size() - 1; counter >= 0; counter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
                Spring spring = springs.get(counter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
                if (spring instanceof AutoPreferredGapSpring) {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
  1705
                    spring.unset();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
                } else if (spring instanceof Group) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
                    ((Group)spring).unsetAutopadding();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
        void calculateAutopadding(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
            for (int counter = springs.size() - 1; counter >= 0; counter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
                Spring spring = springs.get(counter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
                if (spring instanceof AutoPreferredGapSpring) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
                    // Force size to be reset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
                    spring.unset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
                    ((AutoPreferredGapSpring)spring).calculatePadding(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
                } else if (spring instanceof Group) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
                    ((Group)spring).calculateAutopadding(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
            // Clear cached pref/min/max.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
            unset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
        @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
        boolean willHaveZeroSize(boolean treatAutopaddingAsZeroSized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
            for (int i = springs.size() - 1; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
                Spring spring = springs.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
                if (!spring.willHaveZeroSize(treatAutopaddingAsZeroSized)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
     * A {@code Group} that positions and sizes its elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
     * sequentially, one after another.  This class has no public
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
     * constructor, use the {@code createSequentialGroup} method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
     * to create one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
     * In order to align a {@code SequentialGroup} along the baseline
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
     * of a baseline aligned {@code ParallelGroup} you need to specify
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
     * which of the elements of the {@code SequentialGroup} is used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
     * determine the baseline.  The element used to calculate the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
     * baseline is specified using one of the {@code add} methods that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
     * take a {@code boolean}. The last element added with a value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
     * {@code true} for {@code useAsBaseline} is used to calculate the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
     * baseline.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
     * @see #createSequentialGroup
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
    public class SequentialGroup extends Group {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
        private Spring baselineSpring;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
        SequentialGroup() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
        public SequentialGroup addGroup(Group group) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
            return (SequentialGroup)super.addGroup(group);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
         * Adds a {@code Group} to this {@code Group}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
         * @param group the {@code Group} to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
         * @param useAsBaseline whether the specified {@code Group} should
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
         *        be used to calculate the baseline for this {@code Group}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
         * @return this {@code Group}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
        public SequentialGroup addGroup(boolean useAsBaseline, Group group) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
            super.addGroup(group);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
            if (useAsBaseline) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
                baselineSpring = group;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
        public SequentialGroup addComponent(Component component) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
            return (SequentialGroup)super.addComponent(component);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
         * Adds a {@code Component} to this {@code Group}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
         * @param useAsBaseline whether the specified {@code Component} should
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
         *        be used to calculate the baseline for this {@code Group}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
         * @param component the {@code Component} to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
         * @return this {@code Group}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
        public SequentialGroup addComponent(boolean useAsBaseline,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
                Component component) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
            super.addComponent(component);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
            if (useAsBaseline) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
                baselineSpring = springs.get(springs.size() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
        public SequentialGroup addComponent(Component component, int min,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
                int pref, int max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
            return (SequentialGroup)super.addComponent(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
                    component, min, pref, max);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
         * Adds a {@code Component} to this {@code Group}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
         * with the specified size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
         * @param useAsBaseline whether the specified {@code Component} should
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
         *        be used to calculate the baseline for this {@code Group}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
         * @param component the {@code Component} to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
         * @param min the minimum size or one of {@code DEFAULT_SIZE} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
         *            {@code PREFERRED_SIZE}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
         * @param pref the preferred size or one of {@code DEFAULT_SIZE} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
         *            {@code PREFERRED_SIZE}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
         * @param max the maximum size or one of {@code DEFAULT_SIZE} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
         *            {@code PREFERRED_SIZE}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
         * @return this {@code Group}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
        public SequentialGroup addComponent(boolean useAsBaseline,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
                Component component, int min, int pref, int max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
            super.addComponent(component, min, pref, max);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
            if (useAsBaseline) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
                baselineSpring = springs.get(springs.size() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
        public SequentialGroup addGap(int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
            return (SequentialGroup)super.addGap(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
        public SequentialGroup addGap(int min, int pref, int max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
            return (SequentialGroup)super.addGap(min, pref, max);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
         * Adds an element representing the preferred gap between two
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
         * components. The element created to represent the gap is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
         * resizable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
         * @param comp1 the first component
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
         * @param comp2 the second component
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
         * @param type the type of gap; one of the constants defined by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
         *        {@code LayoutStyle}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
         * @return this {@code SequentialGroup}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
         * @throws IllegalArgumentException if {@code type}, {@code comp1} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
         *         {@code comp2} is {@code null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
         * @see LayoutStyle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
        public SequentialGroup addPreferredGap(JComponent comp1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
                JComponent comp2, ComponentPlacement type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
            return addPreferredGap(comp1, comp2, type, DEFAULT_SIZE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
                    PREFERRED_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
         * Adds an element representing the preferred gap between two
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
         * components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
         * @param comp1 the first component
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
         * @param comp2 the second component
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
         * @param type the type of gap
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
         * @param pref the preferred size of the grap; one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
         *        {@code DEFAULT_SIZE} or a value &gt;= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
         * @param max the maximum size of the gap; one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
         *        {@code DEFAULT_SIZE}, {@code PREFERRED_SIZE}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
         *        or a value &gt;= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
         * @return this {@code SequentialGroup}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
         * @throws IllegalArgumentException if {@code type}, {@code comp1} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
         *         {@code comp2} is {@code null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
         * @see LayoutStyle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
        public SequentialGroup addPreferredGap(JComponent comp1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
                JComponent comp2, ComponentPlacement type, int pref,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
                int max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
            if (type == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
                throw new IllegalArgumentException("Type must be non-null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
            if (comp1 == null || comp2 == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
                throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
                        "Components must be non-null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
            checkPreferredGapValues(pref, max);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
            return (SequentialGroup)addSpring(new PreferredGapSpring(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
                    comp1, comp2, type, pref, max));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
         * Adds an element representing the preferred gap between the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
         * nearest components.  During layout, neighboring
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
         * components are found, and the size of the added gap is set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
         * based on the preferred gap between the components.  If no
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
         * neighboring components are found the gap has a size of {@code 0}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
         * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
         * The element created to represent the gap is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
         * resizable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
         * @param type the type of gap; one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
         *        {@code LayoutStyle.ComponentPlacement.RELATED} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
         *        {@code LayoutStyle.ComponentPlacement.UNRELATED}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
         * @return this {@code SequentialGroup}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
         * @see LayoutStyle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
         * @throws IllegalArgumentException if {@code type} is not one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
         *         {@code LayoutStyle.ComponentPlacement.RELATED} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
         *         {@code LayoutStyle.ComponentPlacement.UNRELATED}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
        public SequentialGroup addPreferredGap(ComponentPlacement type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
            return addPreferredGap(type, DEFAULT_SIZE, DEFAULT_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
         * Adds an element representing the preferred gap between the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
         * nearest components.  During layout, neighboring
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
         * components are found, and the minimum of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
         * gap is set based on the size of the preferred gap between the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
         * neighboring components.  If no neighboring components are found the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
         * minimum size is set to 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
         * @param type the type of gap; one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
         *        {@code LayoutStyle.ComponentPlacement.RELATED} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
         *        {@code LayoutStyle.ComponentPlacement.UNRELATED}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
         * @param pref the preferred size of the grap; one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
         *        {@code DEFAULT_SIZE} or a value &gt;= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
         * @param max the maximum size of the gap; one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
         *        {@code DEFAULT_SIZE}, {@code PREFERRED_SIZE}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
         *        or a value &gt;= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
         * @return this {@code SequentialGroup}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
         * @throws IllegalArgumentException if {@code type} is not one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
         *         {@code LayoutStyle.ComponentPlacement.RELATED} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
         *         {@code LayoutStyle.ComponentPlacement.UNRELATED}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
         * @see LayoutStyle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
        public SequentialGroup addPreferredGap(ComponentPlacement type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
                int pref, int max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
            if (type != ComponentPlacement.RELATED &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
                    type != ComponentPlacement.UNRELATED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
                throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
                        "Type must be one of " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
                        "LayoutStyle.ComponentPlacement.RELATED or " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
                        "LayoutStyle.ComponentPlacement.UNRELATED");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
            checkPreferredGapValues(pref, max);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
            hasPreferredPaddingSprings = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
            return (SequentialGroup)addSpring(new AutoPreferredGapSpring(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
                    type, pref, max));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
         * Adds an element representing the preferred gap between an edge
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
         * the container and components that touch the border of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
         * container. This has no effect if the added gap does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
         * touch an edge of the parent container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
         * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
         * The element created to represent the gap is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
         * resizable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
         * @return this {@code SequentialGroup}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
        public SequentialGroup addContainerGap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
            return addContainerGap(DEFAULT_SIZE, DEFAULT_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
         * Adds an element representing the preferred gap between one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
         * edge of the container and the next or previous {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
         * Component} with the specified size. This has no
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
         * effect if the next or previous element is not a {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
         * Component} and does not touch one edge of the parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
         * container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
         * @param pref the preferred size; one of {@code DEFAULT_SIZE} or a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
         *              value &gt;= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
         * @param max the maximum size; one of {@code DEFAULT_SIZE},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
         *        {@code PREFERRED_SIZE} or a value &gt;= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
         * @return this {@code SequentialGroup}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
        public SequentialGroup addContainerGap(int pref, int max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
            if ((pref < 0 && pref != DEFAULT_SIZE) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
                    (max < 0 && max != DEFAULT_SIZE && max != PREFERRED_SIZE)||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
                    (pref >= 0 && max >= 0 && pref > max)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
                throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
                        "Pref and max must be either DEFAULT_VALUE " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
                        "or >= 0 and pref <= max");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
            hasPreferredPaddingSprings = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
            return (SequentialGroup)addSpring(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
                    new ContainerAutoPreferredGapSpring(pref, max));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
        int operator(int a, int b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
            return constrain(a) + constrain(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
        void setValidSize(int axis, int origin, int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
            int pref = getPreferredSize(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
            if (size == pref) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
                // Layout at preferred size
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
                for (Spring spring : springs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
                    int springPref = spring.getPreferredSize(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
                    spring.setSize(axis, origin, springPref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
                    origin += springPref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
            } else if (springs.size() == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
                Spring spring = getSpring(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
                spring.setSize(axis, origin, Math.min(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
                        Math.max(size, spring.getMinimumSize(axis)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
                        spring.getMaximumSize(axis)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
            } else if (springs.size() > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
                // Adjust between min/pref
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
                setValidSizeNotPreferred(axis, origin, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
        private void setValidSizeNotPreferred(int axis, int origin, int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
            int delta = size - getPreferredSize(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
            assert delta != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
            boolean useMin = (delta < 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
            int springCount = springs.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
            if (useMin) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
                delta *= -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
            // The following algorithm if used for resizing springs:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
            // 1. Calculate the resizability of each spring (pref - min or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
            //    max - pref) into a list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
            // 2. Sort the list in ascending order
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
            // 3. Iterate through each of the resizable Springs, attempting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
            //    to give them (pref - size) / resizeCount
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 21244
diff changeset
  2051
            // 4. For any Springs that can not accommodate that much space
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
            //    add the remainder back to the amount to distribute and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
            //    recalculate how must space the remaining springs will get.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
            // 5. Set the size of the springs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
            // First pass, sort the resizable springs into the List resizable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
            List<SpringDelta> resizable = buildResizableList(axis, useMin);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
            int resizableCount = resizable.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
            if (resizableCount > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
                // How much we would like to give each Spring.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
                int sDelta = delta / resizableCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
                // Remaining space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
                int slop = delta - sDelta * resizableCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
                int[] sizes = new int[springCount];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
                int sign = useMin ? -1 : 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
                // Second pass, accumulate the resulting deltas (relative to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
                // preferred) into sizes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
                for (int counter = 0; counter < resizableCount; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
                    SpringDelta springDelta = resizable.get(counter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
                    if ((counter + 1) == resizableCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
                        sDelta += slop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
                    springDelta.delta = Math.min(sDelta, springDelta.delta);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
                    delta -= springDelta.delta;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
                    if (springDelta.delta != sDelta && counter + 1 <
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
                            resizableCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
                        // Spring didn't take all the space, reset how much
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
                        // each spring will get.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
                        sDelta = delta / (resizableCount - counter - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
                        slop = delta - sDelta * (resizableCount - counter - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
                    sizes[springDelta.index] = sign * springDelta.delta;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
                // And finally set the size of each spring
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
                for (int counter = 0; counter < springCount; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
                    Spring spring = getSpring(counter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
                    int sSize = spring.getPreferredSize(axis) + sizes[counter];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
                    spring.setSize(axis, origin, sSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
                    origin += sSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
                // Nothing resizable, use the min or max of each of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
                // springs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
                for (int counter = 0; counter < springCount; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
                    Spring spring = getSpring(counter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
                    int sSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
                    if (useMin) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
                        sSize = spring.getMinimumSize(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
                        sSize = spring.getMaximumSize(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
                    spring.setSize(axis, origin, sSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
                    origin += sSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
         * Returns the sorted list of SpringDelta's for the current set of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
         * Springs. The list is ordered based on the amount of flexibility of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
         * the springs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
        private List<SpringDelta> buildResizableList(int axis,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
                boolean useMin) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
            // First pass, figure out what is resizable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
            int size = springs.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
            List<SpringDelta> sorted = new ArrayList<SpringDelta>(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
            for (int counter = 0; counter < size; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
                Spring spring = getSpring(counter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
                int sDelta;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
                if (useMin) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
                    sDelta = spring.getPreferredSize(axis) -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
                            spring.getMinimumSize(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
                    sDelta = spring.getMaximumSize(axis) -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
                            spring.getPreferredSize(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
                if (sDelta > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
                    sorted.add(new SpringDelta(counter, sDelta));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
            Collections.sort(sorted);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
            return sorted;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
        private int indexOfNextNonZeroSpring(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
                int index, boolean treatAutopaddingAsZeroSized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
            while (index < springs.size()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
                Spring spring = springs.get(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
                if (!spring.willHaveZeroSize(treatAutopaddingAsZeroSized)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
                    return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
                index++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
            return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
        @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
        void insertAutopadding(int axis,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
                List<AutoPreferredGapSpring> leadingPadding,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
                List<AutoPreferredGapSpring> trailingPadding,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
                List<ComponentSpring> leading, List<ComponentSpring> trailing,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
                boolean insert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
            List<AutoPreferredGapSpring> newLeadingPadding =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
                    new ArrayList<AutoPreferredGapSpring>(leadingPadding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
            List<AutoPreferredGapSpring> newTrailingPadding =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
                    new ArrayList<AutoPreferredGapSpring>(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
            List<ComponentSpring> newLeading =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
                    new ArrayList<ComponentSpring>(leading);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
            List<ComponentSpring> newTrailing = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
            int counter = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
            // Warning, this must use springs.size, as it may change during the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
            // loop.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
            while (counter < springs.size()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
                Spring spring = getSpring(counter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
                if (spring instanceof AutoPreferredGapSpring) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
                    if (newLeadingPadding.size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
                        // Autopadding spring. Set the sources of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
                        // autopadding spring based on newLeading.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
                        AutoPreferredGapSpring padding =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
                            (AutoPreferredGapSpring)spring;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
                        padding.setSources(newLeading);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
                        newLeading.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
                        counter = indexOfNextNonZeroSpring(counter + 1, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
                        if (counter == springs.size()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
                            // Last spring in the list, add it to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
                            // trailingPadding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
                            if (!(padding instanceof
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
                                  ContainerAutoPreferredGapSpring)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
                                trailingPadding.add(padding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
                            newLeadingPadding.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
                            newLeadingPadding.add(padding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
                        counter = indexOfNextNonZeroSpring(counter + 1, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
                    // Not a padding spring
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
                    if (newLeading.size() > 0 && insert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
                        // There's leading ComponentSprings, create an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
                        // autopadding spring.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
                        AutoPreferredGapSpring padding =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
                                new AutoPreferredGapSpring();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
                        // Force the newly created spring to be considered
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
                        // by NOT incrementing counter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
                        springs.add(counter, padding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
                    if (spring instanceof ComponentSpring) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
                        // Spring is a Component, make it the target of any
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
                        // leading AutopaddingSpring.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
                        ComponentSpring cSpring = (ComponentSpring)spring;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
                        if (!cSpring.isVisible()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
                            counter++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
                            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
                        for (AutoPreferredGapSpring gapSpring : newLeadingPadding) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
                            gapSpring.addTarget(cSpring, axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
                        newLeading.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
                        newLeadingPadding.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
                        counter = indexOfNextNonZeroSpring(counter + 1, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
                        if (counter == springs.size()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
                            // Last Spring, add it to trailing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
                            trailing.add(cSpring);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
                            // Not that last Spring, add it to leading
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
                            newLeading.add(cSpring);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
                    } else if (spring instanceof Group) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
                        // Forward call to child Group
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
                        if (newTrailing == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
                            newTrailing = new ArrayList<ComponentSpring>(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
                            newTrailing.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
                        newTrailingPadding.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
                        ((Group)spring).insertAutopadding(axis,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
                                newLeadingPadding, newTrailingPadding,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
                                newLeading, newTrailing, insert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
                        newLeading.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
                        newLeadingPadding.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
                        counter = indexOfNextNonZeroSpring(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
                                    counter + 1, (newTrailing.size() == 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
                        if (counter == springs.size()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
                            trailing.addAll(newTrailing);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
                            trailingPadding.addAll(newTrailingPadding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
                            newLeading.addAll(newTrailing);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
                            newLeadingPadding.addAll(newTrailingPadding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
                        // Gap
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
                        newLeadingPadding.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
                        newLeading.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
                        counter++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
        int getBaseline() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
            if (baselineSpring != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
                int baseline = baselineSpring.getBaseline();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
                if (baseline >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
                    int size = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
                    for (Spring spring : springs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
                        if (spring == baselineSpring) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
                            return size + baseline;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
                            size += spring.getPreferredSize(VERTICAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
        BaselineResizeBehavior getBaselineResizeBehavior() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2274
            if (isResizable(VERTICAL)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
                if (!baselineSpring.isResizable(VERTICAL)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
                    // Spring to use for baseline isn't resizable. In this case
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2277
                    // baseline resize behavior can be determined based on how
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 21244
diff changeset
  2278
                    // preceding springs resize.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2279
                    boolean leadingResizable = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2280
                    for (Spring spring : springs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2281
                        if (spring == baselineSpring) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2282
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
                        } else if (spring.isResizable(VERTICAL)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
                            leadingResizable = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
                    boolean trailingResizable = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
                    for (int i = springs.size() - 1; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
                        Spring spring = springs.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
                        if (spring == baselineSpring) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2292
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2293
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2294
                        if (spring.isResizable(VERTICAL)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2295
                            trailingResizable = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2296
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2297
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
                    if (leadingResizable && !trailingResizable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
                        return BaselineResizeBehavior.CONSTANT_DESCENT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
                    } else if (!leadingResizable && trailingResizable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
                        return BaselineResizeBehavior.CONSTANT_ASCENT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2304
                    // If we get here, both leading and trailing springs are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
                    // resizable. Fall through to OTHER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2306
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
                    BaselineResizeBehavior brb = baselineSpring.getBaselineResizeBehavior();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2308
                    if (brb == BaselineResizeBehavior.CONSTANT_ASCENT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2309
                        for (Spring spring : springs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2310
                            if (spring == baselineSpring) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
                                return BaselineResizeBehavior.CONSTANT_ASCENT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
                            if (spring.isResizable(VERTICAL)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
                                return BaselineResizeBehavior.OTHER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
                    } else if (brb == BaselineResizeBehavior.CONSTANT_DESCENT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
                        for (int i = springs.size() - 1; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
                            Spring spring = springs.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
                            if (spring == baselineSpring) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
                                return BaselineResizeBehavior.CONSTANT_DESCENT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
                            if (spring.isResizable(VERTICAL)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2324
                                return BaselineResizeBehavior.OTHER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2325
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2326
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2327
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2329
                return BaselineResizeBehavior.OTHER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2330
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
            // Not resizable, treat as constant_ascent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
            return BaselineResizeBehavior.CONSTANT_ASCENT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
        private void checkPreferredGapValues(int pref, int max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
            if ((pref < 0 && pref != DEFAULT_SIZE && pref != PREFERRED_SIZE) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
                    (max < 0 && max != DEFAULT_SIZE && max != PREFERRED_SIZE)||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
                    (pref >= 0 && max >= 0 && pref > max)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
                throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
                        "Pref and max must be either DEFAULT_SIZE, " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
                        "PREFERRED_SIZE, or >= 0 and pref <= max");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
     * Used by SequentialGroup in calculating resizability of springs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
    private static final class SpringDelta implements Comparable<SpringDelta> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
        // Original index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
        public final int index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
        // Delta, one of pref - min or max - pref.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
        public int delta;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
        public SpringDelta(int index, int delta) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
            this.index = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
            this.delta = delta;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
        public int compareTo(SpringDelta o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
            return delta - o.delta;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2365
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
            return super.toString() + "[index=" + index + ", delta=" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
                    delta + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
     * A {@code Group} that aligns and sizes it's children.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
     * {@code ParallelGroup} aligns it's children in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
     * four possible ways: along the baseline, centered, anchored to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
     * leading edge, or anchored to the trailing edge.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2377
     * <h3>Baseline</h3>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2378
     * A {@code ParallelGroup} that aligns it's children along the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2379
     * baseline must first decide where the baseline is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
     * anchored. The baseline can either be anchored to the top, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2381
     * anchored to the bottom of the group. That is, the distance between the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
     * baseline and the beginning of the group can be a constant
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2383
     * distance, or the distance between the end of the group and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
     * baseline can be a constant distance. The possible choices
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
     * correspond to the {@code BaselineResizeBehavior} constants
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2386
     * {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
     * java.awt.Component.BaselineResizeBehavior#CONSTANT_ASCENT CONSTANT_ASCENT} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2388
     * {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2389
     * java.awt.Component.BaselineResizeBehavior#CONSTANT_DESCENT CONSTANT_DESCENT}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2391
     * The baseline anchor may be explicitly specified by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2392
     * {@code createBaselineGroup} method, or determined based on the elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2393
     * If not explicitly specified, the baseline will be anchored to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2394
     * the bottom if all the elements with a baseline, and that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2395
     * aligned to the baseline, have a baseline resize behavior of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
     * {@code CONSTANT_DESCENT}; otherwise the baseline is anchored to the top
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2397
     * of the group.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
     * Elements aligned to the baseline are resizable if they have have
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
     * a baseline resize behavior of {@code CONSTANT_ASCENT} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
     * {@code CONSTANT_DESCENT}. Elements with a baseline resize
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
     * behavior of {@code OTHER} or {@code CENTER_OFFSET} are not resizable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2404
     * The baseline is calculated based on the preferred height of each
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2405
     * of the elements that have a baseline. The baseline is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2406
     * calculated using the following algorithm:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2407
     * {@code max(maxNonBaselineHeight, maxAscent + maxDescent)}, where the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2408
     * {@code maxNonBaselineHeight} is the maximum height of all elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2409
     * that do not have a baseline, or are not aligned along the baseline.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2410
     * {@code maxAscent} is the maximum ascent (baseline) of all elements that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
     * have a baseline and are aligned along the baseline.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
     * {@code maxDescent} is the maximum descent (preferred height - baseline)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2413
     * of all elements that have a baseline and are aligned along the baseline.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2414
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2415
     * A {@code ParallelGroup} that aligns it's elements along the baseline
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2416
     * is only useful along the vertical axis. If you create a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2417
     * baseline group and use it along the horizontal axis an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2418
     * {@code IllegalStateException} is thrown when you ask
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2419
     * {@code GroupLayout} for the minimum, preferred or maximum size or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2420
     * attempt to layout the components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2421
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2422
     * Elements that are not aligned to the baseline and smaller than the size
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2423
     * of the {@code ParallelGroup} are positioned in one of three
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2424
     * ways: centered, anchored to the leading edge, or anchored to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2425
     * trailing edge.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2426
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2427
     * <h3>Non-baseline {@code ParallelGroup}</h3>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2428
     * {@code ParallelGroup}s created with an alignment other than
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2429
     * {@code BASELINE} align elements that are smaller than the size
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2430
     * of the group in one of three ways: centered, anchored to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2431
     * leading edge, or anchored to the trailing edge.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2432
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2433
     * The leading edge is based on the axis and {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2434
     * ComponentOrientation}.  For the vertical axis the top edge is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2435
     * always the leading edge, and the bottom edge is always the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2436
     * trailing edge. When the {@code ComponentOrientation} is {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2437
     * LEFT_TO_RIGHT}, the leading edge is the left edge and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2438
     * trailing edge the right edge. A {@code ComponentOrientation} of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2439
     * {@code RIGHT_TO_LEFT} flips the left and right edges. Child
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2440
     * elements are aligned based on the specified alignment the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2441
     * element was added with. If you do not specify an alignment, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2442
     * alignment specified for the {@code ParallelGroup} is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2443
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2444
     * To align elements along the baseline you {@code createBaselineGroup},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2445
     * or {@code createParallelGroup} with an alignment of {@code BASELINE}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2446
     * If the group was not created with a baseline alignment, and you attempt
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2447
     * to add an element specifying a baseline alignment, an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2448
     * {@code IllegalArgumentException} is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2449
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2450
     * @see #createParallelGroup()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2451
     * @see #createBaselineGroup(boolean,boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2452
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2453
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2454
    public class ParallelGroup extends Group {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2455
        // How children are layed out.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2456
        private final Alignment childAlignment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2457
        // Whether or not we're resizable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2458
        private final boolean resizable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2459
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2460
        ParallelGroup(Alignment childAlignment, boolean resizable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2461
            this.childAlignment = childAlignment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2462
            this.resizable = resizable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2463
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2464
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2465
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2466
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2467
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2468
        public ParallelGroup addGroup(Group group) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2469
            return (ParallelGroup)super.addGroup(group);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2470
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2471
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2472
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2473
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2474
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2475
        public ParallelGroup addComponent(Component component) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2476
            return (ParallelGroup)super.addComponent(component);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2477
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2478
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2479
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2480
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2481
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2482
        public ParallelGroup addComponent(Component component, int min, int pref,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2483
                int max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2484
            return (ParallelGroup)super.addComponent(component, min, pref, max);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2485
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2486
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2487
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2488
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2489
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2490
        public ParallelGroup addGap(int pref) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2491
            return (ParallelGroup)super.addGap(pref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2492
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2493
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2494
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2495
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2496
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2497
        public ParallelGroup addGap(int min, int pref, int max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2498
            return (ParallelGroup)super.addGap(min, pref, max);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2499
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2500
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2501
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2502
         * Adds a {@code Group} to this {@code ParallelGroup} with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2503
         * specified alignment. If the child is smaller than the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2504
         * {@code Group} it is aligned based on the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2505
         * alignment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2506
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2507
         * @param alignment the alignment
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2508
         * @param group the {@code Group} to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2509
         * @return this {@code ParallelGroup}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2510
         * @throws IllegalArgumentException if {@code alignment} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2511
         *         {@code null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2512
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2513
        public ParallelGroup addGroup(Alignment alignment, Group group) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2514
            checkChildAlignment(alignment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2515
            group.setAlignment(alignment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2516
            return (ParallelGroup)addSpring(group);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2517
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2518
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2519
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2520
         * Adds a {@code Component} to this {@code ParallelGroup} with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2521
         * the specified alignment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2522
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2523
         * @param alignment the alignment
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2524
         * @param component the {@code Component} to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2525
         * @return this {@code Group}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2526
         * @throws IllegalArgumentException if {@code alignment} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2527
         *         {@code null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2528
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2529
        public ParallelGroup addComponent(Component component,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2530
                Alignment alignment) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2531
            return addComponent(component, alignment, DEFAULT_SIZE, DEFAULT_SIZE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2532
                    DEFAULT_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2533
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2534
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2535
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2536
         * Adds a {@code Component} to this {@code ParallelGroup} with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2537
         * specified alignment and size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2538
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2539
         * @param alignment the alignment
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2540
         * @param component the {@code Component} to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2541
         * @param min the minimum size
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2542
         * @param pref the preferred size
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2543
         * @param max the maximum size
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2544
         * @throws IllegalArgumentException if {@code alignment} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2545
         *         {@code null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2546
         * @return this {@code Group}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2547
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2548
        public ParallelGroup addComponent(Component component,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2549
                Alignment alignment, int min, int pref, int max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2550
            checkChildAlignment(alignment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2551
            ComponentSpring spring = new ComponentSpring(component,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2552
                    min, pref, max);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2553
            spring.setAlignment(alignment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2554
            return (ParallelGroup)addSpring(spring);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2555
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2556
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2557
        boolean isResizable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2558
            return resizable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2559
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2560
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2561
        int operator(int a, int b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2562
            return Math.max(a, b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2563
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2564
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2565
        int calculateMinimumSize(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2566
            if (!isResizable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2567
                return getPreferredSize(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2568
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2569
            return super.calculateMinimumSize(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2570
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2571
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2572
        int calculateMaximumSize(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2573
            if (!isResizable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2574
                return getPreferredSize(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2575
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2576
            return super.calculateMaximumSize(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2577
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2578
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2579
        void setValidSize(int axis, int origin, int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2580
            for (Spring spring : springs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2581
                setChildSize(spring, axis, origin, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2582
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2583
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2584
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2585
        void setChildSize(Spring spring, int axis, int origin, int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2586
            Alignment alignment = spring.getAlignment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2587
            int springSize = Math.min(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2588
                    Math.max(spring.getMinimumSize(axis), size),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2589
                    spring.getMaximumSize(axis));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2590
            if (alignment == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2591
                alignment = childAlignment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2592
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2593
            switch (alignment) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2594
                case TRAILING:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2595
                    spring.setSize(axis, origin + size - springSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2596
                            springSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2597
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2598
                case CENTER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2599
                    spring.setSize(axis, origin +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2600
                            (size - springSize) / 2,springSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2601
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2602
                default: // LEADING, or BASELINE
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2603
                    spring.setSize(axis, origin, springSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2604
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2605
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2606
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2607
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2608
        @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2609
        void insertAutopadding(int axis,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2610
                List<AutoPreferredGapSpring> leadingPadding,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2611
                List<AutoPreferredGapSpring> trailingPadding,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2612
                List<ComponentSpring> leading, List<ComponentSpring> trailing,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2613
                boolean insert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2614
            for (Spring spring : springs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2615
                if (spring instanceof ComponentSpring) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2616
                    if (((ComponentSpring)spring).isVisible()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2617
                        for (AutoPreferredGapSpring gapSpring :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2618
                                 leadingPadding) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2619
                            gapSpring.addTarget((ComponentSpring)spring, axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2620
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2621
                        trailing.add((ComponentSpring)spring);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2622
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2623
                } else if (spring instanceof Group) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2624
                    ((Group)spring).insertAutopadding(axis, leadingPadding,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2625
                            trailingPadding, leading, trailing, insert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2626
                } else if (spring instanceof AutoPreferredGapSpring) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2627
                    ((AutoPreferredGapSpring)spring).setSources(leading);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2628
                    trailingPadding.add((AutoPreferredGapSpring)spring);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2629
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2630
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2631
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2632
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2633
        private void checkChildAlignment(Alignment alignment) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2634
            checkChildAlignment(alignment, (this instanceof BaselineGroup));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2635
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2636
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2637
        private void checkChildAlignment(Alignment alignment,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2638
                boolean allowsBaseline) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2639
            if (alignment == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2640
                throw new IllegalArgumentException("Alignment must be non-null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2641
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2642
            if (!allowsBaseline && alignment == Alignment.BASELINE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2643
                throw new IllegalArgumentException("Alignment must be one of:" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2644
                        "LEADING, TRAILING or CENTER");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2645
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2646
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2647
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2648
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2649
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2650
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2651
     * An extension of {@code ParallelGroup} that aligns its
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2652
     * constituent {@code Spring}s along the baseline.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2653
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2654
    private class BaselineGroup extends ParallelGroup {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2655
        // Whether or not all child springs have a baseline
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2656
        private boolean allSpringsHaveBaseline;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2657
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2658
        // max(spring.getBaseline()) of all springs aligned along the baseline
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2659
        // that have a baseline
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2660
        private int prefAscent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2661
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2662
        // max(spring.getPreferredSize().height - spring.getBaseline()) of all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2663
        // springs aligned along the baseline that have a baseline
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2664
        private int prefDescent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2665
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2666
        // Whether baselineAnchoredToTop was explicitly set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2667
        private boolean baselineAnchorSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2668
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2669
        // Whether the baseline is anchored to the top or the bottom.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2670
        // If anchored to the top the baseline is always at prefAscent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2671
        // otherwise the baseline is at (height - prefDescent)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2672
        private boolean baselineAnchoredToTop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2673
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2674
        // Whether or not the baseline has been calculated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2675
        private boolean calcedBaseline;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2676
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2677
        BaselineGroup(boolean resizable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2678
            super(Alignment.LEADING, resizable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2679
            prefAscent = prefDescent = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2680
            calcedBaseline = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2681
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2682
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2683
        BaselineGroup(boolean resizable, boolean baselineAnchoredToTop) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2684
            this(resizable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2685
            this.baselineAnchoredToTop = baselineAnchoredToTop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2686
            baselineAnchorSet = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2687
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2688
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2689
        void unset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2690
            super.unset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2691
            prefAscent = prefDescent = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2692
            calcedBaseline = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2693
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2694
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2695
        void setValidSize(int axis, int origin, int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2696
            checkAxis(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2697
            if (prefAscent == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2698
                super.setValidSize(axis, origin, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2699
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2700
                // do baseline layout
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2701
                baselineLayout(origin, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2702
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2703
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2704
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2705
        int calculateSize(int axis, int type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2706
            checkAxis(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2707
            if (!calcedBaseline) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2708
                calculateBaselineAndResizeBehavior();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2709
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2710
            if (type == MIN_SIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2711
                return calculateMinSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2712
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2713
            if (type == MAX_SIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2714
                return calculateMaxSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2715
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2716
            if (allSpringsHaveBaseline) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2717
                return prefAscent + prefDescent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2718
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2719
            return Math.max(prefAscent + prefDescent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2720
                    super.calculateSize(axis, type));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2721
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2722
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2723
        private void calculateBaselineAndResizeBehavior() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2724
            // calculate baseline
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2725
            prefAscent = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2726
            prefDescent = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2727
            int baselineSpringCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2728
            BaselineResizeBehavior resizeBehavior = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2729
            for (Spring spring : springs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2730
                if (spring.getAlignment() == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2731
                        spring.getAlignment() == Alignment.BASELINE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2732
                    int baseline = spring.getBaseline();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2733
                    if (baseline >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2734
                        if (spring.isResizable(VERTICAL)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2735
                            BaselineResizeBehavior brb = spring.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2736
                                    getBaselineResizeBehavior();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2737
                            if (resizeBehavior == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2738
                                resizeBehavior = brb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2739
                            } else if (brb != resizeBehavior) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2740
                                resizeBehavior = BaselineResizeBehavior.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2741
                                        CONSTANT_ASCENT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2742
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2743
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2744
                        prefAscent = Math.max(prefAscent, baseline);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2745
                        prefDescent = Math.max(prefDescent, spring.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2746
                                getPreferredSize(VERTICAL) - baseline);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2747
                        baselineSpringCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2748
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2749
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2750
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2751
            if (!baselineAnchorSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2752
                if (resizeBehavior == BaselineResizeBehavior.CONSTANT_DESCENT){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2753
                    this.baselineAnchoredToTop = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2754
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2755
                    this.baselineAnchoredToTop = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2756
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2757
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2758
            allSpringsHaveBaseline = (baselineSpringCount == springs.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2759
            calcedBaseline = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2760
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2761
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2762
        private int calculateMaxSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2763
            int maxAscent = prefAscent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2764
            int maxDescent = prefDescent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2765
            int nonBaselineMax = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2766
            for (Spring spring : springs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2767
                int baseline;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2768
                int springMax = spring.getMaximumSize(VERTICAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2769
                if ((spring.getAlignment() == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2770
                        spring.getAlignment() == Alignment.BASELINE) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2771
                        (baseline = spring.getBaseline()) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2772
                    int springPref = spring.getPreferredSize(VERTICAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2773
                    if (springPref != springMax) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2774
                        switch (spring.getBaselineResizeBehavior()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2775
                            case CONSTANT_ASCENT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2776
                                if (baselineAnchoredToTop) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2777
                                    maxDescent = Math.max(maxDescent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2778
                                            springMax - baseline);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2779
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2780
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2781
                            case CONSTANT_DESCENT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2782
                                if (!baselineAnchoredToTop) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2783
                                    maxAscent = Math.max(maxAscent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2784
                                            springMax - springPref + baseline);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2785
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2786
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2787
                            default: // CENTER_OFFSET and OTHER, not resizable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2788
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2789
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2790
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2791
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2792
                    // Not aligned along the baseline, or no baseline.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2793
                    nonBaselineMax = Math.max(nonBaselineMax, springMax);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2794
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2795
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2796
            return Math.max(nonBaselineMax, maxAscent + maxDescent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2797
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2798
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2799
        private int calculateMinSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2800
            int minAscent = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2801
            int minDescent = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2802
            int nonBaselineMin = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2803
            if (baselineAnchoredToTop) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2804
                minAscent = prefAscent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2805
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2806
                minDescent = prefDescent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2807
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2808
            for (Spring spring : springs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2809
                int springMin = spring.getMinimumSize(VERTICAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2810
                int baseline;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2811
                if ((spring.getAlignment() == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2812
                        spring.getAlignment() == Alignment.BASELINE) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2813
                        (baseline = spring.getBaseline()) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2814
                    int springPref = spring.getPreferredSize(VERTICAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2815
                    BaselineResizeBehavior brb = spring.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2816
                            getBaselineResizeBehavior();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2817
                    switch (brb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2818
                        case CONSTANT_ASCENT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2819
                            if (baselineAnchoredToTop) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2820
                                minDescent = Math.max(springMin - baseline,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2821
                                        minDescent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2822
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2823
                                minAscent = Math.max(baseline, minAscent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2824
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2825
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2826
                        case CONSTANT_DESCENT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2827
                            if (!baselineAnchoredToTop) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2828
                                minAscent = Math.max(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2829
                                        baseline - (springPref - springMin),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2830
                                        minAscent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2831
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2832
                                minDescent = Math.max(springPref - baseline,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2833
                                        minDescent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2834
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2835
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2836
                        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2837
                            // CENTER_OFFSET and OTHER are !resizable, use
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2838
                            // the preferred size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2839
                            minAscent = Math.max(baseline, minAscent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2840
                            minDescent = Math.max(springPref - baseline,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2841
                                    minDescent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2842
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2843
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2844
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2845
                    // Not aligned along the baseline, or no baseline.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2846
                    nonBaselineMin = Math.max(nonBaselineMin, springMin);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2847
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2848
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2849
            return Math.max(nonBaselineMin, minAscent + minDescent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2850
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2851
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2852
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2853
         * Lays out springs that have a baseline along the baseline.  All
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2854
         * others are centered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2855
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2856
        private void baselineLayout(int origin, int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2857
            int ascent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2858
            int descent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2859
            if (baselineAnchoredToTop) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2860
                ascent = prefAscent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2861
                descent = size - ascent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2862
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2863
                ascent = size - prefDescent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2864
                descent = prefDescent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2865
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2866
            for (Spring spring : springs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2867
                Alignment alignment = spring.getAlignment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2868
                if (alignment == null || alignment == Alignment.BASELINE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2869
                    int baseline = spring.getBaseline();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2870
                    if (baseline >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2871
                        int springMax = spring.getMaximumSize(VERTICAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2872
                        int springPref = spring.getPreferredSize(VERTICAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2873
                        int height = springPref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2874
                        int y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2875
                        switch(spring.getBaselineResizeBehavior()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2876
                            case CONSTANT_ASCENT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2877
                                y = origin + ascent - baseline;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2878
                                height = Math.min(descent, springMax -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2879
                                        baseline) + baseline;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2880
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2881
                            case CONSTANT_DESCENT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2882
                                height = Math.min(ascent, springMax -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2883
                                        springPref + baseline) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2884
                                        (springPref - baseline);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2885
                                y = origin + ascent +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2886
                                        (springPref - baseline) - height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2887
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2888
                            default: // CENTER_OFFSET & OTHER, not resizable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2889
                                y = origin + ascent - baseline;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2890
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2891
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2892
                        spring.setSize(VERTICAL, y, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2893
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2894
                        setChildSize(spring, VERTICAL, origin, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2895
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2896
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2897
                    setChildSize(spring, VERTICAL, origin, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2898
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2899
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2900
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2901
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2902
        int getBaseline() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2903
            if (springs.size() > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2904
                // Force the baseline to be calculated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2905
                getPreferredSize(VERTICAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2906
                return prefAscent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2907
            } else if (springs.size() == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2908
                return springs.get(0).getBaseline();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2909
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2910
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2911
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2912
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2913
        BaselineResizeBehavior getBaselineResizeBehavior() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2914
            if (springs.size() == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2915
                return springs.get(0).getBaselineResizeBehavior();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2916
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2917
            if (baselineAnchoredToTop) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2918
                return BaselineResizeBehavior.CONSTANT_ASCENT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2919
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2920
            return BaselineResizeBehavior.CONSTANT_DESCENT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2921
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2922
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2923
        // If the axis is VERTICAL, throws an IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2924
        private void checkAxis(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2925
            if (axis == HORIZONTAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2926
                throw new IllegalStateException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2927
                        "Baseline must be used along vertical axis");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2928
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2929
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2930
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2931
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2932
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2933
    private final class ComponentSpring extends Spring {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2934
        private Component component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2935
        private int origin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2936
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2937
        // min/pref/max are either a value >= 0 or one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2938
        // DEFAULT_SIZE or PREFERRED_SIZE
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2939
        private final int min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2940
        private final int pref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2941
        private final int max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2942
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2943
        // Baseline for the component, computed as necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2944
        private int baseline = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2945
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2946
        // Whether or not the size has been requested yet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2947
        private boolean installed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2948
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2949
        private ComponentSpring(Component component, int min, int pref,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2950
                int max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2951
            this.component = component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2952
            if (component == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2953
                throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2954
                        "Component must be non-null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2955
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2956
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2957
            checkSize(min, pref, max, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2958
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2959
            this.min = min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2960
            this.max = max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2961
            this.pref = pref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2962
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2963
            // getComponentInfo makes sure component is a child of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2964
            // Container GroupLayout is the LayoutManager for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2965
            getComponentInfo(component);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2966
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2967
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2968
        int calculateMinimumSize(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2969
            if (isLinked(axis)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2970
                return getLinkSize(axis, MIN_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2971
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2972
            return calculateNonlinkedMinimumSize(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2973
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2974
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2975
        int calculatePreferredSize(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2976
            if (isLinked(axis)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2977
                return getLinkSize(axis, PREF_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2978
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2979
            int min = getMinimumSize(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2980
            int pref = calculateNonlinkedPreferredSize(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2981
            int max = getMaximumSize(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2982
            return Math.min(max, Math.max(min, pref));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2983
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2984
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2985
        int calculateMaximumSize(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2986
            if (isLinked(axis)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2987
                return getLinkSize(axis, MAX_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2988
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2989
            return Math.max(getMinimumSize(axis),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2990
                    calculateNonlinkedMaximumSize(axis));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2991
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2992
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2993
        boolean isVisible() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2994
            return getComponentInfo(getComponent()).isVisible();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2995
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2996
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2997
        int calculateNonlinkedMinimumSize(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2998
            if (!isVisible()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2999
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3000
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3001
            if (min >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3002
                return min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3003
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3004
            if (min == PREFERRED_SIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3005
                return calculateNonlinkedPreferredSize(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3006
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3007
            assert (min == DEFAULT_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3008
            return getSizeAlongAxis(axis, component.getMinimumSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3009
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3010
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3011
        int calculateNonlinkedPreferredSize(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3012
            if (!isVisible()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3013
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3014
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3015
            if (pref >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3016
                return pref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3017
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3018
            assert (pref == DEFAULT_SIZE || pref == PREFERRED_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3019
            return getSizeAlongAxis(axis, component.getPreferredSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3020
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3021
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3022
        int calculateNonlinkedMaximumSize(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3023
            if (!isVisible()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3024
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3025
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3026
            if (max >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3027
                return max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3028
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3029
            if (max == PREFERRED_SIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3030
                return calculateNonlinkedPreferredSize(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3031
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3032
            assert (max == DEFAULT_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3033
            return getSizeAlongAxis(axis, component.getMaximumSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3034
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3035
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3036
        private int getSizeAlongAxis(int axis, Dimension size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3037
            return (axis == HORIZONTAL) ? size.width : size.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3038
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3039
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3040
        private int getLinkSize(int axis, int type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3041
            if (!isVisible()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3042
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3043
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3044
            ComponentInfo ci = getComponentInfo(component);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3045
            return ci.getLinkSize(axis, type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3046
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3047
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3048
        void setSize(int axis, int origin, int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3049
            super.setSize(axis, origin, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3050
            this.origin = origin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3051
            if (size == UNSET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3052
                baseline = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3053
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3054
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3055
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3056
        int getOrigin() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3057
            return origin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3058
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3059
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3060
        void setComponent(Component component) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3061
            this.component = component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3062
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3063
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3064
        Component getComponent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3065
            return component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3066
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3067
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3068
        int getBaseline() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3069
            if (baseline == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3070
                Spring horizontalSpring = getComponentInfo(component).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3071
                        horizontalSpring;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3072
                int width = horizontalSpring.getPreferredSize(HORIZONTAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3073
                int height = getPreferredSize(VERTICAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3074
                if (width > 0 && height > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3075
                    baseline = component.getBaseline(width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3076
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3077
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3078
            return baseline;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3079
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3080
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3081
        BaselineResizeBehavior getBaselineResizeBehavior() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3082
            return getComponent().getBaselineResizeBehavior();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3083
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3084
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3085
        private boolean isLinked(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3086
            return getComponentInfo(component).isLinked(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3087
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3088
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3089
        void installIfNecessary(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3090
            if (!installed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3091
                installed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3092
                if (axis == HORIZONTAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3093
                    getComponentInfo(component).horizontalSpring = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3094
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3095
                    getComponentInfo(component).verticalSpring = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3096
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3097
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3098
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3099
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3100
        @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3101
        boolean willHaveZeroSize(boolean treatAutopaddingAsZeroSized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3102
            return !isVisible();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3105
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3106
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3108
     * Spring representing the preferred distance between two components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3110
    private class PreferredGapSpring extends Spring {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3111
        private final JComponent source;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3112
        private final JComponent target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3113
        private final ComponentPlacement type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3114
        private final int pref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3115
        private final int max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3116
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3117
        PreferredGapSpring(JComponent source, JComponent target,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3118
                ComponentPlacement type, int pref, int max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3119
            this.source = source;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3120
            this.target = target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3121
            this.type = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3122
            this.pref = pref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3123
            this.max = max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3125
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3126
        int calculateMinimumSize(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3127
            return getPadding(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3129
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3130
        int calculatePreferredSize(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3131
            if (pref == DEFAULT_SIZE || pref == PREFERRED_SIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3132
                return getMinimumSize(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3133
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3134
            int min = getMinimumSize(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3135
            int max = getMaximumSize(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3136
            return Math.min(max, Math.max(min, pref));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3138
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3139
        int calculateMaximumSize(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3140
            if (max == PREFERRED_SIZE || max == DEFAULT_SIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3141
                return getPadding(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3142
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3143
            return Math.max(getMinimumSize(axis), max);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3145
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3146
        private int getPadding(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3147
            int position;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3148
            if (axis == HORIZONTAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3149
                position = SwingConstants.EAST;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3150
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3151
                position = SwingConstants.SOUTH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3152
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3153
            return getLayoutStyle0().getPreferredGap(source,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3154
                    target, type, position, host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3156
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3157
        @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3158
        boolean willHaveZeroSize(boolean treatAutopaddingAsZeroSized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3159
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3162
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3163
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3164
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3165
     * Spring represented a certain amount of space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3166
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3167
    private class GapSpring extends Spring {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3168
        private final int min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3169
        private final int pref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3170
        private final int max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3171
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3172
        GapSpring(int min, int pref, int max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3173
            checkSize(min, pref, max, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3174
            this.min = min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3175
            this.pref = pref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3176
            this.max = max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3178
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3179
        int calculateMinimumSize(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3180
            if (min == PREFERRED_SIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3181
                return getPreferredSize(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3182
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3183
            return min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3185
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3186
        int calculatePreferredSize(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3187
            return pref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3189
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3190
        int calculateMaximumSize(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3191
            if (max == PREFERRED_SIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3192
                return getPreferredSize(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3193
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3194
            return max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3196
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3197
        @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3198
        boolean willHaveZeroSize(boolean treatAutopaddingAsZeroSized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3199
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3202
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3203
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3204
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3205
     * Spring reprensenting the distance between any number of sources and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3206
     * targets.  The targets and sources are computed during layout.  An
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3207
     * instance of this can either be dynamically created when
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3208
     * autocreatePadding is true, or explicitly created by the developer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3209
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3210
    private class AutoPreferredGapSpring extends Spring {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3211
        List<ComponentSpring> sources;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3212
        ComponentSpring source;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3213
        private List<AutoPreferredGapMatch> matches;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3214
        int size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3215
        int lastSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3216
        private final int pref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3217
        private final int max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3218
        // Type of gap
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3219
        private ComponentPlacement type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3220
        private boolean userCreated;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3221
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3222
        private AutoPreferredGapSpring() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3223
            this.pref = PREFERRED_SIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3224
            this.max = PREFERRED_SIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3225
            this.type = ComponentPlacement.RELATED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3226
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3227
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3228
        AutoPreferredGapSpring(int pref, int max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3229
            this.pref = pref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3230
            this.max = max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3232
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3233
        AutoPreferredGapSpring(ComponentPlacement type, int pref, int max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3234
            this.type = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3235
            this.pref = pref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3236
            this.max = max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3237
            this.userCreated = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3238
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3239
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3240
        public void setSource(ComponentSpring source) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3241
            this.source = source;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3243
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3244
        public void setSources(List<ComponentSpring> sources) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3245
            this.sources = new ArrayList<ComponentSpring>(sources);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3247
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3248
        public void setUserCreated(boolean userCreated) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3249
            this.userCreated = userCreated;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3250
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3251
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3252
        public boolean getUserCreated() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3253
            return userCreated;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3254
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3255
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3256
        void unset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3257
            lastSize = getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3258
            super.unset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3259
            size = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3260
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3261
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3262
        public void reset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3263
            size = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3264
            sources = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3265
            source = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3266
            matches = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3267
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3268
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3269
        public void calculatePadding(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3270
            size = UNSET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3271
            int maxPadding = UNSET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3272
            if (matches != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3273
                LayoutStyle p = getLayoutStyle0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3274
                int position;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3275
                if (axis == HORIZONTAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3276
                    if (isLeftToRight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3277
                        position = SwingConstants.EAST;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3278
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3279
                        position = SwingConstants.WEST;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3280
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3281
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3282
                    position = SwingConstants.SOUTH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3283
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3284
                for (int i = matches.size() - 1; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3285
                    AutoPreferredGapMatch match = matches.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3286
                    maxPadding = Math.max(maxPadding,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3287
                            calculatePadding(p, position, match.source,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3288
                            match.target));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3289
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3290
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3291
            if (size == UNSET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3292
                size = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3293
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3294
            if (maxPadding == UNSET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3295
                maxPadding = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3296
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3297
            if (lastSize != UNSET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3298
                size += Math.min(maxPadding, lastSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3299
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3300
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3301
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3302
        private int calculatePadding(LayoutStyle p, int position,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3303
                ComponentSpring source,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3304
                ComponentSpring target) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3305
            int delta = target.getOrigin() - (source.getOrigin() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3306
                    source.getSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3307
            if (delta >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3308
                int padding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3309
                if ((source.getComponent() instanceof JComponent) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3310
                        (target.getComponent() instanceof JComponent)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3311
                    padding = p.getPreferredGap(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3312
                            (JComponent)source.getComponent(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3313
                            (JComponent)target.getComponent(), type, position,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3314
                            host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3315
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3316
                    padding = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3317
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3318
                if (padding > delta) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3319
                    size = Math.max(size, padding - delta);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3320
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3321
                return padding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3322
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3323
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3324
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3325
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3326
        public void addTarget(ComponentSpring spring, int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3327
            int oAxis = (axis == HORIZONTAL) ? VERTICAL : HORIZONTAL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3328
            if (source != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3329
                if (areParallelSiblings(source.getComponent(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3330
                        spring.getComponent(), oAxis)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3331
                    addValidTarget(source, spring);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3332
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3333
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3334
                Component component = spring.getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3335
                for (int counter = sources.size() - 1; counter >= 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3336
                         counter--){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3337
                    ComponentSpring source = sources.get(counter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3338
                    if (areParallelSiblings(source.getComponent(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3339
                            component, oAxis)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3340
                        addValidTarget(source, spring);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3341
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3342
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3343
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3344
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3345
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3346
        private void addValidTarget(ComponentSpring source,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3347
                ComponentSpring target) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3348
            if (matches == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3349
                matches = new ArrayList<AutoPreferredGapMatch>(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3350
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3351
            matches.add(new AutoPreferredGapMatch(source, target));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3352
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3353
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3354
        int calculateMinimumSize(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3355
            return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3356
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3357
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3358
        int calculatePreferredSize(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3359
            if (pref == PREFERRED_SIZE || pref == DEFAULT_SIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3360
                return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3361
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3362
            return Math.max(size, pref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3363
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3364
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3365
        int calculateMaximumSize(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3366
            if (max >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3367
                return Math.max(getPreferredSize(axis), max);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3368
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3369
            return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3370
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3371
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3372
        String getMatchDescription() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3373
            return (matches == null) ? "" : matches.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3374
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3375
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3376
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3377
            return super.toString() + getMatchDescription();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3378
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3379
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3380
        @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3381
        boolean willHaveZeroSize(boolean treatAutopaddingAsZeroSized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3382
            return treatAutopaddingAsZeroSized;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3383
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3384
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3385
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3386
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3387
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3388
     * Represents two springs that should have autopadding inserted between
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3389
     * them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3390
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3391
    private final static class AutoPreferredGapMatch {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3392
        public final ComponentSpring source;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3393
        public final ComponentSpring target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3394
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3395
        AutoPreferredGapMatch(ComponentSpring source, ComponentSpring target) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3396
            this.source = source;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3397
            this.target = target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3398
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3399
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3400
        private String toString(ComponentSpring spring) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3401
            return spring.getComponent().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3402
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3403
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3404
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3405
            return "[" + toString(source) + "-" + toString(target) + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3406
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3407
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3408
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3409
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3410
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3411
     * An extension of AutopaddingSpring used for container level padding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3412
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3413
    private class ContainerAutoPreferredGapSpring extends
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3414
            AutoPreferredGapSpring {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3415
        private List<ComponentSpring> targets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3416
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3417
        ContainerAutoPreferredGapSpring() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3418
            super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3419
            setUserCreated(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3420
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3421
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3422
        ContainerAutoPreferredGapSpring(int pref, int max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3423
            super(pref, max);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3424
            setUserCreated(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3425
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3426
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3427
        public void addTarget(ComponentSpring spring, int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3428
            if (targets == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3429
                targets = new ArrayList<ComponentSpring>(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3430
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3431
            targets.add(spring);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3432
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3433
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3434
        public void calculatePadding(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3435
            LayoutStyle p = getLayoutStyle0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3436
            int maxPadding = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3437
            int position;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3438
            size = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3439
            if (targets != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3440
                // Leading
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3441
                if (axis == HORIZONTAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3442
                    if (isLeftToRight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3443
                        position = SwingConstants.WEST;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3444
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3445
                        position = SwingConstants.EAST;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3446
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3447
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3448
                    position = SwingConstants.SOUTH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3449
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3450
                for (int i = targets.size() - 1; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3451
                    ComponentSpring targetSpring = targets.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3452
                    int padding = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3453
                    if (targetSpring.getComponent() instanceof JComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3454
                        padding = p.getContainerGap(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3455
                                (JComponent)targetSpring.getComponent(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3456
                                position, host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3457
                        maxPadding = Math.max(padding, maxPadding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3458
                        padding -= targetSpring.getOrigin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3459
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3460
                        maxPadding = Math.max(padding, maxPadding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3461
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3462
                    size = Math.max(size, padding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3463
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3464
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3465
                // Trailing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3466
                if (axis == HORIZONTAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3467
                    if (isLeftToRight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3468
                        position = SwingConstants.EAST;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3469
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3470
                        position = SwingConstants.WEST;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3471
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3472
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3473
                    position = SwingConstants.SOUTH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3474
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3475
                if (sources != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3476
                    for (int i = sources.size() - 1; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3477
                        ComponentSpring sourceSpring = sources.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3478
                        maxPadding = Math.max(maxPadding,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3479
                                updateSize(p, sourceSpring, position));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3480
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3481
                } else if (source != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3482
                    maxPadding = updateSize(p, source, position);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3483
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3484
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3485
            if (lastSize != UNSET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3486
                size += Math.min(maxPadding, lastSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3487
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3488
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3489
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3490
        private int updateSize(LayoutStyle p, ComponentSpring sourceSpring,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3491
                int position) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3492
            int padding = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3493
            if (sourceSpring.getComponent() instanceof JComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3494
                padding = p.getContainerGap(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3495
                        (JComponent)sourceSpring.getComponent(), position,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3496
                        host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3497
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3498
            int delta = Math.max(0, getParent().getSize() -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3499
                    sourceSpring.getSize() - sourceSpring.getOrigin());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3500
            size = Math.max(size, padding - delta);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3501
            return padding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3502
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3503
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3504
        String getMatchDescription() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3505
            if (targets != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3506
                return "leading: " + targets.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3507
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3508
            if (sources != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3509
                return "trailing: " + sources.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3510
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3511
            return "--";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3512
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3513
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3514
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3515
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3516
    // LinkInfo contains the set of ComponentInfosthat are linked along a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3517
    // particular axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3518
    private static class LinkInfo {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3519
        private final int axis;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3520
        private final List<ComponentInfo> linked;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3521
        private int size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3522
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3523
        LinkInfo(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3524
            linked = new ArrayList<ComponentInfo>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3525
            size = UNSET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3526
            this.axis = axis;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3527
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3528
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3529
        public void add(ComponentInfo child) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3530
            LinkInfo childMaster = child.getLinkInfo(axis, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3531
            if (childMaster == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3532
                linked.add(child);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3533
                child.setLinkInfo(axis, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3534
            } else if (childMaster != this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3535
                linked.addAll(childMaster.linked);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3536
                for (ComponentInfo childInfo : childMaster.linked) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3537
                    childInfo.setLinkInfo(axis, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3538
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3539
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3540
            clearCachedSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3541
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3542
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3543
        public void remove(ComponentInfo info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3544
            linked.remove(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3545
            info.setLinkInfo(axis, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3546
            if (linked.size() == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3547
                linked.get(0).setLinkInfo(axis, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3548
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3549
            clearCachedSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3550
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3551
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3552
        public void clearCachedSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3553
            size = UNSET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3554
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3555
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3556
        public int getSize(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3557
            if (size == UNSET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3558
                size = calculateLinkedSize(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3559
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3560
            return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3561
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3562
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3563
        private int calculateLinkedSize(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3564
            int size = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3565
            for (ComponentInfo info : linked) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3566
                ComponentSpring spring;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3567
                if (axis == HORIZONTAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3568
                    spring = info.horizontalSpring;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3569
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3570
                    assert (axis == VERTICAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3571
                    spring = info.verticalSpring;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3572
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3573
                size = Math.max(size,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3574
                        spring.calculateNonlinkedPreferredSize(axis));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3575
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3576
            return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3577
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3578
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3579
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3580
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3581
     * Tracks the horizontal/vertical Springs for a Component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3582
     * This class is also used to handle Springs that have their sizes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3583
     * linked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3584
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3585
    private class ComponentInfo {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3586
        // Component being layed out
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3587
        private Component component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3588
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3589
        ComponentSpring horizontalSpring;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3590
        ComponentSpring verticalSpring;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3591
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3592
        // If the component's size is linked to other components, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3593
        // horizontalMaster and/or verticalMaster reference the group of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3594
        // linked components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3595
        private LinkInfo horizontalMaster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3596
        private LinkInfo verticalMaster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3597
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3598
        private boolean visible;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3599
        private Boolean honorsVisibility;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3600
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3601
        ComponentInfo(Component component) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3602
            this.component = component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3603
            updateVisibility();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3604
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3605
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3606
        public void dispose() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3607
            // Remove horizontal/vertical springs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3608
            removeSpring(horizontalSpring);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3609
            horizontalSpring = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3610
            removeSpring(verticalSpring);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3611
            verticalSpring = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3612
            // Clean up links
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3613
            if (horizontalMaster != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3614
                horizontalMaster.remove(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3615
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3616
            if (verticalMaster != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3617
                verticalMaster.remove(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3618
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3619
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3620
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3621
        void setHonorsVisibility(Boolean honorsVisibility) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3622
            this.honorsVisibility = honorsVisibility;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3623
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3624
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3625
        private void removeSpring(Spring spring) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3626
            if (spring != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3627
                ((Group)spring.getParent()).springs.remove(spring);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3628
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3629
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3630
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3631
        public boolean isVisible() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3632
            return visible;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3633
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3634
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3635
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3636
         * Updates the cached visibility.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3637
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3638
         * @return true if the visibility changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3639
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3640
        boolean updateVisibility() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3641
            boolean honorsVisibility;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3642
            if (this.honorsVisibility == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3643
                honorsVisibility = GroupLayout.this.getHonorsVisibility();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3644
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3645
                honorsVisibility = this.honorsVisibility;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3646
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3647
            boolean newVisible = (honorsVisibility) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3648
                component.isVisible() : true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3649
            if (visible != newVisible) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3650
                visible = newVisible;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3651
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3652
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3653
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3654
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3655
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3656
        public void setBounds(Insets insets, int parentWidth, boolean ltr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3657
            int x = horizontalSpring.getOrigin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3658
            int w = horizontalSpring.getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3659
            int y = verticalSpring.getOrigin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3660
            int h = verticalSpring.getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3661
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3662
            if (!ltr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3663
                x = parentWidth - x - w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3664
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3665
            component.setBounds(x + insets.left, y + insets.top, w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3666
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3667
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3668
        public void setComponent(Component component) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3669
            this.component = component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3670
            if (horizontalSpring != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3671
                horizontalSpring.setComponent(component);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3672
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3673
            if (verticalSpring != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3674
                verticalSpring.setComponent(component);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3675
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3676
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3677
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3678
        public Component getComponent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3679
            return component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3680
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3681
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3682
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3683
         * Returns true if this component has its size linked to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3684
         * other components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3685
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3686
        public boolean isLinked(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3687
            if (axis == HORIZONTAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3688
                return horizontalMaster != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3689
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3690
            assert (axis == VERTICAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3691
            return (verticalMaster != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3692
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3693
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3694
        private void setLinkInfo(int axis, LinkInfo linkInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3695
            if (axis == HORIZONTAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3696
                horizontalMaster = linkInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3697
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3698
                assert (axis == VERTICAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3699
                verticalMaster = linkInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3700
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3701
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3702
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3703
        public LinkInfo getLinkInfo(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3704
            return getLinkInfo(axis, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3705
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3706
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3707
        private LinkInfo getLinkInfo(int axis, boolean create) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3708
            if (axis == HORIZONTAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3709
                if (horizontalMaster == null && create) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3710
                    // horizontalMaster field is directly set by adding
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3711
                    // us to the LinkInfo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3712
                    new LinkInfo(HORIZONTAL).add(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3713
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3714
                return horizontalMaster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3715
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3716
                assert (axis == VERTICAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3717
                if (verticalMaster == null && create) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3718
                    // verticalMaster field is directly set by adding
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3719
                    // us to the LinkInfo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3720
                    new LinkInfo(VERTICAL).add(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3721
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3722
                return verticalMaster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3723
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3724
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3725
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3726
        public void clearCachedSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3727
            if (horizontalMaster != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3728
                horizontalMaster.clearCachedSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3729
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3730
            if (verticalMaster != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3731
                verticalMaster.clearCachedSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3732
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3733
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3734
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3735
        int getLinkSize(int axis, int type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3736
            if (axis == HORIZONTAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3737
                return horizontalMaster.getSize(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3738
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3739
                assert (axis == VERTICAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3740
                return verticalMaster.getSize(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3741
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3742
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3743
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3744
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3745
}