Dead
[official-gcc.git] / gomp-20050608-branch / libjava / classpath / javax / swing / Spring.java
blob8f7105d496db767c03dd34818b10ff33b0673bc1
1 /* Spring.java --
2 Copyright (C) 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package javax.swing;
40 /**
41 * Calculates the space between component edges, that are layed out by
42 * {@link SpringLayout}.
43 * <p>
44 * A Spring defines a minimum, preferred and maximum distance for each edge
45 * (north, east, south, west) of a component.
46 * </p>
47 * However, springs are not static, their actual values are computed at
48 * runtime. That means, if a Spring C is defined as the sum of Spring A and
49 * Spring B, then the values (min, pref and max) are not calculated at
50 * creation of Spring C, but instead always when {@link #getValue} is
51 * called. So, when Spring A or Spring B changes, this is reflected in
52 * Spring C.
54 * @author Roman Kennke (roman@ontographics.com)
56 public abstract class Spring
59 /** Indicates a not-set value. **/
60 public static final int UNSET = -2147483648;
62 /**
63 * Creates a new Spring object. This constructor is used by the static
64 * methods which create Springs.
66 protected Spring()
68 // Nothing to do here.
71 /**
72 * Creates a Spring which min, pref and max values are all the same.
73 * These kind of Springs are 'struts'.
75 * @param val the constant for min, pref and max values.
76 * @return a Spring object with constant values for min, pref and max.
78 public static Spring constant(int val)
80 return new SimpleSpring(val, val, val);
83 /** Creates a Spring which min, pref and max values are constants.
84 * @param min the constant for the minimum value.
85 * @param pref the constant for the preferred value.
86 * @param max the constant for the maximum value.
87 * @return a Spring object with constant values for min, pref and max.
89 public static Spring constant(int min, int pref, int max)
91 return new SimpleSpring(min, pref, max);
94 /**
95 * Returns the maximum value of the Spring.
97 * @return the maximum value.
99 public abstract int getMaximumValue();
102 * Returns the minimum value of this Spring.
104 * @return the minimum value.
106 public abstract int getMinimumValue();
109 * Return the preferred value of this Spring.
111 * @return the preferred value.
113 public abstract int getPreferredValue();
116 * Return the actual value of this Spring.
118 * @return the actual value of this Spring.
120 public abstract int getValue();
123 * Creates and returns a Spring, which always has the maximum values
124 * min = max(min_s1, min_s2), pref = max(pref_s1, pref_s2), max =
125 * max(max_s1, max_s2).
127 * @param s1 the first summand of the max Spring.
128 * @param s2 the second summand of the max Spring.
129 * @return a Spring which is max(s1, s2).
131 public static Spring max(Spring s1, Spring s2)
133 return new MaxSpring(s1, s2);
137 * Creates and returns a Spring, which is always the negation of s.
138 * min = -min_s, pref = -pref_s, max = -max_pref.
140 * @param s the Spring to be negated.
141 * @return the negative of <code>s</code>.
143 public static Spring minus(Spring s)
145 return new MinusSpring(s);
149 * Sets the actual value. If <code>value</code> is out of the (min, max)
150 * bounds, then the value is adjusted, so that is inside these bounds.
152 * @param value the value to be set.
154 public abstract void setValue(int value);
157 * Creates and returns a Spring, which is always the sum of s1 and s2.
158 * min_sum = min_s1 + min_s2, pref_sum = pref_s1 + pref_s2, max_sum =
159 * max_s1 + max_s2.
161 * @param s1 the 1st summand of the sum Spring.
162 * @param s2 the 2nd summand of the sum Spring.
163 * @return a sum which is <code>s1 + s2</code>.
165 public static Spring sum(Spring s1, Spring s2)
167 return new AddSpring(s1, s2);
171 * A simple Spring, that holds constant values for min, pref and max.
173 * @author Roman Kennke (roman@ontographics.com)
175 private static final class SimpleSpring extends Spring
178 /** The constant value for min. */
179 private final int min;
181 /** The constant value for pref. */
182 private final int pref;
184 /** The constant value for max. */
185 private final int max;
187 /** The actual value of the spring. */
188 private int value;
191 * Creates a new SimpleSpring object.
193 * @param newMin the constant minimum value.
194 * @param newPref the constant preferred value.
195 * @param newMax the constant maximum value.
197 public SimpleSpring(int newMin, int newPref, int newMax)
199 min = newMin;
200 pref = newPref;
201 max = newMax;
202 value = Spring.UNSET;
206 * Returns the maximum value of this Spring.
208 * @return the maximum value.
210 public int getMaximumValue()
212 return max;
216 * Returns the minimum value of this Spring.
218 * @return the minimum value.
220 public int getMinimumValue()
222 return min;
226 * Returns the preferred value of this Spring.
228 * @return the preferred value.
230 public int getPreferredValue()
232 return pref;
236 * Return the actual current value of this Spring.
238 * @return the current value.
240 public int getValue()
243 if (value == Spring.UNSET)
245 value = pref;
248 return value;
252 * Sets the current value.
254 * @param val the value to be set.
256 public void setValue(int val)
259 if (val > max)
261 value = max;
263 else if (val < min)
265 value = min;
267 else
269 value = val;
277 * A Spring, that is the sum of two other Springs.
279 * @author Roman Kennke (roman@ontographics.com)
281 private static final class AddSpring extends Spring
284 /** The springs, that are the 'operands' of this Spring. */
285 private final Spring s1;
286 private final Spring s2;
288 /** The current value for this Spring. */
289 private int value;
292 * Creates a new AddSpring object.
294 * @param s1 the first operand.
295 * @param s2 the second operand.
297 protected AddSpring(Spring s1, Spring s2)
299 super();
300 this.s1 = s1;
301 this.s2 = s2;
302 value = Spring.UNSET;
306 * Returns the maximum value of this Spring.
308 * @return the maximum value.
310 public int getMaximumValue()
312 int max1 = s1.getMaximumValue();
313 int max2 = s2.getMaximumValue();
314 return max1 + max2;
318 * Return the minimum value of this Spring.
320 * @return the minimum value.
322 public int getMinimumValue()
324 int min1 = s1.getMinimumValue();
325 int min2 = s2.getMinimumValue();
326 return min1 + min2;
330 * Returns the preferred value of this Spring.
332 * @return the preferred value.
334 public int getPreferredValue()
336 int pref1 = s1.getPreferredValue();
337 int pref2 = s2.getPreferredValue();
338 return pref1 + pref2;
342 * Returns the actual current value of this Spring.
344 * @return the current value of this Spring.
346 public int getValue()
348 if (value == Spring.UNSET)
350 int val1 = s1.getValue();
351 int val2 = s2.getValue();
352 value = val1 + val2;
354 return value;
358 * Sets the current value.
360 * @param val the value to be set.
362 public void setValue(int val)
365 if (val > getMaximumValue())
367 value = getMaximumValue();
369 else if (val < getMinimumValue())
371 value = getMinimumValue();
373 else
375 value = val;
384 * A Spring that is calculated as the negation of another Spring.
386 * @author Roman Kennke (roman@ontographics.com)
388 private static final class MinusSpring extends Spring
391 /** The Spring from which to calculate the negation. */
392 private final Spring s;
394 /** The current value of this Spring. */
395 private int value;
398 * Creates a new MinusSpring object.
399 * @param s the Spring from which to calculate the negation.
401 protected MinusSpring(Spring s)
403 super();
404 this.s = s;
405 value = Spring.UNSET;
408 /** Returns the maximum value of this Spring.
410 * @return the maximum value.
412 public int getMaximumValue()
414 return -s.getMinimumValue();
418 * Returns the minimum value of this Spring.
420 * @return the minimum value.
422 public int getMinimumValue()
424 return -s.getMaximumValue();
428 * Returns the preferred value of this Spring.
430 * @return the preferred value.
432 public int getPreferredValue()
434 return -s.getPreferredValue();
438 * Returns the current value of this Spring.
440 * @return the current value.
442 public int getValue()
444 if (value == Spring.UNSET)
446 value = -s.getValue();
448 return value;
452 * Sets the current value.
454 * @param val the value to be set.
456 public void setValue(int val)
459 if (val > getMaximumValue())
461 value = getMaximumValue();
463 else if (val < getMinimumValue())
465 value = getMinimumValue();
467 else
469 value = val;
478 * A Spring, that is calculated as the maximum of two Springs.
480 * @author Roman Kennke (roman@ontographics.com)
482 private static final class MaxSpring extends Spring
485 /** The two other Springs from which to calculate the maximum. */
486 private final Spring s1;
487 private final Spring s2;
489 /** The current value of this Spring. */
490 private int value;
493 * Creates a new MaxSpring object.
495 * @param s1 the 1st operand.
496 * @param s2 the 2nd operand.
498 protected MaxSpring(Spring s1, Spring s2)
500 super();
501 this.s1 = s1;
502 this.s2 = s2;
503 value = Spring.UNSET;
508 * Returns the maximum value of this Spring.
510 * @return the maximum value.
512 public int getMaximumValue()
514 int max1 = s1.getMaximumValue();
515 int max2 = s2.getMaximumValue();
516 return Math.max(max1, max2);
520 * Returns the minimum value of this Spring.
522 * @return the minimum value.
524 public int getMinimumValue()
526 int min1 = s1.getMinimumValue();
527 int min2 = s2.getMinimumValue();
528 return Math.max(min1, min2);
532 * Returns the preferred value of this Spring.
534 * @return the preferred value.
536 public int getPreferredValue()
538 int pref1 = s1.getPreferredValue();
539 int pref2 = s2.getPreferredValue();
540 return Math.max(pref1, pref2);
544 * Returns the actual value of this Spring.
546 * @return the current value.
548 public int getValue()
550 if (value == Spring.UNSET)
552 int val1 = s1.getValue();
553 int val2 = s2.getValue();
554 value = Math.max(val1, val2);
556 return value;
560 * Sets the current value.
562 * @param val the value to be set.
564 public void setValue(int val)
567 if (val > getMaximumValue())
569 value = getMaximumValue();
571 else if (val < getMinimumValue())
573 value = getMinimumValue();
575 else
577 value = val;