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)
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., 59 Temple Place, Suite 330, Boston, MA
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
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. */
41 * Calculates the space between component edges, that are layed out by
42 * {@link SpringLayout}.
44 * A Spring defines a minimum, preferred and maximum distance for each edge
45 * (north, east, south, west) of a component.
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
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;
63 * Creates a new Spring object. This constructor is used by the static
64 * methods which create Springs.
71 * Creates a Spring which min, pref and max values are all the same.
72 * These kind of Springs are 'struts'.
74 * @param val the constant for min, pref and max values.
75 * @return a Spring object with constant values for min, pref and max.
77 public static Spring constant(int val)
79 return new SimpleSpring(val, val, val);
82 /** Creates a Spring which min, pref and max values are constants.
83 * @param min the constant for the minimum value.
84 * @param pref the constant for the preferred value.
85 * @param max the constant for the maximum value.
86 * @return a Spring object with constant values for min, pref and max.
88 public static Spring constant(int min, int pref, int max)
90 return new SimpleSpring(min, pref, max);
94 * Returns the maximum value of the Spring.
96 * @return the maximum value.
98 public abstract int getMaximumValue();
101 * Returns the minimum value of this Spring.
103 * @return the minimum value.
105 public abstract int getMinimumValue();
108 * Return the preferred value of this Spring.
110 * @return the preferred value.
112 public abstract int getPreferredValue();
115 * Return the actual value of this Spring.
117 * @return the actual value of this Spring.
119 public abstract int getValue();
122 * Creates and returns a Spring, which always has the maximum values
123 * min = max(min_s1, min_s2), pref = max(pref_s1, pref_s2), max =
124 * max(max_s1, max_s2).
126 * @param s1 the first summand of the max Spring.
127 * @param s2 the second summand of the max Spring.
128 * @return a Spring which is max(s1, s2).
130 public static Spring max(Spring s1, Spring s2)
132 return new MaxSpring(s1, s2);
136 * Creates and returns a Spring, which is always the negation of s.
137 * min = -min_s, pref = -pref_s, max = -max_pref.
139 * @param s the Spring to be negated.
140 * @return the negative of <code>s</code>.
142 public static Spring minus(Spring s)
144 return new MinusSpring(s);
148 * Sets the actual value. If <code>value</code> is out of the (min, max)
149 * bounds, then the value is adjusted, so that is inside these bounds.
151 * @param value the value to be set.
153 public abstract void setValue(int value);
156 * Creates and returns a Spring, which is always the sum of s1 and s2.
157 * min_sum = min_s1 + min_s2, pref_sum = pref_s1 + pref_s2, max_sum =
160 * @param s1 the 1st summand of the sum Spring.
161 * @param s2 the 2nd summand of the sum Spring.
162 * @return a sum which is <code>s1 + s2</code>.
164 public static Spring sum(Spring s1, Spring s2)
166 return new AddSpring(s1, s2);
170 * A simple Spring, that holds constant values for min, pref and max.
172 * @author Roman Kennke (roman@ontographics.com)
174 private static final class SimpleSpring extends Spring
177 /** The constant value for min. */
178 private final int min;
180 /** The constant value for pref. */
181 private final int pref;
183 /** The constant value for max. */
184 private final int max;
186 /** The actual value of the spring. */
190 * Creates a new SimpleSpring object.
192 * @param min the constant minimum value.
193 * @param pref the constant preferred value.
194 * @param max the constant maximum value.
196 public SimpleSpring(int newMin, int newPref, int newMax)
201 value = Spring.UNSET;
205 * Returns the maximum value of this Spring.
207 * @return the maximum value.
209 public int getMaximumValue()
215 * Returns the minimum value of this Spring.
217 * @return the minimum value.
219 public int getMinimumValue()
225 * Returns the preferred value of this Spring.
227 * @return the preferred value.
229 public int getPreferredValue()
235 * Return the actual current value of this Spring.
237 * @return the current value.
239 public int getValue()
242 if (value == Spring.UNSET)
251 * Sets the current value.
253 * @param val the value to be set.
255 public void setValue(int val)
276 * A Spring, that is the sum of two other Springs.
278 * @author Roman Kennke (roman@ontographics.com)
280 private static final class AddSpring extends Spring
283 /** The springs, that are the 'operands' of this Spring. */
284 private final Spring s1;
285 private final Spring s2;
287 /** The current value for this Spring. */
291 * Creates a new AddSpring object.
293 * @param s1 the first operand.
294 * @param s2 the second operand.
296 protected AddSpring(Spring s1, Spring s2)
301 value = Spring.UNSET;
305 * Returns the maximum value of this Spring.
307 * @return the maximum value.
309 public int getMaximumValue()
311 int max1 = s1.getMaximumValue();
312 int max2 = s2.getMaximumValue();
317 * Return the minimum value of this Spring.
319 * @return the minimum value.
321 public int getMinimumValue()
323 int min1 = s1.getMinimumValue();
324 int min2 = s2.getMinimumValue();
329 * Returns the preferred value of this Spring.
331 * @return the preferred value.
333 public int getPreferredValue()
335 int pref1 = s1.getPreferredValue();
336 int pref2 = s2.getPreferredValue();
337 return pref1 + pref2;
341 * Returns the actual current value of this Spring.
343 * @return the current value of this Spring.
345 public int getValue()
347 if (value == Spring.UNSET)
349 int val1 = s1.getValue();
350 int val2 = s2.getValue();
357 * Sets the current value.
359 * @param val the value to be set.
361 public void setValue(int val)
364 if (val > getMaximumValue())
366 value = getMaximumValue();
368 else if (val < getMinimumValue())
370 value = getMinimumValue();
383 * A Spring that is calculated as the negation of another Spring.
385 * @author Roman Kennke (roman@ontographics.com)
387 private static final class MinusSpring extends Spring
390 /** The Spring from which to calculate the negation. */
391 private final Spring s;
393 /** The current value of this Spring. */
397 * Creates a new MinusSpring object.
398 * @param s the Spring from which to calculate the negation.
400 protected MinusSpring(Spring s)
404 value = Spring.UNSET;
407 /** Returns the maximum value of this Spring.
409 * @return the maximum value.
411 public int getMaximumValue()
413 return -s.getMinimumValue();
417 * Returns the minimum value of this Spring.
419 * @return the minimum value.
421 public int getMinimumValue()
423 return -s.getMaximumValue();
427 * Returns the preferred value of this Spring.
429 * @return the preferred value.
431 public int getPreferredValue()
433 return -s.getPreferredValue();
437 * Returns the current value of this Spring.
439 * @return the current value.
441 public int getValue()
443 if (value == Spring.UNSET)
445 value = -s.getValue();
451 * Sets the current value.
453 * @param val the value to be set.
455 public void setValue(int val)
458 if (val > getMaximumValue())
460 value = getMaximumValue();
462 else if (val < getMinimumValue())
464 value = getMinimumValue();
477 * A Spring, that is calculated as the maximum of two Springs.
479 * @author Roman Kennke (roman@ontographics.com)
481 private static final class MaxSpring extends Spring
484 /** The two other Springs from which to calculate the maximum. */
485 private final Spring s1;
486 private final Spring s2;
488 /** The current value of this Spring. */
492 * Creates a new MaxSpring object.
494 * @param s1 the 1st operand.
495 * @param s2 the 2nd operand.
497 protected MaxSpring(Spring s1, Spring s2)
502 value = Spring.UNSET;
507 * Returns the maximum value of this Spring.
509 * @return the maximum value.
511 public int getMaximumValue()
513 int max1 = s1.getMaximumValue();
514 int max2 = s2.getMaximumValue();
515 return Math.max(max1, max2);
519 * Returns the minimum value of this Spring.
521 * @return the minimum value.
523 public int getMinimumValue()
525 int min1 = s1.getMinimumValue();
526 int min2 = s2.getMinimumValue();
527 return Math.max(min1, min2);
531 * Returns the preferred value of this Spring.
533 * @return the preferred value.
535 public int getPreferredValue()
537 int pref1 = s1.getPreferredValue();
538 int pref2 = s2.getPreferredValue();
539 return Math.max(pref1, pref2);
543 * Returns the actual value of this Spring.
545 * @return the current value.
547 public int getValue()
549 if (value == Spring.UNSET)
551 int val1 = s1.getValue();
552 int val2 = s2.getValue();
553 value = Math.max(val1, val2);
559 * Sets the current value.
561 * @param val the value to be set.
563 public void setValue(int val)
566 if (val > getMaximumValue())
568 value = getMaximumValue();
570 else if (val < getMinimumValue())
572 value = getMinimumValue();