jdk/src/share/classes/javax/swing/Spring.java
author malenkov
Wed, 07 May 2008 23:20:32 +0400
changeset 466 6acd5ec503a8
parent 2 90ce3da70b43
child 5506 202f599c92aa
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-2005 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *  An instance of the <code>Spring</code> class holds three properties that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *  characterize its behavior: the <em>minimum</em>, <em>preferred</em>, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *  <em>maximum</em> values. Each of these properties may be involved in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *  defining its fourth, <em>value</em>, property based on a series of rules.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *  <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *  An instance of the <code>Spring</code> class can be visualized as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *  mechanical spring that provides a corrective force as the spring is compressed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *  or stretched away from its preferred value. This force is modelled
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *  as linear function of the distance from the preferred value, but with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *  two different constants -- one for the compressional force and one for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *  tensional one. Those constants are specified by the minimum and maximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *  values of the spring such that a spring at its minimum value produces an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *  equal and opposite force to that which is created when it is at its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *  maximum value. The difference between the <em>preferred</em> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *  <em>minimum</em> values, therefore, represents the ease with which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *  spring can be compressed and the difference between its <em>maximum</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *  and <em>preferred</em> values, indicates the ease with which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *  <code>Spring</code> can be extended.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *  See the {@link #sum} method for details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *  <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *  By defining simple arithmetic operations on <code>Spring</code>s,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *  the behavior of a collection of <code>Spring</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *  can be reduced to that of an ordinary (non-compound) <code>Spring</code>. We define
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *  the "+", "-", <em>max</em>, and <em>min</em> operators on
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *  <code>Spring</code>s so that, in each case, the result is a <code>Spring</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *  whose characteristics bear a useful mathematical relationship to its constituent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *  springs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *  <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *  A <code>Spring</code> can be treated as a pair of intervals
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *  with a single common point: the preferred value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *  The following rules define some of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *  arithmetic operators that can be applied to intervals
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *  (<code>[a, b]</code> refers to the interval
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *  from <code>a</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *  to <code>b</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *  where <code>a &lt;= b</code>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *  <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *  <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *          [a1, b1] + [a2, b2] = [a1 + a2, b1 + b2]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *                      -[a, b] = [-b, -a]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *      max([a1, b1], [a2, b2]) = [max(a1, a2), max(b1, b2)]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *  </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *  <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *  If we denote <code>Spring</code>s as <code>[a, b, c]</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *  where <code>a &lt;= b &lt;= c</code>, we can define the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *  arithmetic operators on <code>Spring</code>s:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *  <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *  <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *          [a1, b1, c1] + [a2, b2, c2] = [a1 + a2, b1 + b2, c1 + c2]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *                           -[a, b, c] = [-c, -b, -a]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *      max([a1, b1, c1], [a2, b2, c2]) = [max(a1, a2), max(b1, b2), max(c1, c2)]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *  </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *  <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *  With both intervals and <code>Spring</code>s we can define "-" and <em>min</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *  in terms of negation:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *  <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *  <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *      X - Y = X + (-Y)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *      min(X, Y) = -max(-X, -Y)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 *  </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 *  <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *  For the static methods in this class that embody the arithmetic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 *  operators, we do not actually perform the operation in question as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 *  that would snapshot the values of the properties of the method's arguments
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 *  at the time the static method is called. Instead, the static methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 *  create a new <code>Spring</code> instance containing references to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 *  the method's arguments so that the characteristics of the new spring track the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 *  potentially changing characteristics of the springs from which it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 *  was made. This is a little like the idea of a <em>lazy value</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 *  in a functional language.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * If you are implementing a <code>SpringLayout</code> you
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * can find further information and examples in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 href="http://java.sun.com/docs/books/tutorial/uiswing/layout/spring.html">How to Use SpringLayout</a>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * a section in <em>The Java Tutorial.</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * the same version of Swing.  As of 1.4, support for long term storage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * of all JavaBeans<sup><font size="-2">TM</font></sup>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * @see SpringLayout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * @see SpringLayout.Constraints
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * @author      Philip Milne
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * @since       1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
public abstract class Spring {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * An integer value signifying that a property value has not yet been calculated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    public static final int UNSET = Integer.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * Used by factory methods to create a <code>Spring</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @see #constant(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @see #constant(int, int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * @see #max
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @see #minus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @see #sum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @see SpringLayout.Constraints
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    protected Spring() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * Returns the <em>minimum</em> value of this <code>Spring</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @return the <code>minimumValue</code> property of this <code>Spring</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    public abstract int getMinimumValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * Returns the <em>preferred</em> value of this <code>Spring</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @return the <code>preferredValue</code> of this <code>Spring</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    public abstract int getPreferredValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * Returns the <em>maximum</em> value of this <code>Spring</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * @return the <code>maximumValue</code> property of this <code>Spring</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    public abstract int getMaximumValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * Returns the current <em>value</em> of this <code>Spring</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @return  the <code>value</code> property of this <code>Spring</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * @see #setValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    public abstract int getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * Sets the current <em>value</em> of this <code>Spring</code> to <code>value</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * @param   value the new setting of the <code>value</code> property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * @see #getValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    public abstract void setValue(int value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    private double range(boolean contract) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        return contract ? (getPreferredValue() - getMinimumValue()) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                          (getMaximumValue() - getPreferredValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    /*pp*/ double getStrain() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        double delta = (getValue() - getPreferredValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        return delta/range(getValue() < getPreferredValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    /*pp*/ void setStrain(double strain) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        setValue(getPreferredValue() + (int)(strain * range(strain < 0)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    /*pp*/ boolean isCyclic(SpringLayout l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    /*pp*/ static abstract class AbstractSpring extends Spring {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        protected int size = UNSET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        public int getValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            return size != UNSET ? size : getPreferredValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        public final void setValue(int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            if (this.size == size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            if (size == UNSET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                setNonClearValue(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        protected void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            size = UNSET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        protected void setNonClearValue(int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            this.size = size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    private static class StaticSpring extends AbstractSpring {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        protected int min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        protected int pref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        protected int max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        public StaticSpring(int pref) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            this(pref, pref, pref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        public StaticSpring(int min, int pref, int max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            this.min = min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            this.pref = pref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            this.max = max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
         public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
             return "StaticSpring [" + min + ", " + pref + ", " + max + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
         public int getMinimumValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            return min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        public int getPreferredValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            return pref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        public int getMaximumValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            return max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    private static class NegativeSpring extends Spring {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        private Spring s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        public NegativeSpring(Spring s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            this.s = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
// Note the use of max value rather than minimum value here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
// See the opening preamble on arithmetic with springs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        public int getMinimumValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            return -s.getMaximumValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        public int getPreferredValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            return -s.getPreferredValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        public int getMaximumValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            return -s.getMinimumValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        public int getValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            return -s.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        public void setValue(int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            // No need to check for UNSET as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            // Integer.MIN_VALUE == -Integer.MIN_VALUE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            s.setValue(-size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        /*pp*/ boolean isCyclic(SpringLayout l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            return s.isCyclic(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    private static class ScaleSpring extends Spring {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        private Spring s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        private float factor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        private ScaleSpring(Spring s, float factor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            this.s = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            this.factor = factor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        public int getMinimumValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            return Math.round((factor < 0 ? s.getMaximumValue() : s.getMinimumValue()) * factor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        public int getPreferredValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            return Math.round(s.getPreferredValue() * factor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        public int getMaximumValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            return Math.round((factor < 0 ? s.getMinimumValue() : s.getMaximumValue()) * factor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        public int getValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            return Math.round(s.getValue() * factor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        public void setValue(int value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            if (value == UNSET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                s.setValue(UNSET);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                s.setValue(Math.round(value / factor));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        /*pp*/ boolean isCyclic(SpringLayout l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            return s.isCyclic(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    /*pp*/ static class WidthSpring extends AbstractSpring {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        /*pp*/ Component c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        public WidthSpring(Component c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            this.c = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        public int getMinimumValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            return c.getMinimumSize().width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        public int getPreferredValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            return c.getPreferredSize().width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        public int getMaximumValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            // We will be doing arithmetic with the results of this call,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            // so if a returned value is Integer.MAX_VALUE we will get
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            // arithmetic overflow. Truncate such values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            return Math.min(Short.MAX_VALUE, c.getMaximumSize().width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     /*pp*/  static class HeightSpring extends AbstractSpring {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        /*pp*/ Component c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        public HeightSpring(Component c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            this.c = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        public int getMinimumValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            return c.getMinimumSize().height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        public int getPreferredValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            return c.getPreferredSize().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
        public int getMaximumValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            return Math.min(Short.MAX_VALUE, c.getMaximumSize().height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
   /*pp*/ static abstract class SpringMap extends Spring {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
       private Spring s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
       public SpringMap(Spring s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
           this.s = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
       protected abstract int map(int i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
       protected abstract int inv(int i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
       public int getMinimumValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
           return map(s.getMinimumValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
       public int getPreferredValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
           return map(s.getPreferredValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
       public int getMaximumValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
           return Math.min(Short.MAX_VALUE, map(s.getMaximumValue()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
       public int getValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
           return map(s.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
       public void setValue(int value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
           if (value == UNSET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
               s.setValue(UNSET);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
           } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
               s.setValue(inv(value));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
           }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
       /*pp*/ boolean isCyclic(SpringLayout l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
           return s.isCyclic(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
// Use the instance variables of the StaticSpring superclass to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
// cache values that have already been calculated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    /*pp*/ static abstract class CompoundSpring extends StaticSpring {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        protected Spring s1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        protected Spring s2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        public CompoundSpring(Spring s1, Spring s2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            super(UNSET);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            this.s1 = s1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            this.s2 = 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
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            return "CompoundSpring of " + s1 + " and " + s2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        protected void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            super.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            min = pref = max = UNSET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            s1.setValue(UNSET);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            s2.setValue(UNSET);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        protected abstract int op(int x, int y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        public int getMinimumValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            if (min == UNSET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                min = op(s1.getMinimumValue(), s2.getMinimumValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            return min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        public int getPreferredValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            if (pref == UNSET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                pref = op(s1.getPreferredValue(), s2.getPreferredValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            return pref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        public int getMaximumValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            if (max == UNSET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                max = op(s1.getMaximumValue(), s2.getMaximumValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            return max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        public int getValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            if (size == UNSET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                size = op(s1.getValue(), s2.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        /*pp*/ boolean isCyclic(SpringLayout l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            return l.isCyclic(s1) || l.isCyclic(s2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     private static class SumSpring extends CompoundSpring {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
         public SumSpring(Spring s1, Spring s2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
             super(s1, s2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
         protected int op(int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
             return x + y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
         protected void setNonClearValue(int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
             super.setNonClearValue(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
             s1.setStrain(this.getStrain());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
             s2.setValue(size - s1.getValue());
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
    private static class MaxSpring extends CompoundSpring {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        public MaxSpring(Spring s1, Spring s2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            super(s1, s2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        protected int op(int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            return Math.max(x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        protected void setNonClearValue(int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            super.setNonClearValue(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            s1.setValue(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            s2.setValue(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * Returns a strut -- a spring whose <em>minimum</em>, <em>preferred</em>, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * <em>maximum</em> values each have the value <code>pref</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * @param  pref the <em>minimum</em>, <em>preferred</em>, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     *         <em>maximum</em> values of the new spring
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * @return a spring whose <em>minimum</em>, <em>preferred</em>, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     *         <em>maximum</em> values each have the value <code>pref</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * @see Spring
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     public static Spring constant(int pref) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
         return constant(pref, pref, pref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * Returns a spring whose <em>minimum</em>, <em>preferred</em>, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * <em>maximum</em> values have the values: <code>min</code>, <code>pref</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * and <code>max</code> respectively.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * @param  min the <em>minimum</em> value of the new spring
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * @param  pref the <em>preferred</em> value of the new spring
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * @param  max the <em>maximum</em> value of the new spring
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * @return a spring whose <em>minimum</em>, <em>preferred</em>, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     *         <em>maximum</em> values have the values: <code>min</code>, <code>pref</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     *         and <code>max</code> respectively
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * @see Spring
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     public static Spring constant(int min, int pref, int max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
         return new StaticSpring(min, pref, max);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * Returns <code>-s</code>: a spring running in the opposite direction to <code>s</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * @return <code>-s</code>: a spring running in the opposite direction to <code>s</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * @see Spring
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    public static Spring minus(Spring s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        return new NegativeSpring(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * Returns <code>s1+s2</code>: a spring representing <code>s1</code> and <code>s2</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * in series. In a sum, <code>s3</code>, of two springs, <code>s1</code> and <code>s2</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * the <em>strains</em> of <code>s1</code>, <code>s2</code>, and <code>s3</code> are maintained
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * at the same level (to within the precision implied by their integer <em>value</em>s).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * The strain of a spring in compression is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     *         value - pref
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     *         ------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     *          pref - min
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * and the strain of a spring in tension is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     *         value - pref
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     *         ------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     *          max - pref
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * When <code>setValue</code> is called on the sum spring, <code>s3</code>, the strain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * in <code>s3</code> is calculated using one of the formulas above. Once the strain of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * the sum is known, the <em>value</em>s of <code>s1</code> and <code>s2</code> are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * then set so that they are have a strain equal to that of the sum. The formulas are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * evaluated so as to take rounding errors into account and ensure that the sum of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * the <em>value</em>s of <code>s1</code> and <code>s2</code> is exactly equal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * the <em>value</em> of <code>s3</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * @return <code>s1+s2</code>: a spring representing <code>s1</code> and <code>s2</code> in series
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * @see Spring
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     public static Spring sum(Spring s1, Spring s2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
         return new SumSpring(s1, s2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * Returns <code>max(s1, s2)</code>: a spring whose value is always greater than (or equal to)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     *         the values of both <code>s1</code> and <code>s2</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * @return <code>max(s1, s2)</code>: a spring whose value is always greater than (or equal to)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     *         the values of both <code>s1</code> and <code>s2</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * @see Spring
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    public static Spring max(Spring s1, Spring s2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        return new MaxSpring(s1, s2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    // Remove these, they're not used often and can be created using minus -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    // as per these implementations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    /*pp*/ static Spring difference(Spring s1, Spring s2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        return sum(s1, minus(s2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    public static Spring min(Spring s1, Spring s2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        return minus(max(minus(s1), minus(s2)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * Returns a spring whose <em>minimum</em>, <em>preferred</em>, <em>maximum</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * and <em>value</em> properties are each multiples of the properties of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * argument spring, <code>s</code>. Minimum and maximum properties are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * swapped when <code>factor</code> is negative (in accordance with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * rules of interval arithmetic).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * When factor is, for example, 0.5f the result represents 'the mid-point'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * of its input - an operation that is useful for centering components in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * a container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * @param s the spring to scale
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * @param factor amount to scale by.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * @return  a spring whose properties are those of the input spring <code>s</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * multiplied by <code>factor</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * @throws NullPointerException if <code>s</code> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    public static Spring scale(Spring s, float factor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        checkArg(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        return new ScaleSpring(s, factor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * Returns a spring whose <em>minimum</em>, <em>preferred</em>, <em>maximum</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * and <em>value</em> properties are defined by the widths of the <em>minimumSize</em>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * <em>preferredSize</em>, <em>maximumSize</em> and <em>size</em> properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * of the supplied component. The returned spring is a 'wrapper' implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * whose methods call the appropriate size methods of the supplied component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * The minimum, preferred, maximum and value properties of the returned spring
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * therefore report the current state of the appropriate properties in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * component and track them as they change.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * @param c Component used for calculating size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * @return  a spring whose properties are defined by the horizontal component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * of the component's size methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * @throws NullPointerException if <code>c</code> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    public static Spring width(Component c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        checkArg(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        return new WidthSpring(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * Returns a spring whose <em>minimum</em>, <em>preferred</em>, <em>maximum</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * and <em>value</em> properties are defined by the heights of the <em>minimumSize</em>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * <em>preferredSize</em>, <em>maximumSize</em> and <em>size</em> properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * of the supplied component. The returned spring is a 'wrapper' implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * whose methods call the appropriate size methods of the supplied component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * The minimum, preferred, maximum and value properties of the returned spring
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * therefore report the current state of the appropriate properties in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * component and track them as they change.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * @param c Component used for calculating size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * @return  a spring whose properties are defined by the vertical component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * of the component's size methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * @throws NullPointerException if <code>c</code> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
    public static Spring height(Component c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        checkArg(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        return new HeightSpring(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * If <code>s</code> is null, this throws an NullPointerException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
    private static void checkArg(Object s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        if (s == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            throw new NullPointerException("Argument must not be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
}