Code/Resource
Windows Develop
Linux-Unix program
Internet-Socket-Network
Web Server
Browser Client
Ftp Server
Ftp Client
Browser Plugins
Proxy Server
Email Server
Email Client
WEB Mail
Firewall-Security
Telnet Server
Telnet Client
ICQ-IM-Chat
Search Engine
Sniffer Package capture
Remote Control
xml-soap-webservice
P2P
WEB(ASP,PHP,...)
TCP/IP Stack
SNMP
Grid Computing
SilverLight
DNS
Cluster Service
Network Security
Communication-Mobile
Game Program
Editor
Multimedia program
Graph program
Compiler program
Compress-Decompress algrithms
Crypt_Decrypt algrithms
Mathimatics-Numerical algorithms
MultiLanguage
Disk/Storage
Java Develop
assembly language
Applications
Other systems
Database system
Embeded-SCM Develop
FlashMX/Flex
source in ebook
Delphi VCL
OS Develop
MiddleWare
MPI
MacOS develop
LabView
ELanguage
Software/Tools
E-Books
Artical/Document
GroupLayout.java
Package: LWUIT1.3code.rar [view]
Upload User: haobig99
Upload Date: 2022-06-15
Package Size: 369k
Code Size: 130k
Category:
J2ME
Development Platform:
Java
- /*
- * Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- *
- * This code is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 only, as
- * published by the Free Software Foundation. Sun designates this
- * particular file as subject to the "Classpath" exception as provided
- * by Sun in the LICENSE file that accompanied this code.
- *
- * This code is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
- * version 2 for more details (a copy is included in the LICENSE file that
- * accompanied this code).
- *
- * You should have received a copy of the GNU General Public License version
- * 2 along with this work; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
- * CA 95054 USA or visit www.sun.com if you need additional information or
- * have any questions.
- */
- package com.sun.lwuit.layouts;
- import com.sun.lwuit.Component;
- import com.sun.lwuit.Container;
- import com.sun.lwuit.Form;
- import com.sun.lwuit.geom.Dimension;
- import java.util.*;
- /**
- * GroupLayout is a LayoutManager that hierarchically groups components to
- * achieve common, and not so common, layouts. Grouping is done by instances
- * of the Group class. GroupLayout supports two types of groups:
- * <table>
- * <tr><td valign=top>Sequential:<td>A sequential group positions its child
- * elements sequentially, one after another.
- * <tr><td valign=top>Parallel:<td>A parallel group positions its child
- * elements in the same space on top of each other. Parallel groups
- * can also align the child elements along their baseline.
- * </table>
- * Each Group can contain any number of child groups, Components or gaps.
- * GroupLayout treats each axis independently. That is, there is a group
- * representing the horizontal axis, and a separate group representing the
- * vertical axis. The horizontal group is responsible for setting the x
- * and width of its contents, where as the vertical group is responsible for
- * setting the y and height of its contents.
- * <p>
- * The following code builds a simple layout consisting of two labels in
- * one column, followed by two textfields in the next column:
- * <pre>
- * JComponent panel = ...;
- * GroupLayout layout = new GroupLayout(panel);
- * panel.setLayout(layout);
- * layout.setAutocreateGaps(true);
- * layout.setAutocreateContainerGaps(true);
- * GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
- * hGroup.add(layout.createParallelGroup().add(label1).add(label2)).
- * add(layout.createParallelGroup().add(tf1).add(tf2));
- * layout.setHorizontalGroup(hGroup);
- * GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();
- * vGroup.add(layout.createParallelGroup(GroupLayout.BASELINE).add(label1).add(tf1)).
- * add(layout.createParallelGroup(GroupLayout.BASELINE).add(label2).add(tf2));
- * layout.setVerticalGroup(vGroup);
- * </pre>
- * <p>
- * This layout consists of the following:
- * <ul><li>The horizontal axis consists of a sequential group containing two
- * parallel groups. The first parallel group consists of the labels,
- * with the second parallel group consisting of the text fields.
- * <li>The vertical axis similarly consists of a sequential group
- * containing two parallel groups. The parallel groups align their
- * contents along the baseline. The first parallel group consists
- * of the first label and text field, and the second group consists
- * of the second label and text field.
- * </ul>
- * There are a couple of things to notice in this code:
- * <ul>
- * <li>You need not explicitly add the components to the container, this
- * is indirectly done by using one of the <code>add</code> methods.
- * <li>The various <code>add</code> methods of <code>Groups</code> return
- * themselves. This allows for easy chaining of invocations. For
- * example, <code>group.add(label1).add(label2);</code> is equivalent to
- * <code>group.add(label1);group.add(label2);</code>.
- * <li>There are no public constructors for the Groups, instead
- * use the create methods of <code>GroupLayout</code>.
- * </ul>
- * GroupLayout offer the ability to automatically insert the appropriate gap
- * between components. This can be turned on using the
- * <code>setAutocreateGaps()</code> method. Similarly you can use
- * the <code>setAutocreateContainerGaps()</code> method to insert gaps
- * between the components and the container.
- *
- * @version $Revision: 1.25 $
- * @author Tomas Pavek
- * @author Jan Stola
- * @author Scott Violet
- * @author Shai Almog
- */
- public class GroupLayout extends Layout {
- /**
- * Compass-direction North (up).
- */
- public static final int NORTH = 1;
- /**
- * Compass-direction east (right).
- */
- public static final int EAST = 3;
- /**
- * Compass-direction south (down).
- */
- public static final int SOUTH = 5;
- /**
- * Compass-direction west (left).
- */
- public static final int WEST = 7;
- // Used in size calculations
- private static final int MIN_SIZE = 0;
- private static final int PREF_SIZE = 1;
- private static final int MAX_SIZE = 2;
- // Used by prepare, indicates min, pref or max isn't going to be used.
- private static final int SPECIFIC_SIZE = 3;
- private static final int UNSET = Integer.MIN_VALUE;
- /**
- * Possible argument when linking sizes of components. Specifies the
- * the two component should share the same size along the horizontal
- * axis.
- *
- * @see #linkSize(Component[], int)
- */
- public static final int HORIZONTAL = 1;
- /**
- * Possible argument when linking sizes of components. Specifies the
- * the two component should share the same size along the vertical
- * axis.
- *
- * @see #linkSize(Component[],int)
- */
- public static final int VERTICAL = 2;
- private static final int NO_ALIGNMENT = 0;
- /**
- * Possible alignment type. Indicates the elements should be
- * aligned to the origin. For the horizontal axis with a left to
- * right orientation this means aligned to the left.
- *
- * @see #createParallelGroup(int)
- */
- public static final int LEADING = 1;
- /**
- * Possible alignment type. Indicates the elements should be
- * aligned to the end. For the horizontal axis with a left to
- * right orientation this means aligned to the right.
- *
- * @see #createParallelGroup(int)
- */
- public static final int TRAILING = 2;
- /**
- * Possible alignment type. Indicates the elements should centered in
- * the spaced provided.
- *
- * @see #createParallelGroup(int)
- */
- public static final int CENTER = 4;
- /**
- * Possible alignment type. Indicates the elements should aligned along
- * their baseline.
- *
- * @see #createParallelGroup(int)
- */
- public static final int BASELINE = 3;
- /**
- * Possible value for the add methods that takes a Component.
- * Indicates the size from the component should be used.
- */
- public static final int DEFAULT_SIZE = -1;
- /**
- * Possible value for the add methods that takes a Component.
- * Indicates the preferred size should be used.
- */
- public static final int PREFERRED_SIZE = -2;
- // Whether or not we automatically try and create the preferred
- // padding between components.
- private boolean autocreatePadding;
- // Whether or not we automatically try and create the preferred
- // padding between containers
- private boolean autocreateContainerPadding;
- /**
- * Group responsible for layout along the horizontal axis. This is NOT
- * the user specified group, use getHorizontalGroup to dig that out.
- */
- private Group horizontalGroup;
- /**
- * Group responsible for layout along the vertical axis. This is NOT
- * the user specified group, use getVerticalGroup to dig that out.
- */
- private Group verticalGroup;
- // Maps from Component to ComponentInfo. This is used for tracking
- // information specific to a Component.
- private Hashtable componentInfos;
- // Container we're doing layout for.
- private Container host;
- // Used by areParallelSiblings, cached to avoid excessive garbage.
- private Vector tmpParallelSet;
- // Indicates Springs have changed in some way since last change.
- private boolean springsChanged;
- // Indicates invalidateLayout has been invoked.
- private boolean isValid;
- // Whether or not any preferred padding (or container padding) springs exist
- private boolean hasPreferredPaddingSprings;
- /**
- * The LayoutStyle instance to use, if null the sharedInstance is used.
- */
- private LayoutStyle layoutStyle;
- /**
- * If true, components that are not visible are treated as though they
- * aren't there.
- */
- private boolean honorsVisibility;
- private static void checkSize(int min, int pref, int max,
- boolean isComponentSpring) {
- checkResizeType(min, isComponentSpring);
- if (!isComponentSpring && pref < 0) {
- throw new IllegalArgumentException("Pref must be >= 0");
- } else if (isComponentSpring) {
- checkResizeType(pref, true);
- }
- checkResizeType(max, isComponentSpring);
- checkLessThan(min, pref);
- checkLessThan(pref, max);
- }
- private static void checkResizeType(int type, boolean isComponentSpring) {
- if (type < 0 && ((isComponentSpring && type != DEFAULT_SIZE &&
- type != PREFERRED_SIZE) ||
- (!isComponentSpring && type != PREFERRED_SIZE))) {
- throw new IllegalArgumentException("Invalid size");
- }
- }
- private static void checkLessThan(int min, int max) {
- if (min >= 0 && max >= 0 && min > max) {
- throw new IllegalArgumentException(
- "Following is not met: min<=pref<=max");
- }
- }
- /**
- * Creates a GroupLayout for the specified JComponent.
- *
- * @param host the Container to layout
- * @throws IllegalArgumentException if host is null
- */
- public GroupLayout(Container host) {
- if (host == null) {
- throw new IllegalArgumentException("Container must be non-null");
- }
- if(host instanceof Form) {
- host = ((Form)host).getContentPane();
- }
- honorsVisibility = true;
- this.host = host;
- setHorizontalGroup(createParallelGroup(LEADING, true));
- setVerticalGroup(createParallelGroup(LEADING, true));
- componentInfos = new Hashtable();
- tmpParallelSet = new Vector();
- }
- /**
- * Sets whether component visibility is considered when sizing and
- * positioning components. A value of <code>true</code> indicates that
- * non-visible components should not be treated as part of the
- * layout. A value of <code>false</code> indicates that components should be
- * positioned and sized regardless of visibility.
- * <p>
- * A value of <code>false</code> is useful when the visibility of components
- * is dynamically adjusted and you don't want surrounding components and
- * the sizing to change.
- * <p>
- * The specified value is used for components that do not have an
- * explicit visibility specified.
- * <p>
- * The default is <code>true</code>.
- *
- * @param honorsVisibility whether component visibility is considered when
- * sizing and positioning components
- * @see #setHonorsVisibility(Component,Boolean)
- */
- public void setHonorsVisibility(boolean honorsVisibility) {
- if (this.honorsVisibility != honorsVisibility) {
- this.honorsVisibility = honorsVisibility;
- springsChanged = true;
- isValid = false;
- invalidateHost();
- }
- }
- /**
- * Returns whether component visibility is considered when sizing and
- * positioning components.
- *
- * @return whether component visibility is considered when sizing and
- * positioning components
- */
- public boolean getHonorsVisibility() {
- return honorsVisibility;
- }
- /**
- * Sets whether the component's visibility is considered for
- * sizing and positioning. A value of <code>Boolean.TRUE</code>
- * indicates that if <code>component</code> is not visible it should
- * not be treated as part of the layout. A value of <code>false</code>
- * indicates that <code>component</code> is positioned and sized
- * regardless of it's visibility. A value of <code>null</code>
- * indicates the value specified by the single argument method <code>
- * setHonorsVisibility</code> should be used.
- * <p>
- * If <code>component</code> is not a child of the <code>Container</code> this
- * <code>GroupLayout</code> is managing, it will be added to the
- * <code>Container</code>.
- *
- * @param component the component
- * @param honorsVisibility whether <code>component</code>'s visibility should be
- * considered for sizing and positioning
- * @throws IllegalArgumentException if <code>component</code> is <code>null</code>
- * @see #setHonorsVisibility(boolean)
- */
- public void setHonorsVisibility(Component component,
- Boolean honorsVisibility) {
- if (component == null) {
- throw new IllegalArgumentException("Component must be non-null");
- }
- getComponentInfo(component).setHonorsVisibility(honorsVisibility);
- springsChanged = true;
- isValid = false;
- invalidateHost();
- }
- /**
- * Returns a textual description of this GroupLayout. The return value
- * is intended for debugging purposes only.
- *
- * @return textual description of this GroupLayout
- **/
- public String toString() {
- if (springsChanged) {
- registerComponents(horizontalGroup, HORIZONTAL);
- registerComponents(verticalGroup, VERTICAL);
- }
- StringBuffer buffer = new StringBuffer();
- buffer.append("HORIZONTALn");
- dump(buffer, horizontalGroup, " ", HORIZONTAL);
- buffer.append("nVERTICALn");
- dump(buffer, verticalGroup, " ", VERTICAL);
- return buffer.toString();
- }
- private void dump(StringBuffer buffer, Spring spring, String indent,
- int axis) {
- String origin = "";
- String padding = "";
- if (spring instanceof ComponentSpring) {
- ComponentSpring cSpring = (ComponentSpring)spring;
- origin = Integer.toString(cSpring.getOrigin()) + " ";
- String name = cSpring.getComponent().toString();
- if (name != null) {
- origin = "name=" + name + ", ";
- }
- }
- if (spring instanceof AutopaddingSpring) {
- AutopaddingSpring paddingSpring = (AutopaddingSpring)spring;
- padding = ", userCreated=" + paddingSpring.getUserCreated() +
- ", matches=" + paddingSpring.getMatchDescription();
- }
- buffer.append(indent + spring.getClass().getName() + " " +
- Integer.toHexString(spring.hashCode()) + " " +
- origin +
- ", size=" + spring.getSize() +
- ", alignment=" + spring.getAlignment() +
- " prefs=[" + spring.getMinimumSize(axis) +
- " " + spring.getPreferredSize(axis) +
- " " + spring.getMaximumSize(axis) +
- padding + "]n");
- if (spring instanceof Group) {
- Vector springs = ((Group)spring).springs;
- indent += " ";
- for (int counter = 0; counter < springs.size(); counter++) {
- dump(buffer, (Spring)springs.elementAt(counter), indent, axis);
- }
- }
- }
- /**
- * Sets whether or not a gap between components
- * should automatically be created. For example, if this is true
- * and you add two components to a <code>SequentialGroup</code> a
- * gap between the two will automatically be created. The default
- * is false.
- *
- * @param autocreatePadding whether or not to automatically created a gap
- * between components and the container
- */
- public void setAutocreateGaps(boolean autocreatePadding) {
- if (this.autocreatePadding != autocreatePadding) {
- this.autocreatePadding = autocreatePadding;
- invalidateHost();
- }
- }
- /**
- * Returns true if gaps between components are automatically be created.
- *
- * @return true if gaps between components should automatically be created
- */
- public boolean getAutocreateGaps() {
- return autocreatePadding;
- }
- /**
- * Sets whether or not gaps between the container and the first/last
- * components should automatically be created. The default
- * is false.
- *
- * @param autocreatePadding whether or not to automatically create
- * gaps between the container and first/last components.
- */
- public void setAutocreateContainerGaps(boolean autocreatePadding) {
- if (autocreatePadding != autocreateContainerPadding) {
- autocreateContainerPadding = autocreatePadding;
- horizontalGroup = createTopLevelGroup(getHorizontalGroup());
- verticalGroup = createTopLevelGroup(getVerticalGroup());
- invalidateHost();
- }
- }
- /**
- * Returns whether or not gaps between the container and the
- * first/last components should automatically be created. The default
- * is false.
- *
- * @return whether or not the gaps between the container and the
- * first/last components should automatically be created
- */
- public boolean getAutocreateContainerGaps() {
- return autocreateContainerPadding;
- }
- /**
- * Sets the <code>Group</code> that is responsible for
- * layout along the horizontal axis.
- *
- * @param group <code>Group</code> responsible for layout along
- * the horizontal axis
- * @throws IllegalArgumentException if group is null
- */
- public void setHorizontalGroup(Group group) {
- if (group == null) {
- throw new IllegalArgumentException("Group must be non-null");
- }
- horizontalGroup = createTopLevelGroup(group);
- invalidateHost();
- }
- /**
- * Returns the <code>Group</code> that is responsible for
- * layout along the horizontal axis.
- *
- * @return <code>ParallelGroup</code> responsible for layout along
- * the horizontal axis.
- */
- public Group getHorizontalGroup() {
- int index = 0;
- if (horizontalGroup.springs.size() > 1) {
- index = 1;
- }
- return (Group)horizontalGroup.springs.elementAt(index);
- }
- /**
- * Sets the <code>Group</code> that is responsible for
- * layout along the vertical axis.
- *
- * @param group <code>Group</code> responsible for layout along
- * the vertical axis.
- * @throws IllegalArgumentException if group is null.
- */
- public void setVerticalGroup(Group group) {
- if (group == null) {
- throw new IllegalArgumentException("Group must be non-null");
- }
- verticalGroup = createTopLevelGroup(group);
- invalidateHost();
- }
- /**
- * Returns the <code>ParallelGroup</code> that is responsible for
- * layout along the vertical axis.
- *
- * @return <code>ParallelGroup</code> responsible for layout along
- * the vertical axis.
- */
- public Group getVerticalGroup() {
- int index = 0;
- if (verticalGroup.springs.size() > 1) {
- index = 1;
- }
- return (Group)verticalGroup.springs.elementAt(index);
- }
- /**
- * Wraps the user specified group in a sequential group. If
- * container gaps should be generate the necessary springs are
- * added.
- */
- private Group createTopLevelGroup(Group specifiedGroup) {
- SequentialGroup group = createSequentialGroup();
- if (getAutocreateContainerGaps()) {
- group.addSpring(new ContainerAutopaddingSpring());
- group.add(specifiedGroup);
- group.addSpring(new ContainerAutopaddingSpring());
- } else {
- group.add(specifiedGroup);
- }
- return group;
- }
- /**
- * Creates and returns a <code>SequentialGroup</code>.
- *
- * @return a new <code>SequentialGroup</code>
- */
- public SequentialGroup createSequentialGroup() {
- return new SequentialGroup();
- }
- /**
- * Creates and returns a <code>ParallelGroup</code> with a
- * <code>LEADING</code> alignment. This is a cover method for the more
- * general <code>createParallelGroup(int)</code> method.
- *
- * @return a new ParallelGroup
- * @see #createParallelGroup(int)
- */
- public ParallelGroup createParallelGroup() {
- return createParallelGroup(LEADING);
- }
- /**
- * Creates and returns an <code>ParallelGroup</code>. The alignment
- * specifies how children elements should be positioned when the
- * the parallel group is given more space than necessary. For example,
- * if a ParallelGroup with an alignment of TRAILING is given 100 pixels
- * and a child only needs 50 pixels, the child will be positioned at the
- * position 50.
- *
- * @param alignment alignment for the elements of the Group, one
- * of <code>LEADING</code>, <code>TRAILING</code>,
- * <code>CENTER</code> or <code>BASELINE</code>.
- * @throws IllegalArgumentException if alignment is not one of
- * <code>LEADING</code>, <code>TRAILING</code>,
- * <code>CENTER</code> or <code>BASELINE</code>
- * @return a new <code>ParallelGroup</code>
- */
- public ParallelGroup createParallelGroup(int alignment) {
- return createParallelGroup(alignment, true);
- }
- /**
- * Creates and returns an <code>ParallelGroup</code>. The alignment
- * specifies how children elements should be positioned when the
- * the parallel group is given more space than necessary. For example,
- * if a ParallelGroup with an alignment of TRAILING is given 100 pixels
- * and a child only needs 50 pixels, the child will be positioned at the
- * position 50.
- *
- * @param alignment alignment for the elements of the Group, one
- * of <code>LEADING</code>, <code>TRAILING</code>,
- * <code>CENTER</code> or <code>BASELINE</code>.
- * @param resizable whether or not the group is resizable. If the group
- * is not resizable the min/max size will be the same as the
- * preferred.
- * @throws IllegalArgumentException if alignment is not one of
- * <code>LEADING</code>, <code>TRAILING</code>,
- * <code>CENTER</code> or <code>BASELINE</code>
- * @return a new <code>ParallelGroup</code>
- */
- public ParallelGroup createParallelGroup(int alignment, boolean resizable) {
- if (alignment == BASELINE) {
- return new BaselineGroup(resizable);
- }
- return new ParallelGroup(alignment, resizable);
- }
- /**
- * Creates and returns a <code>ParallelGroup</code> that aligns it's
- * elements along the baseline.
- *
- * @param resizable whether the group is resizable
- * @param anchorBaselineToTop whether the baseline is anchored to
- * the top or bottom of the group
- * @return parallel group
- * @see #createBaselineGroup
- * @see ParallelGroup
- */
- public ParallelGroup createBaselineGroup(boolean resizable,
- boolean anchorBaselineToTop) {
- return new BaselineGroup(resizable, anchorBaselineToTop);
- }
- /**
- * Forces the set of components to have the same size.
- * This can be used multiple times to force
- * any number of components to share the same size.
- * <p>
- * Linked Components are not be resizable.
- *
- * @param components Components to force to have same size.
- * @throws IllegalArgumentException if <code>components</code> is
- * null, or contains null.
- */
- public void linkSize(Component[] components) {
- linkSize(components, HORIZONTAL | VERTICAL);
- }
- /**
- * Forces the set of components to have the same size.
- * This can be used multiple times to force
- * any number of components to share the same size.
- * <p>
- * Linked Components are not be resizable.
- *
- * @param components Components to force to have same size.
- * @param axis Axis to bind size, one of HORIZONTAL, VERTICAL or
- * HORIZONTAL | VERTICAL
- * @throws IllegalArgumentException if <code>components</code> is
- * null, or contains null.
- * @throws IllegalArgumentException if <code>axis</code> does not
- * contain <code>HORIZONTAL</code> or <code>VERTICAL</code>
- */
- public void linkSize(Component[] components, int axis) {
- if (components == null) {
- throw new IllegalArgumentException("Components must be non-null");
- }
- boolean horizontal = ((axis & HORIZONTAL) == HORIZONTAL);
- boolean vertical = ((axis & VERTICAL) == VERTICAL);
- if (!vertical && !horizontal) {
- throw new IllegalArgumentException(
- "Axis must contain HORIZONTAL or VERTICAL");
- }
- for (int counter = components.length - 1; counter >= 0; counter--) {
- Component c = components[counter];
- if (components[counter] == null) {
- throw new IllegalArgumentException(
- "Components must be non-null");
- }
- // Force the component to be added
- getComponentInfo(c);
- }
- if (horizontal) {
- linkSize0(components, HORIZONTAL);
- }
- if (vertical) {
- linkSize0(components, VERTICAL);
- }
- invalidateHost();
- }
- private void linkSize0(Component[] components, int axis) {
- LinkInfo master = getComponentInfo(
- components[components.length - 1]).getLinkInfo(axis);
- for (int counter = components.length - 2; counter >= 0; counter--) {
- master.add(getComponentInfo(components[counter]));
- }
- }
- /**
- * Removes an existing component replacing it with the specified component.
- *
- * @param existingComponent the Component that should be removed and
- * replaced with newComponent
- * @param newComponent the Component to put in existingComponents place
- * @throws IllegalArgumentException is either of the Components are null or
- * if existingComponent is not being managed by this layout manager
- */
- public void replace(Component existingComponent, Component newComponent) {
- if (existingComponent == null || newComponent == null) {
- throw new IllegalArgumentException("Components must be non-null");
- }
- // Make sure all the components have been registered, otherwise we may
- // not update the correct Springs.
- if (springsChanged) {
- registerComponents(horizontalGroup, HORIZONTAL);
- registerComponents(verticalGroup, VERTICAL);
- }
- ComponentInfo info = (ComponentInfo)componentInfos.
- remove(existingComponent);
- if (info == null) {
- throw new IllegalArgumentException("Component must already exist");
- }
- host.removeComponent(existingComponent);
- if (newComponent.getParent() != host) {
- host.addComponent(newComponent);
- }
- info.setComponent(newComponent);
- componentInfos.put(newComponent, info);
- invalidateHost();
- }
- /**
- * Sets the LayoutStyle this GroupLayout is to use. A value of null can
- * be used to indicate the shared instance of LayoutStyle should be used.
- *
- * @param layoutStyle the LayoutStyle to use
- */
- public void setLayoutStyle(LayoutStyle layoutStyle) {
- this.layoutStyle = layoutStyle;
- invalidateHost();
- }
- /**
- * Returns the LayoutStyle instance to use
- *
- * @return the LayoutStyle instance to use
- */
- public LayoutStyle getLayoutStyle() {
- return layoutStyle;
- }
- private LayoutStyle getLayoutStyle0() {
- LayoutStyle layoutStyle = getLayoutStyle();
- if (layoutStyle == null) {
- layoutStyle = LayoutStyle.getSharedInstance();
- }
- return layoutStyle;
- }
- private void invalidateHost() {
- host.invalidate();
- host.repaint();
- }
- /**
- * Notification that a <code>Component</code> has been removed from
- * the parent container. You should not invoke this method
- * directly, instead invoke <code>removeComponent</code> on the parent
- * <code>Container</code>.
- *
- * @param component the component to be removed
- * @see Container#removeComponent
- */
- public void removeLayoutComponent(Component component) {
- ComponentInfo info = (ComponentInfo)componentInfos.remove(component);
- if (info != null) {
- info.dispose();
- springsChanged = true;
- isValid = false;
- }
- }
- /**
- * Returns the preferred size for the specified container.
- *
- * @param parent the container to return size for
- * @throws IllegalArgumentException if <code>parent</code> is not
- * the same <code>Container</code> that this was created with
- * @throws IllegalStateException if any of the components added to
- * this layout are not in both a horizontal and vertical group
- * @see Container#getPreferredSize
- */
- public Dimension getPreferredSize(Container parent) {
- checkParent(parent);
- prepare(PREF_SIZE);
- return adjustSize(horizontalGroup.getPreferredSize(HORIZONTAL),
- verticalGroup.getPreferredSize(VERTICAL));
- }
- /**
- * Returns the minimum size for the specified container.
- *
- * @param parent the container to return size for
- * @throws IllegalArgumentException if <code>parent</code> is not
- * the same <code>Container</code> that this was created with
- * @throws IllegalStateException if any of the components added to
- * this layout are not in both a horizontal and vertical group
- * @see java.awt.Container#getMinimumSize
- */
- /*public Dimension minimumLayoutSize(Container parent) {
- checkParent(parent);
- prepare(MIN_SIZE);
- return adjustSize(horizontalGroup.getMinimumSize(HORIZONTAL),
- verticalGroup.getMinimumSize(VERTICAL));
- }*/
- /**
- * Lays out the specified container.
- *
- * @param parent the container to be laid out
- * @throws IllegalStateException if any of the components added to
- * this layout are not in both a horizontal and vertical group
- */
- public void layoutContainer(Container parent) {
- // Step 1: Prepare for layout.
- prepare(SPECIFIC_SIZE);
- int insetLeft = parent.getStyle().getMargin(false, Component.LEFT);
- int insetTop = parent.getStyle().getMargin(false, Component.TOP);
- int insetRight = parent.getStyle().getMargin(false, Component.RIGHT);
- int insetBottom = parent.getStyle().getMargin(false, Component.BOTTOM);
- int width = parent.getWidth() - insetLeft - insetRight;
- int height = parent.getHeight() - insetTop - insetBottom;
- boolean ltr = isLeftToRight();
- if (getAutocreateGaps() || getAutocreateContainerGaps() ||
- hasPreferredPaddingSprings) {
- // Step 2: Calculate autopadding springs
- calculateAutopadding(horizontalGroup, HORIZONTAL, SPECIFIC_SIZE, 0,
- width);
- calculateAutopadding(verticalGroup, VERTICAL, SPECIFIC_SIZE, 0,
- height);
- }
- // Step 3: set the size of the groups.
- horizontalGroup.setSize(HORIZONTAL, 0, width);
- verticalGroup.setSize(VERTICAL, 0, height);
- // Step 4: apply the size to the components.
- Enumeration componentInfo = componentInfos.elements();
- while (componentInfo.hasMoreElements()) {
- ComponentInfo info = (ComponentInfo)componentInfo.nextElement();
- Component c = info.getComponent();
- info.setBounds(insetLeft, insetTop, width, ltr);
- }
- }
- /**
- * Returns the maximum size for the specified container.
- *
- * @param parent the container to return size for
- * @throws IllegalArgumentException if <code>parent</code> is not
- * the same <code>Container</code> that this was created with
- * @throws IllegalStateException if any of the components added to
- * this layout are not in both a horizontal and vertical group
- * @see java.awt.Container#getMaximumSize
- */
- /*public Dimension maximumLayoutSize(Container parent) {
- checkParent(parent);
- prepare(MAX_SIZE);
- return adjustSize(horizontalGroup.getMaximumSize(HORIZONTAL),
- verticalGroup.getMaximumSize(VERTICAL));
- }*/
- /**
- * Returns the alignment along the x axis. This specifies how
- * the component would like to be aligned relative to other
- * components. The value should be a number between 0 and 1
- * where 0 represents alignment along the origin, 1 is aligned
- * the furthest away from the origin, 0.5 is centered, etc.
- *
- * @param parent Container hosting this LayoutManager
- * @throws IllegalArgumentException if <code>parent</code> is not
- * the same <code>Container</code> that this was created with
- * @return alignment
- */
- /*public float getLayoutAlignmentX(Container parent) {
- checkParent(parent);
- return .5f;
- }*
- /**
- * Returns the alignment along the y axis. This specifies how
- * the component would like to be aligned relative to other
- * components. The value should be a number between 0 and 1
- * where 0 represents alignment along the origin, 1 is aligned
- * the furthest away from the origin, 0.5 is centered, etc.
- *
- * @param parent Container hosting this LayoutManager
- * @throws IllegalArgumentException if <code>parent</code> is not
- * the same <code>Container</code> that this was created with
- * @return alignment
- */
- /*public float getLayoutAlignmentY(Container parent) {
- checkParent(parent);
- return .5f;
- }*/
- /**
- * Invalidates the layout, indicating that if the layout manager
- * has cached information it should be discarded.
- *
- * @param parent Container hosting this LayoutManager
- * @throws IllegalArgumentException if <code>parent</code> is not
- * the same <code>Container</code> that this was created with
- */
- /*public void invalidateLayout(Container parent) {
- checkParent(parent);
- // invalidateLayout is called from Container.invalidate, which
- // does NOT grab the treelock. All other methods do. To make sure
- // there aren't any possible threading problems we grab the tree lock
- // here.
- //synchronized(parent.getTreeLock()) {
- isValid = false;
- //}
- }*/
- private void prepare(int sizeType) {
- boolean visChanged = false;
- // Step 1: If not-valid, clear springs and update visibility.
- if (!isValid) {
- isValid = true;
- horizontalGroup.setSize(HORIZONTAL, UNSET, UNSET);
- verticalGroup.setSize(VERTICAL, UNSET, UNSET);
- for (Enumeration cis = componentInfos.elements();
- cis.hasMoreElements();) {
- ComponentInfo ci = (ComponentInfo)cis.nextElement();
- if (ci.updateVisibility()) {
- visChanged = true;
- }
- ci.clearCachedSize();
- }
- }
- // Step 2: Make sure components are bound to ComponentInfos
- if (springsChanged) {
- registerComponents(horizontalGroup, HORIZONTAL);
- registerComponents(verticalGroup, VERTICAL);
- }
- // Step 3: Adjust the autopadding. This removes existing
- // autopadding, then recalculates where it should go.
- if (springsChanged || visChanged) {
- checkComponents();
- horizontalGroup.removeAutopadding();
- verticalGroup.removeAutopadding();
- if (getAutocreateGaps()) {
- insertAutopadding(true);
- } else if (hasPreferredPaddingSprings ||
- getAutocreateContainerGaps()) {
- insertAutopadding(false);
- }
- springsChanged = false;
- }
- // Step 4: (for min/pref/max size calculations only) calculate the
- // autopadding. This invokes for unsetting the calculated values, then
- // recalculating them.
- // If sizeType == SPECIFIC_SIZE, it indicates we're doing layout, this
- // step will be done later on.
- if (sizeType != SPECIFIC_SIZE && (getAutocreateGaps() ||
- getAutocreateContainerGaps() || hasPreferredPaddingSprings)) {
- calculateAutopadding(horizontalGroup, HORIZONTAL, sizeType, 0, 0);
- calculateAutopadding(verticalGroup, VERTICAL, sizeType, 0, 0);
- }
- }
- private void calculateAutopadding(Group group, int axis, int sizeType,
- int origin, int size) {
- group.unsetAutopadding();
- switch(sizeType) {
- case MIN_SIZE:
- size = group.getMinimumSize(axis);
- break;
- case PREF_SIZE:
- size = group.getPreferredSize(axis);
- break;
- case MAX_SIZE:
- size = group.getMaximumSize(axis);
- break;
- }
- group.setSize(axis, origin, size);
- group.calculateAutopadding(axis);
- }
- private void checkComponents() {
- Enumeration infos = componentInfos.elements();
- while (infos.hasMoreElements()) {
- ComponentInfo info = (ComponentInfo)infos.nextElement();
- if (info.horizontalSpring == null) {
- throw new IllegalStateException(info.component +
- " is not attached to a horizontal group");
- }
- if (info.verticalSpring == null) {
- throw new IllegalStateException(info.component +
- " is not attached to a vertical group");
- }
- }
- }
- private void registerComponents(Group group, int axis) {
- Vector springs = group.springs;
- for (int counter = springs.size() - 1; counter >= 0; counter--) {
- Spring spring = (Spring)springs.elementAt(counter);
- if (spring instanceof ComponentSpring) {
- ((ComponentSpring)spring).installIfNecessary(axis);
- } else if (spring instanceof Group) {
- registerComponents((Group)spring, axis);
- }
- }
- }
- private Dimension adjustSize(int width, int height) {
- int insetLeft = host.getStyle().getMargin(false, Component.LEFT);
- int insetTop = host.getStyle().getMargin(false, Component.TOP);
- int insetRight = host.getStyle().getMargin(false, Component.RIGHT);
- int insetBottom = host.getStyle().getMargin(false, Component.BOTTOM);
- return new Dimension(width + insetLeft + insetRight,
- height + insetTop + insetBottom);
- }
- private void checkParent(Container parent) {
- if (parent != host) {
- throw new IllegalArgumentException(
- "GroupLayout can only be used with one Container at a time");
- }
- }
- /**
- * Returns the <code>ComponentInfo</code> for the specified Component.
- */
- private ComponentInfo getComponentInfo(Component component) {
- ComponentInfo info = (ComponentInfo)componentInfos.get(component);
- if (info == null) {
- info = new ComponentInfo(component);
- componentInfos.put(component, info);
- if (component.getParent() != host) {
- host.addComponent(component);
- }
- }
- return info;
- }
- /**
- * Adjusts the autopadding springs for the horizontal and vertical
- * groups. If <code>insert</code> is true this will insert auto padding
- * springs, otherwise this will only adjust the springs that
- * comprise auto preferred padding springs.
- */
- private void insertAutopadding(boolean insert) {
- horizontalGroup.insertAutopadding(HORIZONTAL, new Vector(1),
- new Vector(1), new Vector(1), new Vector(1), insert);
- verticalGroup.insertAutopadding(VERTICAL, new Vector(1),
- new Vector(1), new Vector(1), new Vector(1), insert);
- }
- /**
- * Returns true if the two Components have a common ParallelGroup ancestor
- * along the particular axis.
- */
- private boolean areParallelSiblings(Component source, Component target,
- int axis) {
- ComponentInfo sourceInfo = getComponentInfo(source);
- ComponentInfo targetInfo = getComponentInfo(target);
- Spring sourceSpring;
- Spring targetSpring;
- if (axis == HORIZONTAL) {
- sourceSpring = sourceInfo.horizontalSpring;
- targetSpring = targetInfo.horizontalSpring;
- } else {
- sourceSpring = sourceInfo.verticalSpring;
- targetSpring = targetInfo.verticalSpring;
- }
- Vector sourcePath = tmpParallelSet;
- sourcePath.removeAllElements();
- Spring spring = sourceSpring.getParent();
- while (spring != null) {
- sourcePath.addElement(spring);
- spring = spring.getParent();
- }
- spring = targetSpring.getParent();
- while (spring != null) {
- if (sourcePath.contains(spring)) {
- sourcePath.removeAllElements();
- while (spring != null) {
- if (spring instanceof ParallelGroup) {
- return true;
- }
- spring = spring.getParent();
- }
- return false;
- }
- spring = spring.getParent();
- }
- sourcePath.removeAllElements();
- return false;
- }
- private boolean isLeftToRight() {
- // Need bidi support...
- return true;
- //return host.getComponentOrientation().isLeftToRight();
- }
- /**
- * Spring consists of a range: min, pref and max a value some where in
- * the middle of that and a location. Subclasses must override
- * methods to get the min/max/pref and will likely want to override
- * the <code>setSize</code> method. Spring automatically caches the
- * min/max/pref. If the min/pref/max has internally changes, or needs
- * to be updated you must invoked clear.
- */
- abstract class Spring {
- private int size;
- private int min;
- private int max;
- private int pref;
- private Spring parent;
- private int alignment;
- Spring() {
- min = pref = max = UNSET;
- }
- /**
- * Calculates and returns the minimum size.
- *
- * @param axis the axis of layout; one of HORIZONTAL or VERTICAL
- * @return the minimum size
- */
- abstract int calculateMinimumSize(int axis);
- /**
- * Calculates and returns the preferred size.
- *
- * @param axis the axis of layout; one of HORIZONTAL or VERTICAL
- * @return the preferred size
- */
- abstract int calculatePreferredSize(int axis);
- /**
- * Calculates and returns the minimum size.
- *
- * @param axis the axis of layout; one of HORIZONTAL or VERTICAL
- * @return the minimum size
- */
- abstract int calculateMaximumSize(int axis);
- /**
- * Sets the parent of this Spring.
- */
- void setParent(Spring parent) {
- this.parent = parent;
- }
- /**
- * Returns the parent of this spring.
- */
- Spring getParent() {
- return parent;
- }
- // This is here purely as a conveniance for ParallelGroup to avoid
- // having to track alignment separately.
- void setAlignment(int alignment) {
- this.alignment = alignment;
- }
- int getAlignment() {
- return alignment;
- }
- /**
- * Returns the minimum size.
- */
- final int getMinimumSize(int axis) {
- if (min == UNSET) {
- min = constrain(calculateMinimumSize(axis));
- }
- return min;
- }
- /**
- * Returns the preferred size.
- */
- final int getPreferredSize(int axis) {
- if (pref == UNSET) {
- pref = constrain(calculatePreferredSize(axis));
- }
- return pref;
- }
- /**
- * Returns the maximum size.
- */
- final int getMaximumSize(int axis) {
- if (max == UNSET) {
- max = constrain(calculateMaximumSize(axis));
- }
- return max;
- }
- /**
- * Resets the cached min/max/pref.
- */
- void unset() {
- size = min = pref = max = UNSET;
- }
- /**
- * Sets the value and location of the spring. Subclasses
- * will want to invoke super, then do any additional sizing.
- *
- * @param axis HORIZONTAL or VERTICAL
- * @param origin of this Spring
- * @param size of the Spring. If size is UNSET, this invokes
- * clear.
- */
- void setSize(int axis, int origin, int size) {
- this.size = size;
- if (size == UNSET) {
- unset();
- }
- }
- /**
- * Returns the current size.
- */
- int getSize() {
- return size;
- }
- int constrain(int value) {
- return Math.min(value, Short.MAX_VALUE);
- }
- int getBaseline() {
- return -1;
- }
- int getBaselineResizeBehavior() {
- return Component.BRB_OTHER;
- }
- final boolean isResizable(int axis) {
- int min = getMinimumSize(axis);
- int pref = getPreferredSize(axis);
- return (min != pref || pref != getMaximumSize(axis));
- }
- /**
- * Returns true if this Spring will ALWAYS have a zero size. This should
- * NOT check the current size, rather it's meant to
- * quickly test if this Spring will always have a zero size.
- */
- abstract boolean willHaveZeroSize(boolean treatAutopaddingAsZeroSized);
- }
- /**
- * Simple copy constructor for vector
- */
- private static Vector create(Vector v) {
- int size = v.size();
- Vector vec = new Vector(size);
- for(int iter = 0 ; iter < size ; iter++) {
- vec.addElement(v.elementAt(iter));
- }
- return vec;
- }
- /**
- * Adds all vector elements from source to dest
- */
- private static void addAll(Vector dest, Vector source) {
- int size = source.size();
- for(int iter = 0 ; iter < size ; iter++) {
- dest.addElement(source.elementAt(iter));
- }
- }
- /**
- * Group provides for commonality between the two types of operations
- * supported by <code>GroupLayout</code>: laying out components one
- * after another (<code>SequentialGroup</code>) or layout on top
- * of each other (<code>ParallelGroup</code>). Use one of
- * <code>createSequentialGroup</code> or
- * <code>createParallelGroup</code> to create one.
- */
- public abstract class Group extends Spring {
- // private int origin;
- // private int size;
- Vector springs;
- Group() {
- springs = new Vector();
- }
- int indexOf(Spring spring) {
- return springs.indexOf(spring);
- }
- /**
- * Adds the Spring to the list of <code>Spring</code>s and returns
- * the receiver.
- */
- Group addSpring(Spring spring) {
- springs.addElement(spring);
- spring.setParent(this);
- if (!(spring instanceof AutopaddingSpring) ||
- !((AutopaddingSpring)spring).getUserCreated()) {
- springsChanged = true;
- }
- return this;
- }
- //
- // Spring methods
- //
- void setSize(int axis, int origin, int size) {
- super.setSize(axis, origin, size);
- if (size == UNSET) {
- for (int counter = springs.size() - 1; counter >= 0;
- counter--) {
- getSpring(counter).setSize(axis, origin, size);
- }
- } else {
- setValidSize(axis, origin, size);
- }
- }
- /**
- * This is invoked from <code>setSize</code> if passed a value
- * other than UNSET.
- */
- abstract void setValidSize(int axis, int origin, int size);
- int calculateMinimumSize(int axis) {
- return calculateSize(axis, MIN_SIZE);
- }
- int calculatePreferredSize(int axis) {
- return calculateSize(axis, PREF_SIZE);
- }
- int calculateMaximumSize(int axis) {
- return calculateSize(axis, MAX_SIZE);
- }
- /**
- * Used to compute how the two values representing two springs
- * will be combined. For example, a group that layed things out
- * one after the next would return <code>a + b</code>.
- */
- abstract int operator(int a, int b);
- /**
- * Calculates the specified size. This is called from
- * one of the <code>getMinimumSize0</code>,
- * <code>getPreferredSize0</code> or
- * <code>getMaximumSize0</code> methods. This will invoke
- * to <code>operator</code> to combine the values.
- */
- int calculateSize(int axis, int type) {
- int count = springs.size();
- if (count == 0) {
- return 0;
- }
- if (count == 1) {
- return getSpringSize(getSpring(0), axis, type);
- }
- int size = constrain(operator(getSpringSize(getSpring(0), axis, type),
- getSpringSize(getSpring(1), axis, type)));
- for (int counter = 2; counter < count; counter++) {
- size = constrain(operator(size, getSpringSize(getSpring(counter),
- axis, type)));
- }
- return size;
- }
- Spring getSpring(int index) {
- return (Spring)springs.elementAt(index);
- }
- int getSpringSize(Spring spring, int axis, int type) {
- switch(type) {
- case MIN_SIZE:
- return spring.getMinimumSize(axis);
- case PREF_SIZE:
- return spring.getPreferredSize(axis);
- case MAX_SIZE:
- return spring.getMaximumSize(axis);
- }
- //assert false;
- return 0;
- }
- // Padding
- /**
- * Adjusts the autopadding springs in this group and its children.
- * If <code>insert</code> is true this will insert auto padding
- * springs, otherwise this will only adjust the springs that
- * comprise auto preferred padding springs.
- *
- * @param axis the axis of the springs; HORIZONTAL or VERTICAL
- * @param leadingPadding List of AutopaddingSprings that occur before
- * this Group
- * @param trailingPadding any trailing autopadding springs are added
- * to this on exit
- * @param leading List of ComponentSprings that occur before this Group
- * @param trailing any trailing ComponentSpring are added to this
- * List
- * @param insert Whether or not to insert AutopaddingSprings or just
- * adjust any existing AutopaddingSprings.
- */
- abstract void insertAutopadding(int axis, Vector leadingPadding,
- Vector trailingPadding, Vector leading, Vector trailing,
- boolean insert);
- /**
- * Removes any AutopaddingSprings.
- */
- void removeAutopadding() {
- unset();
- for (int counter = springs.size() - 1; counter >= 0; counter--) {
- Spring spring = (Spring)springs.elementAt(counter);
- if (spring instanceof AutopaddingSpring) {
- if (((AutopaddingSpring)spring).getUserCreated()) {
- ((AutopaddingSpring)spring).reset();
- } else {
- springs.removeElementAt(counter);
- }
- } else if (spring instanceof Group) {
- ((Group)spring).removeAutopadding();
- }
- }
- }
- void unsetAutopadding() {
- // Clear cached pref/min/max.
- unset();
- for (int counter = springs.size() - 1; counter >= 0; counter--) {
- Spring spring = (Spring)springs.elementAt(counter);
- if (spring instanceof AutopaddingSpring) {
- ((AutopaddingSpring)spring).unset();
- } else if (spring instanceof Group) {
- ((Group)spring).unsetAutopadding();
- }
- }
- }
- void calculateAutopadding(int axis) {
- for (int counter = springs.size() - 1; counter >= 0; counter--) {
- Spring spring = (Spring)springs.elementAt(counter);
- if (spring instanceof AutopaddingSpring) {
- // Force size to be reset.
- spring.unset();
- ((AutopaddingSpring)spring).calculatePadding(axis);
- } else if (spring instanceof Group) {
- ((Group)spring).calculateAutopadding(axis);
- }
- }
- // Clear cached pref/min/max.
- unset();
- }
- boolean willHaveZeroSize(boolean treatAutopaddingAsZeroSized) {
- for (int i = springs.size() -1; i >= 0; i--) {
- Spring spring = (Spring)springs.elementAt(i);
- if (!spring.willHaveZeroSize(treatAutopaddingAsZeroSized)) {
- return false;
- }
- }
- return true;
- }
- }
- /**
- * A <code>Group</code> that lays out its elements sequentially, one
- * after another. This class has no public constructor, use the
- * <code>createSequentialGroup</code> method to create one.
- *
- * @see #createSequentialGroup()
- */
- public class SequentialGroup extends Group {
- private Spring baselineSpring;
- SequentialGroup() {
- }
- /**
- * Adds the specified <code>Group</code> to this
- * <code>SequentialGroup</code>
- *
- * @param group the Group to add
- * @return this Group
- */
- public SequentialGroup add(Group group) {
- return (SequentialGroup)addSpring(group);
- }
- /**
- * Adds a <code>Group</code> to this <code>Group</code>.
- *
- * @param group the <code>Group</code> to add
- * @param useAsBaseline whether the specified <code>Group</code> should
- * be used to calculate the baseline for this <code>Group</code>
- * @return this <code>Group</code>
- */
- public SequentialGroup add(boolean useAsBaseline, Group group) {
- add(group);
- if (useAsBaseline) {
- baselineSpring = group;
- }
- return this;
- }
- /**
- * Adds the specified Component. If the Component's min/max
- * are different from its pref than the component will be resizable.
- *
- * @param component the Component to add
- * @return this <code>SequentialGroup</code>
- */
- public SequentialGroup add(Component component) {
- return add(component, DEFAULT_SIZE, DEFAULT_SIZE, DEFAULT_SIZE);
- }
- /**
- * Adds a <code>Component</code> to this <code>Group</code>.
- *
- * @param useAsBaseline whether the specified <code>Component</code> should
- * be used to calculate the baseline for this <code>Group</code>
- * @param component the <code>Component</code> to add
- * @return this <code>Group</code>
- */
- public SequentialGroup add(boolean useAsBaseline, Component component) {
- add(component);
- if (useAsBaseline) {
- baselineSpring = getSpring(springs.size() - 1);
- }
- return this;
- }
- /**
- * Adds the specified <code>Component</code>. Min, pref and max
- * can be absolute values, or they can be one of
- * <code>DEFAULT_SIZE</code> or <code>PREFERRED_SIZE</code>. For
- * example, the following:
- * <pre>
- * add(component, PREFERRED_SIZE, PREFERRED_SIZE, 1000);
- * </pre>
- * Forces a max of 1000, with the min and preferred equalling that
- * of the preferred size of <code>component</code>.
- *
- * @param component the Component to add
- * @param min the minimum size
- * @param pref the preferred size
- * @param max the maximum size
- * @throws IllegalArgumentException if min, pref or max are
- * not positive and not one of PREFERRED_SIZE or DEFAULT_SIZE
- * @return this <code>SequentialGroup</code>
- */
- public SequentialGroup add(Component component, int min, int pref,
- int max) {
- return (SequentialGroup)addSpring(new ComponentSpring(
- component, min, pref, max));
- }
- /**
- * Adds a <code>Component</code> to this <code>Group</code>
- * with the specified size.
- *
- * @param useAsBaseline whether the specified <code>Component</code> should
- * be used to calculate the baseline for this <code>Group</code>
- * @param component the <code>Component</code> to add
- * @param min the minimum size or one of <code>DEFAULT_SIZE</code> or
- * <code>PREFERRED_SIZE</code>
- * @param pref the preferred size or one of <code>DEFAULT_SIZE</code> or
- * <code>PREFERRED_SIZE</code>
- * @param max the maximum size or one of <code>DEFAULT_SIZE</code> or
- * <code>PREFERRED_SIZE</code>
- * @return this <code>Group</code>
- */
- public SequentialGroup add(boolean useAsBaseline,
- Component component, int min, int pref, int max) {
- add(component, min, pref, max);
- if (useAsBaseline) {
- baselineSpring = getSpring(springs.size() - 1);
- }
- return this;
- }
- /**
- * Adds a rigid gap.
- *
- * @param pref the size of the gap
- * @throws IllegalArgumentException if min < 0 or pref < 0 or max < 0
- * or the following is not meant min <= pref <= max
- * @return this <code>SequentialGroup</code>
- */
- public SequentialGroup add(int pref) {
- return add(pref, pref, pref);
- }
- /**
- * Adds a gap with the specified size.
- *
- * @param min the minimum size of the gap, or PREFERRED_SIZE
- * @param pref the preferred size of the gap
- * @param max the maximum size of the gap, or PREFERRED_SIZE
- * @throws IllegalArgumentException if min < 0 or pref < 0 or max < 0
- * or the following is not meant min <= pref <= max
- * @return this <code>SequentialGroup</code>
- */
- public SequentialGroup add(int min, int pref, int max) {
- return (SequentialGroup)addSpring(new GapSpring(min, pref, max));
- }
- /**
- * Adds an element representing the preferred gap between the two
- * components.
- *
- * @param comp1 the first component
- * @param comp2 the second component
- * @param type the type of gap; one of the constants defined by
- * LayoutStyle
- * @return this <code>SequentialGroup</code>
- * @throws IllegalArgumentException if <code>type</code> is not a
- * valid LayoutStyle constant
- * @see LayoutStyle
- */
- public SequentialGroup addPreferredGap(Component comp1,
- Component comp2,
- int type) {
- return addPreferredGap(comp1, comp2, type, false);
- }
- /**
- * Adds an element representing the preferred gap between the two
- * components.
- *
- * @param comp1 the first component
- * @param comp2 the second component
- * @param type the type of gap; one of the constants defined by
- * LayoutStyle
- * @param canGrow true if the gap can grow if more
- * space is available
- * @return this <code>SequentialGroup</code>
- * @throws IllegalArgumentException if <code>type</code> is not a
- * valid LayoutStyle constant
- * @see LayoutStyle
- */
- public SequentialGroup addPreferredGap(Component comp1,
- Component comp2,
- int type, boolean canGrow) {
- if (type != LayoutStyle.RELATED &&
- type != LayoutStyle.UNRELATED &&
- type != LayoutStyle.INDENT) {
- throw new IllegalArgumentException("Invalid type argument");
- }
- if (comp1 == null || comp2 == null) {
- throw new IllegalArgumentException(
- "Components must be non-null");
- }
- return (SequentialGroup)addSpring(new PaddingSpring(
- comp1, comp2, type, canGrow));
- }
- /**
- * Adds an element representing the preferred gap between the
- * nearest components. That is, during layout the neighboring
- * components are found, and the min, pref and max of this
- * element is set based on the preferred gap between the
- * components. If no neighboring components are found the
- * min, pref and max are set to 0.
- *
- * @param type the type of gap; one of the LayoutStyle constants
- * @return this SequentialGroup
- * @throws IllegalArgumentException if type is not one of
- * <code>LayoutStyle.RELATED</code> or
- * <code>LayoutStyle.UNRELATED</code>
- * @see LayoutStyle
- */
- public SequentialGroup addPreferredGap(int type) {
- return addPreferredGap(type, DEFAULT_SIZE, DEFAULT_SIZE);
- }
- /**
- * Adds an element for the preferred gap between the
- * nearest components. That is, during layout the neighboring
- * components are found, and the min of this
- * element is set based on the preferred gap between the
- * components. If no neighboring components are found the
- * min is set to 0. This method allows you to specify the
- * preferred and maximum size by way of the <code>pref</code>
- * and <code>max</code> arguments. These can either be a
- * value >= 0, in which case the preferred or max is the max
- * of the argument and the preferred gap, of DEFAULT_VALUE in
- * which case the value is the same as the preferred gap.
- *
- * @param type the type of gap; one of LayoutStyle.RELATED or
- * LayoutStyle.UNRELATED
- * @param pref the preferred size; one of DEFAULT_SIZE or a value > 0
- * @param max the maximum size; one of DEFAULT_SIZE, PREFERRED_SIZE
- * or a value > 0
- * @return this SequentialGroup
- * @throws IllegalArgumentException if type is not one of
- * <code>LayoutStyle.RELATED</code> or
- * <code>LayoutStyle.UNRELATED</code> or pref/max is
- * != DEFAULT_SIZE and < 0, or pref > max
- * @see LayoutStyle
- */
- public SequentialGroup addPreferredGap(int type, int pref,
- int max) {
- if (type != LayoutStyle.RELATED && type != LayoutStyle.UNRELATED) {
- throw new IllegalArgumentException(
- "Padding type must be one of Padding.RELATED or Padding.UNRELATED");
- }
- if ((pref < 0 && pref != DEFAULT_SIZE && pref != PREFERRED_SIZE) ||
- (max < 0 && max != DEFAULT_SIZE && max != PREFERRED_SIZE)||
- (pref >= 0 && max >= 0 && pref > max)) {