2362N/A * Copyright (c) 2001, 2005, Oracle and/or its affiliates. All rights reserved. 0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 0N/A * This code is free software; you can redistribute it and/or modify it 0N/A * under the terms of the GNU General Public License version 2 only, as 2362N/A * published by the Free Software Foundation. Oracle designates this 0N/A * particular file as subject to the "Classpath" exception as provided 2362N/A * by Oracle in the LICENSE file that accompanied this code. 0N/A * This code is distributed in the hope that it will be useful, but WITHOUT 0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 0N/A * version 2 for more details (a copy is included in the LICENSE file that 0N/A * accompanied this code). 0N/A * You should have received a copy of the GNU General Public License version 0N/A * 2 along with this work; if not, write to the Free Software Foundation, 0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2362N/A * or visit www.oracle.com if you need additional information or have any 0N/A * An instance of the <code>Spring</code> class holds three properties that 0N/A * characterize its behavior: the <em>minimum</em>, <em>preferred</em>, and 0N/A * <em>maximum</em> values. Each of these properties may be involved in 0N/A * defining its fourth, <em>value</em>, property based on a series of rules. 0N/A * An instance of the <code>Spring</code> class can be visualized as a 0N/A * mechanical spring that provides a corrective force as the spring is compressed 0N/A * or stretched away from its preferred value. This force is modelled 0N/A * as linear function of the distance from the preferred value, but with 0N/A * two different constants -- one for the compressional force and one for the 0N/A * tensional one. Those constants are specified by the minimum and maximum 0N/A * values of the spring such that a spring at its minimum value produces an 0N/A * equal and opposite force to that which is created when it is at its 0N/A * maximum value. The difference between the <em>preferred</em> and 0N/A * <em>minimum</em> values, therefore, represents the ease with which the 0N/A * spring can be compressed and the difference between its <em>maximum</em> 0N/A * and <em>preferred</em> values, indicates the ease with which the 0N/A * <code>Spring</code> can be extended. 0N/A * See the {@link #sum} method for details. 0N/A * By defining simple arithmetic operations on <code>Spring</code>s, 0N/A * the behavior of a collection of <code>Spring</code>s 0N/A * can be reduced to that of an ordinary (non-compound) <code>Spring</code>. We define 0N/A * the "+", "-", <em>max</em>, and <em>min</em> operators on 0N/A * <code>Spring</code>s so that, in each case, the result is a <code>Spring</code> 0N/A * whose characteristics bear a useful mathematical relationship to its constituent 0N/A * A <code>Spring</code> can be treated as a pair of intervals 0N/A * with a single common point: the preferred value. 0N/A * The following rules define some of the 0N/A * arithmetic operators that can be applied to intervals 0N/A * (<code>[a, b]</code> refers to the interval 0N/A * from <code>a</code> 0N/A * to <code>b</code>, 0N/A * where <code>a <= b</code>). 0N/A * [a1, b1] + [a2, b2] = [a1 + a2, b1 + b2] 0N/A * -[a, b] = [-b, -a] 0N/A * max([a1, b1], [a2, b2]) = [max(a1, a2), max(b1, b2)] 0N/A * If we denote <code>Spring</code>s as <code>[a, b, c]</code>, 0N/A * where <code>a <= b <= c</code>, we can define the same 0N/A * arithmetic operators on <code>Spring</code>s: 0N/A * [a1, b1, c1] + [a2, b2, c2] = [a1 + a2, b1 + b2, c1 + c2] 0N/A * -[a, b, c] = [-c, -b, -a] 0N/A * max([a1, b1, c1], [a2, b2, c2]) = [max(a1, a2), max(b1, b2), max(c1, c2)] 0N/A * With both intervals and <code>Spring</code>s we can define "-" and <em>min</em> 0N/A * in terms of negation: 0N/A * min(X, Y) = -max(-X, -Y) 0N/A * For the static methods in this class that embody the arithmetic 0N/A * operators, we do not actually perform the operation in question as 0N/A * that would snapshot the values of the properties of the method's arguments 0N/A * at the time the static method is called. Instead, the static methods 0N/A * create a new <code>Spring</code> instance containing references to 0N/A * the method's arguments so that the characteristics of the new spring track the 0N/A * potentially changing characteristics of the springs from which it 0N/A * was made. This is a little like the idea of a <em>lazy value</em> 0N/A * in a functional language. 0N/A * If you are implementing a <code>SpringLayout</code> you 0N/A * can find further information and examples in 0N/A * a section in <em>The Java Tutorial.</em> 0N/A * <strong>Warning:</strong> 0N/A * Serialized objects of this class will not be compatible with 0N/A * future Swing releases. The current serialization support is 0N/A * appropriate for short term storage or RMI between applications running 0N/A * the same version of Swing. As of 1.4, support for long term storage 0N/A * of all JavaBeans<sup><font size="-2">TM</font></sup> 0N/A * has been added to the <code>java.beans</code> package. 0N/A * @see SpringLayout.Constraints 0N/A * @author Philip Milne 0N/A * An integer value signifying that a property value has not yet been calculated. 0N/A * Used by factory methods to create a <code>Spring</code>. 0N/A * @see #constant(int) 0N/A * @see #constant(int, int, int) 0N/A * @see SpringLayout.Constraints 0N/A * Returns the <em>minimum</em> value of this <code>Spring</code>. 0N/A * @return the <code>minimumValue</code> property of this <code>Spring</code> 0N/A * Returns the <em>preferred</em> value of this <code>Spring</code>. 0N/A * @return the <code>preferredValue</code> of this <code>Spring</code> 0N/A * Returns the <em>maximum</em> value of this <code>Spring</code>. 0N/A * @return the <code>maximumValue</code> property of this <code>Spring</code> 0N/A * Returns the current <em>value</em> of this <code>Spring</code>. 0N/A * @return the <code>value</code> property of this <code>Spring</code> 0N/A * Sets the current <em>value</em> of this <code>Spring</code> to <code>value</code>. 0N/A * @param value the new setting of the <code>value</code> property 0N/A// Note the use of max value rather than minimum value here. 0N/A// See the opening preamble on arithmetic with springs. 0N/A // No need to check for UNSET as 0N/A // Integer.MIN_VALUE == -Integer.MIN_VALUE. 0N/A // We will be doing arithmetic with the results of this call, 0N/A // so if a returned value is Integer.MAX_VALUE we will get 0N/A // arithmetic overflow. Truncate such values. 0N/A protected abstract int map(
int i);
0N/A protected abstract int inv(
int i);
0N/A// Use the instance variables of the StaticSpring superclass to 0N/A// cache values that have already been calculated. 0N/A return "CompoundSpring of " +
s1 +
" and " +
s2;
0N/A protected abstract int op(
int x,
int y);
0N/A protected int op(
int x,
int y) {
0N/A protected int op(
int x,
int y) {
0N/A * Returns a strut -- a spring whose <em>minimum</em>, <em>preferred</em>, and 0N/A * <em>maximum</em> values each have the value <code>pref</code>. 0N/A * @param pref the <em>minimum</em>, <em>preferred</em>, and 0N/A * <em>maximum</em> values of the new spring 0N/A * @return a spring whose <em>minimum</em>, <em>preferred</em>, and 0N/A * <em>maximum</em> values each have the value <code>pref</code> 0N/A * Returns a spring whose <em>minimum</em>, <em>preferred</em>, and 0N/A * <em>maximum</em> values have the values: <code>min</code>, <code>pref</code>, 0N/A * and <code>max</code> respectively. 0N/A * @param min the <em>minimum</em> value of the new spring 0N/A * @param pref the <em>preferred</em> value of the new spring 0N/A * @param max the <em>maximum</em> value of the new spring 0N/A * @return a spring whose <em>minimum</em>, <em>preferred</em>, and 0N/A * <em>maximum</em> values have the values: <code>min</code>, <code>pref</code>, 0N/A * and <code>max</code> respectively 0N/A * Returns <code>-s</code>: a spring running in the opposite direction to <code>s</code>. 0N/A * @return <code>-s</code>: a spring running in the opposite direction to <code>s</code> 0N/A * Returns <code>s1+s2</code>: a spring representing <code>s1</code> and <code>s2</code> 0N/A * in series. In a sum, <code>s3</code>, of two springs, <code>s1</code> and <code>s2</code>, 0N/A * the <em>strains</em> of <code>s1</code>, <code>s2</code>, and <code>s3</code> are maintained 0N/A * at the same level (to within the precision implied by their integer <em>value</em>s). 0N/A * The strain of a spring in compression is: 0N/A * and the strain of a spring in tension is: 0N/A * When <code>setValue</code> is called on the sum spring, <code>s3</code>, the strain 0N/A * in <code>s3</code> is calculated using one of the formulas above. Once the strain of 0N/A * the sum is known, the <em>value</em>s of <code>s1</code> and <code>s2</code> are 0N/A * then set so that they are have a strain equal to that of the sum. The formulas are 0N/A * evaluated so as to take rounding errors into account and ensure that the sum of 0N/A * the <em>value</em>s of <code>s1</code> and <code>s2</code> is exactly equal to 0N/A * the <em>value</em> of <code>s3</code>. 0N/A * @return <code>s1+s2</code>: a spring representing <code>s1</code> and <code>s2</code> in series 0N/A * Returns <code>max(s1, s2)</code>: a spring whose value is always greater than (or equal to) 0N/A * the values of both <code>s1</code> and <code>s2</code>. 0N/A * @return <code>max(s1, s2)</code>: a spring whose value is always greater than (or equal to) 0N/A * the values of both <code>s1</code> and <code>s2</code> 0N/A // Remove these, they're not used often and can be created using minus - 0N/A // as per these implementations. 0N/A public static Spring min(Spring s1, Spring s2) { 0N/A return minus(max(minus(s1), minus(s2))); 0N/A * Returns a spring whose <em>minimum</em>, <em>preferred</em>, <em>maximum</em> 0N/A * and <em>value</em> properties are each multiples of the properties of the 0N/A * argument spring, <code>s</code>. Minimum and maximum properties are 0N/A * swapped when <code>factor</code> is negative (in accordance with the 0N/A * rules of interval arithmetic). 0N/A * When factor is, for example, 0.5f the result represents 'the mid-point' 0N/A * of its input - an operation that is useful for centering components in 0N/A * @param s the spring to scale 0N/A * @param factor amount to scale by. 0N/A * @return a spring whose properties are those of the input spring <code>s</code> 0N/A * multiplied by <code>factor</code> 0N/A * @throws NullPointerException if <code>s</code> is null 0N/A * Returns a spring whose <em>minimum</em>, <em>preferred</em>, <em>maximum</em> 0N/A * and <em>value</em> properties are defined by the widths of the <em>minimumSize</em>, 0N/A * <em>preferredSize</em>, <em>maximumSize</em> and <em>size</em> properties 0N/A * of the supplied component. The returned spring is a 'wrapper' implementation 0N/A * whose methods call the appropriate size methods of the supplied component. 0N/A * The minimum, preferred, maximum and value properties of the returned spring 0N/A * therefore report the current state of the appropriate properties in the 0N/A * component and track them as they change. 0N/A * @param c Component used for calculating size 0N/A * @return a spring whose properties are defined by the horizontal component 0N/A * of the component's size methods. 0N/A * @throws NullPointerException if <code>c</code> is null 0N/A * Returns a spring whose <em>minimum</em>, <em>preferred</em>, <em>maximum</em> 0N/A * and <em>value</em> properties are defined by the heights of the <em>minimumSize</em>, 0N/A * <em>preferredSize</em>, <em>maximumSize</em> and <em>size</em> properties 0N/A * of the supplied component. The returned spring is a 'wrapper' implementation 0N/A * whose methods call the appropriate size methods of the supplied component. 0N/A * The minimum, preferred, maximum and value properties of the returned spring 0N/A * therefore report the current state of the appropriate properties in the 0N/A * component and track them as they change. 0N/A * @param c Component used for calculating size 0N/A * @return a spring whose properties are defined by the vertical component 0N/A * of the component's size methods. 0N/A * @throws NullPointerException if <code>c</code> is null 0N/A * If <code>s</code> is null, this throws an NullPointerException.