0N/A/*
2362N/A * Copyright (c) 1995, 2006, 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 java.awt;
0N/A
0N/Aimport java.util.Hashtable;
0N/Aimport java.util.Arrays;
0N/A
0N/A/**
0N/A * The <code>GridBagLayout</code> class is a flexible layout
0N/A * manager that aligns components vertically, horizontally or along their
0N/A * baseline without requiring that the components be of the same size.
0N/A * Each <code>GridBagLayout</code> object maintains a dynamic,
0N/A * rectangular grid of cells, with each component occupying
0N/A * one or more cells, called its <em>display area</em>.
0N/A * <p>
0N/A * Each component managed by a <code>GridBagLayout</code> is associated with
0N/A * an instance of {@link GridBagConstraints}. The constraints object
0N/A * specifies where a component's display area should be located on the grid
0N/A * and how the component should be positioned within its display area. In
0N/A * addition to its constraints object, the <code>GridBagLayout</code> also
0N/A * considers each component's minimum and preferred sizes in order to
0N/A * determine a component's size.
0N/A * <p>
0N/A * The overall orientation of the grid depends on the container's
0N/A * {@link ComponentOrientation} property. For horizontal left-to-right
0N/A * orientations, grid coordinate (0,0) is in the upper left corner of the
0N/A * container with x increasing to the right and y increasing downward. For
0N/A * horizontal right-to-left orientations, grid coordinate (0,0) is in the upper
0N/A * right corner of the container with x increasing to the left and y
0N/A * increasing downward.
0N/A * <p>
0N/A * To use a grid bag layout effectively, you must customize one or more
0N/A * of the <code>GridBagConstraints</code> objects that are associated
0N/A * with its components. You customize a <code>GridBagConstraints</code>
0N/A * object by setting one or more of its instance variables:
0N/A * <p>
0N/A * <dl>
0N/A * <dt>{@link GridBagConstraints#gridx},
0N/A * {@link GridBagConstraints#gridy}
0N/A * <dd>Specifies the cell containing the leading corner of the component's
0N/A * display area, where the cell at the origin of the grid has address
0N/A * <code>gridx&nbsp;=&nbsp;0</code>,
0N/A * <code>gridy&nbsp;=&nbsp;0</code>. For horizontal left-to-right layout,
0N/A * a component's leading corner is its upper left. For horizontal
0N/A * right-to-left layout, a component's leading corner is its upper right.
0N/A * Use <code>GridBagConstraints.RELATIVE</code> (the default value)
0N/A * to specify that the component be placed immediately following
0N/A * (along the x axis for <code>gridx</code> or the y axis for
0N/A * <code>gridy</code>) the component that was added to the container
0N/A * just before this component was added.
0N/A * <dt>{@link GridBagConstraints#gridwidth},
0N/A * {@link GridBagConstraints#gridheight}
0N/A * <dd>Specifies the number of cells in a row (for <code>gridwidth</code>)
0N/A * or column (for <code>gridheight</code>)
0N/A * in the component's display area.
0N/A * The default value is 1.
0N/A * Use <code>GridBagConstraints.REMAINDER</code> to specify
0N/A * that the component's display area will be from <code>gridx</code>
0N/A * to the last cell in the row (for <code>gridwidth</code>)
0N/A * or from <code>gridy</code> to the last cell in the column
0N/A * (for <code>gridheight</code>).
0N/A *
0N/A * Use <code>GridBagConstraints.RELATIVE</code> to specify
0N/A * that the component's display area will be from <code>gridx</code>
0N/A * to the next to the last cell in its row (for <code>gridwidth</code>
0N/A * or from <code>gridy</code> to the next to the last cell in its
0N/A * column (for <code>gridheight</code>).
0N/A *
0N/A * <dt>{@link GridBagConstraints#fill}
0N/A * <dd>Used when the component's display area
0N/A * is larger than the component's requested size
0N/A * to determine whether (and how) to resize the component.
0N/A * Possible values are
0N/A * <code>GridBagConstraints.NONE</code> (the default),
0N/A * <code>GridBagConstraints.HORIZONTAL</code>
0N/A * (make the component wide enough to fill its display area
0N/A * horizontally, but don't change its height),
0N/A * <code>GridBagConstraints.VERTICAL</code>
0N/A * (make the component tall enough to fill its display area
0N/A * vertically, but don't change its width), and
0N/A * <code>GridBagConstraints.BOTH</code>
0N/A * (make the component fill its display area entirely).
0N/A * <dt>{@link GridBagConstraints#ipadx},
0N/A * {@link GridBagConstraints#ipady}
0N/A * <dd>Specifies the component's internal padding within the layout,
0N/A * how much to add to the minimum size of the component.
0N/A * The width of the component will be at least its minimum width
0N/A * plus <code>ipadx</code> pixels. Similarly, the height of
0N/A * the component will be at least the minimum height plus
0N/A * <code>ipady</code> pixels.
0N/A * <dt>{@link GridBagConstraints#insets}
0N/A * <dd>Specifies the component's external padding, the minimum
0N/A * amount of space between the component and the edges of its display area.
0N/A * <dt>{@link GridBagConstraints#anchor}
0N/A * <dd>Specifies where the component should be positioned in its display area.
0N/A * There are three kinds of possible values: absolute, orientation-relative,
0N/A * and baseline-relative
0N/A * Orientation relative values are interpreted relative to the container's
0N/A * <code>ComponentOrientation</code> property while absolute values
0N/A * are not. Baseline relative values are calculated relative to the
0N/A * baseline. Valid values are:</dd>
0N/A * <p>
0N/A * <center><table BORDER=0 COLS=3 WIDTH=800
0N/A * SUMMARY="absolute, relative and baseline values as described above">
0N/A * <tr>
0N/A * <th><P ALIGN="LEFT">Absolute Values</th>
0N/A * <th><P ALIGN="LEFT">Orientation Relative Values</th>
0N/A * <th><P ALIGN="LEFT">Baseline Relative Values</th>
0N/A * </tr>
0N/A * <tr>
0N/A * <td>
0N/A * <li><code>GridBagConstraints.NORTH</code></li>
0N/A * <li><code>GridBagConstraints.SOUTH</code></li>
0N/A * <li><code>GridBagConstraints.WEST</code></li>
0N/A * <li><code>GridBagConstraints.EAST</code></li>
0N/A * <li><code>GridBagConstraints.NORTHWEST</code></li>
0N/A * <li><code>GridBagConstraints.NORTHEAST</code></li>
0N/A * <li><code>GridBagConstraints.SOUTHWEST</code></li>
0N/A * <li><code>GridBagConstraints.SOUTHEAST</code></li>
0N/A * <li><code>GridBagConstraints.CENTER</code> (the default)</li>
0N/A * </td>
0N/A * <td>
0N/A * <li><code>GridBagConstraints.PAGE_START</code></li>
0N/A * <li><code>GridBagConstraints.PAGE_END</code></li>
0N/A * <li><code>GridBagConstraints.LINE_START</code></li>
0N/A * <li><code>GridBagConstraints.LINE_END</code></li>
0N/A * <li><code>GridBagConstraints.FIRST_LINE_START</code></li>
0N/A * <li><code>GridBagConstraints.FIRST_LINE_END</code></li>
0N/A * <li><code>GridBagConstraints.LAST_LINE_START</code></li>
0N/A * <li><code>GridBagConstraints.LAST_LINE_END</code></li>
0N/A * </td>
0N/A * <td>
0N/A * <li><code>GridBagConstraints.BASELINE</code></li>
0N/A * <li><code>GridBagConstraints.BASELINE_LEADING</code></li>
0N/A * <li><code>GridBagConstraints.BASELINE_TRAILING</code></li>
0N/A * <li><code>GridBagConstraints.ABOVE_BASELINE</code></li>
0N/A * <li><code>GridBagConstraints.ABOVE_BASELINE_LEADING</code></li>
0N/A * <li><code>GridBagConstraints.ABOVE_BASELINE_TRAILING</code></li>
0N/A * <li><code>GridBagConstraints.BELOW_BASELINE</code></li>
0N/A * <li><code>GridBagConstraints.BELOW_BASELINE_LEADING</code></li>
0N/A * <li><code>GridBagConstraints.BELOW_BASELINE_TRAILING</code></li>
0N/A * </td>
0N/A * </tr>
0N/A * </table></center><p>
0N/A * <dt>{@link GridBagConstraints#weightx},
0N/A * {@link GridBagConstraints#weighty}
0N/A * <dd>Used to determine how to distribute space, which is
0N/A * important for specifying resizing behavior.
0N/A * Unless you specify a weight for at least one component
0N/A * in a row (<code>weightx</code>) and column (<code>weighty</code>),
0N/A * all the components clump together in the center of their container.
0N/A * This is because when the weight is zero (the default),
0N/A * the <code>GridBagLayout</code> object puts any extra space
0N/A * between its grid of cells and the edges of the container.
0N/A * </dl>
0N/A * <p>
0N/A * Each row may have a baseline; the baseline is determined by the
0N/A * components in that row that have a valid baseline and are aligned
0N/A * along the baseline (the component's anchor value is one of {@code
0N/A * BASELINE}, {@code BASELINE_LEADING} or {@code BASELINE_TRAILING}).
0N/A * If none of the components in the row has a valid baseline, the row
0N/A * does not have a baseline.
0N/A * <p>
0N/A * If a component spans rows it is aligned either to the baseline of
0N/A * the start row (if the baseline-resize behavior is {@code
0N/A * CONSTANT_ASCENT}) or the end row (if the baseline-resize behavior
0N/A * is {@code CONSTANT_DESCENT}). The row that the component is
0N/A * aligned to is called the <em>prevailing row</em>.
0N/A * <p>
0N/A * The following figure shows a baseline layout and includes a
0N/A * component that spans rows:
0N/A * <center><table summary="Baseline Layout">
0N/A * <tr ALIGN=CENTER>
0N/A * <td>
0N/A * <img src="doc-files/GridBagLayout-baseline.png"
0N/A * alt="The following text describes this graphic (Figure 1)." ALIGN=center>
0N/A * </td>
0N/A * </table></center>
0N/A * This layout consists of three components:
0N/A * <ul><li>A panel that starts in row 0 and ends in row 1. The panel
0N/A * has a baseline-resize behavior of <code>CONSTANT_DESCENT</code> and has
0N/A * an anchor of <code>BASELINE</code>. As the baseline-resize behavior
0N/A * is <code>CONSTANT_DESCENT</code> the prevailing row for the panel is
0N/A * row 1.
0N/A * <li>Two buttons, each with a baseline-resize behavior of
0N/A * <code>CENTER_OFFSET</code> and an anchor of <code>BASELINE</code>.
0N/A * </ul>
0N/A * Because the second button and the panel share the same prevailing row,
0N/A * they are both aligned along their baseline.
0N/A * <p>
0N/A * Components positioned using one of the baseline-relative values resize
0N/A * differently than when positioned using an absolute or orientation-relative
0N/A * value. How components change is dictated by how the baseline of the
0N/A * prevailing row changes. The baseline is anchored to the
0N/A * bottom of the display area if any components with the same prevailing row
0N/A * have a baseline-resize behavior of <code>CONSTANT_DESCENT</code>,
0N/A * otherwise the baseline is anchored to the top of the display area.
0N/A * The following rules dictate the resize behavior:
0N/A * <ul>
0N/A * <li>Resizable components positioned above the baseline can only
0N/A * grow as tall as the baseline. For example, if the baseline is at 100
0N/A * and anchored at the top, a resizable component positioned above the
0N/A * baseline can never grow more than 100 units.
0N/A * <li>Similarly, resizable components positioned below the baseline can
0N/A * only grow as high as the difference between the display height and the
0N/A * baseline.
0N/A * <li>Resizable components positioned on the baseline with a
0N/A * baseline-resize behavior of <code>OTHER</code> are only resized if
0N/A * the baseline at the resized size fits within the display area. If
0N/A * the baseline is such that it does not fit within the display area
0N/A * the component is not resized.
0N/A * <li>Components positioned on the baseline that do not have a
0N/A * baseline-resize behavior of <code>OTHER</code>
0N/A * can only grow as tall as {@code display height - baseline + baseline of component}.
0N/A * </ul>
0N/A * If you position a component along the baseline, but the
0N/A * component does not have a valid baseline, it will be vertically centered
0N/A * in its space. Similarly if you have positioned a component relative
0N/A * to the baseline and none of the components in the row have a valid
0N/A * baseline the component is vertically centered.
0N/A * <p>
0N/A * The following figures show ten components (all buttons)
0N/A * managed by a grid bag layout. Figure 2 shows the layout for a horizontal,
0N/A * left-to-right container and Figure 3 shows the layout for a horizontal,
0N/A * right-to-left container.
0N/A * <p>
0N/A * <center><table COLS=2 WIDTH=600 summary="layout">
0N/A * <tr ALIGN=CENTER>
0N/A * <td>
0N/A * <img src="doc-files/GridBagLayout-1.gif" alt="The preceeding text describes this graphic (Figure 1)." ALIGN=center HSPACE=10 VSPACE=7>
0N/A * </td>
0N/A * <td>
0N/A * <img src="doc-files/GridBagLayout-2.gif" alt="The preceeding text describes this graphic (Figure 2)." ALIGN=center HSPACE=10 VSPACE=7>
0N/A * </td>
0N/A * <tr ALIGN=CENTER>
0N/A * <td>Figure 2: Horizontal, Left-to-Right</td>
0N/A * <td>Figure 3: Horizontal, Right-to-Left</td>
0N/A * </tr>
0N/A * </table></center>
0N/A * <p>
0N/A * Each of the ten components has the <code>fill</code> field
0N/A * of its associated <code>GridBagConstraints</code> object
0N/A * set to <code>GridBagConstraints.BOTH</code>.
0N/A * In addition, the components have the following non-default constraints:
0N/A * <p>
0N/A * <ul>
0N/A * <li>Button1, Button2, Button3: <code>weightx&nbsp;=&nbsp;1.0</code>
0N/A * <li>Button4: <code>weightx&nbsp;=&nbsp;1.0</code>,
0N/A * <code>gridwidth&nbsp;=&nbsp;GridBagConstraints.REMAINDER</code>
0N/A * <li>Button5: <code>gridwidth&nbsp;=&nbsp;GridBagConstraints.REMAINDER</code>
0N/A * <li>Button6: <code>gridwidth&nbsp;=&nbsp;GridBagConstraints.RELATIVE</code>
0N/A * <li>Button7: <code>gridwidth&nbsp;=&nbsp;GridBagConstraints.REMAINDER</code>
0N/A * <li>Button8: <code>gridheight&nbsp;=&nbsp;2</code>,
0N/A * <code>weighty&nbsp;=&nbsp;1.0</code>
0N/A * <li>Button9, Button 10:
0N/A * <code>gridwidth&nbsp;=&nbsp;GridBagConstraints.REMAINDER</code>
0N/A * </ul>
0N/A * <p>
0N/A * Here is the code that implements the example shown above:
0N/A * <p>
0N/A * <hr><blockquote><pre>
0N/A * import java.awt.*;
0N/A * import java.util.*;
0N/A * import java.applet.Applet;
0N/A *
0N/A * public class GridBagEx1 extends Applet {
0N/A *
0N/A * protected void makebutton(String name,
0N/A * GridBagLayout gridbag,
0N/A * GridBagConstraints c) {
0N/A * Button button = new Button(name);
0N/A * gridbag.setConstraints(button, c);
0N/A * add(button);
0N/A * }
0N/A *
0N/A * public void init() {
0N/A * GridBagLayout gridbag = new GridBagLayout();
0N/A * GridBagConstraints c = new GridBagConstraints();
0N/A *
0N/A * setFont(new Font("SansSerif", Font.PLAIN, 14));
0N/A * setLayout(gridbag);
0N/A *
0N/A * c.fill = GridBagConstraints.BOTH;
0N/A * c.weightx = 1.0;
0N/A * makebutton("Button1", gridbag, c);
0N/A * makebutton("Button2", gridbag, c);
0N/A * makebutton("Button3", gridbag, c);
0N/A *
0N/A * c.gridwidth = GridBagConstraints.REMAINDER; //end row
0N/A * makebutton("Button4", gridbag, c);
0N/A *
0N/A * c.weightx = 0.0; //reset to the default
0N/A * makebutton("Button5", gridbag, c); //another row
0N/A *
0N/A * c.gridwidth = GridBagConstraints.RELATIVE; //next-to-last in row
0N/A * makebutton("Button6", gridbag, c);
0N/A *
0N/A * c.gridwidth = GridBagConstraints.REMAINDER; //end row
0N/A * makebutton("Button7", gridbag, c);
0N/A *
0N/A * c.gridwidth = 1; //reset to the default
0N/A * c.gridheight = 2;
0N/A * c.weighty = 1.0;
0N/A * makebutton("Button8", gridbag, c);
0N/A *
0N/A * c.weighty = 0.0; //reset to the default
0N/A * c.gridwidth = GridBagConstraints.REMAINDER; //end row
0N/A * c.gridheight = 1; //reset to the default
0N/A * makebutton("Button9", gridbag, c);
0N/A * makebutton("Button10", gridbag, c);
0N/A *
0N/A * setSize(300, 100);
0N/A * }
0N/A *
0N/A * public static void main(String args[]) {
0N/A * Frame f = new Frame("GridBag Layout Example");
0N/A * GridBagEx1 ex1 = new GridBagEx1();
0N/A *
0N/A * ex1.init();
0N/A *
0N/A * f.add("Center", ex1);
0N/A * f.pack();
0N/A * f.setSize(f.getPreferredSize());
0N/A * f.show();
0N/A * }
0N/A * }
0N/A * </pre></blockquote><hr>
0N/A * <p>
0N/A * @author Doug Stein
0N/A * @author Bill Spitzak (orignial NeWS & OLIT implementation)
0N/A * @see java.awt.GridBagConstraints
0N/A * @see java.awt.GridBagLayoutInfo
0N/A * @see java.awt.ComponentOrientation
0N/A * @since JDK1.0
0N/A */
0N/Apublic class GridBagLayout implements LayoutManager2,
0N/Ajava.io.Serializable {
0N/A
0N/A static final int EMPIRICMULTIPLIER = 2;
0N/A /**
0N/A * This field is no longer used to reserve arrays and keeped for backward
0N/A * compatibility. Previously, this was
0N/A * the maximum number of grid positions (both horizontal and
0N/A * vertical) that could be laid out by the grid bag layout.
0N/A * Current implementation doesn't impose any limits
0N/A * on the size of a grid.
0N/A */
0N/A protected static final int MAXGRIDSIZE = 512;
0N/A
0N/A /**
0N/A * The smallest grid that can be laid out by the grid bag layout.
0N/A */
0N/A protected static final int MINSIZE = 1;
0N/A /**
0N/A * The preferred grid size that can be laid out by the grid bag layout.
0N/A */
0N/A protected static final int PREFERREDSIZE = 2;
0N/A
0N/A /**
0N/A * This hashtable maintains the association between
0N/A * a component and its gridbag constraints.
0N/A * The Keys in <code>comptable</code> are the components and the
0N/A * values are the instances of <code>GridBagConstraints</code>.
0N/A *
0N/A * @serial
0N/A * @see java.awt.GridBagConstraints
0N/A */
0N/A protected Hashtable<Component,GridBagConstraints> comptable;
0N/A
0N/A /**
0N/A * This field holds a gridbag constraints instance
0N/A * containing the default values, so if a component
0N/A * does not have gridbag constraints associated with
0N/A * it, then the component will be assigned a
0N/A * copy of the <code>defaultConstraints</code>.
0N/A *
0N/A * @serial
0N/A * @see #getConstraints(Component)
0N/A * @see #setConstraints(Component, GridBagConstraints)
0N/A * @see #lookupConstraints(Component)
0N/A */
0N/A protected GridBagConstraints defaultConstraints;
0N/A
0N/A /**
0N/A * This field holds the layout information
0N/A * for the gridbag. The information in this field
0N/A * is based on the most recent validation of the
0N/A * gridbag.
0N/A * If <code>layoutInfo</code> is <code>null</code>
0N/A * this indicates that there are no components in
0N/A * the gridbag or if there are components, they have
0N/A * not yet been validated.
0N/A *
0N/A * @serial
0N/A * @see #getLayoutInfo(Container, int)
0N/A */
0N/A protected GridBagLayoutInfo layoutInfo;
0N/A
0N/A /**
0N/A * This field holds the overrides to the column minimum
0N/A * width. If this field is non-<code>null</code> the values are
0N/A * applied to the gridbag after all of the minimum columns
0N/A * widths have been calculated.
0N/A * If columnWidths has more elements than the number of
0N/A * columns, columns are added to the gridbag to match
0N/A * the number of elements in columnWidth.
0N/A *
0N/A * @serial
0N/A * @see #getLayoutDimensions()
0N/A */
0N/A public int columnWidths[];
0N/A
0N/A /**
0N/A * This field holds the overrides to the row minimum
0N/A * heights. If this field is non-<code>null</code> the values are
0N/A * applied to the gridbag after all of the minimum row
0N/A * heights have been calculated.
0N/A * If <code>rowHeights</code> has more elements than the number of
0N/A * rows, rowa are added to the gridbag to match
0N/A * the number of elements in <code>rowHeights</code>.
0N/A *
0N/A * @serial
0N/A * @see #getLayoutDimensions()
0N/A */
0N/A public int rowHeights[];
0N/A
0N/A /**
0N/A * This field holds the overrides to the column weights.
0N/A * If this field is non-<code>null</code> the values are
0N/A * applied to the gridbag after all of the columns
0N/A * weights have been calculated.
0N/A * If <code>columnWeights[i]</code> &gt; weight for column i, then
0N/A * column i is assigned the weight in <code>columnWeights[i]</code>.
0N/A * If <code>columnWeights</code> has more elements than the number
0N/A * of columns, the excess elements are ignored - they do
0N/A * not cause more columns to be created.
0N/A *
0N/A * @serial
0N/A */
0N/A public double columnWeights[];
0N/A
0N/A /**
0N/A * This field holds the overrides to the row weights.
0N/A * If this field is non-<code>null</code> the values are
0N/A * applied to the gridbag after all of the rows
0N/A * weights have been calculated.
0N/A * If <code>rowWeights[i]</code> &gt; weight for row i, then
0N/A * row i is assigned the weight in <code>rowWeights[i]</code>.
0N/A * If <code>rowWeights</code> has more elements than the number
0N/A * of rows, the excess elements are ignored - they do
0N/A * not cause more rows to be created.
0N/A *
0N/A * @serial
0N/A */
0N/A public double rowWeights[];
0N/A
0N/A /**
0N/A * The component being positioned. This is set before calling into
0N/A * <code>adjustForGravity</code>.
0N/A */
0N/A private Component componentAdjusting;
0N/A
0N/A /**
0N/A * Creates a grid bag layout manager.
0N/A */
0N/A public GridBagLayout () {
0N/A comptable = new Hashtable<Component,GridBagConstraints>();
0N/A defaultConstraints = new GridBagConstraints();
0N/A }
0N/A
0N/A /**
0N/A * Sets the constraints for the specified component in this layout.
0N/A * @param comp the component to be modified
0N/A * @param constraints the constraints to be applied
0N/A */
0N/A public void setConstraints(Component comp, GridBagConstraints constraints) {
0N/A comptable.put(comp, (GridBagConstraints)constraints.clone());
0N/A }
0N/A
0N/A /**
0N/A * Gets the constraints for the specified component. A copy of
0N/A * the actual <code>GridBagConstraints</code> object is returned.
0N/A * @param comp the component to be queried
0N/A * @return the constraint for the specified component in this
0N/A * grid bag layout; a copy of the actual constraint
0N/A * object is returned
0N/A */
0N/A public GridBagConstraints getConstraints(Component comp) {
0N/A GridBagConstraints constraints = comptable.get(comp);
0N/A if (constraints == null) {
0N/A setConstraints(comp, defaultConstraints);
0N/A constraints = comptable.get(comp);
0N/A }
0N/A return (GridBagConstraints)constraints.clone();
0N/A }
0N/A
0N/A /**
0N/A * Retrieves the constraints for the specified component.
0N/A * The return value is not a copy, but is the actual
0N/A * <code>GridBagConstraints</code> object used by the layout mechanism.
0N/A * <p>
0N/A * If <code>comp</code> is not in the <code>GridBagLayout</code>,
0N/A * a set of default <code>GridBagConstraints</code> are returned.
0N/A * A <code>comp</code> value of <code>null</code> is invalid
0N/A * and returns <code>null</code>.
0N/A *
0N/A * @param comp the component to be queried
0N/A * @return the contraints for the specified component
0N/A */
0N/A protected GridBagConstraints lookupConstraints(Component comp) {
0N/A GridBagConstraints constraints = comptable.get(comp);
0N/A if (constraints == null) {
0N/A setConstraints(comp, defaultConstraints);
0N/A constraints = comptable.get(comp);
0N/A }
0N/A return constraints;
0N/A }
0N/A
0N/A /**
0N/A * Removes the constraints for the specified component in this layout
0N/A * @param comp the component to be modified
0N/A */
0N/A private void removeConstraints(Component comp) {
0N/A comptable.remove(comp);
0N/A }
0N/A
0N/A /**
0N/A * Determines the origin of the layout area, in the graphics coordinate
0N/A * space of the target container. This value represents the pixel
0N/A * coordinates of the top-left corner of the layout area regardless of
0N/A * the <code>ComponentOrientation</code> value of the container. This
0N/A * is distinct from the grid origin given by the cell coordinates (0,0).
0N/A * Most applications do not call this method directly.
0N/A * @return the graphics origin of the cell in the top-left
0N/A * corner of the layout grid
0N/A * @see java.awt.ComponentOrientation
0N/A * @since JDK1.1
0N/A */
0N/A public Point getLayoutOrigin () {
0N/A Point origin = new Point(0,0);
0N/A if (layoutInfo != null) {
0N/A origin.x = layoutInfo.startx;
0N/A origin.y = layoutInfo.starty;
0N/A }
0N/A return origin;
0N/A }
0N/A
0N/A /**
0N/A * Determines column widths and row heights for the layout grid.
0N/A * <p>
0N/A * Most applications do not call this method directly.
0N/A * @return an array of two arrays, containing the widths
0N/A * of the layout columns and
0N/A * the heights of the layout rows
0N/A * @since JDK1.1
0N/A */
0N/A public int [][] getLayoutDimensions () {
0N/A if (layoutInfo == null)
0N/A return new int[2][0];
0N/A
0N/A int dim[][] = new int [2][];
0N/A dim[0] = new int[layoutInfo.width];
0N/A dim[1] = new int[layoutInfo.height];
0N/A
0N/A System.arraycopy(layoutInfo.minWidth, 0, dim[0], 0, layoutInfo.width);
0N/A System.arraycopy(layoutInfo.minHeight, 0, dim[1], 0, layoutInfo.height);
0N/A
0N/A return dim;
0N/A }
0N/A
0N/A /**
0N/A * Determines the weights of the layout grid's columns and rows.
0N/A * Weights are used to calculate how much a given column or row
0N/A * stretches beyond its preferred size, if the layout has extra
0N/A * room to fill.
0N/A * <p>
0N/A * Most applications do not call this method directly.
0N/A * @return an array of two arrays, representing the
0N/A * horizontal weights of the layout columns
0N/A * and the vertical weights of the layout rows
0N/A * @since JDK1.1
0N/A */
0N/A public double [][] getLayoutWeights () {
0N/A if (layoutInfo == null)
0N/A return new double[2][0];
0N/A
0N/A double weights[][] = new double [2][];
0N/A weights[0] = new double[layoutInfo.width];
0N/A weights[1] = new double[layoutInfo.height];
0N/A
0N/A System.arraycopy(layoutInfo.weightX, 0, weights[0], 0, layoutInfo.width);
0N/A System.arraycopy(layoutInfo.weightY, 0, weights[1], 0, layoutInfo.height);
0N/A
0N/A return weights;
0N/A }
0N/A
0N/A /**
0N/A * Determines which cell in the layout grid contains the point
0N/A * specified by <code>(x,&nbsp;y)</code>. Each cell is identified
0N/A * by its column index (ranging from 0 to the number of columns
0N/A * minus 1) and its row index (ranging from 0 to the number of
0N/A * rows minus 1).
0N/A * <p>
0N/A * If the <code>(x,&nbsp;y)</code> point lies
0N/A * outside the grid, the following rules are used.
0N/A * The column index is returned as zero if <code>x</code> lies to the
0N/A * left of the layout for a left-to-right container or to the right of
0N/A * the layout for a right-to-left container. The column index is returned
0N/A * as the number of columns if <code>x</code> lies
0N/A * to the right of the layout in a left-to-right container or to the left
0N/A * in a right-to-left container.
0N/A * The row index is returned as zero if <code>y</code> lies above the
0N/A * layout, and as the number of rows if <code>y</code> lies
0N/A * below the layout. The orientation of a container is determined by its
0N/A * <code>ComponentOrientation</code> property.
0N/A * @param x the <i>x</i> coordinate of a point
0N/A * @param y the <i>y</i> coordinate of a point
0N/A * @return an ordered pair of indexes that indicate which cell
0N/A * in the layout grid contains the point
0N/A * (<i>x</i>,&nbsp;<i>y</i>).
0N/A * @see java.awt.ComponentOrientation
0N/A * @since JDK1.1
0N/A */
0N/A public Point location(int x, int y) {
0N/A Point loc = new Point(0,0);
0N/A int i, d;
0N/A
0N/A if (layoutInfo == null)
0N/A return loc;
0N/A
0N/A d = layoutInfo.startx;
0N/A if (!rightToLeft) {
0N/A for (i=0; i<layoutInfo.width; i++) {
0N/A d += layoutInfo.minWidth[i];
0N/A if (d > x)
0N/A break;
0N/A }
0N/A } else {
0N/A for (i=layoutInfo.width-1; i>=0; i--) {
0N/A if (d > x)
0N/A break;
0N/A d += layoutInfo.minWidth[i];
0N/A }
0N/A i++;
0N/A }
0N/A loc.x = i;
0N/A
0N/A d = layoutInfo.starty;
0N/A for (i=0; i<layoutInfo.height; i++) {
0N/A d += layoutInfo.minHeight[i];
0N/A if (d > y)
0N/A break;
0N/A }
0N/A loc.y = i;
0N/A
0N/A return loc;
0N/A }
0N/A
0N/A /**
0N/A * Has no effect, since this layout manager does not use a per-component string.
0N/A */
0N/A public void addLayoutComponent(String name, Component comp) {
0N/A }
0N/A
0N/A /**
0N/A * Adds the specified component to the layout, using the specified
0N/A * <code>constraints</code> object. Note that constraints
0N/A * are mutable and are, therefore, cloned when cached.
0N/A *
0N/A * @param comp the component to be added
0N/A * @param constraints an object that determines how
0N/A * the component is added to the layout
0N/A * @exception IllegalArgumentException if <code>constraints</code>
0N/A * is not a <code>GridBagConstraint</code>
0N/A */
0N/A public void addLayoutComponent(Component comp, Object constraints) {
0N/A if (constraints instanceof GridBagConstraints) {
0N/A setConstraints(comp, (GridBagConstraints)constraints);
0N/A } else if (constraints != null) {
0N/A throw new IllegalArgumentException("cannot add to layout: constraints must be a GridBagConstraint");
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Removes the specified component from this layout.
0N/A * <p>
0N/A * Most applications do not call this method directly.
0N/A * @param comp the component to be removed.
0N/A * @see java.awt.Container#remove(java.awt.Component)
0N/A * @see java.awt.Container#removeAll()
0N/A */
0N/A public void removeLayoutComponent(Component comp) {
0N/A removeConstraints(comp);
0N/A }
0N/A
0N/A /**
0N/A * Determines the preferred size of the <code>parent</code>
0N/A * container using this grid bag layout.
0N/A * <p>
0N/A * Most applications do not call this method directly.
0N/A *
0N/A * @param parent the container in which to do the layout
0N/A * @see java.awt.Container#getPreferredSize
0N/A * @return the preferred size of the <code>parent</code>
0N/A * container
0N/A */
0N/A public Dimension preferredLayoutSize(Container parent) {
0N/A GridBagLayoutInfo info = getLayoutInfo(parent, PREFERREDSIZE);
0N/A return getMinSize(parent, info);
0N/A }
0N/A
0N/A /**
0N/A * Determines the minimum size of the <code>parent</code> container
0N/A * using this grid bag layout.
0N/A * <p>
0N/A * Most applications do not call this method directly.
0N/A * @param parent the container in which to do the layout
0N/A * @see java.awt.Container#doLayout
0N/A * @return the minimum size of the <code>parent</code> container
0N/A */
0N/A public Dimension minimumLayoutSize(Container parent) {
0N/A GridBagLayoutInfo info = getLayoutInfo(parent, MINSIZE);
0N/A return getMinSize(parent, info);
0N/A }
0N/A
0N/A /**
0N/A * Returns the maximum dimensions for this layout given the components
0N/A * in the specified target container.
0N/A * @param target the container which needs to be laid out
0N/A * @see Container
0N/A * @see #minimumLayoutSize(Container)
0N/A * @see #preferredLayoutSize(Container)
0N/A * @return the maximum dimensions for this layout
0N/A */
0N/A public Dimension maximumLayoutSize(Container target) {
0N/A return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
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 * <p>
0N/A * @return the value <code>0.5f</code> to indicate centered
0N/A */
0N/A public float getLayoutAlignmentX(Container parent) {
0N/A return 0.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 * <p>
0N/A * @return the value <code>0.5f</code> to indicate centered
0N/A */
0N/A public float getLayoutAlignmentY(Container parent) {
0N/A return 0.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 public void invalidateLayout(Container target) {
0N/A }
0N/A
0N/A /**
0N/A * Lays out the specified container using this grid bag layout.
0N/A * This method reshapes components in the specified container in
0N/A * order to satisfy the contraints of this <code>GridBagLayout</code>
0N/A * object.
0N/A * <p>
0N/A * Most applications do not call this method directly.
0N/A * @param parent the container in which to do the layout
0N/A * @see java.awt.Container
0N/A * @see java.awt.Container#doLayout
0N/A */
0N/A public void layoutContainer(Container parent) {
0N/A arrangeGrid(parent);
0N/A }
0N/A
0N/A /**
0N/A * Returns a string representation of this grid bag layout's values.
0N/A * @return a string representation of this grid bag layout.
0N/A */
0N/A public String toString() {
0N/A return getClass().getName();
0N/A }
0N/A
0N/A /**
0N/A * Print the layout information. Useful for debugging.
0N/A */
0N/A
0N/A /* DEBUG
0N/A *
0N/A * protected void dumpLayoutInfo(GridBagLayoutInfo s) {
0N/A * int x;
0N/A *
0N/A * System.out.println("Col\tWidth\tWeight");
0N/A * for (x=0; x<s.width; x++) {
0N/A * System.out.println(x + "\t" +
0N/A * s.minWidth[x] + "\t" +
0N/A * s.weightX[x]);
0N/A * }
0N/A * System.out.println("Row\tHeight\tWeight");
0N/A * for (x=0; x<s.height; x++) {
0N/A * System.out.println(x + "\t" +
0N/A * s.minHeight[x] + "\t" +
0N/A * s.weightY[x]);
0N/A * }
0N/A * }
0N/A */
0N/A
0N/A /**
0N/A * Print the layout constraints. Useful for debugging.
0N/A */
0N/A
0N/A /* DEBUG
0N/A *
0N/A * protected void dumpConstraints(GridBagConstraints constraints) {
0N/A * System.out.println(
0N/A * "wt " +
0N/A * constraints.weightx +
0N/A * " " +
0N/A * constraints.weighty +
0N/A * ", " +
0N/A *
0N/A * "box " +
0N/A * constraints.gridx +
0N/A * " " +
0N/A * constraints.gridy +
0N/A * " " +
0N/A * constraints.gridwidth +
0N/A * " " +
0N/A * constraints.gridheight +
0N/A * ", " +
0N/A *
0N/A * "min " +
0N/A * constraints.minWidth +
0N/A * " " +
0N/A * constraints.minHeight +
0N/A * ", " +
0N/A *
0N/A * "pad " +
0N/A * constraints.insets.bottom +
0N/A * " " +
0N/A * constraints.insets.left +
0N/A * " " +
0N/A * constraints.insets.right +
0N/A * " " +
0N/A * constraints.insets.top +
0N/A * " " +
0N/A * constraints.ipadx +
0N/A * " " +
0N/A * constraints.ipady);
0N/A * }
0N/A */
0N/A
0N/A /**
0N/A * Fills in an instance of <code>GridBagLayoutInfo</code> for the
0N/A * current set of managed children. This requires three passes through the
0N/A * set of children:
0N/A *
0N/A * <ol>
0N/A * <li>Figure out the dimensions of the layout grid.
0N/A * <li>Determine which cells the components occupy.
0N/A * <li>Distribute the weights and min sizes amoung the rows/columns.
0N/A * </ol>
0N/A *
0N/A * This also caches the minsizes for all the children when they are
0N/A * first encountered (so subsequent loops don't need to ask again).
0N/A * <p>
0N/A * This method should only be used internally by
0N/A * <code>GridBagLayout</code>.
0N/A *
0N/A * @param parent the layout container
0N/A * @param sizeflag either <code>PREFERREDSIZE</code> or
0N/A * <code>MINSIZE</code>
0N/A * @return the <code>GridBagLayoutInfo</code> for the set of children
0N/A * @since 1.4
0N/A */
0N/A protected GridBagLayoutInfo getLayoutInfo(Container parent, int sizeflag) {
0N/A return GetLayoutInfo(parent, sizeflag);
0N/A }
0N/A
0N/A /*
0N/A * Calculate maximum array sizes to allocate arrays without ensureCapacity
0N/A * we may use preCalculated sizes in whole class because of upper estimation of
0N/A * maximumArrayXIndex and maximumArrayYIndex.
0N/A */
0N/A
0N/A private long[] preInitMaximumArraySizes(Container parent){
0N/A Component components[] = parent.getComponents();
0N/A Component comp;
0N/A GridBagConstraints constraints;
0N/A int curX, curY;
0N/A int curWidth, curHeight;
0N/A int preMaximumArrayXIndex = 0;
0N/A int preMaximumArrayYIndex = 0;
0N/A long [] returnArray = new long[2];
0N/A
0N/A for (int compId = 0 ; compId < components.length ; compId++) {
0N/A comp = components[compId];
0N/A if (!comp.isVisible()) {
0N/A continue;
0N/A }
0N/A
0N/A constraints = lookupConstraints(comp);
0N/A curX = constraints.gridx;
0N/A curY = constraints.gridy;
0N/A curWidth = constraints.gridwidth;
0N/A curHeight = constraints.gridheight;
0N/A
0N/A // -1==RELATIVE, means that column|row equals to previously added component,
0N/A // since each next Component with gridx|gridy == RELATIVE starts from
0N/A // previous position, so we should start from previous component which
0N/A // already used in maximumArray[X|Y]Index calculation. We could just increase
0N/A // maximum by 1 to handle situation when component with gridx=-1 was added.
0N/A if (curX < 0){
0N/A curX = ++preMaximumArrayYIndex;
0N/A }
0N/A if (curY < 0){
0N/A curY = ++preMaximumArrayXIndex;
0N/A }
0N/A // gridwidth|gridheight may be equal to RELATIVE (-1) or REMAINDER (0)
0N/A // in any case using 1 instead of 0 or -1 should be sufficient to for
0N/A // correct maximumArraySizes calculation
0N/A if (curWidth <= 0){
0N/A curWidth = 1;
0N/A }
0N/A if (curHeight <= 0){
0N/A curHeight = 1;
0N/A }
0N/A
0N/A preMaximumArrayXIndex = Math.max(curY + curHeight, preMaximumArrayXIndex);
0N/A preMaximumArrayYIndex = Math.max(curX + curWidth, preMaximumArrayYIndex);
0N/A } //for (components) loop
0N/A // Must specify index++ to allocate well-working arrays.
0N/A /* fix for 4623196.
0N/A * now return long array instead of Point
0N/A */
0N/A returnArray[0] = preMaximumArrayXIndex;
0N/A returnArray[1] = preMaximumArrayYIndex;
0N/A return returnArray;
0N/A } //PreInitMaximumSizes
0N/A
0N/A /**
0N/A * This method is obsolete and supplied for backwards
0N/A * compatability only; new code should call {@link
0N/A * #getLayoutInfo(java.awt.Container, int) getLayoutInfo} instead.
0N/A * This method is the same as <code>getLayoutInfo</code>;
0N/A * refer to <code>getLayoutInfo</code> for details on parameters
0N/A * and return value.
0N/A */
0N/A protected GridBagLayoutInfo GetLayoutInfo(Container parent, int sizeflag) {
0N/A synchronized (parent.getTreeLock()) {
0N/A GridBagLayoutInfo r;
0N/A Component comp;
0N/A GridBagConstraints constraints;
0N/A Dimension d;
0N/A Component components[] = parent.getComponents();
0N/A // Code below will address index curX+curWidth in the case of yMaxArray, weightY
0N/A // ( respectively curY+curHeight for xMaxArray, weightX ) where
0N/A // curX in 0 to preInitMaximumArraySizes.y
0N/A // Thus, the maximum index that could
0N/A // be calculated in the following code is curX+curX.
0N/A // EmpericMultier equals 2 because of this.
0N/A
0N/A int layoutWidth, layoutHeight;
0N/A int []xMaxArray;
0N/A int []yMaxArray;
0N/A int compindex, i, k, px, py, pixels_diff, nextSize;
0N/A int curX = 0; // constraints.gridx
0N/A int curY = 0; // constraints.gridy
0N/A int curWidth = 1; // constraints.gridwidth
0N/A int curHeight = 1; // constraints.gridheight
0N/A int curRow, curCol;
0N/A double weight_diff, weight;
0N/A int maximumArrayXIndex = 0;
0N/A int maximumArrayYIndex = 0;
0N/A int anchor;
0N/A
0N/A /*
0N/A * Pass #1
0N/A *
0N/A * Figure out the dimensions of the layout grid (use a value of 1 for
0N/A * zero or negative widths and heights).
0N/A */
0N/A
0N/A layoutWidth = layoutHeight = 0;
0N/A curRow = curCol = -1;
0N/A long [] arraySizes = preInitMaximumArraySizes(parent);
0N/A
0N/A /* fix for 4623196.
0N/A * If user try to create a very big grid we can
0N/A * get NegativeArraySizeException because of integer value
0N/A * overflow (EMPIRICMULTIPLIER*gridSize might be more then Integer.MAX_VALUE).
0N/A * We need to detect this situation and try to create a
0N/A * grid with Integer.MAX_VALUE size instead.
0N/A */
0N/A maximumArrayXIndex = (EMPIRICMULTIPLIER * arraySizes[0] > Integer.MAX_VALUE )? Integer.MAX_VALUE : EMPIRICMULTIPLIER*(int)arraySizes[0];
0N/A maximumArrayYIndex = (EMPIRICMULTIPLIER * arraySizes[1] > Integer.MAX_VALUE )? Integer.MAX_VALUE : EMPIRICMULTIPLIER*(int)arraySizes[1];
0N/A
0N/A if (rowHeights != null){
0N/A maximumArrayXIndex = Math.max(maximumArrayXIndex, rowHeights.length);
0N/A }
0N/A if (columnWidths != null){
0N/A maximumArrayYIndex = Math.max(maximumArrayYIndex, columnWidths.length);
0N/A }
0N/A
0N/A xMaxArray = new int[maximumArrayXIndex];
0N/A yMaxArray = new int[maximumArrayYIndex];
0N/A
0N/A boolean hasBaseline = false;
0N/A for (compindex = 0 ; compindex < components.length ; compindex++) {
0N/A comp = components[compindex];
0N/A if (!comp.isVisible())
0N/A continue;
0N/A constraints = lookupConstraints(comp);
0N/A
0N/A curX = constraints.gridx;
0N/A curY = constraints.gridy;
0N/A curWidth = constraints.gridwidth;
0N/A if (curWidth <= 0)
0N/A curWidth = 1;
0N/A curHeight = constraints.gridheight;
0N/A if (curHeight <= 0)
0N/A curHeight = 1;
0N/A
0N/A /* If x or y is negative, then use relative positioning: */
0N/A if (curX < 0 && curY < 0) {
0N/A if (curRow >= 0)
0N/A curY = curRow;
0N/A else if (curCol >= 0)
0N/A curX = curCol;
0N/A else
0N/A curY = 0;
0N/A }
0N/A if (curX < 0) {
0N/A px = 0;
0N/A for (i = curY; i < (curY + curHeight); i++) {
0N/A px = Math.max(px, xMaxArray[i]);
0N/A }
0N/A
0N/A curX = px - curX - 1;
0N/A if(curX < 0)
0N/A curX = 0;
0N/A }
0N/A else if (curY < 0) {
0N/A py = 0;
0N/A for (i = curX; i < (curX + curWidth); i++) {
0N/A py = Math.max(py, yMaxArray[i]);
0N/A }
0N/A curY = py - curY - 1;
0N/A if(curY < 0)
0N/A curY = 0;
0N/A }
0N/A
0N/A /* Adjust the grid width and height
0N/A * fix for 5005945: unneccessary loops removed
0N/A */
0N/A px = curX + curWidth;
0N/A if (layoutWidth < px) {
0N/A layoutWidth = px;
0N/A }
0N/A py = curY + curHeight;
0N/A if (layoutHeight < py) {
0N/A layoutHeight = py;
0N/A }
0N/A
0N/A /* Adjust xMaxArray and yMaxArray */
0N/A for (i = curX; i < (curX + curWidth); i++) {
0N/A yMaxArray[i] =py;
0N/A }
0N/A for (i = curY; i < (curY + curHeight); i++) {
0N/A xMaxArray[i] = px;
0N/A }
0N/A
0N/A
0N/A /* Cache the current slave's size. */
0N/A if (sizeflag == PREFERREDSIZE)
0N/A d = comp.getPreferredSize();
0N/A else
0N/A d = comp.getMinimumSize();
0N/A constraints.minWidth = d.width;
0N/A constraints.minHeight = d.height;
0N/A if (calculateBaseline(comp, constraints, d)) {
0N/A hasBaseline = true;
0N/A }
0N/A
0N/A /* Zero width and height must mean that this is the last item (or
0N/A * else something is wrong). */
0N/A if (constraints.gridheight == 0 && constraints.gridwidth == 0)
0N/A curRow = curCol = -1;
0N/A
0N/A /* Zero width starts a new row */
0N/A if (constraints.gridheight == 0 && curRow < 0)
0N/A curCol = curX + curWidth;
0N/A
0N/A /* Zero height starts a new column */
0N/A else if (constraints.gridwidth == 0 && curCol < 0)
0N/A curRow = curY + curHeight;
0N/A } //for (components) loop
0N/A
0N/A
0N/A /*
0N/A * Apply minimum row/column dimensions
0N/A */
0N/A if (columnWidths != null && layoutWidth < columnWidths.length)
0N/A layoutWidth = columnWidths.length;
0N/A if (rowHeights != null && layoutHeight < rowHeights.length)
0N/A layoutHeight = rowHeights.length;
0N/A
0N/A r = new GridBagLayoutInfo(layoutWidth, layoutHeight);
0N/A
0N/A /*
0N/A * Pass #2
0N/A *
0N/A * Negative values for gridX are filled in with the current x value.
0N/A * Negative values for gridY are filled in with the current y value.
0N/A * Negative or zero values for gridWidth and gridHeight end the current
0N/A * row or column, respectively.
0N/A */
0N/A
0N/A curRow = curCol = -1;
0N/A
0N/A Arrays.fill(xMaxArray, 0);
0N/A Arrays.fill(yMaxArray, 0);
0N/A
0N/A int[] maxAscent = null;
0N/A int[] maxDescent = null;
0N/A short[] baselineType = null;
0N/A
0N/A if (hasBaseline) {
0N/A r.maxAscent = maxAscent = new int[layoutHeight];
0N/A r.maxDescent = maxDescent = new int[layoutHeight];
0N/A r.baselineType = baselineType = new short[layoutHeight];
0N/A r.hasBaseline = true;
0N/A }
0N/A
0N/A
0N/A for (compindex = 0 ; compindex < components.length ; compindex++) {
0N/A comp = components[compindex];
0N/A if (!comp.isVisible())
0N/A continue;
0N/A constraints = lookupConstraints(comp);
0N/A
0N/A curX = constraints.gridx;
0N/A curY = constraints.gridy;
0N/A curWidth = constraints.gridwidth;
0N/A curHeight = constraints.gridheight;
0N/A
0N/A /* If x or y is negative, then use relative positioning: */
0N/A if (curX < 0 && curY < 0) {
0N/A if(curRow >= 0)
0N/A curY = curRow;
0N/A else if(curCol >= 0)
0N/A curX = curCol;
0N/A else
0N/A curY = 0;
0N/A }
0N/A
0N/A if (curX < 0) {
0N/A if (curHeight <= 0) {
0N/A curHeight += r.height - curY;
0N/A if (curHeight < 1)
0N/A curHeight = 1;
0N/A }
0N/A
0N/A px = 0;
0N/A for (i = curY; i < (curY + curHeight); i++)
0N/A px = Math.max(px, xMaxArray[i]);
0N/A
0N/A curX = px - curX - 1;
0N/A if(curX < 0)
0N/A curX = 0;
0N/A }
0N/A else if (curY < 0) {
0N/A if (curWidth <= 0) {
0N/A curWidth += r.width - curX;
0N/A if (curWidth < 1)
0N/A curWidth = 1;
0N/A }
0N/A
0N/A py = 0;
0N/A for (i = curX; i < (curX + curWidth); i++){
0N/A py = Math.max(py, yMaxArray[i]);
0N/A }
0N/A
0N/A curY = py - curY - 1;
0N/A if(curY < 0)
0N/A curY = 0;
0N/A }
0N/A
0N/A if (curWidth <= 0) {
0N/A curWidth += r.width - curX;
0N/A if (curWidth < 1)
0N/A curWidth = 1;
0N/A }
0N/A
0N/A if (curHeight <= 0) {
0N/A curHeight += r.height - curY;
0N/A if (curHeight < 1)
0N/A curHeight = 1;
0N/A }
0N/A
0N/A px = curX + curWidth;
0N/A py = curY + curHeight;
0N/A
0N/A for (i = curX; i < (curX + curWidth); i++) { yMaxArray[i] = py; }
0N/A for (i = curY; i < (curY + curHeight); i++) { xMaxArray[i] = px; }
0N/A
0N/A /* Make negative sizes start a new row/column */
0N/A if (constraints.gridheight == 0 && constraints.gridwidth == 0)
0N/A curRow = curCol = -1;
0N/A if (constraints.gridheight == 0 && curRow < 0)
0N/A curCol = curX + curWidth;
0N/A else if (constraints.gridwidth == 0 && curCol < 0)
0N/A curRow = curY + curHeight;
0N/A
0N/A /* Assign the new values to the gridbag slave */
0N/A constraints.tempX = curX;
0N/A constraints.tempY = curY;
0N/A constraints.tempWidth = curWidth;
0N/A constraints.tempHeight = curHeight;
0N/A
0N/A anchor = constraints.anchor;
0N/A if (hasBaseline) {
0N/A switch(anchor) {
0N/A case GridBagConstraints.BASELINE:
0N/A case GridBagConstraints.BASELINE_LEADING:
0N/A case GridBagConstraints.BASELINE_TRAILING:
0N/A if (constraints.ascent >= 0) {
0N/A if (curHeight == 1) {
0N/A maxAscent[curY] =
0N/A Math.max(maxAscent[curY],
0N/A constraints.ascent);
0N/A maxDescent[curY] =
0N/A Math.max(maxDescent[curY],
0N/A constraints.descent);
0N/A }
0N/A else {
0N/A if (constraints.baselineResizeBehavior ==
0N/A Component.BaselineResizeBehavior.
0N/A CONSTANT_DESCENT) {
0N/A maxDescent[curY + curHeight - 1] =
0N/A Math.max(maxDescent[curY + curHeight
0N/A - 1],
0N/A constraints.descent);
0N/A }
0N/A else {
0N/A maxAscent[curY] = Math.max(maxAscent[curY],
0N/A constraints.ascent);
0N/A }
0N/A }
0N/A if (constraints.baselineResizeBehavior ==
0N/A Component.BaselineResizeBehavior.CONSTANT_DESCENT) {
0N/A baselineType[curY + curHeight - 1] |=
0N/A (1 << constraints.
0N/A baselineResizeBehavior.ordinal());
0N/A }
0N/A else {
0N/A baselineType[curY] |= (1 << constraints.
0N/A baselineResizeBehavior.ordinal());
0N/A }
0N/A }
0N/A break;
0N/A case GridBagConstraints.ABOVE_BASELINE:
0N/A case GridBagConstraints.ABOVE_BASELINE_LEADING:
0N/A case GridBagConstraints.ABOVE_BASELINE_TRAILING:
0N/A // Component positioned above the baseline.
0N/A // To make the bottom edge of the component aligned
0N/A // with the baseline the bottom inset is
0N/A // added to the descent, the rest to the ascent.
0N/A pixels_diff = constraints.minHeight +
0N/A constraints.insets.top +
0N/A constraints.ipady;
0N/A maxAscent[curY] = Math.max(maxAscent[curY],
0N/A pixels_diff);
0N/A maxDescent[curY] = Math.max(maxDescent[curY],
0N/A constraints.insets.bottom);
0N/A break;
0N/A case GridBagConstraints.BELOW_BASELINE:
0N/A case GridBagConstraints.BELOW_BASELINE_LEADING:
0N/A case GridBagConstraints.BELOW_BASELINE_TRAILING:
0N/A // Component positioned below the baseline.
0N/A // To make the top edge of the component aligned
0N/A // with the baseline the top inset is
0N/A // added to the ascent, the rest to the descent.
0N/A pixels_diff = constraints.minHeight +
0N/A constraints.insets.bottom + constraints.ipady;
0N/A maxDescent[curY] = Math.max(maxDescent[curY],
0N/A pixels_diff);
0N/A maxAscent[curY] = Math.max(maxAscent[curY],
0N/A constraints.insets.top);
0N/A break;
0N/A }
0N/A }
0N/A }
0N/A
0N/A r.weightX = new double[maximumArrayYIndex];
0N/A r.weightY = new double[maximumArrayXIndex];
0N/A r.minWidth = new int[maximumArrayYIndex];
0N/A r.minHeight = new int[maximumArrayXIndex];
0N/A
0N/A
0N/A /*
0N/A * Apply minimum row/column dimensions and weights
0N/A */
0N/A if (columnWidths != null)
0N/A System.arraycopy(columnWidths, 0, r.minWidth, 0, columnWidths.length);
0N/A if (rowHeights != null)
0N/A System.arraycopy(rowHeights, 0, r.minHeight, 0, rowHeights.length);
0N/A if (columnWeights != null)
0N/A System.arraycopy(columnWeights, 0, r.weightX, 0, Math.min(r.weightX.length, columnWeights.length));
0N/A if (rowWeights != null)
0N/A System.arraycopy(rowWeights, 0, r.weightY, 0, Math.min(r.weightY.length, rowWeights.length));
0N/A
0N/A /*
0N/A * Pass #3
0N/A *
0N/A * Distribute the minimun widths and weights:
0N/A */
0N/A
0N/A nextSize = Integer.MAX_VALUE;
0N/A
0N/A for (i = 1;
0N/A i != Integer.MAX_VALUE;
0N/A i = nextSize, nextSize = Integer.MAX_VALUE) {
0N/A for (compindex = 0 ; compindex < components.length ; compindex++) {
0N/A comp = components[compindex];
0N/A if (!comp.isVisible())
0N/A continue;
0N/A constraints = lookupConstraints(comp);
0N/A
0N/A if (constraints.tempWidth == i) {
0N/A px = constraints.tempX + constraints.tempWidth; /* right column */
0N/A
0N/A /*
0N/A * Figure out if we should use this slave\'s weight. If the weight
0N/A * is less than the total weight spanned by the width of the cell,
0N/A * then discard the weight. Otherwise split the difference
0N/A * according to the existing weights.
0N/A */
0N/A
0N/A weight_diff = constraints.weightx;
0N/A for (k = constraints.tempX; k < px; k++)
0N/A weight_diff -= r.weightX[k];
0N/A if (weight_diff > 0.0) {
0N/A weight = 0.0;
0N/A for (k = constraints.tempX; k < px; k++)
0N/A weight += r.weightX[k];
0N/A for (k = constraints.tempX; weight > 0.0 && k < px; k++) {
0N/A double wt = r.weightX[k];
0N/A double dx = (wt * weight_diff) / weight;
0N/A r.weightX[k] += dx;
0N/A weight_diff -= dx;
0N/A weight -= wt;
0N/A }
0N/A /* Assign the remainder to the rightmost cell */
0N/A r.weightX[px-1] += weight_diff;
0N/A }
0N/A
0N/A /*
0N/A * Calculate the minWidth array values.
0N/A * First, figure out how wide the current slave needs to be.
0N/A * Then, see if it will fit within the current minWidth values.
0N/A * If it will not fit, add the difference according to the
0N/A * weightX array.
0N/A */
0N/A
0N/A pixels_diff =
0N/A constraints.minWidth + constraints.ipadx +
0N/A constraints.insets.left + constraints.insets.right;
0N/A
0N/A for (k = constraints.tempX; k < px; k++)
0N/A pixels_diff -= r.minWidth[k];
0N/A if (pixels_diff > 0) {
0N/A weight = 0.0;
0N/A for (k = constraints.tempX; k < px; k++)
0N/A weight += r.weightX[k];
0N/A for (k = constraints.tempX; weight > 0.0 && k < px; k++) {
0N/A double wt = r.weightX[k];
0N/A int dx = (int)((wt * ((double)pixels_diff)) / weight);
0N/A r.minWidth[k] += dx;
0N/A pixels_diff -= dx;
0N/A weight -= wt;
0N/A }
0N/A /* Any leftovers go into the rightmost cell */
0N/A r.minWidth[px-1] += pixels_diff;
0N/A }
0N/A }
0N/A else if (constraints.tempWidth > i && constraints.tempWidth < nextSize)
0N/A nextSize = constraints.tempWidth;
0N/A
0N/A
0N/A if (constraints.tempHeight == i) {
0N/A py = constraints.tempY + constraints.tempHeight; /* bottom row */
0N/A
0N/A /*
0N/A * Figure out if we should use this slave's weight. If the weight
0N/A * is less than the total weight spanned by the height of the cell,
0N/A * then discard the weight. Otherwise split it the difference
0N/A * according to the existing weights.
0N/A */
0N/A
0N/A weight_diff = constraints.weighty;
0N/A for (k = constraints.tempY; k < py; k++)
0N/A weight_diff -= r.weightY[k];
0N/A if (weight_diff > 0.0) {
0N/A weight = 0.0;
0N/A for (k = constraints.tempY; k < py; k++)
0N/A weight += r.weightY[k];
0N/A for (k = constraints.tempY; weight > 0.0 && k < py; k++) {
0N/A double wt = r.weightY[k];
0N/A double dy = (wt * weight_diff) / weight;
0N/A r.weightY[k] += dy;
0N/A weight_diff -= dy;
0N/A weight -= wt;
0N/A }
0N/A /* Assign the remainder to the bottom cell */
0N/A r.weightY[py-1] += weight_diff;
0N/A }
0N/A
0N/A /*
0N/A * Calculate the minHeight array values.
0N/A * First, figure out how tall the current slave needs to be.
0N/A * Then, see if it will fit within the current minHeight values.
0N/A * If it will not fit, add the difference according to the
0N/A * weightY array.
0N/A */
0N/A
0N/A pixels_diff = -1;
0N/A if (hasBaseline) {
0N/A switch(constraints.anchor) {
0N/A case GridBagConstraints.BASELINE:
0N/A case GridBagConstraints.BASELINE_LEADING:
0N/A case GridBagConstraints.BASELINE_TRAILING:
0N/A if (constraints.ascent >= 0) {
0N/A if (constraints.tempHeight == 1) {
0N/A pixels_diff =
0N/A maxAscent[constraints.tempY] +
0N/A maxDescent[constraints.tempY];
0N/A }
0N/A else if (constraints.baselineResizeBehavior !=
0N/A Component.BaselineResizeBehavior.
0N/A CONSTANT_DESCENT) {
0N/A pixels_diff =
0N/A maxAscent[constraints.tempY] +
0N/A constraints.descent;
0N/A }
0N/A else {
0N/A pixels_diff = constraints.ascent +
0N/A maxDescent[constraints.tempY +
0N/A constraints.tempHeight - 1];
0N/A }
0N/A }
0N/A break;
0N/A case GridBagConstraints.ABOVE_BASELINE:
0N/A case GridBagConstraints.ABOVE_BASELINE_LEADING:
0N/A case GridBagConstraints.ABOVE_BASELINE_TRAILING:
0N/A pixels_diff = constraints.insets.top +
0N/A constraints.minHeight +
0N/A constraints.ipady +
0N/A maxDescent[constraints.tempY];
0N/A break;
0N/A case GridBagConstraints.BELOW_BASELINE:
0N/A case GridBagConstraints.BELOW_BASELINE_LEADING:
0N/A case GridBagConstraints.BELOW_BASELINE_TRAILING:
0N/A pixels_diff = maxAscent[constraints.tempY] +
0N/A constraints.minHeight +
0N/A constraints.insets.bottom +
0N/A constraints.ipady;
0N/A break;
0N/A }
0N/A }
0N/A if (pixels_diff == -1) {
0N/A pixels_diff =
0N/A constraints.minHeight + constraints.ipady +
0N/A constraints.insets.top +
0N/A constraints.insets.bottom;
0N/A }
0N/A for (k = constraints.tempY; k < py; k++)
0N/A pixels_diff -= r.minHeight[k];
0N/A if (pixels_diff > 0) {
0N/A weight = 0.0;
0N/A for (k = constraints.tempY; k < py; k++)
0N/A weight += r.weightY[k];
0N/A for (k = constraints.tempY; weight > 0.0 && k < py; k++) {
0N/A double wt = r.weightY[k];
0N/A int dy = (int)((wt * ((double)pixels_diff)) / weight);
0N/A r.minHeight[k] += dy;
0N/A pixels_diff -= dy;
0N/A weight -= wt;
0N/A }
0N/A /* Any leftovers go into the bottom cell */
0N/A r.minHeight[py-1] += pixels_diff;
0N/A }
0N/A }
0N/A else if (constraints.tempHeight > i &&
0N/A constraints.tempHeight < nextSize)
0N/A nextSize = constraints.tempHeight;
0N/A }
0N/A }
0N/A return r;
0N/A }
0N/A } //getLayoutInfo()
0N/A
0N/A /**
0N/A * Calculate the baseline for the specified component.
0N/A * If {@code c} is positioned along it's baseline, the baseline is
0N/A * obtained and the {@code constraints} ascent, descent and
0N/A * baseline resize behavior are set from the component; and true is
0N/A * returned. Otherwise false is returned.
0N/A */
0N/A private boolean calculateBaseline(Component c,
0N/A GridBagConstraints constraints,
0N/A Dimension size) {
0N/A int anchor = constraints.anchor;
0N/A if (anchor == GridBagConstraints.BASELINE ||
0N/A anchor == GridBagConstraints.BASELINE_LEADING ||
0N/A anchor == GridBagConstraints.BASELINE_TRAILING) {
0N/A // Apply the padding to the component, then ask for the baseline.
0N/A int w = size.width + constraints.ipadx;
0N/A int h = size.height + constraints.ipady;
0N/A constraints.ascent = c.getBaseline(w, h);
0N/A if (constraints.ascent >= 0) {
0N/A // Component has a baseline
0N/A int baseline = constraints.ascent;
0N/A // Adjust the ascent and descent to include the insets.
0N/A constraints.descent = h - constraints.ascent +
0N/A constraints.insets.bottom;
0N/A constraints.ascent += constraints.insets.top;
0N/A constraints.baselineResizeBehavior =
0N/A c.getBaselineResizeBehavior();
0N/A constraints.centerPadding = 0;
0N/A if (constraints.baselineResizeBehavior == Component.
0N/A BaselineResizeBehavior.CENTER_OFFSET) {
0N/A // Component has a baseline resize behavior of
0N/A // CENTER_OFFSET, calculate centerPadding and
0N/A // centerOffset (see the description of
0N/A // CENTER_OFFSET in the enum for detais on this
0N/A // algorithm).
0N/A int nextBaseline = c.getBaseline(w, h + 1);
0N/A constraints.centerOffset = baseline - h / 2;
0N/A if (h % 2 == 0) {
0N/A if (baseline != nextBaseline) {
0N/A constraints.centerPadding = 1;
0N/A }
0N/A }
0N/A else if (baseline == nextBaseline){
0N/A constraints.centerOffset--;
0N/A constraints.centerPadding = 1;
0N/A }
0N/A }
0N/A }
0N/A return true;
0N/A }
0N/A else {
0N/A constraints.ascent = -1;
0N/A return false;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Adjusts the x, y, width, and height fields to the correct
0N/A * values depending on the constraint geometry and pads.
0N/A * This method should only be used internally by
0N/A * <code>GridBagLayout</code>.
0N/A *
0N/A * @param constraints the constraints to be applied
0N/A * @param r the <code>Rectangle</code> to be adjusted
0N/A * @since 1.4
0N/A */
0N/A protected void adjustForGravity(GridBagConstraints constraints,
0N/A Rectangle r) {
0N/A AdjustForGravity(constraints, r);
0N/A }
0N/A
0N/A /**
0N/A * This method is obsolete and supplied for backwards
0N/A * compatability only; new code should call {@link
0N/A * #adjustForGravity(java.awt.GridBagConstraints, java.awt.Rectangle)
0N/A * adjustForGravity} instead.
0N/A * This method is the same as <code>adjustForGravity</code>;
0N/A * refer to <code>adjustForGravity</code> for details
0N/A * on parameters.
0N/A */
0N/A protected void AdjustForGravity(GridBagConstraints constraints,
0N/A Rectangle r) {
0N/A int diffx, diffy;
0N/A int cellY = r.y;
0N/A int cellHeight = r.height;
0N/A
0N/A if (!rightToLeft) {
0N/A r.x += constraints.insets.left;
0N/A } else {
0N/A r.x -= r.width - constraints.insets.right;
0N/A }
0N/A r.width -= (constraints.insets.left + constraints.insets.right);
0N/A r.y += constraints.insets.top;
0N/A r.height -= (constraints.insets.top + constraints.insets.bottom);
0N/A
0N/A diffx = 0;
0N/A if ((constraints.fill != GridBagConstraints.HORIZONTAL &&
0N/A constraints.fill != GridBagConstraints.BOTH)
0N/A && (r.width > (constraints.minWidth + constraints.ipadx))) {
0N/A diffx = r.width - (constraints.minWidth + constraints.ipadx);
0N/A r.width = constraints.minWidth + constraints.ipadx;
0N/A }
0N/A
0N/A diffy = 0;
0N/A if ((constraints.fill != GridBagConstraints.VERTICAL &&
0N/A constraints.fill != GridBagConstraints.BOTH)
0N/A && (r.height > (constraints.minHeight + constraints.ipady))) {
0N/A diffy = r.height - (constraints.minHeight + constraints.ipady);
0N/A r.height = constraints.minHeight + constraints.ipady;
0N/A }
0N/A
0N/A switch (constraints.anchor) {
0N/A case GridBagConstraints.BASELINE:
0N/A r.x += diffx/2;
0N/A alignOnBaseline(constraints, r, cellY, cellHeight);
0N/A break;
0N/A case GridBagConstraints.BASELINE_LEADING:
0N/A if (rightToLeft) {
0N/A r.x += diffx;
0N/A }
0N/A alignOnBaseline(constraints, r, cellY, cellHeight);
0N/A break;
0N/A case GridBagConstraints.BASELINE_TRAILING:
0N/A if (!rightToLeft) {
0N/A r.x += diffx;
0N/A }
0N/A alignOnBaseline(constraints, r, cellY, cellHeight);
0N/A break;
0N/A case GridBagConstraints.ABOVE_BASELINE:
0N/A r.x += diffx/2;
0N/A alignAboveBaseline(constraints, r, cellY, cellHeight);
0N/A break;
0N/A case GridBagConstraints.ABOVE_BASELINE_LEADING:
0N/A if (rightToLeft) {
0N/A r.x += diffx;
0N/A }
0N/A alignAboveBaseline(constraints, r, cellY, cellHeight);
0N/A break;
0N/A case GridBagConstraints.ABOVE_BASELINE_TRAILING:
0N/A if (!rightToLeft) {
0N/A r.x += diffx;
0N/A }
0N/A alignAboveBaseline(constraints, r, cellY, cellHeight);
0N/A break;
0N/A case GridBagConstraints.BELOW_BASELINE:
0N/A r.x += diffx/2;
0N/A alignBelowBaseline(constraints, r, cellY, cellHeight);
0N/A break;
0N/A case GridBagConstraints.BELOW_BASELINE_LEADING:
0N/A if (rightToLeft) {
0N/A r.x += diffx;
0N/A }
0N/A alignBelowBaseline(constraints, r, cellY, cellHeight);
0N/A break;
0N/A case GridBagConstraints.BELOW_BASELINE_TRAILING:
0N/A if (!rightToLeft) {
0N/A r.x += diffx;
0N/A }
0N/A alignBelowBaseline(constraints, r, cellY, cellHeight);
0N/A break;
0N/A case GridBagConstraints.CENTER:
0N/A r.x += diffx/2;
0N/A r.y += diffy/2;
0N/A break;
0N/A case GridBagConstraints.PAGE_START:
0N/A case GridBagConstraints.NORTH:
0N/A r.x += diffx/2;
0N/A break;
0N/A case GridBagConstraints.NORTHEAST:
0N/A r.x += diffx;
0N/A break;
0N/A case GridBagConstraints.EAST:
0N/A r.x += diffx;
0N/A r.y += diffy/2;
0N/A break;
0N/A case GridBagConstraints.SOUTHEAST:
0N/A r.x += diffx;
0N/A r.y += diffy;
0N/A break;
0N/A case GridBagConstraints.PAGE_END:
0N/A case GridBagConstraints.SOUTH:
0N/A r.x += diffx/2;
0N/A r.y += diffy;
0N/A break;
0N/A case GridBagConstraints.SOUTHWEST:
0N/A r.y += diffy;
0N/A break;
0N/A case GridBagConstraints.WEST:
0N/A r.y += diffy/2;
0N/A break;
0N/A case GridBagConstraints.NORTHWEST:
0N/A break;
0N/A case GridBagConstraints.LINE_START:
0N/A if (rightToLeft) {
0N/A r.x += diffx;
0N/A }
0N/A r.y += diffy/2;
0N/A break;
0N/A case GridBagConstraints.LINE_END:
0N/A if (!rightToLeft) {
0N/A r.x += diffx;
0N/A }
0N/A r.y += diffy/2;
0N/A break;
0N/A case GridBagConstraints.FIRST_LINE_START:
0N/A if (rightToLeft) {
0N/A r.x += diffx;
0N/A }
0N/A break;
0N/A case GridBagConstraints.FIRST_LINE_END:
0N/A if (!rightToLeft) {
0N/A r.x += diffx;
0N/A }
0N/A break;
0N/A case GridBagConstraints.LAST_LINE_START:
0N/A if (rightToLeft) {
0N/A r.x += diffx;
0N/A }
0N/A r.y += diffy;
0N/A break;
0N/A case GridBagConstraints.LAST_LINE_END:
0N/A if (!rightToLeft) {
0N/A r.x += diffx;
0N/A }
0N/A r.y += diffy;
0N/A break;
0N/A default:
0N/A throw new IllegalArgumentException("illegal anchor value");
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Positions on the baseline.
0N/A *
0N/A * @param cellY the location of the row, does not include insets
0N/A * @param cellHeight the height of the row, does not take into account
0N/A * insets
0N/A * @param r available bounds for the component, is padded by insets and
0N/A * ipady
0N/A */
0N/A private void alignOnBaseline(GridBagConstraints cons, Rectangle r,
0N/A int cellY, int cellHeight) {
0N/A if (cons.ascent >= 0) {
0N/A if (cons.baselineResizeBehavior == Component.
0N/A BaselineResizeBehavior.CONSTANT_DESCENT) {
0N/A // Anchor to the bottom.
0N/A // Baseline is at (cellY + cellHeight - maxDescent).
0N/A // Bottom of component (maxY) is at baseline + descent
0N/A // of component. We need to subtract the bottom inset here
0N/A // as the descent in the constraints object includes the
0N/A // bottom inset.
0N/A int maxY = cellY + cellHeight -
0N/A layoutInfo.maxDescent[cons.tempY + cons.tempHeight - 1] +
0N/A cons.descent - cons.insets.bottom;
0N/A if (!cons.isVerticallyResizable()) {
0N/A // Component not resizable, calculate y location
0N/A // from maxY - height.
0N/A r.y = maxY - cons.minHeight;
0N/A r.height = cons.minHeight;
0N/A } else {
0N/A // Component is resizable. As brb is constant descent,
0N/A // can expand component to fill region above baseline.
0N/A // Subtract out the top inset so that components insets
0N/A // are honored.
0N/A r.height = maxY - cellY - cons.insets.top;
0N/A }
0N/A }
0N/A else {
0N/A // BRB is not constant_descent
0N/A int baseline; // baseline for the row, relative to cellY
0N/A // Component baseline, includes insets.top
0N/A int ascent = cons.ascent;
0N/A if (layoutInfo.hasConstantDescent(cons.tempY)) {
0N/A // Mixed ascent/descent in same row, calculate position
0N/A // off maxDescent
0N/A baseline = cellHeight - layoutInfo.maxDescent[cons.tempY];
0N/A }
0N/A else {
0N/A // Only ascents/unknown in this row, anchor to top
0N/A baseline = layoutInfo.maxAscent[cons.tempY];
0N/A }
0N/A if (cons.baselineResizeBehavior == Component.
0N/A BaselineResizeBehavior.OTHER) {
0N/A // BRB is other, which means we can only determine
0N/A // the baseline by asking for it again giving the
0N/A // size we plan on using for the component.
0N/A boolean fits = false;
0N/A ascent = componentAdjusting.getBaseline(r.width, r.height);
0N/A if (ascent >= 0) {
0N/A // Component has a baseline, pad with top inset
0N/A // (this follows from calculateBaseline which
0N/A // does the same).
0N/A ascent += cons.insets.top;
0N/A }
0N/A if (ascent >= 0 && ascent <= baseline) {
0N/A // Components baseline fits within rows baseline.
0N/A // Make sure the descent fits within the space as well.
0N/A if (baseline + (r.height - ascent - cons.insets.top) <=
0N/A cellHeight - cons.insets.bottom) {
0N/A // It fits, we're good.
0N/A fits = true;
0N/A }
0N/A else if (cons.isVerticallyResizable()) {
0N/A // Doesn't fit, but it's resizable. Try
0N/A // again assuming we'll get ascent again.
0N/A int ascent2 = componentAdjusting.getBaseline(
0N/A r.width, cellHeight - cons.insets.bottom -
0N/A baseline + ascent);
0N/A if (ascent2 >= 0) {
0N/A ascent2 += cons.insets.top;
0N/A }
0N/A if (ascent2 >= 0 && ascent2 <= ascent) {
0N/A // It'll fit
0N/A r.height = cellHeight - cons.insets.bottom -
0N/A baseline + ascent;
0N/A ascent = ascent2;
0N/A fits = true;
0N/A }
0N/A }
0N/A }
0N/A if (!fits) {
0N/A // Doesn't fit, use min size and original ascent
0N/A ascent = cons.ascent;
0N/A r.width = cons.minWidth;
0N/A r.height = cons.minHeight;
0N/A }
0N/A }
0N/A // Reset the components y location based on
0N/A // components ascent and baseline for row. Because ascent
0N/A // includes the baseline
0N/A r.y = cellY + baseline - ascent + cons.insets.top;
0N/A if (cons.isVerticallyResizable()) {
0N/A switch(cons.baselineResizeBehavior) {
0N/A case CONSTANT_ASCENT:
0N/A r.height = Math.max(cons.minHeight,cellY + cellHeight -
0N/A r.y - cons.insets.bottom);
0N/A break;
0N/A case CENTER_OFFSET:
0N/A {
0N/A int upper = r.y - cellY - cons.insets.top;
0N/A int lower = cellY + cellHeight - r.y -
0N/A cons.minHeight - cons.insets.bottom;
0N/A int delta = Math.min(upper, lower);
0N/A delta += delta;
0N/A if (delta > 0 &&
0N/A (cons.minHeight + cons.centerPadding +
0N/A delta) / 2 + cons.centerOffset != baseline) {
0N/A // Off by 1
0N/A delta--;
0N/A }
0N/A r.height = cons.minHeight + delta;
0N/A r.y = cellY + baseline -
0N/A (r.height + cons.centerPadding) / 2 -
0N/A cons.centerOffset;
0N/A }
0N/A break;
0N/A case OTHER:
0N/A // Handled above
0N/A break;
0N/A default:
0N/A break;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A else {
0N/A centerVertically(cons, r, cellHeight);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Positions the specified component above the baseline. That is
0N/A * the bottom edge of the component will be aligned along the baseline.
0N/A * If the row does not have a baseline, this centers the component.
0N/A */
0N/A private void alignAboveBaseline(GridBagConstraints cons, Rectangle r,
0N/A int cellY, int cellHeight) {
0N/A if (layoutInfo.hasBaseline(cons.tempY)) {
0N/A int maxY; // Baseline for the row
0N/A if (layoutInfo.hasConstantDescent(cons.tempY)) {
0N/A // Prefer descent
0N/A maxY = cellY + cellHeight - layoutInfo.maxDescent[cons.tempY];
0N/A }
0N/A else {
0N/A // Prefer ascent
0N/A maxY = cellY + layoutInfo.maxAscent[cons.tempY];
0N/A }
0N/A if (cons.isVerticallyResizable()) {
0N/A // Component is resizable. Top edge is offset by top
0N/A // inset, bottom edge on baseline.
0N/A r.y = cellY + cons.insets.top;
0N/A r.height = maxY - r.y;
0N/A }
0N/A else {
0N/A // Not resizable.
0N/A r.height = cons.minHeight + cons.ipady;
0N/A r.y = maxY - r.height;
0N/A }
0N/A }
0N/A else {
0N/A centerVertically(cons, r, cellHeight);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Positions below the baseline.
0N/A */
0N/A private void alignBelowBaseline(GridBagConstraints cons, Rectangle r,
0N/A int cellY, int cellHeight) {
0N/A if (layoutInfo.hasBaseline(cons.tempY)) {
0N/A if (layoutInfo.hasConstantDescent(cons.tempY)) {
0N/A // Prefer descent
0N/A r.y = cellY + cellHeight - layoutInfo.maxDescent[cons.tempY];
0N/A }
0N/A else {
0N/A // Prefer ascent
0N/A r.y = cellY + layoutInfo.maxAscent[cons.tempY];
0N/A }
0N/A if (cons.isVerticallyResizable()) {
0N/A r.height = cellY + cellHeight - r.y - cons.insets.bottom;
0N/A }
0N/A }
0N/A else {
0N/A centerVertically(cons, r, cellHeight);
0N/A }
0N/A }
0N/A
0N/A private void centerVertically(GridBagConstraints cons, Rectangle r,
0N/A int cellHeight) {
0N/A if (!cons.isVerticallyResizable()) {
0N/A r.y += Math.max(0, (cellHeight - cons.insets.top -
0N/A cons.insets.bottom - cons.minHeight -
0N/A cons.ipady) / 2);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Figures out the minimum size of the
0N/A * master based on the information from <code>getLayoutInfo</code>.
0N/A * This method should only be used internally by
0N/A * <code>GridBagLayout</code>.
0N/A *
0N/A * @param parent the layout container
0N/A * @param info the layout info for this parent
0N/A * @return a <code>Dimension</code> object containing the
0N/A * minimum size
0N/A * @since 1.4
0N/A */
0N/A protected Dimension getMinSize(Container parent, GridBagLayoutInfo info) {
0N/A return GetMinSize(parent, info);
0N/A }
0N/A
0N/A /**
0N/A * This method is obsolete and supplied for backwards
0N/A * compatability only; new code should call {@link
0N/A * #getMinSize(java.awt.Container, GridBagLayoutInfo) getMinSize} instead.
0N/A * This method is the same as <code>getMinSize</code>;
0N/A * refer to <code>getMinSize</code> for details on parameters
0N/A * and return value.
0N/A */
0N/A protected Dimension GetMinSize(Container parent, GridBagLayoutInfo info) {
0N/A Dimension d = new Dimension();
0N/A int i, t;
0N/A Insets insets = parent.getInsets();
0N/A
0N/A t = 0;
0N/A for(i = 0; i < info.width; i++)
0N/A t += info.minWidth[i];
0N/A d.width = t + insets.left + insets.right;
0N/A
0N/A t = 0;
0N/A for(i = 0; i < info.height; i++)
0N/A t += info.minHeight[i];
0N/A d.height = t + insets.top + insets.bottom;
0N/A
0N/A return d;
0N/A }
0N/A
0N/A transient boolean rightToLeft = false;
0N/A
0N/A /**
0N/A * Lays out the grid.
0N/A * This method should only be used internally by
0N/A * <code>GridBagLayout</code>.
0N/A *
0N/A * @param parent the layout container
0N/A * @since 1.4
0N/A */
0N/A protected void arrangeGrid(Container parent) {
0N/A ArrangeGrid(parent);
0N/A }
0N/A
0N/A /**
0N/A * This method is obsolete and supplied for backwards
0N/A * compatability only; new code should call {@link
0N/A * #arrangeGrid(Container) arrangeGrid} instead.
0N/A * This method is the same as <code>arrangeGrid</code>;
0N/A * refer to <code>arrangeGrid</code> for details on the
0N/A * parameter.
0N/A */
0N/A protected void ArrangeGrid(Container parent) {
0N/A Component comp;
0N/A int compindex;
0N/A GridBagConstraints constraints;
0N/A Insets insets = parent.getInsets();
0N/A Component components[] = parent.getComponents();
0N/A Dimension d;
0N/A Rectangle r = new Rectangle();
0N/A int i, diffw, diffh;
0N/A double weight;
0N/A GridBagLayoutInfo info;
0N/A
0N/A rightToLeft = !parent.getComponentOrientation().isLeftToRight();
0N/A
0N/A /*
0N/A * If the parent has no slaves anymore, then don't do anything
0N/A * at all: just leave the parent's size as-is.
0N/A */
0N/A if (components.length == 0 &&
0N/A (columnWidths == null || columnWidths.length == 0) &&
0N/A (rowHeights == null || rowHeights.length == 0)) {
0N/A return;
0N/A }
0N/A
0N/A /*
0N/A * Pass #1: scan all the slaves to figure out the total amount
0N/A * of space needed.
0N/A */
0N/A
0N/A info = getLayoutInfo(parent, PREFERREDSIZE);
0N/A d = getMinSize(parent, info);
0N/A
0N/A if (parent.width < d.width || parent.height < d.height) {
0N/A info = getLayoutInfo(parent, MINSIZE);
0N/A d = getMinSize(parent, info);
0N/A }
0N/A
0N/A layoutInfo = info;
0N/A r.width = d.width;
0N/A r.height = d.height;
0N/A
0N/A /*
0N/A * DEBUG
0N/A *
0N/A * DumpLayoutInfo(info);
0N/A * for (compindex = 0 ; compindex < components.length ; compindex++) {
0N/A * comp = components[compindex];
0N/A * if (!comp.isVisible())
0N/A * continue;
0N/A * constraints = lookupConstraints(comp);
0N/A * DumpConstraints(constraints);
0N/A * }
0N/A * System.out.println("minSize " + r.width + " " + r.height);
0N/A */
0N/A
0N/A /*
0N/A * If the current dimensions of the window don't match the desired
0N/A * dimensions, then adjust the minWidth and minHeight arrays
0N/A * according to the weights.
0N/A */
0N/A
0N/A diffw = parent.width - r.width;
0N/A if (diffw != 0) {
0N/A weight = 0.0;
0N/A for (i = 0; i < info.width; i++)
0N/A weight += info.weightX[i];
0N/A if (weight > 0.0) {
0N/A for (i = 0; i < info.width; i++) {
0N/A int dx = (int)(( ((double)diffw) * info.weightX[i]) / weight);
0N/A info.minWidth[i] += dx;
0N/A r.width += dx;
0N/A if (info.minWidth[i] < 0) {
0N/A r.width -= info.minWidth[i];
0N/A info.minWidth[i] = 0;
0N/A }
0N/A }
0N/A }
0N/A diffw = parent.width - r.width;
0N/A }
0N/A
0N/A else {
0N/A diffw = 0;
0N/A }
0N/A
0N/A diffh = parent.height - r.height;
0N/A if (diffh != 0) {
0N/A weight = 0.0;
0N/A for (i = 0; i < info.height; i++)
0N/A weight += info.weightY[i];
0N/A if (weight > 0.0) {
0N/A for (i = 0; i < info.height; i++) {
0N/A int dy = (int)(( ((double)diffh) * info.weightY[i]) / weight);
0N/A info.minHeight[i] += dy;
0N/A r.height += dy;
0N/A if (info.minHeight[i] < 0) {
0N/A r.height -= info.minHeight[i];
0N/A info.minHeight[i] = 0;
0N/A }
0N/A }
0N/A }
0N/A diffh = parent.height - r.height;
0N/A }
0N/A
0N/A else {
0N/A diffh = 0;
0N/A }
0N/A
0N/A /*
0N/A * DEBUG
0N/A *
0N/A * System.out.println("Re-adjusted:");
0N/A * DumpLayoutInfo(info);
0N/A */
0N/A
0N/A /*
0N/A * Now do the actual layout of the slaves using the layout information
0N/A * that has been collected.
0N/A */
0N/A
0N/A info.startx = diffw/2 + insets.left;
0N/A info.starty = diffh/2 + insets.top;
0N/A
0N/A for (compindex = 0 ; compindex < components.length ; compindex++) {
0N/A comp = components[compindex];
0N/A if (!comp.isVisible()){
0N/A continue;
0N/A }
0N/A constraints = lookupConstraints(comp);
0N/A
0N/A if (!rightToLeft) {
0N/A r.x = info.startx;
0N/A for(i = 0; i < constraints.tempX; i++)
0N/A r.x += info.minWidth[i];
0N/A } else {
0N/A r.x = parent.width - (diffw/2 + insets.right);
0N/A for(i = 0; i < constraints.tempX; i++)
0N/A r.x -= info.minWidth[i];
0N/A }
0N/A
0N/A r.y = info.starty;
0N/A for(i = 0; i < constraints.tempY; i++)
0N/A r.y += info.minHeight[i];
0N/A
0N/A r.width = 0;
0N/A for(i = constraints.tempX;
0N/A i < (constraints.tempX + constraints.tempWidth);
0N/A i++) {
0N/A r.width += info.minWidth[i];
0N/A }
0N/A
0N/A r.height = 0;
0N/A for(i = constraints.tempY;
0N/A i < (constraints.tempY + constraints.tempHeight);
0N/A i++) {
0N/A r.height += info.minHeight[i];
0N/A }
0N/A
0N/A componentAdjusting = comp;
0N/A adjustForGravity(constraints, r);
0N/A
0N/A /* fix for 4408108 - components were being created outside of the container */
0N/A /* fix for 4969409 "-" replaced by "+" */
0N/A if (r.x < 0) {
0N/A r.width += r.x;
0N/A r.x = 0;
0N/A }
0N/A
0N/A if (r.y < 0) {
0N/A r.height += r.y;
0N/A r.y = 0;
0N/A }
0N/A
0N/A /*
0N/A * If the window is too small to be interesting then
0N/A * unmap it. Otherwise configure it and then make sure
0N/A * it's mapped.
0N/A */
0N/A
0N/A if ((r.width <= 0) || (r.height <= 0)) {
0N/A comp.setBounds(0, 0, 0, 0);
0N/A }
0N/A else {
0N/A if (comp.x != r.x || comp.y != r.y ||
0N/A comp.width != r.width || comp.height != r.height) {
0N/A comp.setBounds(r.x, r.y, r.width, r.height);
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A // Added for serial backwards compatability (4348425)
0N/A static final long serialVersionUID = 8838754796412211005L;
0N/A}