jdk/src/java.desktop/share/classes/javax/swing/Spring.java
author aghaisas
Mon, 10 Jul 2017 14:55:29 +0530
changeset 47151 362dcbee0613
parent 25859 3317bb8137f4
permissions -rw-r--r--
6919529: NPE from MultiUIDefaults.getUIError Reviewed-by: aghaisas, psadhukhan, serb Contributed-by: shashidhara.veerabhadraiah@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
25386
9ef80c24fd74 8046590: fix doclint issues in swing classes, part 1 of 4
yan
parents: 23010
diff changeset
     2
 * Copyright (c) 2001, 2014, Oracle and/or its affiliates. All rights reserved.
2
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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
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>).
21982
fd6e5fe509df 8029264: [doclint] more doclint and tidy cleanup
yan
parents: 20458
diff changeset
    68
 *
2
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:
21982
fd6e5fe509df 8029264: [doclint] more doclint and tidy cleanup
yan
parents: 20458
diff changeset
    81
 *
2
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:
21982
fd6e5fe509df 8029264: [doclint] more doclint and tidy cleanup
yan
parents: 20458
diff changeset
    92
 *
2
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
20455
f6f9a0c2796b 8020688: Broken links in documentation at http://docs.oracle.com/javase/6/docs/api/index.
mcherkas
parents: 5506
diff changeset
   112
 href="http://docs.oracle.com/javase/tutorial/uiswing/layout/spring.html">How to Use SpringLayout</a>,
2
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
20458
f2423fb3fd19 8025840: Fix all the doclint warnings about trademark
cl
parents: 20455
diff changeset
   120
 * of all JavaBeans&trade;
2
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
 */
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 21982
diff changeset
   130
@SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
public abstract class Spring {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * An integer value signifying that a property value has not yet been calculated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    public static final int UNSET = Integer.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * Used by factory methods to create a <code>Spring</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @see #constant(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * @see #constant(int, int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @see #max
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @see #minus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @see #sum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @see SpringLayout.Constraints
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    protected Spring() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * Returns the <em>minimum</em> value of this <code>Spring</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @return the <code>minimumValue</code> property of this <code>Spring</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    public abstract int getMinimumValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * Returns the <em>preferred</em> value of this <code>Spring</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @return the <code>preferredValue</code> of this <code>Spring</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    public abstract int getPreferredValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * Returns the <em>maximum</em> value of this <code>Spring</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @return the <code>maximumValue</code> property of this <code>Spring</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    public abstract int getMaximumValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * Returns the current <em>value</em> of this <code>Spring</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @return  the <code>value</code> property of this <code>Spring</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @see #setValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    public abstract int getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * Sets the current <em>value</em> of this <code>Spring</code> to <code>value</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @param   value the new setting of the <code>value</code> property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * @see #getValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    public abstract void setValue(int value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    private double range(boolean contract) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        return contract ? (getPreferredValue() - getMinimumValue()) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                          (getMaximumValue() - getPreferredValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    /*pp*/ double getStrain() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        double delta = (getValue() - getPreferredValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        return delta/range(getValue() < getPreferredValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    /*pp*/ void setStrain(double strain) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        setValue(getPreferredValue() + (int)(strain * range(strain < 0)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    /*pp*/ boolean isCyclic(SpringLayout l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    /*pp*/ static abstract class AbstractSpring extends Spring {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        protected int size = UNSET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        public int getValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            return size != UNSET ? size : getPreferredValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        public final void setValue(int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            if (this.size == size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            if (size == UNSET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                setNonClearValue(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        protected void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            size = UNSET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        protected void setNonClearValue(int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            this.size = size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    private static class StaticSpring extends AbstractSpring {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        protected int min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        protected int pref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        protected int max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        public StaticSpring(int pref) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            this(pref, pref, pref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        public StaticSpring(int min, int pref, int max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            this.min = min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            this.pref = pref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            this.max = max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
         public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
             return "StaticSpring [" + min + ", " + pref + ", " + max + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
         public int getMinimumValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            return min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        public int getPreferredValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            return pref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        public int getMaximumValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            return max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    private static class NegativeSpring extends Spring {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        private Spring s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        public NegativeSpring(Spring s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            this.s = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
// Note the use of max value rather than minimum value here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
// See the opening preamble on arithmetic with springs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        public int getMinimumValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            return -s.getMaximumValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        public int getPreferredValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            return -s.getPreferredValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        public int getMaximumValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            return -s.getMinimumValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        public int getValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            return -s.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        public void setValue(int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            // No need to check for UNSET as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            // Integer.MIN_VALUE == -Integer.MIN_VALUE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            s.setValue(-size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        /*pp*/ boolean isCyclic(SpringLayout l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            return s.isCyclic(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    private static class ScaleSpring extends Spring {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        private Spring s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        private float factor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        private ScaleSpring(Spring s, float factor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            this.s = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            this.factor = factor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        public int getMinimumValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            return Math.round((factor < 0 ? s.getMaximumValue() : s.getMinimumValue()) * factor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        public int getPreferredValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            return Math.round(s.getPreferredValue() * factor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        public int getMaximumValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            return Math.round((factor < 0 ? s.getMinimumValue() : s.getMaximumValue()) * factor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        public int getValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            return Math.round(s.getValue() * factor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        public void setValue(int value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            if (value == UNSET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                s.setValue(UNSET);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                s.setValue(Math.round(value / factor));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        /*pp*/ boolean isCyclic(SpringLayout l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            return s.isCyclic(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    /*pp*/ static class WidthSpring extends AbstractSpring {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        /*pp*/ Component c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        public WidthSpring(Component c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            this.c = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        public int getMinimumValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            return c.getMinimumSize().width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        public int getPreferredValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            return c.getPreferredSize().width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        public int getMaximumValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            // We will be doing arithmetic with the results of this call,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            // so if a returned value is Integer.MAX_VALUE we will get
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            // arithmetic overflow. Truncate such values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            return Math.min(Short.MAX_VALUE, c.getMaximumSize().width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     /*pp*/  static class HeightSpring extends AbstractSpring {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        /*pp*/ Component c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        public HeightSpring(Component c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            this.c = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        public int getMinimumValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            return c.getMinimumSize().height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        public int getPreferredValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            return c.getPreferredSize().height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        public int getMaximumValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            return Math.min(Short.MAX_VALUE, c.getMaximumSize().height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
   /*pp*/ static abstract class SpringMap extends Spring {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
       private Spring s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
       public SpringMap(Spring s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
           this.s = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
       protected abstract int map(int i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
       protected abstract int inv(int i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
       public int getMinimumValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
           return map(s.getMinimumValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
       public int getPreferredValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
           return map(s.getPreferredValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
       public int getMaximumValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
           return Math.min(Short.MAX_VALUE, map(s.getMaximumValue()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
       public int getValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
           return map(s.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
       public void setValue(int value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
           if (value == UNSET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
               s.setValue(UNSET);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
           } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
               s.setValue(inv(value));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
           }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
       /*pp*/ boolean isCyclic(SpringLayout l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
           return s.isCyclic(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
// Use the instance variables of the StaticSpring superclass to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
// cache values that have already been calculated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    /*pp*/ static abstract class CompoundSpring extends StaticSpring {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        protected Spring s1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        protected Spring s2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        public CompoundSpring(Spring s1, Spring s2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            super(UNSET);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            this.s1 = s1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            this.s2 = s2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            return "CompoundSpring of " + s1 + " and " + s2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        protected void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            super.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            min = pref = max = UNSET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            s1.setValue(UNSET);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            s2.setValue(UNSET);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        protected abstract int op(int x, int y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        public int getMinimumValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            if (min == UNSET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                min = op(s1.getMinimumValue(), s2.getMinimumValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            return min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        public int getPreferredValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            if (pref == UNSET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                pref = op(s1.getPreferredValue(), s2.getPreferredValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            return pref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        public int getMaximumValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            if (max == UNSET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                max = op(s1.getMaximumValue(), s2.getMaximumValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            return max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        public int getValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            if (size == UNSET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                size = op(s1.getValue(), s2.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        /*pp*/ boolean isCyclic(SpringLayout l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            return l.isCyclic(s1) || l.isCyclic(s2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     private static class SumSpring extends CompoundSpring {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
         public SumSpring(Spring s1, Spring s2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
             super(s1, s2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
         protected int op(int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
             return x + y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
         protected void setNonClearValue(int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
             super.setNonClearValue(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
             s1.setStrain(this.getStrain());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
             s2.setValue(size - s1.getValue());
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 static class MaxSpring extends CompoundSpring {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        public MaxSpring(Spring s1, Spring s2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            super(s1, s2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        protected int op(int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            return Math.max(x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        protected void setNonClearValue(int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            super.setNonClearValue(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            s1.setValue(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            s2.setValue(size);
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * Returns a strut -- a spring whose <em>minimum</em>, <em>preferred</em>, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * <em>maximum</em> values each have the value <code>pref</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * @param  pref the <em>minimum</em>, <em>preferred</em>, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     *         <em>maximum</em> values of the new spring
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * @return a spring whose <em>minimum</em>, <em>preferred</em>, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     *         <em>maximum</em> values each have the value <code>pref</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * @see Spring
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     public static Spring constant(int pref) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
         return constant(pref, pref, pref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * Returns a spring whose <em>minimum</em>, <em>preferred</em>, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * <em>maximum</em> values have the values: <code>min</code>, <code>pref</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * and <code>max</code> respectively.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * @param  min the <em>minimum</em> value of the new spring
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * @param  pref the <em>preferred</em> value of the new spring
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * @param  max the <em>maximum</em> value of the new spring
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * @return a spring whose <em>minimum</em>, <em>preferred</em>, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     *         <em>maximum</em> values have the values: <code>min</code>, <code>pref</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     *         and <code>max</code> respectively
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * @see Spring
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     public static Spring constant(int min, int pref, int max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
         return new StaticSpring(min, pref, max);
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
    /**
25386
9ef80c24fd74 8046590: fix doclint issues in swing classes, part 1 of 4
yan
parents: 23010
diff changeset
   550
     * Returns {@code -s}: a spring running in the opposite direction to {@code s}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     *
25386
9ef80c24fd74 8046590: fix doclint issues in swing classes, part 1 of 4
yan
parents: 23010
diff changeset
   552
     * @param s a {@code Spring} object
9ef80c24fd74 8046590: fix doclint issues in swing classes, part 1 of 4
yan
parents: 23010
diff changeset
   553
     * @return {@code -s}: a spring running in the opposite direction to {@code s}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * @see Spring
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    public static Spring minus(Spring s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        return new NegativeSpring(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * Returns <code>s1+s2</code>: a spring representing <code>s1</code> and <code>s2</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * 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
   564
     * the <em>strains</em> of <code>s1</code>, <code>s2</code>, and <code>s3</code> are maintained
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * at the same level (to within the precision implied by their integer <em>value</em>s).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * The strain of a spring in compression is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     *         value - pref
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     *         ------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     *          pref - min
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * and the strain of a spring in tension is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     *         value - pref
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     *         ------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     *          max - pref
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * When <code>setValue</code> is called on the sum spring, <code>s3</code>, the strain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * in <code>s3</code> is calculated using one of the formulas above. Once the strain of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * 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
   581
     * 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
   582
     * evaluated so as to take rounding errors into account and ensure that the sum of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * the <em>value</em>s of <code>s1</code> and <code>s2</code> is exactly equal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * the <em>value</em> of <code>s3</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     *
25386
9ef80c24fd74 8046590: fix doclint issues in swing classes, part 1 of 4
yan
parents: 23010
diff changeset
   586
     * @param s1 a {@code Spring} object
9ef80c24fd74 8046590: fix doclint issues in swing classes, part 1 of 4
yan
parents: 23010
diff changeset
   587
     * @param s2 a {@code Spring} object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * @return <code>s1+s2</code>: a spring representing <code>s1</code> and <code>s2</code> in series
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * @see Spring
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     public static Spring sum(Spring s1, Spring s2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
         return new SumSpring(s1, s2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    /**
25386
9ef80c24fd74 8046590: fix doclint issues in swing classes, part 1 of 4
yan
parents: 23010
diff changeset
   597
     * Returns {@code max(s1, s2)}: a spring whose value is always greater than (or equal to)
9ef80c24fd74 8046590: fix doclint issues in swing classes, part 1 of 4
yan
parents: 23010
diff changeset
   598
     *         the values of both {@code s1} and {@code s2}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     *
25386
9ef80c24fd74 8046590: fix doclint issues in swing classes, part 1 of 4
yan
parents: 23010
diff changeset
   600
     * @param s1 a {@code Spring} object
9ef80c24fd74 8046590: fix doclint issues in swing classes, part 1 of 4
yan
parents: 23010
diff changeset
   601
     * @param s2 a {@code Spring} object
9ef80c24fd74 8046590: fix doclint issues in swing classes, part 1 of 4
yan
parents: 23010
diff changeset
   602
     * @return {@code max(s1, s2)}: a spring whose value is always greater than (or equal to)
9ef80c24fd74 8046590: fix doclint issues in swing classes, part 1 of 4
yan
parents: 23010
diff changeset
   603
     *         the values of both {@code s1} and {@code s2}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * @see Spring
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    public static Spring max(Spring s1, Spring s2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        return new MaxSpring(s1, s2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    // Remove these, they're not used often and can be created using minus -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    // as per these implementations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    /*pp*/ static Spring difference(Spring s1, Spring s2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        return sum(s1, minus(s2));
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
    public static Spring min(Spring s1, Spring s2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        return minus(max(minus(s1), minus(s2)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * Returns a spring whose <em>minimum</em>, <em>preferred</em>, <em>maximum</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * and <em>value</em> properties are each multiples of the properties of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * argument spring, <code>s</code>. Minimum and maximum properties are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * swapped when <code>factor</code> is negative (in accordance with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * rules of interval arithmetic).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * When factor is, for example, 0.5f the result represents 'the mid-point'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * of its input - an operation that is useful for centering components in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * a container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * @param s the spring to scale
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * @param factor amount to scale by.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * @return  a spring whose properties are those of the input spring <code>s</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * multiplied by <code>factor</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * @throws NullPointerException if <code>s</code> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
    public static Spring scale(Spring s, float factor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        checkArg(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        return new ScaleSpring(s, factor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * Returns a spring whose <em>minimum</em>, <em>preferred</em>, <em>maximum</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * and <em>value</em> properties are defined by the widths of the <em>minimumSize</em>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * <em>preferredSize</em>, <em>maximumSize</em> and <em>size</em> properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * of the supplied component. The returned spring is a 'wrapper' implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * whose methods call the appropriate size methods of the supplied component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * The minimum, preferred, maximum and value properties of the returned spring
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * therefore report the current state of the appropriate properties in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * component and track them as they change.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * @param c Component used for calculating size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * @return  a spring whose properties are defined by the horizontal component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * of the component's size methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * @throws NullPointerException if <code>c</code> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    public static Spring width(Component c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        checkArg(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        return new WidthSpring(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * Returns a spring whose <em>minimum</em>, <em>preferred</em>, <em>maximum</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * and <em>value</em> properties are defined by the heights of the <em>minimumSize</em>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * <em>preferredSize</em>, <em>maximumSize</em> and <em>size</em> properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * of the supplied component. The returned spring is a 'wrapper' implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * whose methods call the appropriate size methods of the supplied component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * The minimum, preferred, maximum and value properties of the returned spring
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * therefore report the current state of the appropriate properties in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * component and track them as they change.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     * @param c Component used for calculating size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * @return  a spring whose properties are defined by the vertical component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * of the component's size methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     * @throws NullPointerException if <code>c</code> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    public static Spring height(Component c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        checkArg(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        return new HeightSpring(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * If <code>s</code> is null, this throws an NullPointerException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
    private static void checkArg(Object s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        if (s == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            throw new NullPointerException("Argument must not be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
}