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