0N/A/*
3261N/A * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/Apackage javax.swing;
0N/A
0N/Aimport java.awt.Component;
0N/Aimport java.awt.Container;
0N/Aimport java.awt.Dimension;
0N/Aimport java.awt.Insets;
0N/Aimport java.awt.LayoutManager2;
0N/Aimport java.util.*;
0N/Aimport static java.awt.Component.BaselineResizeBehavior;
0N/Aimport static javax.swing.LayoutStyle.ComponentPlacement;
0N/Aimport static javax.swing.SwingConstants.HORIZONTAL;
0N/Aimport static javax.swing.SwingConstants.VERTICAL;
0N/A
0N/A/**
0N/A * {@code GroupLayout} is a {@code LayoutManager} that hierarchically
0N/A * groups components in order to position them in a {@code Container}.
0N/A * {@code GroupLayout} is intended for use by builders, but may be
0N/A * hand-coded as well.
0N/A * Grouping is done by instances of the {@link Group Group} class. {@code
0N/A * GroupLayout} supports two types of groups. A sequential group
0N/A * positions its child elements sequentially, one after another. A
0N/A * parallel group aligns its child elements in one of four ways.
0N/A * <p>
0N/A * Each group may contain any number of elements, where an element is
0N/A * a {@code Group}, {@code Component}, or gap. A gap can be thought
0N/A * of as an invisible component with a minimum, preferred and maximum
0N/A * size. In addition {@code GroupLayout} supports a preferred gap,
0N/A * whose value comes from {@code LayoutStyle}.
0N/A * <p>
0N/A * Elements are similar to a spring. Each element has a range as
0N/A * specified by a minimum, preferred and maximum. Gaps have either a
0N/A * developer-specified range, or a range determined by {@code
0N/A * LayoutStyle}. The range for {@code Component}s is determined from
0N/A * the {@code Component}'s {@code getMinimumSize}, {@code
0N/A * getPreferredSize} and {@code getMaximumSize} methods. In addition,
0N/A * when adding {@code Component}s you may specify a particular range
0N/A * to use instead of that from the component. The range for a {@code
0N/A * Group} is determined by the type of group. A {@code ParallelGroup}'s
0N/A * range is the maximum of the ranges of its elements. A {@code
0N/A * SequentialGroup}'s range is the sum of the ranges of its elements.
0N/A * <p>
0N/A * {@code GroupLayout} treats each axis independently. That is, there
0N/A * is a group representing the horizontal axis, and a group
0N/A * representing the vertical axis. The horizontal group is
0N/A * responsible for determining the minimum, preferred and maximum size
0N/A * along the horizontal axis as well as setting the x and width of the
0N/A * components contained in it. The vertical group is responsible for
0N/A * determining the minimum, preferred and maximum size along the
0N/A * vertical axis as well as setting the y and height of the
0N/A * components contained in it. Each {@code Component} must exist in both
0N/A * a horizontal and vertical group, otherwise an {@code IllegalStateException}
0N/A * is thrown during layout, or when the minimum, preferred or
0N/A * maximum size is requested.
0N/A * <p>
0N/A * The following diagram shows a sequential group along the horizontal
0N/A * axis. The sequential group contains three components. A parallel group
0N/A * was used along the vertical axis.
0N/A * <p align="center">
0N/A * <img src="doc-files/groupLayout.1.gif">
0N/A * <p>
0N/A * To reinforce that each axis is treated independently the diagram shows
0N/A * the range of each group and element along each axis. The
0N/A * range of each component has been projected onto the axes,
0N/A * and the groups are rendered in blue (horizontal) and red (vertical).
0N/A * For readability there is a gap between each of the elements in the
0N/A * sequential group.
0N/A * <p>
0N/A * The sequential group along the horizontal axis is rendered as a solid
0N/A * blue line. Notice the sequential group is the sum of the children elements
0N/A * it contains.
0N/A * <p>
0N/A * Along the vertical axis the parallel group is the maximum of the height
0N/A * of each of the components. As all three components have the same height,
0N/A * the parallel group has the same height.
0N/A * <p>
0N/A * The following diagram shows the same three components, but with the
0N/A * parallel group along the horizontal axis and the sequential group along
0N/A * the vertical axis.
0N/A * <p>
0N/A * <p align="center">
0N/A * <img src="doc-files/groupLayout.2.gif">
0N/A * <p>
0N/A * As {@code c1} is the largest of the three components, the parallel
0N/A * group is sized to {@code c1}. As {@code c2} and {@code c3} are smaller
0N/A * than {@code c1} they are aligned based on the alignment specified
0N/A * for the component (if specified) or the default alignment of the
0N/A * parallel group. In the diagram {@code c2} and {@code c3} were created
0N/A * with an alignment of {@code LEADING}. If the component orientation were
0N/A * right-to-left then {@code c2} and {@code c3} would be positioned on
0N/A * the opposite side.
0N/A * <p>
0N/A * The following diagram shows a sequential group along both the horizontal
0N/A * and vertical axis.
0N/A * <p align="center">
0N/A * <img src="doc-files/groupLayout.3.gif">
0N/A * <p>
0N/A * {@code GroupLayout} provides the ability to insert gaps between
0N/A * {@code Component}s. The size of the gap is determined by an
0N/A * instance of {@code LayoutStyle}. This may be turned on using the
0N/A * {@code setAutoCreateGaps} method. Similarly, you may use
0N/A * the {@code setAutoCreateContainerGaps} method to insert gaps
0N/A * between components that touch the edge of the parent container and the
0N/A * container.
0N/A * <p>
0N/A * The following builds a panel consisting of two labels in
0N/A * one column, followed by two textfields in the next column:
0N/A * <pre>
0N/A * JComponent panel = ...;
0N/A * GroupLayout layout = new GroupLayout(panel);
0N/A * panel.setLayout(layout);
0N/A *
0N/A * // Turn on automatically adding gaps between components
0N/A * layout.setAutoCreateGaps(true);
0N/A *
0N/A * // Turn on automatically creating gaps between components that touch
0N/A * // the edge of the container and the container.
0N/A * layout.setAutoCreateContainerGaps(true);
0N/A *
0N/A * // Create a sequential group for the horizontal axis.
0N/A *
0N/A * GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
0N/A *
0N/A * // The sequential group in turn contains two parallel groups.
0N/A * // One parallel group contains the labels, the other the text fields.
0N/A * // Putting the labels in a parallel group along the horizontal axis
0N/A * // positions them at the same x location.
0N/A * //
0N/A * // Variable indentation is used to reinforce the level of grouping.
0N/A * hGroup.addGroup(layout.createParallelGroup().
0N/A * addComponent(label1).addComponent(label2));
0N/A * hGroup.addGroup(layout.createParallelGroup().
0N/A * addComponent(tf1).addComponent(tf2));
0N/A * layout.setHorizontalGroup(hGroup);
0N/A *
0N/A * // Create a sequential group for the vertical axis.
0N/A * GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();
0N/A *
0N/A * // The sequential group contains two parallel groups that align
0N/A * // the contents along the baseline. The first parallel group contains
0N/A * // the first label and text field, and the second parallel group contains
0N/A * // the second label and text field. By using a sequential group
0N/A * // the labels and text fields are positioned vertically after one another.
0N/A * vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
0N/A * addComponent(label1).addComponent(tf1));
0N/A * vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
0N/A * addComponent(label2).addComponent(tf2));
0N/A * layout.setVerticalGroup(vGroup);
0N/A * </pre>
0N/A * <p>
0N/A * When run the following is produced.
0N/A * <p align="center">
0N/A * <img src="doc-files/groupLayout.example.png">
0N/A * <p>
0N/A * This layout consists of the following.
0N/A * <ul><li>The horizontal axis consists of a sequential group containing two
0N/A * parallel groups. The first parallel group contains the labels,
0N/A * and the second parallel group contains the text fields.
0N/A * <li>The vertical axis consists of a sequential group
0N/A * containing two parallel groups. The parallel groups are configured
0N/A * to align their components along the baseline. The first parallel
0N/A * group contains the first label and first text field, and
0N/A * the second group consists of the second label and second
0N/A * text field.
0N/A * </ul>
0N/A * There are a couple of things to notice in this code:
0N/A * <ul>
0N/A * <li>You need not explicitly add the components to the container; this
0N/A * is indirectly done by using one of the {@code add} methods of
0N/A * {@code Group}.
0N/A * <li>The various {@code add} methods return
0N/A * the caller. This allows for easy chaining of invocations. For
0N/A * example, {@code group.addComponent(label1).addComponent(label2);} is
0N/A * equivalent to
0N/A * {@code group.addComponent(label1); group.addComponent(label2);}.
0N/A * <li>There are no public constructors for {@code Group}s; instead
0N/A * use the create methods of {@code GroupLayout}.
0N/A * </ul>
0N/A *
0N/A * @author Tomas Pavek
0N/A * @author Jan Stola
0N/A * @author Scott Violet
0N/A * @since 1.6
0N/A */
0N/Apublic class GroupLayout implements LayoutManager2 {
0N/A // Used in size calculations
0N/A private static final int MIN_SIZE = 0;
0N/A
0N/A private static final int PREF_SIZE = 1;
0N/A
0N/A private static final int MAX_SIZE = 2;
0N/A
0N/A // Used by prepare, indicates min, pref or max isn't going to be used.
0N/A private static final int SPECIFIC_SIZE = 3;
0N/A
0N/A private static final int UNSET = Integer.MIN_VALUE;
0N/A
0N/A /**
0N/A * Indicates the size from the component or gap should be used for a
0N/A * particular range value.
0N/A *
0N/A * @see Group
0N/A */
0N/A public static final int DEFAULT_SIZE = -1;
0N/A
0N/A /**
0N/A * Indicates the preferred size from the component or gap should
0N/A * be used for a particular range value.
0N/A *
0N/A * @see Group
0N/A */
0N/A public static final int PREFERRED_SIZE = -2;
0N/A
0N/A // Whether or not we automatically try and create the preferred
0N/A // padding between components.
0N/A private boolean autocreatePadding;
0N/A
0N/A // Whether or not we automatically try and create the preferred
0N/A // padding between components the touch the edge of the container and
0N/A // the container.
0N/A private boolean autocreateContainerPadding;
0N/A
0N/A /**
0N/A * Group responsible for layout along the horizontal axis. This is NOT
0N/A * the user specified group, use getHorizontalGroup to dig that out.
0N/A */
0N/A private Group horizontalGroup;
0N/A
0N/A /**
0N/A * Group responsible for layout along the vertical axis. This is NOT
0N/A * the user specified group, use getVerticalGroup to dig that out.
0N/A */
0N/A private Group verticalGroup;
0N/A
0N/A // Maps from Component to ComponentInfo. This is used for tracking
0N/A // information specific to a Component.
0N/A private Map<Component,ComponentInfo> componentInfos;
0N/A
0N/A // Container we're doing layout for.
0N/A private Container host;
0N/A
0N/A // Used by areParallelSiblings, cached to avoid excessive garbage.
0N/A private Set<Spring> tmpParallelSet;
0N/A
0N/A // Indicates Springs have changed in some way since last change.
0N/A private boolean springsChanged;
0N/A
0N/A // Indicates invalidateLayout has been invoked.
0N/A private boolean isValid;
0N/A
0N/A // Whether or not any preferred padding (or container padding) springs
0N/A // exist
0N/A private boolean hasPreferredPaddingSprings;
0N/A
0N/A /**
0N/A * The LayoutStyle instance to use, if null the sharedInstance is used.
0N/A */
0N/A private LayoutStyle layoutStyle;
0N/A
0N/A /**
0N/A * If true, components that are not visible are treated as though they
0N/A * aren't there.
0N/A */
0N/A private boolean honorsVisibility;
0N/A
0N/A
0N/A /**
0N/A * Enumeration of the possible ways {@code ParallelGroup} can align
0N/A * its children.
0N/A *
0N/A * @see #createParallelGroup(Alignment)
0N/A * @since 1.6
0N/A */
0N/A public enum Alignment {
0N/A /**
0N/A * Indicates the elements should be
0N/A * aligned to the origin. For the horizontal axis with a left to
0N/A * right orientation this means aligned to the left edge. For the
0N/A * vertical axis leading means aligned to the top edge.
0N/A *
0N/A * @see #createParallelGroup(Alignment)
0N/A */
0N/A LEADING,
0N/A
0N/A /**
0N/A * Indicates the elements should be aligned to the end of the
0N/A * region. For the horizontal axis with a left to right
0N/A * orientation this means aligned to the right edge. For the
0N/A * vertical axis trailing means aligned to the bottom edge.
0N/A *
0N/A * @see #createParallelGroup(Alignment)
0N/A */
0N/A TRAILING,
0N/A
0N/A /**
0N/A * Indicates the elements should be centered in
0N/A * the region.
0N/A *
0N/A * @see #createParallelGroup(Alignment)
0N/A */
0N/A CENTER,
0N/A
0N/A /**
0N/A * Indicates the elements should be aligned along
0N/A * their baseline.
0N/A *
0N/A * @see #createParallelGroup(Alignment)
0N/A * @see #createBaselineGroup(boolean,boolean)
0N/A */
0N/A BASELINE
0N/A }
0N/A
0N/A
0N/A private static void checkSize(int min, int pref, int max,
0N/A boolean isComponentSpring) {
0N/A checkResizeType(min, isComponentSpring);
0N/A if (!isComponentSpring && pref < 0) {
0N/A throw new IllegalArgumentException("Pref must be >= 0");
0N/A } else if (isComponentSpring) {
0N/A checkResizeType(pref, true);
0N/A }
0N/A checkResizeType(max, isComponentSpring);
0N/A checkLessThan(min, pref);
0N/A checkLessThan(pref, max);
0N/A }
0N/A
0N/A private static void checkResizeType(int type, boolean isComponentSpring) {
0N/A if (type < 0 && ((isComponentSpring && type != DEFAULT_SIZE &&
0N/A type != PREFERRED_SIZE) ||
0N/A (!isComponentSpring && type != PREFERRED_SIZE))) {
0N/A throw new IllegalArgumentException("Invalid size");
0N/A }
0N/A }
0N/A
0N/A private static void checkLessThan(int min, int max) {
0N/A if (min >= 0 && max >= 0 && min > max) {
0N/A throw new IllegalArgumentException(
0N/A "Following is not met: min<=pref<=max");
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Creates a {@code GroupLayout} for the specified {@code Container}.
0N/A *
0N/A * @param host the {@code Container} the {@code GroupLayout} is
0N/A * the {@code LayoutManager} for
0N/A * @throws IllegalArgumentException if host is {@code null}
0N/A */
0N/A public GroupLayout(Container host) {
0N/A if (host == null) {
0N/A throw new IllegalArgumentException("Container must be non-null");
0N/A }
0N/A honorsVisibility = true;
0N/A this.host = host;
0N/A setHorizontalGroup(createParallelGroup(Alignment.LEADING, true));
0N/A setVerticalGroup(createParallelGroup(Alignment.LEADING, true));
0N/A componentInfos = new HashMap<Component,ComponentInfo>();
0N/A tmpParallelSet = new HashSet<Spring>();
0N/A }
0N/A
0N/A /**
0N/A * Sets whether component visiblity is considered when sizing and
0N/A * positioning components. A value of {@code true} indicates that
0N/A * non-visible components should not be treated as part of the
0N/A * layout. A value of {@code false} indicates that components should be
0N/A * positioned and sized regardless of visibility.
0N/A * <p>
0N/A * A value of {@code false} is useful when the visibility of components
0N/A * is dynamically adjusted and you don't want surrounding components and
0N/A * the sizing to change.
0N/A * <p>
0N/A * The specified value is used for components that do not have an
0N/A * explicit visibility specified.
0N/A * <p>
0N/A * The default is {@code true}.
0N/A *
0N/A * @param honorsVisibility whether component visiblity is considered when
0N/A * sizing and positioning components
0N/A * @see #setHonorsVisibility(Component,Boolean)
0N/A */
0N/A public void setHonorsVisibility(boolean honorsVisibility) {
0N/A if (this.honorsVisibility != honorsVisibility) {
0N/A this.honorsVisibility = honorsVisibility;
0N/A springsChanged = true;
0N/A isValid = false;
0N/A invalidateHost();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns whether component visiblity is considered when sizing and
0N/A * positioning components.
0N/A *
0N/A * @return whether component visiblity is considered when sizing and
0N/A * positioning components
0N/A */
0N/A public boolean getHonorsVisibility() {
0N/A return honorsVisibility;
0N/A }
0N/A
0N/A /**
0N/A * Sets whether the component's visiblity is considered for
0N/A * sizing and positioning. A value of {@code Boolean.TRUE}
0N/A * indicates that if {@code component} is not visible it should
0N/A * not be treated as part of the layout. A value of {@code false}
0N/A * indicates that {@code component} is positioned and sized
0N/A * regardless of it's visibility. A value of {@code null}
0N/A * indicates the value specified by the single argument method {@code
0N/A * setHonorsVisibility} should be used.
0N/A * <p>
0N/A * If {@code component} is not a child of the {@code Container} this
0N/A * {@code GroupLayout} is managine, it will be added to the
0N/A * {@code Container}.
0N/A *
0N/A * @param component the component
0N/A * @param honorsVisibility whether {@code component}'s visiblity should be
0N/A * considered for sizing and positioning
0N/A * @throws IllegalArgumentException if {@code component} is {@code null}
0N/A * @see #setHonorsVisibility(Component,Boolean)
0N/A */
0N/A public void setHonorsVisibility(Component component,
0N/A Boolean honorsVisibility) {
0N/A if (component == null) {
0N/A throw new IllegalArgumentException("Component must be non-null");
0N/A }
0N/A getComponentInfo(component).setHonorsVisibility(honorsVisibility);
0N/A springsChanged = true;
0N/A isValid = false;
0N/A invalidateHost();
0N/A }
0N/A
0N/A /**
0N/A * Sets whether a gap between components should automatically be
0N/A * created. For example, if this is {@code true} and you add two
0N/A * components to a {@code SequentialGroup} a gap between the
0N/A * two components is automatically be created. The default is
0N/A * {@code false}.
0N/A *
0N/A * @param autoCreatePadding whether a gap between components is
0N/A * automatically created
0N/A */
0N/A public void setAutoCreateGaps(boolean autoCreatePadding) {
0N/A if (this.autocreatePadding != autoCreatePadding) {
0N/A this.autocreatePadding = autoCreatePadding;
0N/A invalidateHost();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns {@code true} if gaps between components are automatically
0N/A * created.
0N/A *
0N/A * @return {@code true} if gaps between components are automatically
0N/A * created
0N/A */
0N/A public boolean getAutoCreateGaps() {
0N/A return autocreatePadding;
0N/A }
0N/A
0N/A /**
0N/A * Sets whether a gap between the container and components that
0N/A * touch the border of the container should automatically be
0N/A * created. The default is {@code false}.
0N/A *
0N/A * @param autoCreateContainerPadding whether a gap between the container and
0N/A * components that touch the border of the container should
0N/A * automatically be created
0N/A */
0N/A public void setAutoCreateContainerGaps(boolean autoCreateContainerPadding){
0N/A if (this.autocreateContainerPadding != autoCreateContainerPadding) {
0N/A this.autocreateContainerPadding = autoCreateContainerPadding;
0N/A horizontalGroup = createTopLevelGroup(getHorizontalGroup());
0N/A verticalGroup = createTopLevelGroup(getVerticalGroup());
0N/A invalidateHost();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns {@code true} if gaps between the container and components that
0N/A * border the container are automatically created.
0N/A *
0N/A * @return {@code true} if gaps between the container and components that
0N/A * border the container are automatically created
0N/A */
0N/A public boolean getAutoCreateContainerGaps() {
0N/A return autocreateContainerPadding;
0N/A }
0N/A
0N/A /**
0N/A * Sets the {@code Group} that positions and sizes
0N/A * components along the horizontal axis.
0N/A *
0N/A * @param group the {@code Group} that positions and sizes
0N/A * components along the horizontal axis
0N/A * @throws IllegalArgumentException if group is {@code null}
0N/A */
0N/A public void setHorizontalGroup(Group group) {
0N/A if (group == null) {
0N/A throw new IllegalArgumentException("Group must be non-null");
0N/A }
0N/A horizontalGroup = createTopLevelGroup(group);
0N/A invalidateHost();
0N/A }
0N/A
0N/A /**
0N/A * Returns the {@code Group} that positions and sizes components
0N/A * along the horizontal axis.
0N/A *
0N/A * @return the {@code Group} responsible for positioning and
0N/A * sizing component along the horizontal axis
0N/A */
0N/A private Group getHorizontalGroup() {
0N/A int index = 0;
0N/A if (horizontalGroup.springs.size() > 1) {
0N/A index = 1;
0N/A }
0N/A return (Group)horizontalGroup.springs.get(index);
0N/A }
0N/A
0N/A /**
0N/A * Sets the {@code Group} that positions and sizes
0N/A * components along the vertical axis.
0N/A *
0N/A * @param group the {@code Group} that positions and sizes
0N/A * components along the vertical axis
0N/A * @throws IllegalArgumentException if group is {@code null}
0N/A */
0N/A public void setVerticalGroup(Group group) {
0N/A if (group == null) {
0N/A throw new IllegalArgumentException("Group must be non-null");
0N/A }
0N/A verticalGroup = createTopLevelGroup(group);
0N/A invalidateHost();
0N/A }
0N/A
0N/A /**
0N/A * Returns the {@code Group} that positions and sizes components
0N/A * along the vertical axis.
0N/A *
0N/A * @return the {@code Group} responsible for positioning and
0N/A * sizing component along the vertical axis
0N/A */
0N/A private Group getVerticalGroup() {
0N/A int index = 0;
0N/A if (verticalGroup.springs.size() > 1) {
0N/A index = 1;
0N/A }
0N/A return (Group)verticalGroup.springs.get(index);
0N/A }
0N/A
0N/A /**
0N/A * Wraps the user specified group in a sequential group. If
0N/A * container gaps should be generated the necessary springs are
0N/A * added.
0N/A */
0N/A private Group createTopLevelGroup(Group specifiedGroup) {
0N/A SequentialGroup group = createSequentialGroup();
0N/A if (getAutoCreateContainerGaps()) {
0N/A group.addSpring(new ContainerAutoPreferredGapSpring());
0N/A group.addGroup(specifiedGroup);
0N/A group.addSpring(new ContainerAutoPreferredGapSpring());
0N/A } else {
0N/A group.addGroup(specifiedGroup);
0N/A }
0N/A return group;
0N/A }
0N/A
0N/A /**
0N/A * Creates and returns a {@code SequentialGroup}.
0N/A *
0N/A * @return a new {@code SequentialGroup}
0N/A */
0N/A public SequentialGroup createSequentialGroup() {
0N/A return new SequentialGroup();
0N/A }
0N/A
0N/A /**
0N/A * Creates and returns a {@code ParallelGroup} with an alignment of
0N/A * {@code Alignment.LEADING}. This is a cover method for the more
0N/A * general {@code createParallelGroup(Alignment)} method.
0N/A *
0N/A * @return a new {@code ParallelGroup}
0N/A * @see #createParallelGroup(Alignment)
0N/A */
0N/A public ParallelGroup createParallelGroup() {
0N/A return createParallelGroup(Alignment.LEADING);
0N/A }
0N/A
0N/A /**
0N/A * Creates and returns a {@code ParallelGroup} with the specified
0N/A * alignment. This is a cover method for the more general {@code
0N/A * createParallelGroup(Alignment,boolean)} method with {@code true}
0N/A * supplied for the second argument.
0N/A *
0N/A * @param alignment the alignment for the elements of the group
0N/A * @throws IllegalArgumentException if {@code alignment} is {@code null}
0N/A * @return a new {@code ParallelGroup}
0N/A * @see #createBaselineGroup
0N/A * @see ParallelGroup
0N/A */
0N/A public ParallelGroup createParallelGroup(Alignment alignment) {
0N/A return createParallelGroup(alignment, true);
0N/A }
0N/A
0N/A /**
0N/A * Creates and returns a {@code ParallelGroup} with the specified
0N/A * alignment and resize behavior. The {@code
0N/A * alignment} argument specifies how children elements are
0N/A * positioned that do not fill the group. For example, if a {@code
0N/A * ParallelGroup} with an alignment of {@code TRAILING} is given
0N/A * 100 and a child only needs 50, the child is
0N/A * positioned at the position 50 (with a component orientation of
0N/A * left-to-right).
0N/A * <p>
0N/A * Baseline alignment is only useful when used along the vertical
0N/A * axis. A {@code ParallelGroup} created with a baseline alignment
0N/A * along the horizontal axis is treated as {@code LEADING}.
0N/A * <p>
0N/A * Refer to {@link GroupLayout.ParallelGroup ParallelGroup} for details on
0N/A * the behavior of baseline groups.
0N/A *
0N/A * @param alignment the alignment for the elements of the group
0N/A * @param resizable {@code true} if the group is resizable; if the group
0N/A * is not resizable the preferred size is used for the
0N/A * minimum and maximum size of the group
0N/A * @throws IllegalArgumentException if {@code alignment} is {@code null}
0N/A * @return a new {@code ParallelGroup}
0N/A * @see #createBaselineGroup
0N/A * @see GroupLayout.ParallelGroup
0N/A */
0N/A public ParallelGroup createParallelGroup(Alignment alignment,
0N/A boolean resizable){
3031N/A if (alignment == null) {
3031N/A throw new IllegalArgumentException("alignment must be non null");
3031N/A }
3031N/A
0N/A if (alignment == Alignment.BASELINE) {
0N/A return new BaselineGroup(resizable);
0N/A }
0N/A return new ParallelGroup(alignment, resizable);
0N/A }
0N/A
0N/A /**
0N/A * Creates and returns a {@code ParallelGroup} that aligns it's
0N/A * elements along the baseline.
0N/A *
0N/A * @param resizable whether the group is resizable
0N/A * @param anchorBaselineToTop whether the baseline is anchored to
0N/A * the top or bottom of the group
0N/A * @see #createBaselineGroup
0N/A * @see ParallelGroup
0N/A */
0N/A public ParallelGroup createBaselineGroup(boolean resizable,
0N/A boolean anchorBaselineToTop) {
0N/A return new BaselineGroup(resizable, anchorBaselineToTop);
0N/A }
0N/A
0N/A /**
0N/A * Forces the specified components to have the same size
0N/A * regardless of their preferred, minimum or maximum sizes. Components that
0N/A * are linked are given the maximum of the preferred size of each of
0N/A * the linked components. For example, if you link two components with
0N/A * a preferred width of 10 and 20, both components are given a width of 20.
0N/A * <p>
0N/A * This can be used multiple times to force any number of
0N/A * components to share the same size.
0N/A * <p>
0N/A * Linked Components are not be resizable.
0N/A *
0N/A * @param components the {@code Component}s that are to have the same size
0N/A * @throws IllegalArgumentException if {@code components} is
0N/A * {@code null}, or contains {@code null}
0N/A * @see #linkSize(int,Component[])
0N/A */
0N/A public void linkSize(Component... components) {
0N/A linkSize(SwingConstants.HORIZONTAL, components);
0N/A linkSize(SwingConstants.VERTICAL, components);
0N/A }
0N/A
0N/A /**
0N/A * Forces the specified components to have the same size along the
0N/A * specified axis regardless of their preferred, minimum or
0N/A * maximum sizes. Components that are linked are given the maximum
0N/A * of the preferred size of each of the linked components. For
0N/A * example, if you link two components along the horizontal axis
0N/A * and the preferred width is 10 and 20, both components are given
0N/A * a width of 20.
0N/A * <p>
0N/A * This can be used multiple times to force any number of
0N/A * components to share the same size.
0N/A * <p>
0N/A * Linked {@code Component}s are not be resizable.
0N/A *
0N/A * @param components the {@code Component}s that are to have the same size
0N/A * @param axis the axis to link the size along; one of
0N/A * {@code SwingConstants.HORIZONTAL} or
0N/A * {@code SwingConstans.VERTICAL}
0N/A * @throws IllegalArgumentException if {@code components} is
0N/A * {@code null}, or contains {@code null}; or {@code axis}
0N/A * is not {@code SwingConstants.HORIZONTAL} or
0N/A * {@code SwingConstants.VERTICAL}
0N/A */
0N/A public void linkSize(int axis, Component... components) {
0N/A if (components == null) {
0N/A throw new IllegalArgumentException("Components must be non-null");
0N/A }
0N/A for (int counter = components.length - 1; counter >= 0; counter--) {
0N/A Component c = components[counter];
0N/A if (components[counter] == null) {
0N/A throw new IllegalArgumentException(
0N/A "Components must be non-null");
0N/A }
0N/A // Force the component to be added
0N/A getComponentInfo(c);
0N/A }
0N/A int glAxis;
0N/A if (axis == SwingConstants.HORIZONTAL) {
0N/A glAxis = HORIZONTAL;
0N/A } else if (axis == SwingConstants.VERTICAL) {
0N/A glAxis = VERTICAL;
0N/A } else {
0N/A throw new IllegalArgumentException("Axis must be one of " +
0N/A "SwingConstants.HORIZONTAL or SwingConstants.VERTICAL");
0N/A }
0N/A LinkInfo master = getComponentInfo(
0N/A components[components.length - 1]).getLinkInfo(glAxis);
0N/A for (int counter = components.length - 2; counter >= 0; counter--) {
0N/A master.add(getComponentInfo(components[counter]));
0N/A }
0N/A invalidateHost();
0N/A }
0N/A
0N/A /**
0N/A * Replaces an existing component with a new one.
0N/A *
0N/A * @param existingComponent the component that should be removed
0N/A * and replaced with {@code newComponent}
0N/A * @param newComponent the component to put in
0N/A * {@code existingComponent}'s place
0N/A * @throws IllegalArgumentException if either of the components are
0N/A * {@code null} or {@code existingComponent} is not being managed
0N/A * by this layout manager
0N/A */
0N/A public void replace(Component existingComponent, Component newComponent) {
0N/A if (existingComponent == null || newComponent == null) {
0N/A throw new IllegalArgumentException("Components must be non-null");
0N/A }
0N/A // Make sure all the components have been registered, otherwise we may
0N/A // not update the correct Springs.
0N/A if (springsChanged) {
0N/A registerComponents(horizontalGroup, HORIZONTAL);
0N/A registerComponents(verticalGroup, VERTICAL);
0N/A }
0N/A ComponentInfo info = componentInfos.remove(existingComponent);
0N/A if (info == null) {
0N/A throw new IllegalArgumentException("Component must already exist");
0N/A }
0N/A host.remove(existingComponent);
0N/A if (newComponent.getParent() != host) {
0N/A host.add(newComponent);
0N/A }
0N/A info.setComponent(newComponent);
0N/A componentInfos.put(newComponent, info);
0N/A invalidateHost();
0N/A }
0N/A
0N/A /**
0N/A * Sets the {@code LayoutStyle} used to calculate the preferred
0N/A * gaps between components. A value of {@code null} indicates the
0N/A * shared instance of {@code LayoutStyle} should be used.
0N/A *
0N/A * @param layoutStyle the {@code LayoutStyle} to use
0N/A * @see LayoutStyle
0N/A */
0N/A public void setLayoutStyle(LayoutStyle layoutStyle) {
0N/A this.layoutStyle = layoutStyle;
0N/A invalidateHost();
0N/A }
0N/A
0N/A /**
0N/A * Returns the {@code LayoutStyle} used for calculating the preferred
0N/A * gap between components. This returns the value specified to
0N/A * {@code setLayoutStyle}, which may be {@code null}.
0N/A *
0N/A * @return the {@code LayoutStyle} used for calculating the preferred
0N/A * gap between components
0N/A */
0N/A public LayoutStyle getLayoutStyle() {
0N/A return layoutStyle;
0N/A }
0N/A
0N/A private LayoutStyle getLayoutStyle0() {
0N/A LayoutStyle layoutStyle = getLayoutStyle();
0N/A if (layoutStyle == null) {
0N/A layoutStyle = LayoutStyle.getInstance();
0N/A }
0N/A return layoutStyle;
0N/A }
0N/A
0N/A private void invalidateHost() {
0N/A if (host instanceof JComponent) {
0N/A ((JComponent)host).revalidate();
0N/A } else {
0N/A host.invalidate();
0N/A }
0N/A host.repaint();
0N/A }
0N/A
0N/A //
0N/A // LayoutManager
0N/A //
0N/A /**
0N/A * Notification that a {@code Component} has been added to
0N/A * the parent container. You should not invoke this method
0N/A * directly, instead you should use one of the {@code Group}
0N/A * methods to add a {@code Component}.
0N/A *
0N/A * @param name the string to be associated with the component
0N/A * @param component the {@code Component} to be added
0N/A */
0N/A public void addLayoutComponent(String name, Component component) {
0N/A }
0N/A
0N/A /**
0N/A * Notification that a {@code Component} has been removed from
0N/A * the parent container. You should not invoke this method
0N/A * directly, instead invoke {@code remove} on the parent
0N/A * {@code Container}.
0N/A *
0N/A * @param component the component to be removed
0N/A * @see java.awt.Component#remove
0N/A */
0N/A public void removeLayoutComponent(Component component) {
0N/A ComponentInfo info = componentInfos.remove(component);
0N/A if (info != null) {
0N/A info.dispose();
0N/A springsChanged = true;
0N/A isValid = false;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the preferred size for the specified container.
0N/A *
0N/A * @param parent the container to return the preferred size for
0N/A * @return the preferred size for {@code parent}
0N/A * @throws IllegalArgumentException if {@code parent} is not
0N/A * the same {@code Container} this was created with
0N/A * @throws IllegalStateException if any of the components added to
0N/A * this layout are not in both a horizontal and vertical group
0N/A * @see java.awt.Container#getPreferredSize
0N/A */
0N/A public Dimension preferredLayoutSize(Container parent) {
0N/A checkParent(parent);
0N/A prepare(PREF_SIZE);
0N/A return adjustSize(horizontalGroup.getPreferredSize(HORIZONTAL),
0N/A verticalGroup.getPreferredSize(VERTICAL));
0N/A }
0N/A
0N/A /**
0N/A * Returns the minimum size for the specified container.
0N/A *
0N/A * @param parent the container to return the size for
0N/A * @return the minimum size for {@code parent}
0N/A * @throws IllegalArgumentException if {@code parent} is not
0N/A * the same {@code Container} that this was created with
0N/A * @throws IllegalStateException if any of the components added to
0N/A * this layout are not in both a horizontal and vertical group
0N/A * @see java.awt.Container#getMinimumSize
0N/A */
0N/A public Dimension minimumLayoutSize(Container parent) {
0N/A checkParent(parent);
0N/A prepare(MIN_SIZE);
0N/A return adjustSize(horizontalGroup.getMinimumSize(HORIZONTAL),
0N/A verticalGroup.getMinimumSize(VERTICAL));
0N/A }
0N/A
0N/A /**
0N/A * Lays out the specified container.
0N/A *
0N/A * @param parent the container to be laid out
0N/A * @throws IllegalStateException if any of the components added to
0N/A * this layout are not in both a horizontal and vertical group
0N/A */
0N/A public void layoutContainer(Container parent) {
0N/A // Step 1: Prepare for layout.
0N/A prepare(SPECIFIC_SIZE);
0N/A Insets insets = parent.getInsets();
0N/A int width = parent.getWidth() - insets.left - insets.right;
0N/A int height = parent.getHeight() - insets.top - insets.bottom;
0N/A boolean ltr = isLeftToRight();
0N/A if (getAutoCreateGaps() || getAutoCreateContainerGaps() ||
0N/A hasPreferredPaddingSprings) {
0N/A // Step 2: Calculate autopadding springs
0N/A calculateAutopadding(horizontalGroup, HORIZONTAL, SPECIFIC_SIZE, 0,
0N/A width);
0N/A calculateAutopadding(verticalGroup, VERTICAL, SPECIFIC_SIZE, 0,
0N/A height);
0N/A }
0N/A // Step 3: set the size of the groups.
0N/A horizontalGroup.setSize(HORIZONTAL, 0, width);
0N/A verticalGroup.setSize(VERTICAL, 0, height);
0N/A // Step 4: apply the size to the components.
0N/A for (ComponentInfo info : componentInfos.values()) {
0N/A info.setBounds(insets, width, ltr);
0N/A }
0N/A }
0N/A
0N/A //
0N/A // LayoutManager2
0N/A //
0N/A /**
0N/A * Notification that a {@code Component} has been added to
0N/A * the parent container. You should not invoke this method
0N/A * directly, instead you should use one of the {@code Group}
0N/A * methods to add a {@code Component}.
0N/A *
0N/A * @param component the component added
0N/A * @param constraints description of where to place the component
0N/A */
0N/A public void addLayoutComponent(Component component, Object constraints) {
0N/A }
0N/A
0N/A /**
0N/A * Returns the maximum size for the specified container.
0N/A *
0N/A * @param parent the container to return the size for
0N/A * @return the maximum size for {@code parent}
0N/A * @throws IllegalArgumentException if {@code parent} is not
0N/A * the same {@code Container} that this was created with
0N/A * @throws IllegalStateException if any of the components added to
0N/A * this layout are not in both a horizontal and vertical group
0N/A * @see java.awt.Container#getMaximumSize
0N/A */
0N/A public Dimension maximumLayoutSize(Container parent) {
0N/A checkParent(parent);
0N/A prepare(MAX_SIZE);
0N/A return adjustSize(horizontalGroup.getMaximumSize(HORIZONTAL),
0N/A verticalGroup.getMaximumSize(VERTICAL));
0N/A }
0N/A
0N/A /**
0N/A * Returns the alignment along the x axis. This specifies how
0N/A * the component would like to be aligned relative to other
0N/A * components. The value should be a number between 0 and 1
0N/A * where 0 represents alignment along the origin, 1 is aligned
0N/A * the furthest away from the origin, 0.5 is centered, etc.
0N/A *
0N/A * @param parent the {@code Container} hosting this {@code LayoutManager}
0N/A * @throws IllegalArgumentException if {@code parent} is not
0N/A * the same {@code Container} that this was created with
0N/A * @return the alignment; this implementation returns {@code .5}
0N/A */
0N/A public float getLayoutAlignmentX(Container parent) {
0N/A checkParent(parent);
0N/A return .5f;
0N/A }
0N/A
0N/A /**
0N/A * Returns the alignment along the y axis. This specifies how
0N/A * the component would like to be aligned relative to other
0N/A * components. The value should be a number between 0 and 1
0N/A * where 0 represents alignment along the origin, 1 is aligned
0N/A * the furthest away from the origin, 0.5 is centered, etc.
0N/A *
0N/A * @param parent the {@code Container} hosting this {@code LayoutManager}
0N/A * @throws IllegalArgumentException if {@code parent} is not
0N/A * the same {@code Container} that this was created with
0N/A * @return alignment; this implementation returns {@code .5}
0N/A */
0N/A public float getLayoutAlignmentY(Container parent) {
0N/A checkParent(parent);
0N/A return .5f;
0N/A }
0N/A
0N/A /**
0N/A * Invalidates the layout, indicating that if the layout manager
0N/A * has cached information it should be discarded.
0N/A *
0N/A * @param parent the {@code Container} hosting this LayoutManager
0N/A * @throws IllegalArgumentException if {@code parent} is not
0N/A * the same {@code Container} that this was created with
0N/A */
0N/A public void invalidateLayout(Container parent) {
0N/A checkParent(parent);
0N/A // invalidateLayout is called from Container.invalidate, which
0N/A // does NOT grab the treelock. All other methods do. To make sure
0N/A // there aren't any possible threading problems we grab the tree lock
0N/A // here.
0N/A synchronized(parent.getTreeLock()) {
0N/A isValid = false;
0N/A }
0N/A }
0N/A
0N/A private void prepare(int sizeType) {
0N/A boolean visChanged = false;
0N/A // Step 1: If not-valid, clear springs and update visibility.
0N/A if (!isValid) {
0N/A isValid = true;
0N/A horizontalGroup.setSize(HORIZONTAL, UNSET, UNSET);
0N/A verticalGroup.setSize(VERTICAL, UNSET, UNSET);
0N/A for (ComponentInfo ci : componentInfos.values()) {
0N/A if (ci.updateVisibility()) {
0N/A visChanged = true;
0N/A }
0N/A ci.clearCachedSize();
0N/A }
0N/A }
0N/A // Step 2: Make sure components are bound to ComponentInfos
0N/A if (springsChanged) {
0N/A registerComponents(horizontalGroup, HORIZONTAL);
0N/A registerComponents(verticalGroup, VERTICAL);
0N/A }
0N/A // Step 3: Adjust the autopadding. This removes existing
0N/A // autopadding, then recalculates where it should go.
0N/A if (springsChanged || visChanged) {
0N/A checkComponents();
0N/A horizontalGroup.removeAutopadding();
0N/A verticalGroup.removeAutopadding();
0N/A if (getAutoCreateGaps()) {
0N/A insertAutopadding(true);
0N/A } else if (hasPreferredPaddingSprings ||
0N/A getAutoCreateContainerGaps()) {
0N/A insertAutopadding(false);
0N/A }
0N/A springsChanged = false;
0N/A }
0N/A // Step 4: (for min/pref/max size calculations only) calculate the
0N/A // autopadding. This invokes for unsetting the calculated values, then
0N/A // recalculating them.
0N/A // If sizeType == SPECIFIC_SIZE, it indicates we're doing layout, this
0N/A // step will be done later on.
0N/A if (sizeType != SPECIFIC_SIZE && (getAutoCreateGaps() ||
0N/A getAutoCreateContainerGaps() || hasPreferredPaddingSprings)) {
0N/A calculateAutopadding(horizontalGroup, HORIZONTAL, sizeType, 0, 0);
0N/A calculateAutopadding(verticalGroup, VERTICAL, sizeType, 0, 0);
0N/A }
0N/A }
0N/A
0N/A private void calculateAutopadding(Group group, int axis, int sizeType,
0N/A int origin, int size) {
0N/A group.unsetAutopadding();
0N/A switch(sizeType) {
0N/A case MIN_SIZE:
0N/A size = group.getMinimumSize(axis);
0N/A break;
0N/A case PREF_SIZE:
0N/A size = group.getPreferredSize(axis);
0N/A break;
0N/A case MAX_SIZE:
0N/A size = group.getMaximumSize(axis);
0N/A break;
0N/A default:
0N/A break;
0N/A }
0N/A group.setSize(axis, origin, size);
0N/A group.calculateAutopadding(axis);
0N/A }
0N/A
0N/A private void checkComponents() {
0N/A for (ComponentInfo info : componentInfos.values()) {
0N/A if (info.horizontalSpring == null) {
0N/A throw new IllegalStateException(info.component +
0N/A " is not attached to a horizontal group");
0N/A }
0N/A if (info.verticalSpring == null) {
0N/A throw new IllegalStateException(info.component +
0N/A " is not attached to a vertical group");
0N/A }
0N/A }
0N/A }
0N/A
0N/A private void registerComponents(Group group, int axis) {
0N/A List<Spring> springs = group.springs;
0N/A for (int counter = springs.size() - 1; counter >= 0; counter--) {
0N/A Spring spring = springs.get(counter);
0N/A if (spring instanceof ComponentSpring) {
0N/A ((ComponentSpring)spring).installIfNecessary(axis);
0N/A } else if (spring instanceof Group) {
0N/A registerComponents((Group)spring, axis);
0N/A }
0N/A }
0N/A }
0N/A
0N/A private Dimension adjustSize(int width, int height) {
0N/A Insets insets = host.getInsets();
0N/A return new Dimension(width + insets.left + insets.right,
0N/A height + insets.top + insets.bottom);
0N/A }
0N/A
0N/A private void checkParent(Container parent) {
0N/A if (parent != host) {
0N/A throw new IllegalArgumentException(
0N/A "GroupLayout can only be used with one Container at a time");
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the {@code ComponentInfo} for the specified Component,
0N/A * creating one if necessary.
0N/A */
0N/A private ComponentInfo getComponentInfo(Component component) {
625N/A ComponentInfo info = componentInfos.get(component);
0N/A if (info == null) {
0N/A info = new ComponentInfo(component);
0N/A componentInfos.put(component, info);
0N/A if (component.getParent() != host) {
0N/A host.add(component);
0N/A }
0N/A }
0N/A return info;
0N/A }
0N/A
0N/A /**
0N/A * Adjusts the autopadding springs for the horizontal and vertical
0N/A * groups. If {@code insert} is {@code true} this will insert auto padding
0N/A * springs, otherwise this will only adjust the springs that
0N/A * comprise auto preferred padding springs.
0N/A */
0N/A private void insertAutopadding(boolean insert) {
0N/A horizontalGroup.insertAutopadding(HORIZONTAL,
0N/A new ArrayList<AutoPreferredGapSpring>(1),
0N/A new ArrayList<AutoPreferredGapSpring>(1),
0N/A new ArrayList<ComponentSpring>(1),
0N/A new ArrayList<ComponentSpring>(1), insert);
0N/A verticalGroup.insertAutopadding(VERTICAL,
0N/A new ArrayList<AutoPreferredGapSpring>(1),
0N/A new ArrayList<AutoPreferredGapSpring>(1),
0N/A new ArrayList<ComponentSpring>(1),
0N/A new ArrayList<ComponentSpring>(1), insert);
0N/A }
0N/A
0N/A /**
0N/A * Returns {@code true} if the two Components have a common ParallelGroup
0N/A * ancestor along the particular axis.
0N/A */
0N/A private boolean areParallelSiblings(Component source, Component target,
0N/A int axis) {
0N/A ComponentInfo sourceInfo = getComponentInfo(source);
0N/A ComponentInfo targetInfo = getComponentInfo(target);
0N/A Spring sourceSpring;
0N/A Spring targetSpring;
0N/A if (axis == HORIZONTAL) {
0N/A sourceSpring = sourceInfo.horizontalSpring;
0N/A targetSpring = targetInfo.horizontalSpring;
0N/A } else {
0N/A sourceSpring = sourceInfo.verticalSpring;
0N/A targetSpring = targetInfo.verticalSpring;
0N/A }
0N/A Set<Spring> sourcePath = tmpParallelSet;
0N/A sourcePath.clear();
0N/A Spring spring = sourceSpring.getParent();
0N/A while (spring != null) {
0N/A sourcePath.add(spring);
0N/A spring = spring.getParent();
0N/A }
0N/A spring = targetSpring.getParent();
0N/A while (spring != null) {
0N/A if (sourcePath.contains(spring)) {
0N/A sourcePath.clear();
0N/A while (spring != null) {
0N/A if (spring instanceof ParallelGroup) {
0N/A return true;
0N/A }
0N/A spring = spring.getParent();
0N/A }
0N/A return false;
0N/A }
0N/A spring = spring.getParent();
0N/A }
0N/A sourcePath.clear();
0N/A return false;
0N/A }
0N/A
0N/A private boolean isLeftToRight() {
0N/A return host.getComponentOrientation().isLeftToRight();
0N/A }
0N/A
0N/A /**
0N/A * Returns a string representation of this {@code GroupLayout}.
0N/A * This method is intended to be used for debugging purposes,
0N/A * and the content and format of the returned string may vary
0N/A * between implementations.
0N/A *
0N/A * @return a string representation of this {@code GroupLayout}
0N/A **/
0N/A public String toString() {
0N/A if (springsChanged) {
0N/A registerComponents(horizontalGroup, HORIZONTAL);
0N/A registerComponents(verticalGroup, VERTICAL);
0N/A }
0N/A StringBuffer buffer = new StringBuffer();
0N/A buffer.append("HORIZONTAL\n");
0N/A createSpringDescription(buffer, horizontalGroup, " ", HORIZONTAL);
0N/A buffer.append("\nVERTICAL\n");
0N/A createSpringDescription(buffer, verticalGroup, " ", VERTICAL);
0N/A return buffer.toString();
0N/A }
0N/A
0N/A private void createSpringDescription(StringBuffer buffer, Spring spring,
0N/A String indent, int axis) {
0N/A String origin = "";
0N/A String padding = "";
0N/A if (spring instanceof ComponentSpring) {
0N/A ComponentSpring cSpring = (ComponentSpring)spring;
0N/A origin = Integer.toString(cSpring.getOrigin()) + " ";
0N/A String name = cSpring.getComponent().getName();
0N/A if (name != null) {
0N/A origin = "name=" + name + ", ";
0N/A }
0N/A }
0N/A if (spring instanceof AutoPreferredGapSpring) {
0N/A AutoPreferredGapSpring paddingSpring =
0N/A (AutoPreferredGapSpring)spring;
0N/A padding = ", userCreated=" + paddingSpring.getUserCreated() +
0N/A ", matches=" + paddingSpring.getMatchDescription();
0N/A }
0N/A buffer.append(indent + spring.getClass().getName() + " " +
0N/A Integer.toHexString(spring.hashCode()) + " " +
0N/A origin +
0N/A ", size=" + spring.getSize() +
0N/A ", alignment=" + spring.getAlignment() +
0N/A " prefs=[" + spring.getMinimumSize(axis) +
0N/A " " + spring.getPreferredSize(axis) +
0N/A " " + spring.getMaximumSize(axis) +
0N/A padding + "]\n");
0N/A if (spring instanceof Group) {
0N/A List<Spring> springs = ((Group)spring).springs;
0N/A indent += " ";
0N/A for (int counter = 0; counter < springs.size(); counter++) {
0N/A createSpringDescription(buffer, springs.get(counter), indent,
0N/A axis);
0N/A }
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Spring consists of a range: min, pref and max, a value some where in
0N/A * the middle of that, and a location. Spring caches the
0N/A * min/max/pref. If the min/pref/max has internally changes, or needs
0N/A * to be updated you must invoke clear.
0N/A */
0N/A private abstract class Spring {
0N/A private int size;
0N/A private int min;
0N/A private int max;
0N/A private int pref;
0N/A private Spring parent;
0N/A
0N/A private Alignment alignment;
0N/A
0N/A Spring() {
0N/A min = pref = max = UNSET;
0N/A }
0N/A
0N/A /**
0N/A * Calculates and returns the minimum size.
0N/A *
0N/A * @param axis the axis of layout; one of HORIZONTAL or VERTICAL
0N/A * @return the minimum size
0N/A */
0N/A abstract int calculateMinimumSize(int axis);
0N/A
0N/A /**
0N/A * Calculates and returns the preferred size.
0N/A *
0N/A * @param axis the axis of layout; one of HORIZONTAL or VERTICAL
0N/A * @return the preferred size
0N/A */
0N/A abstract int calculatePreferredSize(int axis);
0N/A
0N/A /**
0N/A * Calculates and returns the minimum size.
0N/A *
0N/A * @param axis the axis of layout; one of HORIZONTAL or VERTICAL
0N/A * @return the minimum size
0N/A */
0N/A abstract int calculateMaximumSize(int axis);
0N/A
0N/A /**
0N/A * Sets the parent of this Spring.
0N/A */
0N/A void setParent(Spring parent) {
0N/A this.parent = parent;
0N/A }
0N/A
0N/A /**
0N/A * Returns the parent of this spring.
0N/A */
0N/A Spring getParent() {
0N/A return parent;
0N/A }
0N/A
0N/A // This is here purely as a conveniance for ParallelGroup to avoid
0N/A // having to track alignment separately.
0N/A void setAlignment(Alignment alignment) {
0N/A this.alignment = alignment;
0N/A }
0N/A
0N/A /**
0N/A * Alignment for this Spring, this may be null.
0N/A */
0N/A Alignment getAlignment() {
0N/A return alignment;
0N/A }
0N/A
0N/A /**
0N/A * Returns the minimum size.
0N/A */
0N/A final int getMinimumSize(int axis) {
0N/A if (min == UNSET) {
0N/A min = constrain(calculateMinimumSize(axis));
0N/A }
0N/A return min;
0N/A }
0N/A
0N/A /**
0N/A * Returns the preferred size.
0N/A */
0N/A final int getPreferredSize(int axis) {
0N/A if (pref == UNSET) {
0N/A pref = constrain(calculatePreferredSize(axis));
0N/A }
0N/A return pref;
0N/A }
0N/A
0N/A /**
0N/A * Returns the maximum size.
0N/A */
0N/A final int getMaximumSize(int axis) {
0N/A if (max == UNSET) {
0N/A max = constrain(calculateMaximumSize(axis));
0N/A }
0N/A return max;
0N/A }
0N/A
0N/A /**
0N/A * Sets the value and location of the spring. Subclasses
0N/A * will want to invoke super, then do any additional sizing.
0N/A *
0N/A * @param axis HORIZONTAL or VERTICAL
0N/A * @param origin of this Spring
0N/A * @param size of the Spring. If size is UNSET, this invokes
0N/A * clear.
0N/A */
0N/A void setSize(int axis, int origin, int size) {
0N/A this.size = size;
0N/A if (size == UNSET) {
0N/A unset();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Resets the cached min/max/pref.
0N/A */
0N/A void unset() {
0N/A size = min = pref = max = UNSET;
0N/A }
0N/A
0N/A /**
0N/A * Returns the current size.
0N/A */
0N/A int getSize() {
0N/A return size;
0N/A }
0N/A
0N/A int constrain(int value) {
0N/A return Math.min(value, Short.MAX_VALUE);
0N/A }
0N/A
0N/A int getBaseline() {
0N/A return -1;
0N/A }
0N/A
0N/A BaselineResizeBehavior getBaselineResizeBehavior() {
0N/A return BaselineResizeBehavior.OTHER;
0N/A }
0N/A
0N/A final boolean isResizable(int axis) {
0N/A int min = getMinimumSize(axis);
0N/A int pref = getPreferredSize(axis);
0N/A return (min != pref || pref != getMaximumSize(axis));
0N/A }
0N/A
0N/A /**
0N/A * Returns {@code true} if this spring will ALWAYS have a zero
0N/A * size. This should NOT check the current size, rather it's
0N/A * meant to quickly test if this Spring will always have a
0N/A * zero size.
0N/A *
0N/A * @param treatAutopaddingAsZeroSized if {@code true}, auto padding
0N/A * springs should be treated as having a size of {@code 0}
0N/A * @return {@code true} if this spring will have a zero size,
0N/A * {@code false} otherwise
0N/A */
0N/A abstract boolean willHaveZeroSize(boolean treatAutopaddingAsZeroSized);
0N/A }
0N/A
0N/A /**
0N/A * {@code Group} provides the basis for the two types of
0N/A * operations supported by {@code GroupLayout}: laying out
0N/A * components one after another ({@link SequentialGroup SequentialGroup})
0N/A * or aligned ({@link ParallelGroup ParallelGroup}). {@code Group} and
0N/A * its subclasses have no public constructor; to create one use
0N/A * one of {@code createSequentialGroup} or
0N/A * {@code createParallelGroup}. Additionally, taking a {@code Group}
0N/A * created from one {@code GroupLayout} and using it with another
0N/A * will produce undefined results.
0N/A * <p>
0N/A * Various methods in {@code Group} and its subclasses allow you
0N/A * to explicitly specify the range. The arguments to these methods
0N/A * can take two forms, either a value greater than or equal to 0,
0N/A * or one of {@code DEFAULT_SIZE} or {@code PREFERRED_SIZE}. A
0N/A * value greater than or equal to {@code 0} indicates a specific
0N/A * size. {@code DEFAULT_SIZE} indicates the corresponding size
0N/A * from the component should be used. For example, if {@code
0N/A * DEFAULT_SIZE} is passed as the minimum size argument, the
0N/A * minimum size is obtained from invoking {@code getMinimumSize}
0N/A * on the component. Likewise, {@code PREFERRED_SIZE} indicates
0N/A * the value from {@code getPreferredSize} should be used.
0N/A * The following example adds {@code myComponent} to {@code group}
0N/A * with specific values for the range. That is, the minimum is
0N/A * explicitly specified as 100, preferred as 200, and maximum as
0N/A * 300.
0N/A * <pre>
0N/A * group.addComponent(myComponent, 100, 200, 300);
0N/A * </pre>
0N/A * The following example adds {@code myComponent} to {@code group} using
0N/A * a combination of the forms. The minimum size is forced to be the
0N/A * same as the preferred size, the preferred size is determined by
0N/A * using {@code myComponent.getPreferredSize} and the maximum is
0N/A * determined by invoking {@code getMaximumSize} on the component.
0N/A * <pre>
0N/A * group.addComponent(myComponent, GroupLayout.PREFERRED_SIZE,
0N/A * GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE);
0N/A * </pre>
0N/A * <p>
0N/A * Unless otherwise specified all the methods of {@code Group} and
0N/A * its subclasses that allow you to specify a range throw an
0N/A * {@code IllegalArgumentException} if passed an invalid range. An
0N/A * invalid range is one in which any of the values are &lt; 0 and
0N/A * not one of {@code PREFERRED_SIZE} or {@code DEFAULT_SIZE}, or
0N/A * the following is not met (for specific values): {@code min}
0N/A * &lt;= {@code pref} &lt;= {@code max}.
0N/A * <p>
0N/A * Similarly any methods that take a {@code Component} throw a
2779N/A * {@code IllegalArgumentException} if passed {@code null} and any methods
2779N/A * that take a {@code Group} throw an {@code NullPointerException} if
0N/A * passed {@code null}.
0N/A *
0N/A * @see #createSequentialGroup
0N/A * @see #createParallelGroup
0N/A * @since 1.6
0N/A */
0N/A public abstract class Group extends Spring {
0N/A // private int origin;
0N/A // private int size;
0N/A List<Spring> springs;
0N/A
0N/A Group() {
0N/A springs = new ArrayList<Spring>();
0N/A }
0N/A
0N/A /**
0N/A * Adds a {@code Group} to this {@code Group}.
0N/A *
0N/A * @param group the {@code Group} to add
0N/A * @return this {@code Group}
0N/A */
0N/A public Group addGroup(Group group) {
0N/A return addSpring(group);
0N/A }
0N/A
0N/A /**
0N/A * Adds a {@code Component} to this {@code Group}.
0N/A *
0N/A * @param component the {@code Component} to add
0N/A * @return this {@code Group}
0N/A */
0N/A public Group addComponent(Component component) {
0N/A return addComponent(component, DEFAULT_SIZE, DEFAULT_SIZE,
0N/A DEFAULT_SIZE);
0N/A }
0N/A
0N/A /**
0N/A * Adds a {@code Component} to this {@code Group}
0N/A * with the specified size.
0N/A *
0N/A * @param component the {@code Component} to add
0N/A * @param min the minimum size or one of {@code DEFAULT_SIZE} or
0N/A * {@code PREFERRED_SIZE}
0N/A * @param pref the preferred size or one of {@code DEFAULT_SIZE} or
0N/A * {@code PREFERRED_SIZE}
0N/A * @param max the maximum size or one of {@code DEFAULT_SIZE} or
0N/A * {@code PREFERRED_SIZE}
0N/A * @return this {@code Group}
0N/A */
0N/A public Group addComponent(Component component, int min, int pref,
0N/A int max) {
0N/A return addSpring(new ComponentSpring(component, min, pref, max));
0N/A }
0N/A
0N/A /**
0N/A * Adds a rigid gap to this {@code Group}.
0N/A *
0N/A * @param size the size of the gap
0N/A * @return this {@code Group}
0N/A * @throws IllegalArgumentException if {@code size} is less than
0N/A * {@code 0}
0N/A */
0N/A public Group addGap(int size) {
0N/A return addGap(size, size, size);
0N/A }
0N/A
0N/A /**
0N/A * Adds a gap to this {@code Group} with the specified size.
0N/A *
0N/A * @param min the minimum size of the gap
0N/A * @param pref the preferred size of the gap
0N/A * @param max the maximum size of the gap
0N/A * @throws IllegalArgumentException if any of the values are
0N/A * less than {@code 0}
0N/A * @return this {@code Group}
0N/A */
0N/A public Group addGap(int min, int pref, int max) {
0N/A return addSpring(new GapSpring(min, pref, max));
0N/A }
0N/A
0N/A Spring getSpring(int index) {
0N/A return springs.get(index);
0N/A }
0N/A
0N/A int indexOf(Spring spring) {
0N/A return springs.indexOf(spring);
0N/A }
0N/A
0N/A /**
0N/A * Adds the Spring to the list of {@code Spring}s and returns
0N/A * the receiver.
0N/A */
0N/A Group addSpring(Spring spring) {
0N/A springs.add(spring);
0N/A spring.setParent(this);
0N/A if (!(spring instanceof AutoPreferredGapSpring) ||
0N/A !((AutoPreferredGapSpring)spring).getUserCreated()) {
0N/A springsChanged = true;
0N/A }
0N/A return this;
0N/A }
0N/A
0N/A //
0N/A // Spring methods
0N/A //
0N/A
0N/A void setSize(int axis, int origin, int size) {
0N/A super.setSize(axis, origin, size);
0N/A if (size == UNSET) {
0N/A for (int counter = springs.size() - 1; counter >= 0;
0N/A counter--) {
0N/A getSpring(counter).setSize(axis, origin, size);
0N/A }
0N/A } else {
0N/A setValidSize(axis, origin, size);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * This is invoked from {@code setSize} if passed a value
0N/A * other than UNSET.
0N/A */
0N/A abstract void setValidSize(int axis, int origin, int size);
0N/A
0N/A int calculateMinimumSize(int axis) {
0N/A return calculateSize(axis, MIN_SIZE);
0N/A }
0N/A
0N/A int calculatePreferredSize(int axis) {
0N/A return calculateSize(axis, PREF_SIZE);
0N/A }
0N/A
0N/A int calculateMaximumSize(int axis) {
0N/A return calculateSize(axis, MAX_SIZE);
0N/A }
0N/A
0N/A /**
0N/A * Calculates the specified size. This is called from
0N/A * one of the {@code getMinimumSize0},
0N/A * {@code getPreferredSize0} or
0N/A * {@code getMaximumSize0} methods. This will invoke
0N/A * to {@code operator} to combine the values.
0N/A */
0N/A int calculateSize(int axis, int type) {
0N/A int count = springs.size();
0N/A if (count == 0) {
0N/A return 0;
0N/A }
0N/A if (count == 1) {
0N/A return getSpringSize(getSpring(0), axis, type);
0N/A }
0N/A int size = constrain(operator(getSpringSize(getSpring(0), axis,
0N/A type), getSpringSize(getSpring(1), axis, type)));
0N/A for (int counter = 2; counter < count; counter++) {
0N/A size = constrain(operator(size, getSpringSize(
0N/A getSpring(counter), axis, type)));
0N/A }
0N/A return size;
0N/A }
0N/A
0N/A int getSpringSize(Spring spring, int axis, int type) {
0N/A switch(type) {
0N/A case MIN_SIZE:
0N/A return spring.getMinimumSize(axis);
0N/A case PREF_SIZE:
0N/A return spring.getPreferredSize(axis);
0N/A case MAX_SIZE:
0N/A return spring.getMaximumSize(axis);
0N/A }
0N/A assert false;
0N/A return 0;
0N/A }
0N/A
0N/A /**
0N/A * Used to compute how the two values representing two springs
0N/A * will be combined. For example, a group that layed things out
0N/A * one after the next would return {@code a + b}.
0N/A */
0N/A abstract int operator(int a, int b);
0N/A
0N/A //
0N/A // Padding
0N/A //
0N/A
0N/A /**
0N/A * Adjusts the autopadding springs in this group and its children.
0N/A * If {@code insert} is true this will insert auto padding
0N/A * springs, otherwise this will only adjust the springs that
0N/A * comprise auto preferred padding springs.
0N/A *
0N/A * @param axis the axis of the springs; HORIZONTAL or VERTICAL
0N/A * @param leadingPadding List of AutopaddingSprings that occur before
0N/A * this Group
0N/A * @param trailingPadding any trailing autopadding springs are added
0N/A * to this on exit
0N/A * @param leading List of ComponentSprings that occur before this Group
0N/A * @param trailing any trailing ComponentSpring are added to this
0N/A * List
0N/A * @param insert Whether or not to insert AutopaddingSprings or just
0N/A * adjust any existing AutopaddingSprings.
0N/A */
0N/A abstract void insertAutopadding(int axis,
0N/A List<AutoPreferredGapSpring> leadingPadding,
0N/A List<AutoPreferredGapSpring> trailingPadding,
0N/A List<ComponentSpring> leading, List<ComponentSpring> trailing,
0N/A boolean insert);
0N/A
0N/A /**
0N/A * Removes any AutopaddingSprings for this Group and its children.
0N/A */
0N/A void removeAutopadding() {
0N/A unset();
0N/A for (int counter = springs.size() - 1; counter >= 0; counter--) {
0N/A Spring spring = springs.get(counter);
0N/A if (spring instanceof AutoPreferredGapSpring) {
0N/A if (((AutoPreferredGapSpring)spring).getUserCreated()) {
0N/A ((AutoPreferredGapSpring)spring).reset();
0N/A } else {
0N/A springs.remove(counter);
0N/A }
0N/A } else if (spring instanceof Group) {
0N/A ((Group)spring).removeAutopadding();
0N/A }
0N/A }
0N/A }
0N/A
0N/A void unsetAutopadding() {
0N/A // Clear cached pref/min/max.
0N/A unset();
0N/A for (int counter = springs.size() - 1; counter >= 0; counter--) {
0N/A Spring spring = springs.get(counter);
0N/A if (spring instanceof AutoPreferredGapSpring) {
625N/A spring.unset();
0N/A } else if (spring instanceof Group) {
0N/A ((Group)spring).unsetAutopadding();
0N/A }
0N/A }
0N/A }
0N/A
0N/A void calculateAutopadding(int axis) {
0N/A for (int counter = springs.size() - 1; counter >= 0; counter--) {
0N/A Spring spring = springs.get(counter);
0N/A if (spring instanceof AutoPreferredGapSpring) {
0N/A // Force size to be reset.
0N/A spring.unset();
0N/A ((AutoPreferredGapSpring)spring).calculatePadding(axis);
0N/A } else if (spring instanceof Group) {
0N/A ((Group)spring).calculateAutopadding(axis);
0N/A }
0N/A }
0N/A // Clear cached pref/min/max.
0N/A unset();
0N/A }
0N/A
0N/A @Override
0N/A boolean willHaveZeroSize(boolean treatAutopaddingAsZeroSized) {
0N/A for (int i = springs.size() - 1; i >= 0; i--) {
0N/A Spring spring = springs.get(i);
0N/A if (!spring.willHaveZeroSize(treatAutopaddingAsZeroSized)) {
0N/A return false;
0N/A }
0N/A }
0N/A return true;
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * A {@code Group} that positions and sizes its elements
0N/A * sequentially, one after another. This class has no public
0N/A * constructor, use the {@code createSequentialGroup} method
0N/A * to create one.
0N/A * <p>
0N/A * In order to align a {@code SequentialGroup} along the baseline
0N/A * of a baseline aligned {@code ParallelGroup} you need to specify
0N/A * which of the elements of the {@code SequentialGroup} is used to
0N/A * determine the baseline. The element used to calculate the
0N/A * baseline is specified using one of the {@code add} methods that
0N/A * take a {@code boolean}. The last element added with a value of
0N/A * {@code true} for {@code useAsBaseline} is used to calculate the
0N/A * baseline.
0N/A *
0N/A * @see #createSequentialGroup
0N/A * @since 1.6
0N/A */
0N/A public class SequentialGroup extends Group {
0N/A private Spring baselineSpring;
0N/A
0N/A SequentialGroup() {
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A */
0N/A public SequentialGroup addGroup(Group group) {
0N/A return (SequentialGroup)super.addGroup(group);
0N/A }
0N/A
0N/A /**
0N/A * Adds a {@code Group} to this {@code Group}.
0N/A *
0N/A * @param group the {@code Group} to add
0N/A * @param useAsBaseline whether the specified {@code Group} should
0N/A * be used to calculate the baseline for this {@code Group}
0N/A * @return this {@code Group}
0N/A */
0N/A public SequentialGroup addGroup(boolean useAsBaseline, Group group) {
0N/A super.addGroup(group);
0N/A if (useAsBaseline) {
0N/A baselineSpring = group;
0N/A }
0N/A return this;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A */
0N/A public SequentialGroup addComponent(Component component) {
0N/A return (SequentialGroup)super.addComponent(component);
0N/A }
0N/A
0N/A /**
0N/A * Adds a {@code Component} to this {@code Group}.
0N/A *
0N/A * @param useAsBaseline whether the specified {@code Component} should
0N/A * be used to calculate the baseline for this {@code Group}
0N/A * @param component the {@code Component} to add
0N/A * @return this {@code Group}
0N/A */
0N/A public SequentialGroup addComponent(boolean useAsBaseline,
0N/A Component component) {
0N/A super.addComponent(component);
0N/A if (useAsBaseline) {
0N/A baselineSpring = springs.get(springs.size() - 1);
0N/A }
0N/A return this;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A */
0N/A public SequentialGroup addComponent(Component component, int min,
0N/A int pref, int max) {
0N/A return (SequentialGroup)super.addComponent(
0N/A component, min, pref, max);
0N/A }
0N/A
0N/A /**
0N/A * Adds a {@code Component} to this {@code Group}
0N/A * with the specified size.
0N/A *
0N/A * @param useAsBaseline whether the specified {@code Component} should
0N/A * be used to calculate the baseline for this {@code Group}
0N/A * @param component the {@code Component} to add
0N/A * @param min the minimum size or one of {@code DEFAULT_SIZE} or
0N/A * {@code PREFERRED_SIZE}
0N/A * @param pref the preferred size or one of {@code DEFAULT_SIZE} or
0N/A * {@code PREFERRED_SIZE}
0N/A * @param max the maximum size or one of {@code DEFAULT_SIZE} or
0N/A * {@code PREFERRED_SIZE}
0N/A * @return this {@code Group}
0N/A */
0N/A public SequentialGroup addComponent(boolean useAsBaseline,
0N/A Component component, int min, int pref, int max) {
0N/A super.addComponent(component, min, pref, max);
0N/A if (useAsBaseline) {
0N/A baselineSpring = springs.get(springs.size() - 1);
0N/A }
0N/A return this;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A */
0N/A public SequentialGroup addGap(int size) {
0N/A return (SequentialGroup)super.addGap(size);
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A */
0N/A public SequentialGroup addGap(int min, int pref, int max) {
0N/A return (SequentialGroup)super.addGap(min, pref, max);
0N/A }
0N/A
0N/A /**
0N/A * Adds an element representing the preferred gap between two
0N/A * components. The element created to represent the gap is not
0N/A * resizable.
0N/A *
0N/A * @param comp1 the first component
0N/A * @param comp2 the second component
0N/A * @param type the type of gap; one of the constants defined by
0N/A * {@code LayoutStyle}
0N/A * @return this {@code SequentialGroup}
0N/A * @throws IllegalArgumentException if {@code type}, {@code comp1} or
0N/A * {@code comp2} is {@code null}
0N/A * @see LayoutStyle
0N/A */
0N/A public SequentialGroup addPreferredGap(JComponent comp1,
0N/A JComponent comp2, ComponentPlacement type) {
0N/A return addPreferredGap(comp1, comp2, type, DEFAULT_SIZE,
0N/A PREFERRED_SIZE);
0N/A }
0N/A
0N/A /**
0N/A * Adds an element representing the preferred gap between two
0N/A * components.
0N/A *
0N/A * @param comp1 the first component
0N/A * @param comp2 the second component
0N/A * @param type the type of gap
0N/A * @param pref the preferred size of the grap; one of
0N/A * {@code DEFAULT_SIZE} or a value &gt;= 0
0N/A * @param max the maximum size of the gap; one of
0N/A * {@code DEFAULT_SIZE}, {@code PREFERRED_SIZE}
0N/A * or a value &gt;= 0
0N/A * @return this {@code SequentialGroup}
0N/A * @throws IllegalArgumentException if {@code type}, {@code comp1} or
0N/A * {@code comp2} is {@code null}
0N/A * @see LayoutStyle
0N/A */
0N/A public SequentialGroup addPreferredGap(JComponent comp1,
0N/A JComponent comp2, ComponentPlacement type, int pref,
0N/A int max) {
0N/A if (type == null) {
0N/A throw new IllegalArgumentException("Type must be non-null");
0N/A }
0N/A if (comp1 == null || comp2 == null) {
0N/A throw new IllegalArgumentException(
0N/A "Components must be non-null");
0N/A }
0N/A checkPreferredGapValues(pref, max);
0N/A return (SequentialGroup)addSpring(new PreferredGapSpring(
0N/A comp1, comp2, type, pref, max));
0N/A }
0N/A
0N/A /**
0N/A * Adds an element representing the preferred gap between the
0N/A * nearest components. During layout, neighboring
0N/A * components are found, and the size of the added gap is set
0N/A * based on the preferred gap between the components. If no
0N/A * neighboring components are found the gap has a size of {@code 0}.
0N/A * <p>
0N/A * The element created to represent the gap is not
0N/A * resizable.
0N/A *
0N/A * @param type the type of gap; one of
0N/A * {@code LayoutStyle.ComponentPlacement.RELATED} or
0N/A * {@code LayoutStyle.ComponentPlacement.UNRELATED}
0N/A * @return this {@code SequentialGroup}
0N/A * @see LayoutStyle
0N/A * @throws IllegalArgumentException if {@code type} is not one of
0N/A * {@code LayoutStyle.ComponentPlacement.RELATED} or
0N/A * {@code LayoutStyle.ComponentPlacement.UNRELATED}
0N/A */
0N/A public SequentialGroup addPreferredGap(ComponentPlacement type) {
0N/A return addPreferredGap(type, DEFAULT_SIZE, DEFAULT_SIZE);
0N/A }
0N/A
0N/A /**
0N/A * Adds an element representing the preferred gap between the
0N/A * nearest components. During layout, neighboring
0N/A * components are found, and the minimum of this
0N/A * gap is set based on the size of the preferred gap between the
0N/A * neighboring components. If no neighboring components are found the
0N/A * minimum size is set to 0.
0N/A *
0N/A * @param type the type of gap; one of
0N/A * {@code LayoutStyle.ComponentPlacement.RELATED} or
0N/A * {@code LayoutStyle.ComponentPlacement.UNRELATED}
0N/A * @param pref the preferred size of the grap; one of
0N/A * {@code DEFAULT_SIZE} or a value &gt;= 0
0N/A * @param max the maximum size of the gap; one of
0N/A * {@code DEFAULT_SIZE}, {@code PREFERRED_SIZE}
0N/A * or a value &gt;= 0
0N/A * @return this {@code SequentialGroup}
0N/A * @throws IllegalArgumentException if {@code type} is not one of
0N/A * {@code LayoutStyle.ComponentPlacement.RELATED} or
0N/A * {@code LayoutStyle.ComponentPlacement.UNRELATED}
0N/A * @see LayoutStyle
0N/A */
0N/A public SequentialGroup addPreferredGap(ComponentPlacement type,
0N/A int pref, int max) {
0N/A if (type != ComponentPlacement.RELATED &&
0N/A type != ComponentPlacement.UNRELATED) {
0N/A throw new IllegalArgumentException(
0N/A "Type must be one of " +
0N/A "LayoutStyle.ComponentPlacement.RELATED or " +
0N/A "LayoutStyle.ComponentPlacement.UNRELATED");
0N/A }
0N/A checkPreferredGapValues(pref, max);
0N/A hasPreferredPaddingSprings = true;
0N/A return (SequentialGroup)addSpring(new AutoPreferredGapSpring(
0N/A type, pref, max));
0N/A }
0N/A
0N/A /**
0N/A * Adds an element representing the preferred gap between an edge
0N/A * the container and components that touch the border of the
0N/A * container. This has no effect if the added gap does not
0N/A * touch an edge of the parent container.
0N/A * <p>
0N/A * The element created to represent the gap is not
0N/A * resizable.
0N/A *
0N/A * @return this {@code SequentialGroup}
0N/A */
0N/A public SequentialGroup addContainerGap() {
0N/A return addContainerGap(DEFAULT_SIZE, DEFAULT_SIZE);
0N/A }
0N/A
0N/A /**
0N/A * Adds an element representing the preferred gap between one
0N/A * edge of the container and the next or previous {@code
0N/A * Component} with the specified size. This has no
0N/A * effect if the next or previous element is not a {@code
0N/A * Component} and does not touch one edge of the parent
0N/A * container.
0N/A *
0N/A * @param pref the preferred size; one of {@code DEFAULT_SIZE} or a
0N/A * value &gt;= 0
0N/A * @param max the maximum size; one of {@code DEFAULT_SIZE},
0N/A * {@code PREFERRED_SIZE} or a value &gt;= 0
0N/A * @return this {@code SequentialGroup}
0N/A */
0N/A public SequentialGroup addContainerGap(int pref, int max) {
0N/A if ((pref < 0 && pref != DEFAULT_SIZE) ||
0N/A (max < 0 && max != DEFAULT_SIZE && max != PREFERRED_SIZE)||
0N/A (pref >= 0 && max >= 0 && pref > max)) {
0N/A throw new IllegalArgumentException(
0N/A "Pref and max must be either DEFAULT_VALUE " +
0N/A "or >= 0 and pref <= max");
0N/A }
0N/A hasPreferredPaddingSprings = true;
0N/A return (SequentialGroup)addSpring(
0N/A new ContainerAutoPreferredGapSpring(pref, max));
0N/A }
0N/A
0N/A int operator(int a, int b) {
0N/A return constrain(a) + constrain(b);
0N/A }
0N/A
0N/A void setValidSize(int axis, int origin, int size) {
0N/A int pref = getPreferredSize(axis);
0N/A if (size == pref) {
0N/A // Layout at preferred size
0N/A for (Spring spring : springs) {
0N/A int springPref = spring.getPreferredSize(axis);
0N/A spring.setSize(axis, origin, springPref);
0N/A origin += springPref;
0N/A }
0N/A } else if (springs.size() == 1) {
0N/A Spring spring = getSpring(0);
0N/A spring.setSize(axis, origin, Math.min(
0N/A Math.max(size, spring.getMinimumSize(axis)),
0N/A spring.getMaximumSize(axis)));
0N/A } else if (springs.size() > 1) {
0N/A // Adjust between min/pref
0N/A setValidSizeNotPreferred(axis, origin, size);
0N/A }
0N/A }
0N/A
0N/A private void setValidSizeNotPreferred(int axis, int origin, int size) {
0N/A int delta = size - getPreferredSize(axis);
0N/A assert delta != 0;
0N/A boolean useMin = (delta < 0);
0N/A int springCount = springs.size();
0N/A if (useMin) {
0N/A delta *= -1;
0N/A }
0N/A
0N/A // The following algorithm if used for resizing springs:
0N/A // 1. Calculate the resizability of each spring (pref - min or
0N/A // max - pref) into a list.
0N/A // 2. Sort the list in ascending order
0N/A // 3. Iterate through each of the resizable Springs, attempting
0N/A // to give them (pref - size) / resizeCount
0N/A // 4. For any Springs that can not accomodate that much space
0N/A // add the remainder back to the amount to distribute and
0N/A // recalculate how must space the remaining springs will get.
0N/A // 5. Set the size of the springs.
0N/A
0N/A // First pass, sort the resizable springs into the List resizable
0N/A List<SpringDelta> resizable = buildResizableList(axis, useMin);
0N/A int resizableCount = resizable.size();
0N/A
0N/A if (resizableCount > 0) {
0N/A // How much we would like to give each Spring.
0N/A int sDelta = delta / resizableCount;
0N/A // Remaining space.
0N/A int slop = delta - sDelta * resizableCount;
0N/A int[] sizes = new int[springCount];
0N/A int sign = useMin ? -1 : 1;
0N/A // Second pass, accumulate the resulting deltas (relative to
0N/A // preferred) into sizes.
0N/A for (int counter = 0; counter < resizableCount; counter++) {
0N/A SpringDelta springDelta = resizable.get(counter);
0N/A if ((counter + 1) == resizableCount) {
0N/A sDelta += slop;
0N/A }
0N/A springDelta.delta = Math.min(sDelta, springDelta.delta);
0N/A delta -= springDelta.delta;
0N/A if (springDelta.delta != sDelta && counter + 1 <
0N/A resizableCount) {
0N/A // Spring didn't take all the space, reset how much
0N/A // each spring will get.
0N/A sDelta = delta / (resizableCount - counter - 1);
0N/A slop = delta - sDelta * (resizableCount - counter - 1);
0N/A }
0N/A sizes[springDelta.index] = sign * springDelta.delta;
0N/A }
0N/A
0N/A // And finally set the size of each spring
0N/A for (int counter = 0; counter < springCount; counter++) {
0N/A Spring spring = getSpring(counter);
0N/A int sSize = spring.getPreferredSize(axis) + sizes[counter];
0N/A spring.setSize(axis, origin, sSize);
0N/A origin += sSize;
0N/A }
0N/A } else {
0N/A // Nothing resizable, use the min or max of each of the
0N/A // springs.
0N/A for (int counter = 0; counter < springCount; counter++) {
0N/A Spring spring = getSpring(counter);
0N/A int sSize;
0N/A if (useMin) {
0N/A sSize = spring.getMinimumSize(axis);
0N/A } else {
0N/A sSize = spring.getMaximumSize(axis);
0N/A }
0N/A spring.setSize(axis, origin, sSize);
0N/A origin += sSize;
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the sorted list of SpringDelta's for the current set of
0N/A * Springs. The list is ordered based on the amount of flexibility of
0N/A * the springs.
0N/A */
0N/A private List<SpringDelta> buildResizableList(int axis,
0N/A boolean useMin) {
0N/A // First pass, figure out what is resizable
0N/A int size = springs.size();
0N/A List<SpringDelta> sorted = new ArrayList<SpringDelta>(size);
0N/A for (int counter = 0; counter < size; counter++) {
0N/A Spring spring = getSpring(counter);
0N/A int sDelta;
0N/A if (useMin) {
0N/A sDelta = spring.getPreferredSize(axis) -
0N/A spring.getMinimumSize(axis);
0N/A } else {
0N/A sDelta = spring.getMaximumSize(axis) -
0N/A spring.getPreferredSize(axis);
0N/A }
0N/A if (sDelta > 0) {
0N/A sorted.add(new SpringDelta(counter, sDelta));
0N/A }
0N/A }
0N/A Collections.sort(sorted);
0N/A return sorted;
0N/A }
0N/A
0N/A private int indexOfNextNonZeroSpring(
0N/A int index, boolean treatAutopaddingAsZeroSized) {
0N/A while (index < springs.size()) {
0N/A Spring spring = springs.get(index);
0N/A if (!spring.willHaveZeroSize(treatAutopaddingAsZeroSized)) {
0N/A return index;
0N/A }
0N/A index++;
0N/A }
0N/A return index;
0N/A }
0N/A
0N/A @Override
0N/A void insertAutopadding(int axis,
0N/A List<AutoPreferredGapSpring> leadingPadding,
0N/A List<AutoPreferredGapSpring> trailingPadding,
0N/A List<ComponentSpring> leading, List<ComponentSpring> trailing,
0N/A boolean insert) {
0N/A List<AutoPreferredGapSpring> newLeadingPadding =
0N/A new ArrayList<AutoPreferredGapSpring>(leadingPadding);
0N/A List<AutoPreferredGapSpring> newTrailingPadding =
0N/A new ArrayList<AutoPreferredGapSpring>(1);
0N/A List<ComponentSpring> newLeading =
0N/A new ArrayList<ComponentSpring>(leading);
0N/A List<ComponentSpring> newTrailing = null;
0N/A int counter = 0;
0N/A // Warning, this must use springs.size, as it may change during the
0N/A // loop.
0N/A while (counter < springs.size()) {
0N/A Spring spring = getSpring(counter);
0N/A if (spring instanceof AutoPreferredGapSpring) {
0N/A if (newLeadingPadding.size() == 0) {
0N/A // Autopadding spring. Set the sources of the
0N/A // autopadding spring based on newLeading.
0N/A AutoPreferredGapSpring padding =
0N/A (AutoPreferredGapSpring)spring;
0N/A padding.setSources(newLeading);
0N/A newLeading.clear();
0N/A counter = indexOfNextNonZeroSpring(counter + 1, true);
0N/A if (counter == springs.size()) {
0N/A // Last spring in the list, add it to
0N/A // trailingPadding.
0N/A if (!(padding instanceof
0N/A ContainerAutoPreferredGapSpring)) {
0N/A trailingPadding.add(padding);
0N/A }
0N/A } else {
0N/A newLeadingPadding.clear();
0N/A newLeadingPadding.add(padding);
0N/A }
0N/A } else {
0N/A counter = indexOfNextNonZeroSpring(counter + 1, true);
0N/A }
0N/A } else {
0N/A // Not a padding spring
0N/A if (newLeading.size() > 0 && insert) {
0N/A // There's leading ComponentSprings, create an
0N/A // autopadding spring.
0N/A AutoPreferredGapSpring padding =
0N/A new AutoPreferredGapSpring();
0N/A // Force the newly created spring to be considered
0N/A // by NOT incrementing counter
0N/A springs.add(counter, padding);
0N/A continue;
0N/A }
0N/A if (spring instanceof ComponentSpring) {
0N/A // Spring is a Component, make it the target of any
0N/A // leading AutopaddingSpring.
0N/A ComponentSpring cSpring = (ComponentSpring)spring;
0N/A if (!cSpring.isVisible()) {
0N/A counter++;
0N/A continue;
0N/A }
0N/A for (AutoPreferredGapSpring gapSpring : newLeadingPadding) {
0N/A gapSpring.addTarget(cSpring, axis);
0N/A }
0N/A newLeading.clear();
0N/A newLeadingPadding.clear();
0N/A counter = indexOfNextNonZeroSpring(counter + 1, false);
0N/A if (counter == springs.size()) {
0N/A // Last Spring, add it to trailing
0N/A trailing.add(cSpring);
0N/A } else {
0N/A // Not that last Spring, add it to leading
0N/A newLeading.add(cSpring);
0N/A }
0N/A } else if (spring instanceof Group) {
0N/A // Forward call to child Group
0N/A if (newTrailing == null) {
0N/A newTrailing = new ArrayList<ComponentSpring>(1);
0N/A } else {
0N/A newTrailing.clear();
0N/A }
0N/A newTrailingPadding.clear();
0N/A ((Group)spring).insertAutopadding(axis,
0N/A newLeadingPadding, newTrailingPadding,
0N/A newLeading, newTrailing, insert);
0N/A newLeading.clear();
0N/A newLeadingPadding.clear();
0N/A counter = indexOfNextNonZeroSpring(
0N/A counter + 1, (newTrailing.size() == 0));
0N/A if (counter == springs.size()) {
0N/A trailing.addAll(newTrailing);
0N/A trailingPadding.addAll(newTrailingPadding);
0N/A } else {
0N/A newLeading.addAll(newTrailing);
0N/A newLeadingPadding.addAll(newTrailingPadding);
0N/A }
0N/A } else {
0N/A // Gap
0N/A newLeadingPadding.clear();
0N/A newLeading.clear();
0N/A counter++;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A int getBaseline() {
0N/A if (baselineSpring != null) {
0N/A int baseline = baselineSpring.getBaseline();
0N/A if (baseline >= 0) {
0N/A int size = 0;
0N/A for (Spring spring : springs) {
0N/A if (spring == baselineSpring) {
0N/A return size + baseline;
0N/A } else {
0N/A size += spring.getPreferredSize(VERTICAL);
0N/A }
0N/A }
0N/A }
0N/A }
0N/A return -1;
0N/A }
0N/A
0N/A BaselineResizeBehavior getBaselineResizeBehavior() {
0N/A if (isResizable(VERTICAL)) {
0N/A if (!baselineSpring.isResizable(VERTICAL)) {
0N/A // Spring to use for baseline isn't resizable. In this case
0N/A // baseline resize behavior can be determined based on how
0N/A // preceeding springs resize.
0N/A boolean leadingResizable = false;
0N/A for (Spring spring : springs) {
0N/A if (spring == baselineSpring) {
0N/A break;
0N/A } else if (spring.isResizable(VERTICAL)) {
0N/A leadingResizable = true;
0N/A break;
0N/A }
0N/A }
0N/A boolean trailingResizable = false;
0N/A for (int i = springs.size() - 1; i >= 0; i--) {
0N/A Spring spring = springs.get(i);
0N/A if (spring == baselineSpring) {
0N/A break;
0N/A }
0N/A if (spring.isResizable(VERTICAL)) {
0N/A trailingResizable = true;
0N/A break;
0N/A }
0N/A }
0N/A if (leadingResizable && !trailingResizable) {
0N/A return BaselineResizeBehavior.CONSTANT_DESCENT;
0N/A } else if (!leadingResizable && trailingResizable) {
0N/A return BaselineResizeBehavior.CONSTANT_ASCENT;
0N/A }
0N/A // If we get here, both leading and trailing springs are
0N/A // resizable. Fall through to OTHER.
0N/A } else {
0N/A BaselineResizeBehavior brb = baselineSpring.getBaselineResizeBehavior();
0N/A if (brb == BaselineResizeBehavior.CONSTANT_ASCENT) {
0N/A for (Spring spring : springs) {
0N/A if (spring == baselineSpring) {
0N/A return BaselineResizeBehavior.CONSTANT_ASCENT;
0N/A }
0N/A if (spring.isResizable(VERTICAL)) {
0N/A return BaselineResizeBehavior.OTHER;
0N/A }
0N/A }
0N/A } else if (brb == BaselineResizeBehavior.CONSTANT_DESCENT) {
0N/A for (int i = springs.size() - 1; i >= 0; i--) {
0N/A Spring spring = springs.get(i);
0N/A if (spring == baselineSpring) {
0N/A return BaselineResizeBehavior.CONSTANT_DESCENT;
0N/A }
0N/A if (spring.isResizable(VERTICAL)) {
0N/A return BaselineResizeBehavior.OTHER;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A return BaselineResizeBehavior.OTHER;
0N/A }
0N/A // Not resizable, treat as constant_ascent
0N/A return BaselineResizeBehavior.CONSTANT_ASCENT;
0N/A }
0N/A
0N/A private void checkPreferredGapValues(int pref, int max) {
0N/A if ((pref < 0 && pref != DEFAULT_SIZE && pref != PREFERRED_SIZE) ||
0N/A (max < 0 && max != DEFAULT_SIZE && max != PREFERRED_SIZE)||
0N/A (pref >= 0 && max >= 0 && pref > max)) {
0N/A throw new IllegalArgumentException(
0N/A "Pref and max must be either DEFAULT_SIZE, " +
0N/A "PREFERRED_SIZE, or >= 0 and pref <= max");
0N/A }
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Used by SequentialGroup in calculating resizability of springs.
0N/A */
0N/A private static final class SpringDelta implements Comparable<SpringDelta> {
0N/A // Original index.
0N/A public final int index;
0N/A // Delta, one of pref - min or max - pref.
0N/A public int delta;
0N/A
0N/A public SpringDelta(int index, int delta) {
0N/A this.index = index;
0N/A this.delta = delta;
0N/A }
0N/A
0N/A public int compareTo(SpringDelta o) {
0N/A return delta - o.delta;
0N/A }
0N/A
0N/A public String toString() {
0N/A return super.toString() + "[index=" + index + ", delta=" +
0N/A delta + "]";
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * A {@code Group} that aligns and sizes it's children.
0N/A * {@code ParallelGroup} aligns it's children in
0N/A * four possible ways: along the baseline, centered, anchored to the
0N/A * leading edge, or anchored to the trailing edge.
0N/A * <h3>Baseline</h3>
0N/A * A {@code ParallelGroup} that aligns it's children along the
0N/A * baseline must first decide where the baseline is
0N/A * anchored. The baseline can either be anchored to the top, or
0N/A * anchored to the bottom of the group. That is, the distance between the
0N/A * baseline and the beginning of the group can be a constant
0N/A * distance, or the distance between the end of the group and the
0N/A * baseline can be a constant distance. The possible choices
0N/A * correspond to the {@code BaselineResizeBehavior} constants
0N/A * {@link
0N/A * java.awt.Component.BaselineResizeBehavior#CONSTANT_ASCENT CONSTANT_ASCENT} and
0N/A * {@link
0N/A * java.awt.Component.BaselineResizeBehavior#CONSTANT_DESCENT CONSTANT_DESCENT}.
0N/A * <p>
0N/A * The baseline anchor may be explicitly specified by the
0N/A * {@code createBaselineGroup} method, or determined based on the elements.
0N/A * If not explicitly specified, the baseline will be anchored to
0N/A * the bottom if all the elements with a baseline, and that are
0N/A * aligned to the baseline, have a baseline resize behavior of
0N/A * {@code CONSTANT_DESCENT}; otherwise the baseline is anchored to the top
0N/A * of the group.
0N/A * <p>
0N/A * Elements aligned to the baseline are resizable if they have have
0N/A * a baseline resize behavior of {@code CONSTANT_ASCENT} or
0N/A * {@code CONSTANT_DESCENT}. Elements with a baseline resize
0N/A * behavior of {@code OTHER} or {@code CENTER_OFFSET} are not resizable.
0N/A * <p>
0N/A * The baseline is calculated based on the preferred height of each
0N/A * of the elements that have a baseline. The baseline is
0N/A * calculated using the following algorithm:
0N/A * {@code max(maxNonBaselineHeight, maxAscent + maxDescent)}, where the
0N/A * {@code maxNonBaselineHeight} is the maximum height of all elements
0N/A * that do not have a baseline, or are not aligned along the baseline.
0N/A * {@code maxAscent} is the maximum ascent (baseline) of all elements that
0N/A * have a baseline and are aligned along the baseline.
0N/A * {@code maxDescent} is the maximum descent (preferred height - baseline)
0N/A * of all elements that have a baseline and are aligned along the baseline.
0N/A * <p>
0N/A * A {@code ParallelGroup} that aligns it's elements along the baseline
0N/A * is only useful along the vertical axis. If you create a
0N/A * baseline group and use it along the horizontal axis an
0N/A * {@code IllegalStateException} is thrown when you ask
0N/A * {@code GroupLayout} for the minimum, preferred or maximum size or
0N/A * attempt to layout the components.
0N/A * <p>
0N/A * Elements that are not aligned to the baseline and smaller than the size
0N/A * of the {@code ParallelGroup} are positioned in one of three
0N/A * ways: centered, anchored to the leading edge, or anchored to the
0N/A * trailing edge.
0N/A *
0N/A * <h3>Non-baseline {@code ParallelGroup}</h3>
0N/A * {@code ParallelGroup}s created with an alignment other than
0N/A * {@code BASELINE} align elements that are smaller than the size
0N/A * of the group in one of three ways: centered, anchored to the
0N/A * leading edge, or anchored to the trailing edge.
0N/A * <p>
0N/A * The leading edge is based on the axis and {@code
0N/A * ComponentOrientation}. For the vertical axis the top edge is
0N/A * always the leading edge, and the bottom edge is always the
0N/A * trailing edge. When the {@code ComponentOrientation} is {@code
0N/A * LEFT_TO_RIGHT}, the leading edge is the left edge and the
0N/A * trailing edge the right edge. A {@code ComponentOrientation} of
0N/A * {@code RIGHT_TO_LEFT} flips the left and right edges. Child
0N/A * elements are aligned based on the specified alignment the
0N/A * element was added with. If you do not specify an alignment, the
0N/A * alignment specified for the {@code ParallelGroup} is used.
0N/A * <p>
0N/A * To align elements along the baseline you {@code createBaselineGroup},
0N/A * or {@code createParallelGroup} with an alignment of {@code BASELINE}.
0N/A * If the group was not created with a baseline alignment, and you attempt
0N/A * to add an element specifying a baseline alignment, an
0N/A * {@code IllegalArgumentException} is thrown.
0N/A *
0N/A * @see #createParallelGroup()
0N/A * @see #createBaselineGroup(boolean,boolean)
0N/A * @since 1.6
0N/A */
0N/A public class ParallelGroup extends Group {
0N/A // How children are layed out.
0N/A private final Alignment childAlignment;
0N/A // Whether or not we're resizable.
0N/A private final boolean resizable;
0N/A
0N/A ParallelGroup(Alignment childAlignment, boolean resizable) {
0N/A this.childAlignment = childAlignment;
0N/A this.resizable = resizable;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A */
0N/A public ParallelGroup addGroup(Group group) {
0N/A return (ParallelGroup)super.addGroup(group);
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A */
0N/A public ParallelGroup addComponent(Component component) {
0N/A return (ParallelGroup)super.addComponent(component);
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A */
0N/A public ParallelGroup addComponent(Component component, int min, int pref,
0N/A int max) {
0N/A return (ParallelGroup)super.addComponent(component, min, pref, max);
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A */
0N/A public ParallelGroup addGap(int pref) {
0N/A return (ParallelGroup)super.addGap(pref);
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A */
0N/A public ParallelGroup addGap(int min, int pref, int max) {
0N/A return (ParallelGroup)super.addGap(min, pref, max);
0N/A }
0N/A
0N/A /**
0N/A * Adds a {@code Group} to this {@code ParallelGroup} with the
0N/A * specified alignment. If the child is smaller than the
0N/A * {@code Group} it is aligned based on the specified
0N/A * alignment.
0N/A *
0N/A * @param alignment the alignment
0N/A * @param group the {@code Group} to add
0N/A * @return this {@code ParallelGroup}
0N/A * @throws IllegalArgumentException if {@code alignment} is
0N/A * {@code null}
0N/A */
0N/A public ParallelGroup addGroup(Alignment alignment, Group group) {
0N/A checkChildAlignment(alignment);
0N/A group.setAlignment(alignment);
0N/A return (ParallelGroup)addSpring(group);
0N/A }
0N/A
0N/A /**
0N/A * Adds a {@code Component} to this {@code ParallelGroup} with
0N/A * the specified alignment.
0N/A *
0N/A * @param alignment the alignment
0N/A * @param component the {@code Component} to add
0N/A * @return this {@code Group}
0N/A * @throws IllegalArgumentException if {@code alignment} is
0N/A * {@code null}
0N/A */
0N/A public ParallelGroup addComponent(Component component,
0N/A Alignment alignment) {
0N/A return addComponent(component, alignment, DEFAULT_SIZE, DEFAULT_SIZE,
0N/A DEFAULT_SIZE);
0N/A }
0N/A
0N/A /**
0N/A * Adds a {@code Component} to this {@code ParallelGroup} with the
0N/A * specified alignment and size.
0N/A *
0N/A * @param alignment the alignment
0N/A * @param component the {@code Component} to add
0N/A * @param min the minimum size
0N/A * @param pref the preferred size
0N/A * @param max the maximum size
0N/A * @throws IllegalArgumentException if {@code alignment} is
0N/A * {@code null}
0N/A * @return this {@code Group}
0N/A */
0N/A public ParallelGroup addComponent(Component component,
0N/A Alignment alignment, int min, int pref, int max) {
0N/A checkChildAlignment(alignment);
0N/A ComponentSpring spring = new ComponentSpring(component,
0N/A min, pref, max);
0N/A spring.setAlignment(alignment);
0N/A return (ParallelGroup)addSpring(spring);
0N/A }
0N/A
0N/A boolean isResizable() {
0N/A return resizable;
0N/A }
0N/A
0N/A int operator(int a, int b) {
0N/A return Math.max(a, b);
0N/A }
0N/A
0N/A int calculateMinimumSize(int axis) {
0N/A if (!isResizable()) {
0N/A return getPreferredSize(axis);
0N/A }
0N/A return super.calculateMinimumSize(axis);
0N/A }
0N/A
0N/A int calculateMaximumSize(int axis) {
0N/A if (!isResizable()) {
0N/A return getPreferredSize(axis);
0N/A }
0N/A return super.calculateMaximumSize(axis);
0N/A }
0N/A
0N/A void setValidSize(int axis, int origin, int size) {
0N/A for (Spring spring : springs) {
0N/A setChildSize(spring, axis, origin, size);
0N/A }
0N/A }
0N/A
0N/A void setChildSize(Spring spring, int axis, int origin, int size) {
0N/A Alignment alignment = spring.getAlignment();
0N/A int springSize = Math.min(
0N/A Math.max(spring.getMinimumSize(axis), size),
0N/A spring.getMaximumSize(axis));
0N/A if (alignment == null) {
0N/A alignment = childAlignment;
0N/A }
0N/A switch (alignment) {
0N/A case TRAILING:
0N/A spring.setSize(axis, origin + size - springSize,
0N/A springSize);
0N/A break;
0N/A case CENTER:
0N/A spring.setSize(axis, origin +
0N/A (size - springSize) / 2,springSize);
0N/A break;
0N/A default: // LEADING, or BASELINE
0N/A spring.setSize(axis, origin, springSize);
0N/A break;
0N/A }
0N/A }
0N/A
0N/A @Override
0N/A void insertAutopadding(int axis,
0N/A List<AutoPreferredGapSpring> leadingPadding,
0N/A List<AutoPreferredGapSpring> trailingPadding,
0N/A List<ComponentSpring> leading, List<ComponentSpring> trailing,
0N/A boolean insert) {
0N/A for (Spring spring : springs) {
0N/A if (spring instanceof ComponentSpring) {
0N/A if (((ComponentSpring)spring).isVisible()) {
0N/A for (AutoPreferredGapSpring gapSpring :
0N/A leadingPadding) {
0N/A gapSpring.addTarget((ComponentSpring)spring, axis);
0N/A }
0N/A trailing.add((ComponentSpring)spring);
0N/A }
0N/A } else if (spring instanceof Group) {
0N/A ((Group)spring).insertAutopadding(axis, leadingPadding,
0N/A trailingPadding, leading, trailing, insert);
0N/A } else if (spring instanceof AutoPreferredGapSpring) {
0N/A ((AutoPreferredGapSpring)spring).setSources(leading);
0N/A trailingPadding.add((AutoPreferredGapSpring)spring);
0N/A }
0N/A }
0N/A }
0N/A
0N/A private void checkChildAlignment(Alignment alignment) {
0N/A checkChildAlignment(alignment, (this instanceof BaselineGroup));
0N/A }
0N/A
0N/A private void checkChildAlignment(Alignment alignment,
0N/A boolean allowsBaseline) {
0N/A if (alignment == null) {
0N/A throw new IllegalArgumentException("Alignment must be non-null");
0N/A }
0N/A if (!allowsBaseline && alignment == Alignment.BASELINE) {
0N/A throw new IllegalArgumentException("Alignment must be one of:" +
0N/A "LEADING, TRAILING or CENTER");
0N/A }
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * An extension of {@code ParallelGroup} that aligns its
0N/A * constituent {@code Spring}s along the baseline.
0N/A */
0N/A private class BaselineGroup extends ParallelGroup {
0N/A // Whether or not all child springs have a baseline
0N/A private boolean allSpringsHaveBaseline;
0N/A
0N/A // max(spring.getBaseline()) of all springs aligned along the baseline
0N/A // that have a baseline
0N/A private int prefAscent;
0N/A
0N/A // max(spring.getPreferredSize().height - spring.getBaseline()) of all
0N/A // springs aligned along the baseline that have a baseline
0N/A private int prefDescent;
0N/A
0N/A // Whether baselineAnchoredToTop was explicitly set
0N/A private boolean baselineAnchorSet;
0N/A
0N/A // Whether the baseline is anchored to the top or the bottom.
0N/A // If anchored to the top the baseline is always at prefAscent,
0N/A // otherwise the baseline is at (height - prefDescent)
0N/A private boolean baselineAnchoredToTop;
0N/A
0N/A // Whether or not the baseline has been calculated.
0N/A private boolean calcedBaseline;
0N/A
0N/A BaselineGroup(boolean resizable) {
0N/A super(Alignment.LEADING, resizable);
0N/A prefAscent = prefDescent = -1;
0N/A calcedBaseline = false;
0N/A }
0N/A
0N/A BaselineGroup(boolean resizable, boolean baselineAnchoredToTop) {
0N/A this(resizable);
0N/A this.baselineAnchoredToTop = baselineAnchoredToTop;
0N/A baselineAnchorSet = true;
0N/A }
0N/A
0N/A void unset() {
0N/A super.unset();
0N/A prefAscent = prefDescent = -1;
0N/A calcedBaseline = false;
0N/A }
0N/A
0N/A void setValidSize(int axis, int origin, int size) {
0N/A checkAxis(axis);
0N/A if (prefAscent == -1) {
0N/A super.setValidSize(axis, origin, size);
0N/A } else {
0N/A // do baseline layout
0N/A baselineLayout(origin, size);
0N/A }
0N/A }
0N/A
0N/A int calculateSize(int axis, int type) {
0N/A checkAxis(axis);
0N/A if (!calcedBaseline) {
0N/A calculateBaselineAndResizeBehavior();
0N/A }
0N/A if (type == MIN_SIZE) {
0N/A return calculateMinSize();
0N/A }
0N/A if (type == MAX_SIZE) {
0N/A return calculateMaxSize();
0N/A }
0N/A if (allSpringsHaveBaseline) {
0N/A return prefAscent + prefDescent;
0N/A }
0N/A return Math.max(prefAscent + prefDescent,
0N/A super.calculateSize(axis, type));
0N/A }
0N/A
0N/A private void calculateBaselineAndResizeBehavior() {
0N/A // calculate baseline
0N/A prefAscent = 0;
0N/A prefDescent = 0;
0N/A int baselineSpringCount = 0;
0N/A BaselineResizeBehavior resizeBehavior = null;
0N/A for (Spring spring : springs) {
0N/A if (spring.getAlignment() == null ||
0N/A spring.getAlignment() == Alignment.BASELINE) {
0N/A int baseline = spring.getBaseline();
0N/A if (baseline >= 0) {
0N/A if (spring.isResizable(VERTICAL)) {
0N/A BaselineResizeBehavior brb = spring.
0N/A getBaselineResizeBehavior();
0N/A if (resizeBehavior == null) {
0N/A resizeBehavior = brb;
0N/A } else if (brb != resizeBehavior) {
0N/A resizeBehavior = BaselineResizeBehavior.
0N/A CONSTANT_ASCENT;
0N/A }
0N/A }
0N/A prefAscent = Math.max(prefAscent, baseline);
0N/A prefDescent = Math.max(prefDescent, spring.
0N/A getPreferredSize(VERTICAL) - baseline);
0N/A baselineSpringCount++;
0N/A }
0N/A }
0N/A }
0N/A if (!baselineAnchorSet) {
0N/A if (resizeBehavior == BaselineResizeBehavior.CONSTANT_DESCENT){
0N/A this.baselineAnchoredToTop = false;
0N/A } else {
0N/A this.baselineAnchoredToTop = true;
0N/A }
0N/A }
0N/A allSpringsHaveBaseline = (baselineSpringCount == springs.size());
0N/A calcedBaseline = true;
0N/A }
0N/A
0N/A private int calculateMaxSize() {
0N/A int maxAscent = prefAscent;
0N/A int maxDescent = prefDescent;
0N/A int nonBaselineMax = 0;
0N/A for (Spring spring : springs) {
0N/A int baseline;
0N/A int springMax = spring.getMaximumSize(VERTICAL);
0N/A if ((spring.getAlignment() == null ||
0N/A spring.getAlignment() == Alignment.BASELINE) &&
0N/A (baseline = spring.getBaseline()) >= 0) {
0N/A int springPref = spring.getPreferredSize(VERTICAL);
0N/A if (springPref != springMax) {
0N/A switch (spring.getBaselineResizeBehavior()) {
0N/A case CONSTANT_ASCENT:
0N/A if (baselineAnchoredToTop) {
0N/A maxDescent = Math.max(maxDescent,
0N/A springMax - baseline);
0N/A }
0N/A break;
0N/A case CONSTANT_DESCENT:
0N/A if (!baselineAnchoredToTop) {
0N/A maxAscent = Math.max(maxAscent,
0N/A springMax - springPref + baseline);
0N/A }
0N/A break;
0N/A default: // CENTER_OFFSET and OTHER, not resizable
0N/A break;
0N/A }
0N/A }
0N/A } else {
0N/A // Not aligned along the baseline, or no baseline.
0N/A nonBaselineMax = Math.max(nonBaselineMax, springMax);
0N/A }
0N/A }
0N/A return Math.max(nonBaselineMax, maxAscent + maxDescent);
0N/A }
0N/A
0N/A private int calculateMinSize() {
0N/A int minAscent = 0;
0N/A int minDescent = 0;
0N/A int nonBaselineMin = 0;
0N/A if (baselineAnchoredToTop) {
0N/A minAscent = prefAscent;
0N/A } else {
0N/A minDescent = prefDescent;
0N/A }
0N/A for (Spring spring : springs) {
0N/A int springMin = spring.getMinimumSize(VERTICAL);
0N/A int baseline;
0N/A if ((spring.getAlignment() == null ||
0N/A spring.getAlignment() == Alignment.BASELINE) &&
0N/A (baseline = spring.getBaseline()) >= 0) {
0N/A int springPref = spring.getPreferredSize(VERTICAL);
0N/A BaselineResizeBehavior brb = spring.
0N/A getBaselineResizeBehavior();
0N/A switch (brb) {
0N/A case CONSTANT_ASCENT:
0N/A if (baselineAnchoredToTop) {
0N/A minDescent = Math.max(springMin - baseline,
0N/A minDescent);
0N/A } else {
0N/A minAscent = Math.max(baseline, minAscent);
0N/A }
0N/A break;
0N/A case CONSTANT_DESCENT:
0N/A if (!baselineAnchoredToTop) {
0N/A minAscent = Math.max(
0N/A baseline - (springPref - springMin),
0N/A minAscent);
0N/A } else {
0N/A minDescent = Math.max(springPref - baseline,
0N/A minDescent);
0N/A }
0N/A break;
0N/A default:
0N/A // CENTER_OFFSET and OTHER are !resizable, use
0N/A // the preferred size.
0N/A minAscent = Math.max(baseline, minAscent);
0N/A minDescent = Math.max(springPref - baseline,
0N/A minDescent);
0N/A break;
0N/A }
0N/A } else {
0N/A // Not aligned along the baseline, or no baseline.
0N/A nonBaselineMin = Math.max(nonBaselineMin, springMin);
0N/A }
0N/A }
0N/A return Math.max(nonBaselineMin, minAscent + minDescent);
0N/A }
0N/A
0N/A /**
0N/A * Lays out springs that have a baseline along the baseline. All
0N/A * others are centered.
0N/A */
0N/A private void baselineLayout(int origin, int size) {
0N/A int ascent;
0N/A int descent;
0N/A if (baselineAnchoredToTop) {
0N/A ascent = prefAscent;
0N/A descent = size - ascent;
0N/A } else {
0N/A ascent = size - prefDescent;
0N/A descent = prefDescent;
0N/A }
0N/A for (Spring spring : springs) {
0N/A Alignment alignment = spring.getAlignment();
0N/A if (alignment == null || alignment == Alignment.BASELINE) {
0N/A int baseline = spring.getBaseline();
0N/A if (baseline >= 0) {
0N/A int springMax = spring.getMaximumSize(VERTICAL);
0N/A int springPref = spring.getPreferredSize(VERTICAL);
0N/A int height = springPref;
0N/A int y;
0N/A switch(spring.getBaselineResizeBehavior()) {
0N/A case CONSTANT_ASCENT:
0N/A y = origin + ascent - baseline;
0N/A height = Math.min(descent, springMax -
0N/A baseline) + baseline;
0N/A break;
0N/A case CONSTANT_DESCENT:
0N/A height = Math.min(ascent, springMax -
0N/A springPref + baseline) +
0N/A (springPref - baseline);
0N/A y = origin + ascent +
0N/A (springPref - baseline) - height;
0N/A break;
0N/A default: // CENTER_OFFSET & OTHER, not resizable
0N/A y = origin + ascent - baseline;
0N/A break;
0N/A }
0N/A spring.setSize(VERTICAL, y, height);
0N/A } else {
0N/A setChildSize(spring, VERTICAL, origin, size);
0N/A }
0N/A } else {
0N/A setChildSize(spring, VERTICAL, origin, size);
0N/A }
0N/A }
0N/A }
0N/A
0N/A int getBaseline() {
0N/A if (springs.size() > 1) {
0N/A // Force the baseline to be calculated
0N/A getPreferredSize(VERTICAL);
0N/A return prefAscent;
0N/A } else if (springs.size() == 1) {
0N/A return springs.get(0).getBaseline();
0N/A }
0N/A return -1;
0N/A }
0N/A
0N/A BaselineResizeBehavior getBaselineResizeBehavior() {
0N/A if (springs.size() == 1) {
0N/A return springs.get(0).getBaselineResizeBehavior();
0N/A }
0N/A if (baselineAnchoredToTop) {
0N/A return BaselineResizeBehavior.CONSTANT_ASCENT;
0N/A }
0N/A return BaselineResizeBehavior.CONSTANT_DESCENT;
0N/A }
0N/A
0N/A // If the axis is VERTICAL, throws an IllegalStateException
0N/A private void checkAxis(int axis) {
0N/A if (axis == HORIZONTAL) {
0N/A throw new IllegalStateException(
0N/A "Baseline must be used along vertical axis");
0N/A }
0N/A }
0N/A }
0N/A
0N/A
0N/A private final class ComponentSpring extends Spring {
0N/A private Component component;
0N/A private int origin;
0N/A
0N/A // min/pref/max are either a value >= 0 or one of
0N/A // DEFAULT_SIZE or PREFERRED_SIZE
0N/A private final int min;
0N/A private final int pref;
0N/A private final int max;
0N/A
0N/A // Baseline for the component, computed as necessary.
0N/A private int baseline = -1;
0N/A
0N/A // Whether or not the size has been requested yet.
0N/A private boolean installed;
0N/A
0N/A private ComponentSpring(Component component, int min, int pref,
0N/A int max) {
0N/A this.component = component;
0N/A if (component == null) {
0N/A throw new IllegalArgumentException(
0N/A "Component must be non-null");
0N/A }
0N/A
0N/A checkSize(min, pref, max, true);
0N/A
0N/A this.min = min;
0N/A this.max = max;
0N/A this.pref = pref;
0N/A
0N/A // getComponentInfo makes sure component is a child of the
0N/A // Container GroupLayout is the LayoutManager for.
0N/A getComponentInfo(component);
0N/A }
0N/A
0N/A int calculateMinimumSize(int axis) {
0N/A if (isLinked(axis)) {
0N/A return getLinkSize(axis, MIN_SIZE);
0N/A }
0N/A return calculateNonlinkedMinimumSize(axis);
0N/A }
0N/A
0N/A int calculatePreferredSize(int axis) {
0N/A if (isLinked(axis)) {
0N/A return getLinkSize(axis, PREF_SIZE);
0N/A }
0N/A int min = getMinimumSize(axis);
0N/A int pref = calculateNonlinkedPreferredSize(axis);
0N/A int max = getMaximumSize(axis);
0N/A return Math.min(max, Math.max(min, pref));
0N/A }
0N/A
0N/A int calculateMaximumSize(int axis) {
0N/A if (isLinked(axis)) {
0N/A return getLinkSize(axis, MAX_SIZE);
0N/A }
0N/A return Math.max(getMinimumSize(axis),
0N/A calculateNonlinkedMaximumSize(axis));
0N/A }
0N/A
0N/A boolean isVisible() {
0N/A return getComponentInfo(getComponent()).isVisible();
0N/A }
0N/A
0N/A int calculateNonlinkedMinimumSize(int axis) {
0N/A if (!isVisible()) {
0N/A return 0;
0N/A }
0N/A if (min >= 0) {
0N/A return min;
0N/A }
0N/A if (min == PREFERRED_SIZE) {
0N/A return calculateNonlinkedPreferredSize(axis);
0N/A }
0N/A assert (min == DEFAULT_SIZE);
0N/A return getSizeAlongAxis(axis, component.getMinimumSize());
0N/A }
0N/A
0N/A int calculateNonlinkedPreferredSize(int axis) {
0N/A if (!isVisible()) {
0N/A return 0;
0N/A }
0N/A if (pref >= 0) {
0N/A return pref;
0N/A }
0N/A assert (pref == DEFAULT_SIZE || pref == PREFERRED_SIZE);
0N/A return getSizeAlongAxis(axis, component.getPreferredSize());
0N/A }
0N/A
0N/A int calculateNonlinkedMaximumSize(int axis) {
0N/A if (!isVisible()) {
0N/A return 0;
0N/A }
0N/A if (max >= 0) {
0N/A return max;
0N/A }
0N/A if (max == PREFERRED_SIZE) {
0N/A return calculateNonlinkedPreferredSize(axis);
0N/A }
0N/A assert (max == DEFAULT_SIZE);
0N/A return getSizeAlongAxis(axis, component.getMaximumSize());
0N/A }
0N/A
0N/A private int getSizeAlongAxis(int axis, Dimension size) {
0N/A return (axis == HORIZONTAL) ? size.width : size.height;
0N/A }
0N/A
0N/A private int getLinkSize(int axis, int type) {
0N/A if (!isVisible()) {
0N/A return 0;
0N/A }
0N/A ComponentInfo ci = getComponentInfo(component);
0N/A return ci.getLinkSize(axis, type);
0N/A }
0N/A
0N/A void setSize(int axis, int origin, int size) {
0N/A super.setSize(axis, origin, size);
0N/A this.origin = origin;
0N/A if (size == UNSET) {
0N/A baseline = -1;
0N/A }
0N/A }
0N/A
0N/A int getOrigin() {
0N/A return origin;
0N/A }
0N/A
0N/A void setComponent(Component component) {
0N/A this.component = component;
0N/A }
0N/A
0N/A Component getComponent() {
0N/A return component;
0N/A }
0N/A
0N/A int getBaseline() {
0N/A if (baseline == -1) {
0N/A Spring horizontalSpring = getComponentInfo(component).
0N/A horizontalSpring;
0N/A int width = horizontalSpring.getPreferredSize(HORIZONTAL);
0N/A int height = getPreferredSize(VERTICAL);
0N/A if (width > 0 && height > 0) {
0N/A baseline = component.getBaseline(width, height);
0N/A }
0N/A }
0N/A return baseline;
0N/A }
0N/A
0N/A BaselineResizeBehavior getBaselineResizeBehavior() {
0N/A return getComponent().getBaselineResizeBehavior();
0N/A }
0N/A
0N/A private boolean isLinked(int axis) {
0N/A return getComponentInfo(component).isLinked(axis);
0N/A }
0N/A
0N/A void installIfNecessary(int axis) {
0N/A if (!installed) {
0N/A installed = true;
0N/A if (axis == HORIZONTAL) {
0N/A getComponentInfo(component).horizontalSpring = this;
0N/A } else {
0N/A getComponentInfo(component).verticalSpring = this;
0N/A }
0N/A }
0N/A }
0N/A
0N/A @Override
0N/A boolean willHaveZeroSize(boolean treatAutopaddingAsZeroSized) {
0N/A return !isVisible();
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Spring representing the preferred distance between two components.
0N/A */
0N/A private class PreferredGapSpring extends Spring {
0N/A private final JComponent source;
0N/A private final JComponent target;
0N/A private final ComponentPlacement type;
0N/A private final int pref;
0N/A private final int max;
0N/A
0N/A PreferredGapSpring(JComponent source, JComponent target,
0N/A ComponentPlacement type, int pref, int max) {
0N/A this.source = source;
0N/A this.target = target;
0N/A this.type = type;
0N/A this.pref = pref;
0N/A this.max = max;
0N/A }
0N/A
0N/A int calculateMinimumSize(int axis) {
0N/A return getPadding(axis);
0N/A }
0N/A
0N/A int calculatePreferredSize(int axis) {
0N/A if (pref == DEFAULT_SIZE || pref == PREFERRED_SIZE) {
0N/A return getMinimumSize(axis);
0N/A }
0N/A int min = getMinimumSize(axis);
0N/A int max = getMaximumSize(axis);
0N/A return Math.min(max, Math.max(min, pref));
0N/A }
0N/A
0N/A int calculateMaximumSize(int axis) {
0N/A if (max == PREFERRED_SIZE || max == DEFAULT_SIZE) {
0N/A return getPadding(axis);
0N/A }
0N/A return Math.max(getMinimumSize(axis), max);
0N/A }
0N/A
0N/A private int getPadding(int axis) {
0N/A int position;
0N/A if (axis == HORIZONTAL) {
0N/A position = SwingConstants.EAST;
0N/A } else {
0N/A position = SwingConstants.SOUTH;
0N/A }
0N/A return getLayoutStyle0().getPreferredGap(source,
0N/A target, type, position, host);
0N/A }
0N/A
0N/A @Override
0N/A boolean willHaveZeroSize(boolean treatAutopaddingAsZeroSized) {
0N/A return false;
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Spring represented a certain amount of space.
0N/A */
0N/A private class GapSpring extends Spring {
0N/A private final int min;
0N/A private final int pref;
0N/A private final int max;
0N/A
0N/A GapSpring(int min, int pref, int max) {
0N/A checkSize(min, pref, max, false);
0N/A this.min = min;
0N/A this.pref = pref;
0N/A this.max = max;
0N/A }
0N/A
0N/A int calculateMinimumSize(int axis) {
0N/A if (min == PREFERRED_SIZE) {
0N/A return getPreferredSize(axis);
0N/A }
0N/A return min;
0N/A }
0N/A
0N/A int calculatePreferredSize(int axis) {
0N/A return pref;
0N/A }
0N/A
0N/A int calculateMaximumSize(int axis) {
0N/A if (max == PREFERRED_SIZE) {
0N/A return getPreferredSize(axis);
0N/A }
0N/A return max;
0N/A }
0N/A
0N/A @Override
0N/A boolean willHaveZeroSize(boolean treatAutopaddingAsZeroSized) {
0N/A return false;
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Spring reprensenting the distance between any number of sources and
0N/A * targets. The targets and sources are computed during layout. An
0N/A * instance of this can either be dynamically created when
0N/A * autocreatePadding is true, or explicitly created by the developer.
0N/A */
0N/A private class AutoPreferredGapSpring extends Spring {
0N/A List<ComponentSpring> sources;
0N/A ComponentSpring source;
0N/A private List<AutoPreferredGapMatch> matches;
0N/A int size;
0N/A int lastSize;
0N/A private final int pref;
0N/A private final int max;
0N/A // Type of gap
0N/A private ComponentPlacement type;
0N/A private boolean userCreated;
0N/A
0N/A private AutoPreferredGapSpring() {
0N/A this.pref = PREFERRED_SIZE;
0N/A this.max = PREFERRED_SIZE;
0N/A this.type = ComponentPlacement.RELATED;
0N/A }
0N/A
0N/A AutoPreferredGapSpring(int pref, int max) {
0N/A this.pref = pref;
0N/A this.max = max;
0N/A }
0N/A
0N/A AutoPreferredGapSpring(ComponentPlacement type, int pref, int max) {
0N/A this.type = type;
0N/A this.pref = pref;
0N/A this.max = max;
0N/A this.userCreated = true;
0N/A }
0N/A
0N/A public void setSource(ComponentSpring source) {
0N/A this.source = source;
0N/A }
0N/A
0N/A public void setSources(List<ComponentSpring> sources) {
0N/A this.sources = new ArrayList<ComponentSpring>(sources);
0N/A }
0N/A
0N/A public void setUserCreated(boolean userCreated) {
0N/A this.userCreated = userCreated;
0N/A }
0N/A
0N/A public boolean getUserCreated() {
0N/A return userCreated;
0N/A }
0N/A
0N/A void unset() {
0N/A lastSize = getSize();
0N/A super.unset();
0N/A size = 0;
0N/A }
0N/A
0N/A public void reset() {
0N/A size = 0;
0N/A sources = null;
0N/A source = null;
0N/A matches = null;
0N/A }
0N/A
0N/A public void calculatePadding(int axis) {
0N/A size = UNSET;
0N/A int maxPadding = UNSET;
0N/A if (matches != null) {
0N/A LayoutStyle p = getLayoutStyle0();
0N/A int position;
0N/A if (axis == HORIZONTAL) {
0N/A if (isLeftToRight()) {
0N/A position = SwingConstants.EAST;
0N/A } else {
0N/A position = SwingConstants.WEST;
0N/A }
0N/A } else {
0N/A position = SwingConstants.SOUTH;
0N/A }
0N/A for (int i = matches.size() - 1; i >= 0; i--) {
0N/A AutoPreferredGapMatch match = matches.get(i);
0N/A maxPadding = Math.max(maxPadding,
0N/A calculatePadding(p, position, match.source,
0N/A match.target));
0N/A }
0N/A }
0N/A if (size == UNSET) {
0N/A size = 0;
0N/A }
0N/A if (maxPadding == UNSET) {
0N/A maxPadding = 0;
0N/A }
0N/A if (lastSize != UNSET) {
0N/A size += Math.min(maxPadding, lastSize);
0N/A }
0N/A }
0N/A
0N/A private int calculatePadding(LayoutStyle p, int position,
0N/A ComponentSpring source,
0N/A ComponentSpring target) {
0N/A int delta = target.getOrigin() - (source.getOrigin() +
0N/A source.getSize());
0N/A if (delta >= 0) {
0N/A int padding;
0N/A if ((source.getComponent() instanceof JComponent) &&
0N/A (target.getComponent() instanceof JComponent)) {
0N/A padding = p.getPreferredGap(
0N/A (JComponent)source.getComponent(),
0N/A (JComponent)target.getComponent(), type, position,
0N/A host);
0N/A } else {
0N/A padding = 10;
0N/A }
0N/A if (padding > delta) {
0N/A size = Math.max(size, padding - delta);
0N/A }
0N/A return padding;
0N/A }
0N/A return 0;
0N/A }
0N/A
0N/A public void addTarget(ComponentSpring spring, int axis) {
0N/A int oAxis = (axis == HORIZONTAL) ? VERTICAL : HORIZONTAL;
0N/A if (source != null) {
0N/A if (areParallelSiblings(source.getComponent(),
0N/A spring.getComponent(), oAxis)) {
0N/A addValidTarget(source, spring);
0N/A }
0N/A } else {
0N/A Component component = spring.getComponent();
0N/A for (int counter = sources.size() - 1; counter >= 0;
0N/A counter--){
0N/A ComponentSpring source = sources.get(counter);
0N/A if (areParallelSiblings(source.getComponent(),
0N/A component, oAxis)) {
0N/A addValidTarget(source, spring);
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A private void addValidTarget(ComponentSpring source,
0N/A ComponentSpring target) {
0N/A if (matches == null) {
0N/A matches = new ArrayList<AutoPreferredGapMatch>(1);
0N/A }
0N/A matches.add(new AutoPreferredGapMatch(source, target));
0N/A }
0N/A
0N/A int calculateMinimumSize(int axis) {
0N/A return size;
0N/A }
0N/A
0N/A int calculatePreferredSize(int axis) {
0N/A if (pref == PREFERRED_SIZE || pref == DEFAULT_SIZE) {
0N/A return size;
0N/A }
0N/A return Math.max(size, pref);
0N/A }
0N/A
0N/A int calculateMaximumSize(int axis) {
0N/A if (max >= 0) {
0N/A return Math.max(getPreferredSize(axis), max);
0N/A }
0N/A return size;
0N/A }
0N/A
0N/A String getMatchDescription() {
0N/A return (matches == null) ? "" : matches.toString();
0N/A }
0N/A
0N/A public String toString() {
0N/A return super.toString() + getMatchDescription();
0N/A }
0N/A
0N/A @Override
0N/A boolean willHaveZeroSize(boolean treatAutopaddingAsZeroSized) {
0N/A return treatAutopaddingAsZeroSized;
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Represents two springs that should have autopadding inserted between
0N/A * them.
0N/A */
0N/A private final static class AutoPreferredGapMatch {
0N/A public final ComponentSpring source;
0N/A public final ComponentSpring target;
0N/A
0N/A AutoPreferredGapMatch(ComponentSpring source, ComponentSpring target) {
0N/A this.source = source;
0N/A this.target = target;
0N/A }
0N/A
0N/A private String toString(ComponentSpring spring) {
0N/A return spring.getComponent().getName();
0N/A }
0N/A
0N/A public String toString() {
0N/A return "[" + toString(source) + "-" + toString(target) + "]";
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * An extension of AutopaddingSpring used for container level padding.
0N/A */
0N/A private class ContainerAutoPreferredGapSpring extends
0N/A AutoPreferredGapSpring {
0N/A private List<ComponentSpring> targets;
0N/A
0N/A ContainerAutoPreferredGapSpring() {
0N/A super();
0N/A setUserCreated(true);
0N/A }
0N/A
0N/A ContainerAutoPreferredGapSpring(int pref, int max) {
0N/A super(pref, max);
0N/A setUserCreated(true);
0N/A }
0N/A
0N/A public void addTarget(ComponentSpring spring, int axis) {
0N/A if (targets == null) {
0N/A targets = new ArrayList<ComponentSpring>(1);
0N/A }
0N/A targets.add(spring);
0N/A }
0N/A
0N/A public void calculatePadding(int axis) {
0N/A LayoutStyle p = getLayoutStyle0();
0N/A int maxPadding = 0;
0N/A int position;
0N/A size = 0;
0N/A if (targets != null) {
0N/A // Leading
0N/A if (axis == HORIZONTAL) {
0N/A if (isLeftToRight()) {
0N/A position = SwingConstants.WEST;
0N/A } else {
0N/A position = SwingConstants.EAST;
0N/A }
0N/A } else {
0N/A position = SwingConstants.SOUTH;
0N/A }
0N/A for (int i = targets.size() - 1; i >= 0; i--) {
0N/A ComponentSpring targetSpring = targets.get(i);
0N/A int padding = 10;
0N/A if (targetSpring.getComponent() instanceof JComponent) {
0N/A padding = p.getContainerGap(
0N/A (JComponent)targetSpring.getComponent(),
0N/A position, host);
0N/A maxPadding = Math.max(padding, maxPadding);
0N/A padding -= targetSpring.getOrigin();
0N/A } else {
0N/A maxPadding = Math.max(padding, maxPadding);
0N/A }
0N/A size = Math.max(size, padding);
0N/A }
0N/A } else {
0N/A // Trailing
0N/A if (axis == HORIZONTAL) {
0N/A if (isLeftToRight()) {
0N/A position = SwingConstants.EAST;
0N/A } else {
0N/A position = SwingConstants.WEST;
0N/A }
0N/A } else {
0N/A position = SwingConstants.SOUTH;
0N/A }
0N/A if (sources != null) {
0N/A for (int i = sources.size() - 1; i >= 0; i--) {
0N/A ComponentSpring sourceSpring = sources.get(i);
0N/A maxPadding = Math.max(maxPadding,
0N/A updateSize(p, sourceSpring, position));
0N/A }
0N/A } else if (source != null) {
0N/A maxPadding = updateSize(p, source, position);
0N/A }
0N/A }
0N/A if (lastSize != UNSET) {
0N/A size += Math.min(maxPadding, lastSize);
0N/A }
0N/A }
0N/A
0N/A private int updateSize(LayoutStyle p, ComponentSpring sourceSpring,
0N/A int position) {
0N/A int padding = 10;
0N/A if (sourceSpring.getComponent() instanceof JComponent) {
0N/A padding = p.getContainerGap(
0N/A (JComponent)sourceSpring.getComponent(), position,
0N/A host);
0N/A }
0N/A int delta = Math.max(0, getParent().getSize() -
0N/A sourceSpring.getSize() - sourceSpring.getOrigin());
0N/A size = Math.max(size, padding - delta);
0N/A return padding;
0N/A }
0N/A
0N/A String getMatchDescription() {
0N/A if (targets != null) {
0N/A return "leading: " + targets.toString();
0N/A }
0N/A if (sources != null) {
0N/A return "trailing: " + sources.toString();
0N/A }
0N/A return "--";
0N/A }
0N/A }
0N/A
0N/A
0N/A // LinkInfo contains the set of ComponentInfosthat are linked along a
0N/A // particular axis.
0N/A private static class LinkInfo {
0N/A private final int axis;
0N/A private final List<ComponentInfo> linked;
0N/A private int size;
0N/A
0N/A LinkInfo(int axis) {
0N/A linked = new ArrayList<ComponentInfo>();
0N/A size = UNSET;
0N/A this.axis = axis;
0N/A }
0N/A
0N/A public void add(ComponentInfo child) {
0N/A LinkInfo childMaster = child.getLinkInfo(axis, false);
0N/A if (childMaster == null) {
0N/A linked.add(child);
0N/A child.setLinkInfo(axis, this);
0N/A } else if (childMaster != this) {
0N/A linked.addAll(childMaster.linked);
0N/A for (ComponentInfo childInfo : childMaster.linked) {
0N/A childInfo.setLinkInfo(axis, this);
0N/A }
0N/A }
0N/A clearCachedSize();
0N/A }
0N/A
0N/A public void remove(ComponentInfo info) {
0N/A linked.remove(info);
0N/A info.setLinkInfo(axis, null);
0N/A if (linked.size() == 1) {
0N/A linked.get(0).setLinkInfo(axis, null);
0N/A }
0N/A clearCachedSize();
0N/A }
0N/A
0N/A public void clearCachedSize() {
0N/A size = UNSET;
0N/A }
0N/A
0N/A public int getSize(int axis) {
0N/A if (size == UNSET) {
0N/A size = calculateLinkedSize(axis);
0N/A }
0N/A return size;
0N/A }
0N/A
0N/A private int calculateLinkedSize(int axis) {
0N/A int size = 0;
0N/A for (ComponentInfo info : linked) {
0N/A ComponentSpring spring;
0N/A if (axis == HORIZONTAL) {
0N/A spring = info.horizontalSpring;
0N/A } else {
0N/A assert (axis == VERTICAL);
0N/A spring = info.verticalSpring;
0N/A }
0N/A size = Math.max(size,
0N/A spring.calculateNonlinkedPreferredSize(axis));
0N/A }
0N/A return size;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Tracks the horizontal/vertical Springs for a Component.
0N/A * This class is also used to handle Springs that have their sizes
0N/A * linked.
0N/A */
0N/A private class ComponentInfo {
0N/A // Component being layed out
0N/A private Component component;
0N/A
0N/A ComponentSpring horizontalSpring;
0N/A ComponentSpring verticalSpring;
0N/A
0N/A // If the component's size is linked to other components, the
0N/A // horizontalMaster and/or verticalMaster reference the group of
0N/A // linked components.
0N/A private LinkInfo horizontalMaster;
0N/A private LinkInfo verticalMaster;
0N/A
0N/A private boolean visible;
0N/A private Boolean honorsVisibility;
0N/A
0N/A ComponentInfo(Component component) {
0N/A this.component = component;
0N/A updateVisibility();
0N/A }
0N/A
0N/A public void dispose() {
0N/A // Remove horizontal/vertical springs
0N/A removeSpring(horizontalSpring);
0N/A horizontalSpring = null;
0N/A removeSpring(verticalSpring);
0N/A verticalSpring = null;
0N/A // Clean up links
0N/A if (horizontalMaster != null) {
0N/A horizontalMaster.remove(this);
0N/A }
0N/A if (verticalMaster != null) {
0N/A verticalMaster.remove(this);
0N/A }
0N/A }
0N/A
0N/A void setHonorsVisibility(Boolean honorsVisibility) {
0N/A this.honorsVisibility = honorsVisibility;
0N/A }
0N/A
0N/A private void removeSpring(Spring spring) {
0N/A if (spring != null) {
0N/A ((Group)spring.getParent()).springs.remove(spring);
0N/A }
0N/A }
0N/A
0N/A public boolean isVisible() {
0N/A return visible;
0N/A }
0N/A
0N/A /**
0N/A * Updates the cached visibility.
0N/A *
0N/A * @return true if the visibility changed
0N/A */
0N/A boolean updateVisibility() {
0N/A boolean honorsVisibility;
0N/A if (this.honorsVisibility == null) {
0N/A honorsVisibility = GroupLayout.this.getHonorsVisibility();
0N/A } else {
0N/A honorsVisibility = this.honorsVisibility;
0N/A }
0N/A boolean newVisible = (honorsVisibility) ?
0N/A component.isVisible() : true;
0N/A if (visible != newVisible) {
0N/A visible = newVisible;
0N/A return true;
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A public void setBounds(Insets insets, int parentWidth, boolean ltr) {
0N/A int x = horizontalSpring.getOrigin();
0N/A int w = horizontalSpring.getSize();
0N/A int y = verticalSpring.getOrigin();
0N/A int h = verticalSpring.getSize();
0N/A
0N/A if (!ltr) {
0N/A x = parentWidth - x - w;
0N/A }
0N/A component.setBounds(x + insets.left, y + insets.top, w, h);
0N/A }
0N/A
0N/A public void setComponent(Component component) {
0N/A this.component = component;
0N/A if (horizontalSpring != null) {
0N/A horizontalSpring.setComponent(component);
0N/A }
0N/A if (verticalSpring != null) {
0N/A verticalSpring.setComponent(component);
0N/A }
0N/A }
0N/A
0N/A public Component getComponent() {
0N/A return component;
0N/A }
0N/A
0N/A /**
0N/A * Returns true if this component has its size linked to
0N/A * other components.
0N/A */
0N/A public boolean isLinked(int axis) {
0N/A if (axis == HORIZONTAL) {
0N/A return horizontalMaster != null;
0N/A }
0N/A assert (axis == VERTICAL);
0N/A return (verticalMaster != null);
0N/A }
0N/A
0N/A private void setLinkInfo(int axis, LinkInfo linkInfo) {
0N/A if (axis == HORIZONTAL) {
0N/A horizontalMaster = linkInfo;
0N/A } else {
0N/A assert (axis == VERTICAL);
0N/A verticalMaster = linkInfo;
0N/A }
0N/A }
0N/A
0N/A public LinkInfo getLinkInfo(int axis) {
0N/A return getLinkInfo(axis, true);
0N/A }
0N/A
0N/A private LinkInfo getLinkInfo(int axis, boolean create) {
0N/A if (axis == HORIZONTAL) {
0N/A if (horizontalMaster == null && create) {
0N/A // horizontalMaster field is directly set by adding
0N/A // us to the LinkInfo.
0N/A new LinkInfo(HORIZONTAL).add(this);
0N/A }
0N/A return horizontalMaster;
0N/A } else {
0N/A assert (axis == VERTICAL);
0N/A if (verticalMaster == null && create) {
0N/A // verticalMaster field is directly set by adding
0N/A // us to the LinkInfo.
0N/A new LinkInfo(VERTICAL).add(this);
0N/A }
0N/A return verticalMaster;
0N/A }
0N/A }
0N/A
0N/A public void clearCachedSize() {
0N/A if (horizontalMaster != null) {
0N/A horizontalMaster.clearCachedSize();
0N/A }
0N/A if (verticalMaster != null) {
0N/A verticalMaster.clearCachedSize();
0N/A }
0N/A }
0N/A
0N/A int getLinkSize(int axis, int type) {
0N/A if (axis == HORIZONTAL) {
0N/A return horizontalMaster.getSize(axis);
0N/A } else {
0N/A assert (axis == VERTICAL);
0N/A return verticalMaster.getSize(axis);
0N/A }
0N/A }
0N/A
0N/A }
0N/A}