KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > Spring


1 /*
2  * @(#)Spring.java 1.9 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package javax.swing;
8
9 import java.awt.Component JavaDoc;
10 import java.util.*;
11
12 /**
13  * An instance of the <code>Spring</code> class holds three properties that
14  * characterize its behavior: the <em>minimum</em>, <em>preferred</em>, and
15  * <em>maximum</em> values. Each of these properties may be involved in
16  * defining its fourth, <em>value</em>, property based on a series of rules.
17  * <p>
18  * An instance of the <code>Spring</code> class can be visualized as a
19  * mechanical spring that provides a corrective force as the spring is compressed
20  * or stretched away from its preferred value. This force is modelled
21  * as linear function of the distance from the preferred value, but with
22  * two different constants -- one for the compressional force and one for the
23  * tensional one. Those constants are specified by the minimum and maximum
24  * values of the spring such that a spring at its minimum value produces an
25  * equal and opposite force to that which is created when it is at its
26  * maximum value. The difference between the <em>preferred</em> and
27  * <em>minimum</em> values, therefore, represents the ease with which the
28  * spring can be compressed and the difference between its <em>maximum</em>
29  * and <em>preferred</em> values, indicates the ease with which the
30  * <code>Spring</code> can be extended.
31  * See the {@link #sum} method for details.
32  *
33  * <p>
34  * By defining simple arithmetic operations on <code>Spring</code>s,
35  * the behavior of a collection of <code>Spring</code>s
36  * can be reduced to that of an ordinary (non-compound) <code>Spring</code>. We define
37  * the "+", "-", <em>max</em>, and <em>min</em> operators on
38  * <code>Spring</code>s so that, in each case, the result is a <code>Spring</code>
39  * whose characteristics bear a useful mathematical relationship to its constituent
40  * springs.
41  *
42  * <p>
43  * A <code>Spring</code> can be treated as a pair of intervals
44  * with a single common point: the preferred value.
45  * The following rules define some of the
46  * arithmetic operators that can be applied to intervals
47  * (<code>[a, b]</code> refers to the interval
48  * from <code>a</code>
49  * to <code>b</code>,
50  * where <code>a &lt;= b</code>).
51  * <p>
52  * <pre>
53  * [a1, b1] + [a2, b2] = [a1 + a2, b1 + b2]
54  *
55  * -[a, b] = [-b, -a]
56  *
57  * max([a1, b1], [a2, b2]) = [max(a1, a2), max(b1, b2)]
58  * </pre>
59  * <p>
60  *
61  * If we denote <code>Spring</code>s as <code>[a, b, c]</code>,
62  * where <code>a &lt;= b &lt;= c</code>, we can define the same
63  * arithmetic operators on <code>Spring</code>s:
64  * <p>
65  * <pre>
66  * [a1, b1, c1] + [a2, b2, c2] = [a1 + a2, b1 + b2, c1 + c2]
67  *
68  * -[a, b, c] = [-c, -b, -a]
69  *
70  * max([a1, b1, c1], [a2, b2, c2]) = [max(a1, a2), max(b1, b2), max(c1, c2)]
71  * </pre>
72  * <p>
73  * With both intervals and <code>Spring</code>s we can define "-" and <em>min</em>
74  * in terms of negation:
75  * <p>
76  * <pre>
77  * X - Y = X + (-Y)
78  *
79  * min(X, Y) = -max(-X, -Y)
80  * </pre>
81  * <p>
82  * For the static methods in this class that embody the arithmetic
83  * operators, we do not actually perform the operation in question as
84  * that would snapshot the values of the properties of the method's arguments
85  * at the time the static method is called. Instead, the static methods
86  * create a new <code>Spring</code> instance containing references to
87  * the method's arguments so that the characteristics of the new spring track the
88  * potentially changing characteristics of the springs from which it
89  * was made. This is a little like the idea of a <em>lazy value</em>
90  * in a functional language.
91  * <p>
92  * If you are implementing a <code>SpringLayout</code> you
93  * can find further information and examples in
94  * <a
95  href="http://java.sun.com/docs/books/tutorial/uiswing/layout/spring.html">How to Use SpringLayout</a>,
96  * a section in <em>The Java Tutorial.</em>
97  * <p>
98  * <strong>Warning:</strong>
99  * Serialized objects of this class will not be compatible with
100  * future Swing releases. The current serialization support is
101  * appropriate for short term storage or RMI between applications running
102  * the same version of Swing. As of 1.4, support for long term storage
103  * of all JavaBeans<sup><font size="-2">TM</font></sup>
104  * has been added to the <code>java.beans</code> package.
105  * Please see {@link java.beans.XMLEncoder}.
106  *
107  * @see SpringLayout
108  * @see SpringLayout.Constraints
109  *
110  * @version 1.9 12/19/03
111  * @author Philip Milne
112  * @since 1.4
113  */

