jdk/src/share/classes/javax/swing/SpringLayout.java
author malenkov
Wed, 07 May 2008 23:20:32 +0400
changeset 466 6acd5ec503a8
parent 2 90ce3da70b43
child 1301 15e81207e1f2
permissions -rw-r--r--
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE Summary: Add the Transient annotation and support it (JSR-273) Reviewed-by: peterz, loneid
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2001-2006 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.awt.Component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.Container;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.Dimension;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.FontMetrics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.Insets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.LayoutManager2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.Rectangle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * A <code>SpringLayout</code> lays out the children of its associated container
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * according to a set of constraints.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * See <a href="http://java.sun.com/docs/books/tutorial/uiswing/layout/spring.html">How to Use SpringLayout</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * in <em>The Java Tutorial</em> for examples of using
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <code>SpringLayout</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * Each constraint,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * represented by a <code>Spring</code> object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * controls the vertical or horizontal distance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * between two component edges.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * The edges can belong to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * any child of the container,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * or to the container itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * For example,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * the allowable width of a component
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * can be expressed using a constraint
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * that controls the distance between the west (left) and east (right)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * edges of the component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * The allowable <em>y</em> coordinates for a component
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * can be expressed by constraining the distance between
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * the north (top) edge of the component
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * and the north edge of its container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * Every child of a <code>SpringLayout</code>-controlled container,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * as well as the container itself,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * has exactly one set of constraints
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * associated with it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * These constraints are represented by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * a <code>SpringLayout.Constraints</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * By default,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * <code>SpringLayout</code> creates constraints
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * that make their associated component
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * have the minimum, preferred, and maximum sizes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * returned by the component's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * {@link java.awt.Component#getMinimumSize},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * {@link java.awt.Component#getPreferredSize}, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * {@link java.awt.Component#getMaximumSize}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * methods. The <em>x</em> and <em>y</em> positions are initially not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * constrained, so that until you constrain them the <code>Component</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * will be positioned at 0,0 relative to the <code>Insets</code> of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * parent <code>Container</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * You can change
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * a component's constraints in several ways.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * You can
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * use one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * {@link #putConstraint putConstraint}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * to establish a spring
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * linking the edges of two components within the same container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * Or you can get the appropriate <code>SpringLayout.Constraints</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * object using
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * {@link #getConstraints getConstraints}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * and then modify one or more of its springs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * Or you can get the spring for a particular edge of a component
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * using {@link #getConstraint getConstraint},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * and modify it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * You can also associate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * your own <code>SpringLayout.Constraints</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * with a component by specifying the constraints object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * when you add the component to its container
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * (using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * {@link Container#add(Component, Object)}).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * The <code>Spring</code> object representing each constraint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * has a minimum, preferred, maximum, and current value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * The current value of the spring
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * is somewhere between the minimum and maximum values,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * according to the formula given in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * {@link Spring#sum} method description.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * When the minimum, preferred, and maximum values are the same,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * the current value is always equal to them;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * this inflexible spring is called a <em>strut</em>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * You can create struts using the factory method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * {@link Spring#constant(int)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * The <code>Spring</code> class also provides factory methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * for creating other kinds of springs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * including springs that depend on other springs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * In a <code>SpringLayout</code>, the position of each edge is dependent on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * the position of just one other edge. If a constraint is subsequently added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * to create a new binding for an edge, the previous binding is discarded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * and the edge remains dependent on a single edge.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * Springs should only be attached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * between edges of the container and its immediate children; the behavior
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * of the <code>SpringLayout</code> when presented with constraints linking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * the edges of components from different containers (either internal or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * external) is undefined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * <h3>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * SpringLayout vs. Other Layout Managers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * </h3>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * <hr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 * <strong>Note:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 * Unlike many layout managers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 * <code>SpringLayout</code> doesn't automatically set the location of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 * the components it manages.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 * If you hand-code a GUI that uses <code>SpringLayout</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 * remember to initialize component locations by constraining the west/east
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 * and north/south locations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 * Depending on the constraints you use,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 * you may also need to set the size of the container explicitly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 * <hr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 * Despite the simplicity of <code>SpringLayout</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 * it can emulate the behavior of most other layout managers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 * For some features,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 * such as the line breaking provided by <code>FlowLayout</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 * you'll need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 * create a special-purpose subclass of the <code>Spring</code> class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 * <code>SpringLayout</code> also provides a way to solve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 * many of the difficult layout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 * problems that cannot be solved by nesting combinations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 * of <code>Box</code>es. That said, <code>SpringLayout</code> honors the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
 * <code>LayoutManager2</code> contract correctly and so can be nested with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
 * other layout managers -- a technique that can be preferable to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
 * creating the constraints implied by the other layout managers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
 * The asymptotic complexity of the layout operation of a <code>SpringLayout</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
 * is linear in the number of constraints (and/or components).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
 * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
 * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
 * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
 * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
 * the same version of Swing.  As of 1.4, support for long term storage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
 * of all JavaBeans<sup><font size="-2">TM</font></sup>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
 * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
 * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
 * @see Spring
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
 * @see SpringLayout.Constraints
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
 * @author      Philip Milne
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
 * @author      Scott Violet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
 * @author      Joe Winchester
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
 * @since       1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
public class SpringLayout implements LayoutManager2 {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    private Map componentConstraints = new HashMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    private Spring cyclicReference = Spring.constant(Spring.UNSET);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    private Set cyclicSprings;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    private Set acyclicSprings;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * Specifies the top edge of a component's bounding rectangle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    public static final String NORTH  = "North";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * Specifies the bottom edge of a component's bounding rectangle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    public static final String SOUTH  = "South";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * Specifies the right edge of a component's bounding rectangle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    public static final String EAST   = "East";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * Specifies the left edge of a component's bounding rectangle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    public static final String WEST   = "West";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * Specifies the horizontal center of a component's bounding rectangle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    public static final String HORIZONTAL_CENTER   = "HorizontalCenter";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * Specifies the vertical center of a component's bounding rectangle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    public static final String VERTICAL_CENTER   = "VerticalCenter";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * Specifies the baseline of a component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    public static final String BASELINE   = "Baseline";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * Specifies the width of a component's bounding rectangle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    public static final String WIDTH = "Width";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * Specifies the height of a component's bounding rectangle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    public static final String HEIGHT = "Height";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    private static String[] ALL_HORIZONTAL = {WEST, WIDTH, EAST, HORIZONTAL_CENTER};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    private static String[] ALL_VERTICAL = {NORTH, HEIGHT, SOUTH, VERTICAL_CENTER, BASELINE};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * A <code>Constraints</code> object holds the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * constraints that govern the way a component's size and position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * change in a container controlled by a <code>SpringLayout</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * A <code>Constraints</code> object is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * like a <code>Rectangle</code>, in that it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * has <code>x</code>, <code>y</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * <code>width</code>, and <code>height</code> properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * In the <code>Constraints</code> object, however,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * these properties have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * <code>Spring</code> values instead of integers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * In addition,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * a <code>Constraints</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * can be manipulated as four edges
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * -- north, south, east, and west --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * using the <code>constraint</code> property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * The following formulas are always true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * for a <code>Constraints</code> object (here WEST and <code>x</code> are synonyms, as are and NORTH and <code>y</code>):
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     *               EAST = WEST + WIDTH
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     *              SOUTH = NORTH + HEIGHT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     *  HORIZONTAL_CENTER = WEST + WIDTH/2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *    VERTICAL_CENTER = NORTH + HEIGHT/2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     *  ABSOLUTE_BASELINE = NORTH + RELATIVE_BASELINE*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * For example, if you have specified the WIDTH and WEST (X) location
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * the EAST is calculated as WEST + WIDTH.  If you instead specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * the WIDTH and EAST locations the WEST (X) location is then calculated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * as EAST - WIDTH.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * [RELATIVE_BASELINE is a private constraint that is set automatically when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * the SpringLayout.Constraints(Component) constuctor is called or when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * a constraints object is registered with a SpringLayout object.]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * <b>Note</b>: In this document,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * operators represent methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * in the <code>Spring</code> class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * For example, "a + b" is equal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * <code>Spring.sum(a, b)</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * and "a - b" is equal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * <code>Spring.sum(a, Spring.minus(b))</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * See the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * {@link Spring <code>Spring</code> API documentation}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * for further details
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * of spring arithmetic.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * Because a <code>Constraints</code> object's properties --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * representing its edges, size, and location -- can all be set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * independently and yet are interrelated,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * a <code>Constraints</code> object can become <em>over-constrained</em>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * For example, if the <code>WEST</code>, <code>WIDTH</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * <code>EAST</code> edges are all set, steps must be taken to ensure that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * the first of the formulas above holds.  To do this, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * <code>Constraints</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * object throws away the <em>least recently set</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * constraint so as to make the formulas hold.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    public static class Constraints {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
       private Spring x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
       private Spring y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
       private Spring width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
       private Spring height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
       private Spring east;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
       private Spring south;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        private Spring horizontalCenter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        private Spring verticalCenter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        private Spring baseline;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        private List<String> horizontalHistory = new ArrayList<String>(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        private List<String> verticalHistory = new ArrayList<String>(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        // Used for baseline calculations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        private Component c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
       /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        * Creates an empty <code>Constraints</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
       public Constraints() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
       /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        * Creates a <code>Constraints</code> object with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        * specified values for its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        * <code>x</code> and <code>y</code> properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        * The <code>height</code> and <code>width</code> springs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        * have <code>null</code> values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        * @param x  the spring controlling the component's <em>x</em> value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        * @param y  the spring controlling the component's <em>y</em> value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
       public Constraints(Spring x, Spring y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
           setX(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
           setY(y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
       /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        * Creates a <code>Constraints</code> object with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        * specified values for its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        * <code>x</code>, <code>y</code>, <code>width</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        * and <code>height</code> properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        * Note: If the <code>SpringLayout</code> class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        * encounters <code>null</code> values in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        * <code>Constraints</code> object of a given component,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        * it replaces them with suitable defaults.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        * @param x  the spring value for the <code>x</code> property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        * @param y  the spring value for the <code>y</code> property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        * @param width  the spring value for the <code>width</code> property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        * @param height  the spring value for the <code>height</code> property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
       public Constraints(Spring x, Spring y, Spring width, Spring height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
           setX(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
           setY(y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
           setWidth(width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
           setHeight(height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
         * Creates a <code>Constraints</code> object with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
         * suitable <code>x</code>, <code>y</code>, <code>width</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
         * <code>height</code> springs for component, <code>c</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
         * The <code>x</code> and <code>y</code> springs are constant
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
         * springs  initialised with the component's location at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
         * the time this method is called. The <code>width</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
         * <code>height</code> springs are special springs, created by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
         * the <code>Spring.width()</code> and <code>Spring.height()</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
         * methods, which track the size characteristics of the component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
         * when they change.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
         * @param c  the component whose characteristics will be reflected by this Constraints object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
         * @throws NullPointerException if <code>c</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
         * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        public Constraints(Component c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            this.c = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            setX(Spring.constant(c.getX()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            setY(Spring.constant(c.getY()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            setWidth(Spring.width(c));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            setHeight(Spring.height(c));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        private void pushConstraint(String name, Spring value, boolean horizontal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            boolean valid = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            List<String> history = horizontal ? horizontalHistory :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                                                verticalHistory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            if (history.contains(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                history.remove(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                valid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            } else if (history.size() == 2 && value != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                history.remove(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                valid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            if (value != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                history.add(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            if (!valid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                String[] all = horizontal ? ALL_HORIZONTAL : ALL_VERTICAL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                for (int i = 0; i < all.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                    String s = all[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                    if (!history.contains(s)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                        setConstraint(s, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
       private Spring sum(Spring s1, Spring s2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
           return (s1 == null || s2 == null) ? null : Spring.sum(s1, s2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
       private Spring difference(Spring s1, Spring s2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
           return (s1 == null || s2 == null) ? null : Spring.difference(s1, s2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        private Spring scale(Spring s, float factor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            return (s == null) ? null : Spring.scale(s, factor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        private int getBaselineFromHeight(int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            if (height < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                // Bad Scott, Bad Scott!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                return -c.getBaseline(c.getPreferredSize().width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                                      -height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            return c.getBaseline(c.getPreferredSize().width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        private int getHeightFromBaseLine(int baseline) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            Dimension prefSize = c.getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            int prefHeight = prefSize.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            int prefBaseline = c.getBaseline(prefSize.width, prefHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            if (prefBaseline == baseline) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                // If prefBaseline < 0, then no baseline, assume preferred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                // height.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                // If prefBaseline == baseline, then specified baseline
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                // matches preferred baseline, return preferred height
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                return prefHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            // Valid baseline
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            switch(c.getBaselineResizeBehavior()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            case CONSTANT_DESCENT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                return prefHeight + (baseline - prefBaseline);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            case CENTER_OFFSET:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                return prefHeight + 2 * (baseline - prefBaseline);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            case CONSTANT_ASCENT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                // Component baseline and specified baseline will NEVER
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                // match, fall through to default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            default: // OTHER
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                // No way to map from baseline to height.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            return Integer.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
         private Spring heightToRelativeBaseline(Spring s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            return new Spring.SpringMap(s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                 protected int map(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                    return getBaselineFromHeight(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                 }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                 protected int inv(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                     return getHeightFromBaseLine(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                 }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        private Spring relativeBaselineToHeight(Spring s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            return new Spring.SpringMap(s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                protected int map(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                    return getHeightFromBaseLine(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                 }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                 protected int inv(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                    return getBaselineFromHeight(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                 }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        private boolean defined(List history, String s1, String s2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            return history.contains(s1) && history.contains(s2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
       /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        * Sets the <code>x</code> property,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        * which controls the <code>x</code> value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        * of a component's location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        * @param x the spring controlling the <code>x</code> value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        *          of a component's location
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        * @see #getX
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        * @see SpringLayout.Constraints
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
       public void setX(Spring x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
           this.x = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
           pushConstraint(WEST, x, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
       /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        * Returns the value of the <code>x</code> property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        * @return the spring controlling the <code>x</code> value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        *         of a component's location
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        * @see #setX
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        * @see SpringLayout.Constraints
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
       public Spring getX() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
           if (x == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
               if (defined(horizontalHistory, EAST, WIDTH)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                   x = difference(east, width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
               } else if (defined(horizontalHistory, HORIZONTAL_CENTER, WIDTH)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                   x = difference(horizontalCenter, scale(width, 0.5f));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
               } else if (defined(horizontalHistory, HORIZONTAL_CENTER, EAST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                   x = difference(scale(horizontalCenter, 2f), east);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
               }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
           }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
           return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
       /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        * Sets the <code>y</code> property,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        * which controls the <code>y</code> value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        * of a component's location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        * @param y the spring controlling the <code>y</code> value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        *          of a component's location
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        * @see #getY
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        * @see SpringLayout.Constraints
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
       public void setY(Spring y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
           this.y = y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
           pushConstraint(NORTH, y, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
       /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        * Returns the value of the <code>y</code> property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        * @return the spring controlling the <code>y</code> value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        *         of a component's location
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        * @see #setY
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        * @see SpringLayout.Constraints
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
       public Spring getY() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
           if (y == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
               if (defined(verticalHistory, SOUTH, HEIGHT)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                   y = difference(south, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
               } else if (defined(verticalHistory, VERTICAL_CENTER, HEIGHT)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                   y = difference(verticalCenter, scale(height, 0.5f));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
               } else if (defined(verticalHistory, VERTICAL_CENTER, SOUTH)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                   y = difference(scale(verticalCenter, 2f), south);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
               } else if (defined(verticalHistory, BASELINE, HEIGHT)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                   y = difference(baseline, heightToRelativeBaseline(height));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
               } else if (defined(verticalHistory, BASELINE, SOUTH)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                   y = scale(difference(baseline, heightToRelativeBaseline(south)), 2f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
               } else if (defined(verticalHistory, BASELINE, VERTICAL_CENTER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                   y = scale(difference(baseline, heightToRelativeBaseline(scale(verticalCenter, 2))), 1f/(1-2*0.5f));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
               }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
           }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
           return y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
       /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        * Sets the <code>width</code> property,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        * which controls the width of a component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        * @param width the spring controlling the width of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        * <code>Constraints</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        * @see #getWidth
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        * @see SpringLayout.Constraints
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
       public void setWidth(Spring width) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
           this.width = width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
           pushConstraint(WIDTH, width, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
       /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        * Returns the value of the <code>width</code> property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        * @return the spring controlling the width of a component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        * @see #setWidth
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        * @see SpringLayout.Constraints
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
       public Spring getWidth() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
           if (width == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
               if (horizontalHistory.contains(EAST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                   width = difference(east, getX());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
               } else if (horizontalHistory.contains(HORIZONTAL_CENTER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                   width = scale(difference(horizontalCenter, getX()), 2f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
               }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
           }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
           return width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
       /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        * Sets the <code>height</code> property,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        * which controls the height of a component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        * @param height the spring controlling the height of this <code>Constraints</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        * object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        * @see #getHeight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        * @see SpringLayout.Constraints
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
       public void setHeight(Spring height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
           this.height = height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
           pushConstraint(HEIGHT, height, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
       /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        * Returns the value of the <code>height</code> property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        * @return the spring controlling the height of a component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        * @see #setHeight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        * @see SpringLayout.Constraints
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
       public Spring getHeight() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
           if (height == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
               if (verticalHistory.contains(SOUTH)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                   height = difference(south, getY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
               } else if (verticalHistory.contains(VERTICAL_CENTER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                   height = scale(difference(verticalCenter, getY()), 2f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
               } else if (verticalHistory.contains(BASELINE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                   height = relativeBaselineToHeight(difference(baseline, getY()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
               }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
           }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
           return height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
       private void setEast(Spring east) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
           this.east = east;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
           pushConstraint(EAST, east, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
       private Spring getEast() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
           if (east == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
               east = sum(getX(), getWidth());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
           }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
           return east;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
       private void setSouth(Spring south) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
           this.south = south;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
           pushConstraint(SOUTH, south, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
       private Spring getSouth() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
           if (south == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
               south = sum(getY(), getHeight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
           }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
           return south;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        private Spring getHorizontalCenter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            if (horizontalCenter == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                horizontalCenter = sum(getX(), scale(getWidth(), 0.5f));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
            return horizontalCenter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        private void setHorizontalCenter(Spring horizontalCenter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            this.horizontalCenter = horizontalCenter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
            pushConstraint(HORIZONTAL_CENTER, horizontalCenter, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        private Spring getVerticalCenter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
            if (verticalCenter == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                verticalCenter = sum(getY(), scale(getHeight(), 0.5f));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            return verticalCenter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        private void setVerticalCenter(Spring verticalCenter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
            this.verticalCenter = verticalCenter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
            pushConstraint(VERTICAL_CENTER, verticalCenter, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        private Spring getBaseline() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
            if (baseline == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                baseline = sum(getY(), heightToRelativeBaseline(getHeight()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            return baseline;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        private void setBaseline(Spring baseline) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            this.baseline = baseline;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
            pushConstraint(BASELINE, baseline, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
       /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        * Sets the spring controlling the specified edge.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        * The edge must have one of the following values:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        * <code>SpringLayout.NORTH</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        * <code>SpringLayout.SOUTH</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        * <code>SpringLayout.EAST</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        * <code>SpringLayout.WEST</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        * <code>SpringLayout.HORIZONTAL_CENTER</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        * <code>SpringLayout.VERTICAL_CENTER</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        * <code>SpringLayout.BASELINE</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        * <code>SpringLayout.WIDTH</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        * <code>SpringLayout.HEIGHT</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        * For any other <code>String</code> value passed as the edge,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        * no action is taken. For a <code>null</code> edge, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        * <code>NullPointerException</code> is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        * @param edgeName the edge to be set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        * @param s the spring controlling the specified edge
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        * @throws NullPointerException if <code>edgeName</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        * @see #getConstraint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        * @see #NORTH
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        * @see #SOUTH
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        * @see #EAST
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        * @see #WEST
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        * @see #HORIZONTAL_CENTER
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        * @see #VERTICAL_CENTER
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        * @see #BASELINE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        * @see #WIDTH
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        * @see #HEIGHT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        * @see SpringLayout.Constraints
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
       public void setConstraint(String edgeName, Spring s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
           edgeName = edgeName.intern();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
           if (edgeName == WEST) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
               setX(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
           } else if (edgeName == NORTH) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
               setY(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
           } else if (edgeName == EAST) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
               setEast(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
           } else if (edgeName == SOUTH) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
               setSouth(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
           } else if (edgeName == HORIZONTAL_CENTER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
               setHorizontalCenter(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
           } else if (edgeName == WIDTH) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
               setWidth(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
           } else if (edgeName == HEIGHT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
               setHeight(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
           } else if (edgeName == VERTICAL_CENTER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
               setVerticalCenter(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
           } else if (edgeName == BASELINE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
               setBaseline(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
           }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
       /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        * Returns the value of the specified edge, which may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        * a derived value, or even <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        * The edge must have one of the following values:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        * <code>SpringLayout.NORTH</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        * <code>SpringLayout.SOUTH</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        * <code>SpringLayout.EAST</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        * <code>SpringLayout.WEST</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        * <code>SpringLayout.HORIZONTAL_CENTER</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        * <code>SpringLayout.VERTICAL_CENTER</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        * <code>SpringLayout.BASELINE</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        * <code>SpringLayout.WIDTH</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        * <code>SpringLayout.HEIGHT</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        * For any other <code>String</code> value passed as the edge,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        * <code>null</code> will be returned. Throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        * <code>NullPointerException</code> for a <code>null</code> edge.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        * @param edgeName the edge whose value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        *                 is to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        * @return the spring controlling the specified edge, may be <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        * @throws NullPointerException if <code>edgeName</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        * @see #setConstraint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        * @see #NORTH
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        * @see #SOUTH
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        * @see #EAST
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        * @see #WEST
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        * @see #HORIZONTAL_CENTER
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        * @see #VERTICAL_CENTER
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        * @see #BASELINE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        * @see #WIDTH
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        * @see #HEIGHT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        * @see SpringLayout.Constraints
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
       public Spring getConstraint(String edgeName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
           edgeName = edgeName.intern();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
           return (edgeName == WEST)  ? getX() :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
                   (edgeName == NORTH) ? getY() :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                   (edgeName == EAST)  ? getEast() :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                   (edgeName == SOUTH) ? getSouth() :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                   (edgeName == WIDTH)  ? getWidth() :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                   (edgeName == HEIGHT) ? getHeight() :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                   (edgeName == HORIZONTAL_CENTER) ? getHorizontalCenter() :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
                   (edgeName == VERTICAL_CENTER)  ? getVerticalCenter() :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
                   (edgeName == BASELINE) ? getBaseline() :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
                  null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
       /*pp*/ void reset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
           Spring[] allSprings = {x, y, width, height, east, south,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
               horizontalCenter, verticalCenter, baseline};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
           for (int i = 0; i < allSprings.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
               Spring s = allSprings[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
               if (s != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
                   s.setValue(Spring.UNSET);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
               }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
           }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
   private static class SpringProxy extends Spring {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
       private String edgeName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
       private Component c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
       private SpringLayout l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
       public SpringProxy(String edgeName, Component c, SpringLayout l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
           this.edgeName = edgeName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
           this.c = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
           this.l = l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
       private Spring getConstraint() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
           return l.getConstraints(c).getConstraint(edgeName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
       public int getMinimumValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
           return getConstraint().getMinimumValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
       public int getPreferredValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
           return getConstraint().getPreferredValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
       public int getMaximumValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
           return getConstraint().getMaximumValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
       public int getValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
           return getConstraint().getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
       public void setValue(int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
           getConstraint().setValue(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
       /*pp*/ boolean isCyclic(SpringLayout l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
           return l.isCyclic(getConstraint());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
       public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
           return "SpringProxy for " + edgeName + " edge of " + c.getName() + ".";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     * Constructs a new <code>SpringLayout</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
    public SpringLayout() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
    private void resetCyclicStatuses() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        cyclicSprings = new HashSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
        acyclicSprings = new HashSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
    private void setParent(Container p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        resetCyclicStatuses();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
        Constraints pc = getConstraints(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
        pc.setX(Spring.constant(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
        pc.setY(Spring.constant(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
        // The applyDefaults() method automatically adds width and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
        // height springs that delegate their calculations to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
        // getMinimumSize(), getPreferredSize() and getMaximumSize()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
        // methods of the relevant component. In the case of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
        // parent this will cause an infinite loop since these
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        // methods, in turn, delegate their calculations to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        // layout manager. Check for this case and replace the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
        // the springs that would cause this problem with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        // constant springs that supply default values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
        Spring width = pc.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
        if (width instanceof Spring.WidthSpring && ((Spring.WidthSpring)width).c == p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
            pc.setWidth(Spring.constant(0, 0, Integer.MAX_VALUE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        Spring height = pc.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        if (height instanceof Spring.HeightSpring && ((Spring.HeightSpring)height).c == p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
            pc.setHeight(Spring.constant(0, 0, Integer.MAX_VALUE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
    /*pp*/ boolean isCyclic(Spring s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        if (s == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
        if (cyclicSprings.contains(s)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
        if (acyclicSprings.contains(s)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        cyclicSprings.add(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
        boolean result = s.isCyclic(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
        if (!result) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
            acyclicSprings.add(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
            cyclicSprings.remove(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
            System.err.println(s + " is cyclic. ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
    private Spring abandonCycles(Spring s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
        return isCyclic(s) ? cyclicReference : s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
    // LayoutManager methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
     * Has no effect,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     * since this layout manager does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     * use a per-component string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
    public void addLayoutComponent(String name, Component c) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     * Removes the constraints associated with the specified component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
     * @param c the component being removed from the container
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
    public void removeLayoutComponent(Component c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
        componentConstraints.remove(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
    private static Dimension addInsets(int width, int height, Container p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
        Insets i = p.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
        return new Dimension(width + i.left + i.right, height + i.top + i.bottom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
    public Dimension minimumLayoutSize(Container parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
        setParent(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
        Constraints pc = getConstraints(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
        return addInsets(abandonCycles(pc.getWidth()).getMinimumValue(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
                         abandonCycles(pc.getHeight()).getMinimumValue(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
                         parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
    public Dimension preferredLayoutSize(Container parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
        setParent(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
        Constraints pc = getConstraints(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
        return addInsets(abandonCycles(pc.getWidth()).getPreferredValue(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
                         abandonCycles(pc.getHeight()).getPreferredValue(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
                         parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
    // LayoutManager2 methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
    public Dimension maximumLayoutSize(Container parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
        setParent(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
        Constraints pc = getConstraints(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
        return addInsets(abandonCycles(pc.getWidth()).getMaximumValue(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
                         abandonCycles(pc.getHeight()).getMaximumValue(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
                         parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
     * If <code>constraints</code> is an instance of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
     * <code>SpringLayout.Constraints</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
     * associates the constraints with the specified component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     * @param   component the component being added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
     * @param   constraints the component's constraints
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
     * @see SpringLayout.Constraints
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
    public void addLayoutComponent(Component component, Object constraints) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
        if (constraints instanceof Constraints) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
            putConstraints(component, (Constraints)constraints);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     * Returns 0.5f (centered).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
    public float getLayoutAlignmentX(Container p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
        return 0.5f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
     * Returns 0.5f (centered).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
    public float getLayoutAlignmentY(Container p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
        return 0.5f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
    public void invalidateLayout(Container p) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
    // End of LayoutManger2 methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
     * Links edge <code>e1</code> of component <code>c1</code> to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
     * edge <code>e2</code> of component <code>c2</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
     * with a fixed distance between the edges. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
     * constraint will cause the assignment
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     *     value(e1, c1) = value(e2, c2) + pad</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
     * to take place during all subsequent layout operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
     * @param   e1 the edge of the dependent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
     * @param   c1 the component of the dependent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
     * @param   pad the fixed distance between dependent and anchor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
     * @param   e2 the edge of the anchor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
     * @param   c2 the component of the anchor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
     * @see #putConstraint(String, Component, Spring, String, Component)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
    public void putConstraint(String e1, Component c1, int pad, String e2, Component c2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
        putConstraint(e1, c1, Spring.constant(pad), e2, c2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
     * Links edge <code>e1</code> of component <code>c1</code> to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
     * edge <code>e2</code> of component <code>c2</code>. As edge
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
     * <code>(e2, c2)</code> changes value, edge <code>(e1, c1)</code> will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
     * be calculated by taking the (spring) sum of <code>(e2, c2)</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
     * and <code>s</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
     * Each edge must have one of the following values:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
     * <code>SpringLayout.NORTH</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
     * <code>SpringLayout.SOUTH</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
     * <code>SpringLayout.EAST</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
     * <code>SpringLayout.WEST</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
     * <code>SpringLayout.VERTICAL_CENTER</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     * <code>SpringLayout.HORIZONTAL_CENTER</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     * <code>SpringLayout.BASELINE</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     * @param   e1 the edge of the dependent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     * @param   c1 the component of the dependent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     * @param   s the spring linking dependent and anchor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
     * @param   e2 the edge of the anchor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
     * @param   c2 the component of the anchor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
     * @see #putConstraint(String, Component, int, String, Component)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
     * @see #NORTH
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
     * @see #SOUTH
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     * @see #EAST
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
     * @see #WEST
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
     * @see #VERTICAL_CENTER
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
     * @see #HORIZONTAL_CENTER
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
     * @see #BASELINE
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
    public void putConstraint(String e1, Component c1, Spring s, String e2, Component c2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
        putConstraint(e1, c1, Spring.sum(s, getConstraint(e2, c2)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
    private void putConstraint(String e, Component c, Spring s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
        if (s != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
            getConstraints(c).setConstraint(e, s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
    private Constraints applyDefaults(Component c, Constraints constraints) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
        if (constraints == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
            constraints = new Constraints();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
        if (constraints.c == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
            constraints.c = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
        if (constraints.horizontalHistory.size() < 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
            applyDefaults(constraints, WEST, Spring.constant(0), WIDTH,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
                          Spring.width(c), constraints.horizontalHistory);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
        if (constraints.verticalHistory.size() < 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
            applyDefaults(constraints, NORTH, Spring.constant(0), HEIGHT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
                          Spring.height(c), constraints.verticalHistory);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
        return constraints;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
    private void applyDefaults(Constraints constraints, String name1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
                               Spring spring1, String name2, Spring spring2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
                               List<String> history) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
        if (history.size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
            constraints.setConstraint(name1, spring1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
            constraints.setConstraint(name2, spring2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
            // At this point there must be exactly one constraint defined already.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
            // Check width/height first.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
            if (constraints.getConstraint(name2) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
                constraints.setConstraint(name2, spring2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
                // If width/height is already defined, install a default for x/y.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
                constraints.setConstraint(name1, spring1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
            // Either way, leave the user's constraint topmost on the stack.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
            Collections.rotate(history, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
    private void putConstraints(Component component, Constraints constraints) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
        componentConstraints.put(component, applyDefaults(component, constraints));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
     * Returns the constraints for the specified component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
     * Note that,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
     * unlike the <code>GridBagLayout</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
     * <code>getConstraints</code> method,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
     * this method does not clone constraints.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
     * If no constraints
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
     * have been associated with this component,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
     * this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
     * returns a default constraints object positioned at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
     * 0,0 relative to the parent's Insets and its width/height
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
     * constrained to the minimum, maximum, and preferred sizes of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
     * component. The size characteristics
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
     * are not frozen at the time this method is called;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
     * instead this method returns a constraints object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
     * whose characteristics track the characteristics
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
     * of the component as they change.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
     * @param       c the component whose constraints will be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
     * @return      the constraints for the specified component
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
    public Constraints getConstraints(Component c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
       Constraints result = (Constraints)componentConstraints.get(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
       if (result == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
           if (c instanceof javax.swing.JComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
                Object cp = ((javax.swing.JComponent)c).getClientProperty(SpringLayout.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
                if (cp instanceof Constraints) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
                    return applyDefaults(c, (Constraints)cp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
            result = new Constraints();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
            putConstraints(c, result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
       return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
     * Returns the spring controlling the distance between
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
     * the specified edge of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
     * the component and the top or left edge of its parent. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
     * method, instead of returning the current binding for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
     * edge, returns a proxy that tracks the characteristics
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
     * of the edge even if the edge is subsequently rebound.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
     * Proxies are intended to be used in builder envonments
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
     * where it is useful to allow the user to define the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
     * constraints for a layout in any order. Proxies do, however,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
     * provide the means to create cyclic dependencies amongst
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
     * the constraints of a layout. Such cycles are detected
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
     * internally by <code>SpringLayout</code> so that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
     * the layout operation always terminates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
     * @param edgeName must be one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
     * <code>SpringLayout.NORTH</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
     * <code>SpringLayout.SOUTH</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
     * <code>SpringLayout.EAST</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
     * <code>SpringLayout.WEST</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
     * <code>SpringLayout.VERTICAL_CENTER</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
     * <code>SpringLayout.HORIZONTAL_CENTER</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
     * <code>SpringLayout.BASELINE</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
     * @param c the component whose edge spring is desired
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
     * @return a proxy for the spring controlling the distance between the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
     *         specified edge and the top or left edge of its parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
     * @see #NORTH
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
     * @see #SOUTH
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
     * @see #EAST
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
     * @see #WEST
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
     * @see #VERTICAL_CENTER
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
     * @see #HORIZONTAL_CENTER
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
     * @see #BASELINE
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
    public Spring getConstraint(String edgeName, Component c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
        // The interning here is unnecessary; it was added for efficiency.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
        edgeName = edgeName.intern();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
        return new SpringProxy(edgeName, c, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
    public void layoutContainer(Container parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
        setParent(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
        int n = parent.getComponentCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
        getConstraints(parent).reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
        for (int i = 0 ; i < n ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
            getConstraints(parent.getComponent(i)).reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
        Insets insets = parent.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
        Constraints pc = getConstraints(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
        abandonCycles(pc.getX()).setValue(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
        abandonCycles(pc.getY()).setValue(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
        abandonCycles(pc.getWidth()).setValue(parent.getWidth() -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
                                              insets.left - insets.right);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
        abandonCycles(pc.getHeight()).setValue(parent.getHeight() -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
                                               insets.top - insets.bottom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
        for (int i = 0 ; i < n ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
            Component c = parent.getComponent(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
            Constraints cc = getConstraints(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
            int x = abandonCycles(cc.getX()).getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
            int y = abandonCycles(cc.getY()).getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
            int width = abandonCycles(cc.getWidth()).getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
            int height = abandonCycles(cc.getHeight()).getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
            c.setBounds(insets.left + x, insets.top + y, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
}