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