114 public abstract class Spring {
115
116     /**
117      * An integer value signifying that a property value has not yet been calculated.
118      */

119     public static final int UNSET = Integer.MIN_VALUE;
120
121     /**
122      * Used by factory methods to create a <code>Spring</code>.
123      *
124      * @see #constant(int)
125      * @see #constant(int, int, int)
126      * @see #max
127      * @see #minus
128      * @see #sum
129      * @see SpringLayout.Constraints
130      */

131     protected Spring() {}
132
133     /**
134      * Returns the <em>minimum</em> value of this <code>Spring</code>.
135      *
136      * @return the <code>minimumValue</code> property of this <code>Spring</code>
137      */

138     public abstract int getMinimumValue();
139
140     /**
141      * Returns the <em>preferred</em> value of this <code>Spring</code>.
142      *
143      * @return the <code>preferredValue</code> of this <code>Spring</code>
144      */

145     public abstract int getPreferredValue();
146
147     /**
148      * Returns the <em>maximum</em> value of this <code>Spring</code>.
149      *
150      * @return the <code>maximumValue</code> property of this <code>Spring</code>
151      */

152     public abstract int getMaximumValue();
153
154     /**
155      * Returns the current <em>value</em> of this <code>Spring</code>.
156      *
157      * @return the <code>value</code> property of this <code>Spring</code>
158      *
159      * @see #setValue
160      */

161     public abstract int getValue();
162
163     /**
164      * Sets the current <em>value</em> of this <code>Spring</code> to <code>value</code>.
165      *
166      * @param value the new setting of the <code>value</code> property
167      *
168      * @see #getValue
169      */

170     public abstract void setValue(int value);
171
172     private double range(boolean contract) {
173         return contract ? (getPreferredValue() - getMinimumValue()) :
174                           (getMaximumValue() - getPreferredValue());
175     }
176
177     /*pp*/ double getStrain() {
178         double delta = (getValue() - getPreferredValue());
179         return delta/range(getValue() < getPreferredValue());
180     }
181
182     /*pp*/ void setStrain(double strain) {
183         setValue(getPreferredValue() + (int)(strain * range(strain < 0)));
184     }
185
186     /*pp*/ boolean isCyclic(SpringLayout JavaDoc l) {
187         return false;
188     }
189
190     /*pp*/ static abstract class AbstractSpring extends Spring JavaDoc {
191         protected int size = UNSET;
192
193         public int getValue() {
194             return size != UNSET ? size : getPreferredValue();
195         }
196
197         public void setValue(int size) {
198             if (size == UNSET) {
199                 clear();
200                 return;
201             }
202             this.size = size;
203         }
204
205         protected void clear() {
206             size = UNSET;
207         }
208     }
209
210     private static class StaticSpring extends AbstractSpring {
211         protected int min;
212         protected int pref;
213         protected int max;
214
215         public StaticSpring() {}
216
217         public StaticSpring(int pref) {
218             this(pref, pref, pref);
219         }
220
221         public StaticSpring(int min, int pref, int max) {
222             this.min = min;
223             this.pref = pref;
224             this.max = max;
225             this.size = pref;
226         }
227
228          public String JavaDoc toString() {
229              return "StaticSpring [" + min + ", " + pref + ", " + max + "]";
230          }
231
232          public int getMinimumValue() {
233             return min;
234         }
235
236         public int getPreferredValue() {
237             return pref;
238         }
239
240         public int getMaximumValue() {
241             return max;
242         }
243     }
244
245     private static class NegativeSpring extends Spring JavaDoc {
246         private Spring JavaDoc s;
247
248         public NegativeSpring(Spring JavaDoc s) {
249             this.s = s;
250         }
251
252 // Note the use of max value rather than minimum value here.
253
// See the opening preamble on arithmetic with springs.
254

255         public int getMinimumValue() {
256             return -s.getMaximumValue();
257         }
258
259         public int getPreferredValue() {
260             return -s.getPreferredValue();
261         }
262
263         public int getMaximumValue() {
264             return -s.getMinimumValue();
265         }
266
267         public int getValue() {
268             return -s.getValue();
269         }
270
271         public void setValue(int size) {
272             // No need to check for UNSET as
273
// Integer.MIN_VALUE == -Integer.MIN_VALUE.
274
s.setValue(-size);
275         }
276
277         /*pp*/ boolean isCyclic(SpringLayout JavaDoc l) {
278             return s.isCyclic(l);
279         }
280     }
281
282     private static class ScaleSpring extends Spring JavaDoc {
283         private Spring JavaDoc s;
284         private float factor;
285
286         private ScaleSpring(Spring JavaDoc s, float factor) {
287             this.s = s;
288             this.factor = factor;
289         }
290
291         public int getMinimumValue() {
292             return Math.round((factor < 0 ? s.getMaximumValue() : s.getMinimumValue()) * factor);
293         }
294
295         public int getPreferredValue() {
296             return Math.round(s.getPreferredValue() * factor);
297         }
298
299         public int getMaximumValue() {
300             return Math.round((factor < 0 ? s.getMinimumValue() : s.getMaximumValue()) * factor);
301         }
302
303         public int getValue() {
304             return Math.round(s.getValue() * factor);
305         }
306
307         public void setValue(int value) {
308             if (value == UNSET) {
309                 s.setValue(UNSET);
310             } else {
311                 s.setValue(Math.round(value / factor));
312             }
313         }
314
315         /*pp*/ boolean isCyclic(SpringLayout JavaDoc l) {
316             return s.isCyclic(l);
317         }
318     }
319
320     /*pp*/ static class WidthSpring extends AbstractSpring {
321         /*pp*/ Component JavaDoc c;
322
323         public WidthSpring(Component JavaDoc c) {
324             this.c = c;
325         }
326
327         public int getMinimumValue() {
328             return c.getMinimumSize().width;
329         }
330
331         public int getPreferredValue() {
332             return c.getPreferredSize().width;
333         }
334
335         public int getMaximumValue() {
336             // We will be doing arithmetic with the results of this call,
337
// so if a returned value is Integer.MAX_VALUE we will get
338
// arithmetic overflow. Truncate such values.
339
return Math.min(Short.MAX_VALUE, c.getMaximumSize().width);
340         }
341     }
342
343      /*pp*/ static class HeightSpring extends AbstractSpring {
344         /*pp*/ Component JavaDoc c;
345
346         public HeightSpring(Component JavaDoc c) {
347             this.c = c;
348         }
349
350         public int getMinimumValue() {
351             return c.getMinimumSize().height;
352         }
353
354         public int getPreferredValue() {
355             return c.getPreferredSize().height;
356         }
357
358         public int getMaximumValue() {
359             return Math.min(Short.MAX_VALUE, c.getMaximumSize().height);
360         }
361     }
362
363 // Use the instance variables of the StaticSpring superclass to
364
// cache values that have already been calculated.
365
/*pp*/ static abstract class CompoundSpring extends StaticSpring {
366         protected Spring JavaDoc s1;
367         protected Spring JavaDoc s2;
368
369         public CompoundSpring(Spring JavaDoc s1, Spring JavaDoc s2) {
370             clear();
371             this.s1 = s1;
372             this.s2 = s2;
373         }
374
375         public String JavaDoc toString() {
376             return "CompoundSpring of " + s1 + " and " + s2;
377         }
378
379         protected void clear() {
380             min = pref = max = size = UNSET;
381         }
382
383         public void setValue(int size) {
384             if (size == UNSET) {
385                 if (this.size != UNSET) {
386                     super.setValue(size);
387                     s1.setValue(UNSET);
388                     s2.setValue(UNSET);
389                     return;
390                 }
391             }
392             super.setValue(size);
393         }
394
395         protected abstract int op(int x, int y);
396
397         public int getMinimumValue() {
398             if (min == UNSET) {
399                 min = op(s1.getMinimumValue(), s2.getMinimumValue());
400             }
401             return min;
402         }
403
404         public int getPreferredValue() {
405             if (pref == UNSET) {
406                 pref = op(s1.getPreferredValue(), s2.getPreferredValue());
407             }
408             return pref;
409         }
410
411         public int getMaximumValue() {
412             if (max == UNSET) {
413                 max = op(s1.getMaximumValue(), s2.getMaximumValue());
414             }
415             return max;
416         }
417
418         public int getValue() {
419             if (size == UNSET) {
420                 size = op(s1.getValue(), s2.getValue());
421             }
422             return size;
423         }
424
425         /*pp*/ boolean isCyclic(SpringLayout JavaDoc l) {
426             return l.isCyclic(s1) || l.isCyclic(s2);
427         }
428     };
429
430      private static class SumSpring extends CompoundSpring {
431          public SumSpring(Spring JavaDoc s1, Spring JavaDoc s2) {
432              super(s1, s2);
433          }
434
435          protected int op(int x, int y) {
436              return x + y;
437          }
438
439          public void setValue(int size) {
440              super.setValue(size);
441              if (size == UNSET) {
442                  return;
443              }
444              s1.setStrain(this.getStrain());
445              s2.setValue(size - s1.getValue());
446          }
447      }
448
449     private static class MaxSpring extends CompoundSpring {
450
451         public MaxSpring(Spring JavaDoc s1, Spring JavaDoc s2) {
452             super(s1, s2);
453         }
454
455         protected int op(int x, int y) {
456             return Math.max(x, y);
457         }
458
459         public void setValue(int size) {
460             super.setValue(size);
461             if (size == UNSET) {
462                 return;
463             }
464             // Pending should also check max bounds here.
465
if (s1.getPreferredValue() < s2.getPreferredValue()) {
466                 s1.setValue(Math.min(size, s1.getPreferredValue()));
467                 s2.setValue(size);
468             }
469             else {
470                 s1.setValue(size);
471                 s2.setValue(Math.min(size, s2.getPreferredValue()));
472             }
473         }
474     }
475
476     /**
477      * Returns a strut -- a spring whose <em>minimum</em>, <em>preferred</em>, and
478      * <em>maximum</em> values each have the value <code>pref</code>.
479      *
480      * @param pref the <em>minimum</em>, <em>preferred</em>, and
481      * <em>maximum</em> values of the new spring
482      * @return a spring whose <em>minimum</em>, <em>preferred</em>, and
483      * <em>maximum</em> values each have the value <code>pref</code>
484      *
485      * @see Spring
486      */

487      public static Spring JavaDoc constant(int pref) {
488          return constant(pref, pref, pref);
489      }
490
491     /**
492      * Returns a spring whose <em>minimum</em>, <em>preferred</em>, and
493      * <em>maximum</em> values have the values: <code>min</code>, <code>pref</code>,
494      * and <code>max</code> respectively.
495      *
496      * @param min the <em>minimum</em> value of the new spring
497      * @param pref the <em>preferred</em> value of the new spring
498      * @param max the <em>maximum</em> value of the new spring
499      * @return a spring whose <em>minimum</em>, <em>preferred</em>, and
500      * <em>maximum</em> values have the values: <code>min</code>, <code>pref</code>,
501      * and <code>max</code> respectively
502      *
503      * @see Spring
504      */

505      public static Spring JavaDoc constant(int min, int pref, int max) {
506          return new StaticSpring(min, pref, max);
507      }
508
509
510     /**
511      * Returns <code>-s</code>: a spring running in the opposite direction to <code>s</code>.
512      *
513      * @return <code>-s</code>: a spring running in the opposite direction to <code>s</code>
514      *
515      * @see Spring
516      */

517     public static Spring JavaDoc minus(Spring JavaDoc s) {
518         return new NegativeSpring(s);
519     }
520
521     /**
522      * Returns <code>s1+s2</code>: a spring representing <code>s1</code> and <code>s2</code>
523      * in series. In a sum, <code>s3</code>, of two springs, <code>s1</code> and <code>s2</code>,
524      * the <em>strains</em> of <code>s1</code>, <code>s2</code>, and <code>s3</code> are maintained
525      * at the same level (to within the precision implied by their integer <em>value</em>s).
526      * The strain of a spring in compression is:
527      * <pre>
528      * value - pref
529      * ------------
530      * pref - min
531      * </pre>
532      * and the strain of a spring in tension is:
533      * <pre>
534      * value - pref
535      * ------------
536      * max - pref
537      * </pre>
538      * When <code>setValue</code> is called on the sum spring, <code>s3</code>, the strain
539      * in <code>s3</code> is calculated using one of the formulas above. Once the strain of
540      * the sum is known, the <em>value</em>s of <code>s1</code> and <code>s2</code> are
541      * then set so that they are have a strain equal to that of the sum. The formulas are
542      * evaluated so as to take rounding errors into account and ensure that the sum of
543      * the <em>value</em>s of <code>s1</code> and <code>s2</code> is exactly equal to
544      * the <em>value</em> of <code>s3</code>.
545      *
546      * @return <code>s1+s2</code>: a spring representing <code>s1</code> and <code>s2</code> in series
547      *
548      * @see Spring
549      */

550      public static Spring JavaDoc sum(Spring JavaDoc s1, Spring JavaDoc s2) {
551          return new SumSpring(s1, s2);
552      }
553
554     /**
555      * Returns <code>max(s1, s2)</code>: a spring whose value is always greater than (or equal to)
556      * the values of both <code>s1</code> and <code>s2</code>.
557      *
558      * @return <code>max(s1, s2)</code>: a spring whose value is always greater than (or equal to)
559      * the values of both <code>s1</code> and <code>s2</code>
560      * @see Spring
561      */

562     public static Spring JavaDoc max(Spring JavaDoc s1, Spring JavaDoc s2) {
563         return new MaxSpring(s1, s2);
564     }
565
566     // Remove these, they're not used often and can be created using minus -
567
// as per these implementations.
568

569     /*pp*/ static Spring JavaDoc difference(Spring JavaDoc s1, Spring JavaDoc s2) {
570         return sum(s1, minus(s2));
571     }
572
573     /*
574     public static Spring min(Spring s1, Spring s2) {
575         return minus(max(minus(s1), minus(s2)));
576     }
577     */

578
579     /**
580      * Returns a spring whose <em>minimum</em>, <em>preferred</em>, <em>maximum</em>
581      * and <em>value</em> properties are each multiples of the properties of the
582      * argument spring, <code>s</code>. Minimum and maximum properties are
583      * swapped when <code>factor</code> is negative (in accordance with the
584      * rules of interval arithmetic).
585      * <p>
586      * When factor is, for example, 0.5f the result represents 'the mid-point'
587      * of its input - an operation that is useful for centering components in
588      * a container.
589      *
590      * @param s the spring to scale
591      * @param factor amount to scale by.
592      * @return a spring whose properties are those of the input spring <code>s</code>
593      * multiplied by <code>factor</code>
594      * @throws NullPointerException if <code>s</code> is null
595      * @since 1.5
596      */

597     public static Spring JavaDoc scale(Spring JavaDoc s, float factor) {
598         checkArg(s);
599         return new ScaleSpring(s, factor);
600     }
601
602     /**
603      * Returns a spring whose <em>minimum</em>, <em>preferred</em>, <em>maximum</em>
604      * and <em>value</em> properties are defined by the widths of the <em>minimumSize</em>,
605      * <em>preferredSize</em>, <em>maximumSize</em> and <em>size</em> properties
606      * of the supplied component. The returned spring is a 'wrapper' implementation
607      * whose methods call the appropriate size methods of the supplied component.
608      * The minimum, preferred, maximum and value properties of the returned spring
609      * therefore report the current state of the appropriate properties in the
610      * component and track them as they change.
611      *
612      * @param c Component used for calculating size
613      * @return a spring whose properties are defined by the horizontal component
614      * of the component's size methods.
615      * @throws NullPointerException if <code>c</code> is null
616      * @since 1.5
617      */

618     public static Spring JavaDoc width(Component JavaDoc c) {
619         checkArg(c);
620         return new WidthSpring(c);
621     }
622
623     /**
624      * Returns a spring whose <em>minimum</em>, <em>preferred</em>, <em>maximum</em>
625      * and <em>value</em> properties are defined by the heights of the <em>minimumSize</em>,
626      * <em>preferredSize</em>, <em>maximumSize</em> and <em>size</em> properties
627      * of the supplied component. The returned spring is a 'wrapper' implementation
628      * whose methods call the appropriate size methods of the supplied component.
629      * The minimum, preferred, maximum and value properties of the returned spring
630      * therefore report the current state of the appropriate properties in the
631      * component and track them as they change.
632      *
633      * @param c Component used for calculating size
634      * @return a spring whose properties are defined by the vertical component
635      * of the component's size methods.
636      * @throws NullPointerException if <code>c</code> is null
637      * @since 1.5
638      */

639     public static Spring JavaDoc height(Component JavaDoc c) {
640         checkArg(c);
641         return new HeightSpring(c);
642     }
643
644
645     /**
646      * If <code>s</code> is null, this throws an NullPointerException.
647      */

648     private static void checkArg(Object JavaDoc s) {
649         if (s == null) {
650             throw new NullPointerException JavaDoc("Argument must not be null");
651         }
652     }
653 }
654
Popular Tags