GNU Classpath (0.20) | |
Frames | No Frames |
1: /* Spring.java -- 2: Copyright (C) 2004 Free Software Foundation, Inc. 3: 4: This file is part of GNU Classpath. 5: 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. 10: 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. 15: 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. 20: 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. 25: 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. */ 37: 38: package javax.swing; 39: 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. 53: * 54: * @author Roman Kennke (roman@ontographics.com) 55: */ 56: public abstract class Spring 57: { 58: 59: /** Indicates a not-set value. **/ 60: public static final int UNSET = -2147483648; 61: 62: /** 63: * Creates a new Spring object. This constructor is used by the static 64: * methods which create Springs. 65: */ 66: protected Spring() 67: { 68: // Nothing to do here. 69: } 70: 71: /** 72: * Creates a Spring which min, pref and max values are all the same. 73: * These kind of Springs are 'struts'. 74: * 75: * @param val the constant for min, pref and max values. 76: * @return a Spring object with constant values for min, pref and max. 77: */ 78: public static Spring constant(int val) 79: { 80: return new SimpleSpring(val, val, val); 81: } 82: 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. 88: */ 89: public static Spring constant(int min, int pref, int max) 90: { 91: return new SimpleSpring(min, pref, max); 92: } 93: 94: /** 95: * Returns the maximum value of the Spring. 96: * 97: * @return the maximum value. 98: */ 99: public abstract int getMaximumValue(); 100: 101: /** 102: * Returns the minimum value of this Spring. 103: * 104: * @return the minimum value. 105: */ 106: public abstract int getMinimumValue(); 107: 108: /** 109: * Return the preferred value of this Spring. 110: * 111: * @return the preferred value. 112: */ 113: public abstract int getPreferredValue(); 114: 115: /** 116: * Return the actual value of this Spring. 117: * 118: * @return the actual value of this Spring. 119: */ 120: public abstract int getValue(); 121: 122: /** 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). 126: * 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). 130: */ 131: public static Spring max(Spring s1, Spring s2) 132: { 133: return new MaxSpring(s1, s2); 134: } 135: 136: /** 137: * Creates and returns a Spring, which is always the negation of s. 138: * min = -min_s, pref = -pref_s, max = -max_pref. 139: * 140: * @param s the Spring to be negated. 141: * @return the negative of <code>s</code>. 142: */ 143: public static Spring minus(Spring s) 144: { 145: return new MinusSpring(s); 146: } 147: 148: /** 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. 151: * 152: * @param value the value to be set. 153: */ 154: public abstract void setValue(int value); 155: 156: /** 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. 160: * 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>. 164: */ 165: public static Spring sum(Spring s1, Spring s2) 166: { 167: return new AddSpring(s1, s2); 168: } 169: 170: /** 171: * A simple Spring, that holds constant values for min, pref and max. 172: * 173: * @author Roman Kennke (roman@ontographics.com) 174: */ 175: private static final class SimpleSpring extends Spring 176: { 177: 178: /** The constant value for min. */ 179: private final int min; 180: 181: /** The constant value for pref. */ 182: private final int pref; 183: 184: /** The constant value for max. */ 185: private final int max; 186: 187: /** The actual value of the spring. */ 188: private int value; 189: 190: /** 191: * Creates a new SimpleSpring object. 192: * 193: * @param newMin the constant minimum value. 194: * @param newPref the constant preferred value. 195: * @param newMax the constant maximum value. 196: */ 197: public SimpleSpring(int newMin, int newPref, int newMax) 198: { 199: min = newMin; 200: pref = newPref; 201: max = newMax; 202: value = Spring.UNSET; 203: } 204: 205: /** 206: * Returns the maximum value of this Spring. 207: * 208: * @return the maximum value. 209: */ 210: public int getMaximumValue() 211: { 212: return max; 213: } 214: 215: /** 216: * Returns the minimum value of this Spring. 217: * 218: * @return the minimum value. 219: */ 220: public int getMinimumValue() 221: { 222: return min; 223: } 224: 225: /** 226: * Returns the preferred value of this Spring. 227: * 228: * @return the preferred value. 229: */ 230: public int getPreferredValue() 231: { 232: return pref; 233: } 234: 235: /** 236: * Return the actual current value of this Spring. 237: * 238: * @return the current value. 239: */ 240: public int getValue() 241: { 242: 243: if (value == Spring.UNSET) 244: { 245: value = pref; 246: } 247: 248: return value; 249: } 250: 251: /** 252: * Sets the current value. 253: * 254: * @param val the value to be set. 255: */ 256: public void setValue(int val) 257: { 258: 259: if (val > max) 260: { 261: value = max; 262: } 263: else if (val < min) 264: { 265: value = min; 266: } 267: else 268: { 269: value = val; 270: } 271: } 272: 273: } 274: 275: 276: /** 277: * A Spring, that is the sum of two other Springs. 278: * 279: * @author Roman Kennke (roman@ontographics.com) 280: */ 281: private static final class AddSpring extends Spring 282: { 283: 284: /** The springs, that are the 'operands' of this Spring. */ 285: private final Spring s1; 286: private final Spring s2; 287: 288: /** The current value for this Spring. */ 289: private int value; 290: 291: /** 292: * Creates a new AddSpring object. 293: * 294: * @param s1 the first operand. 295: * @param s2 the second operand. 296: */ 297: protected AddSpring(Spring s1, Spring s2) 298: { 299: super(); 300: this.s1 = s1; 301: this.s2 = s2; 302: value = Spring.UNSET; 303: } 304: 305: /** 306: * Returns the maximum value of this Spring. 307: * 308: * @return the maximum value. 309: */ 310: public int getMaximumValue() 311: { 312: int max1 = s1.getMaximumValue(); 313: int max2 = s2.getMaximumValue(); 314: return max1 + max2; 315: } 316: 317: /** 318: * Return the minimum value of this Spring. 319: * 320: * @return the minimum value. 321: */ 322: public int getMinimumValue() 323: { 324: int min1 = s1.getMinimumValue(); 325: int min2 = s2.getMinimumValue(); 326: return min1 + min2; 327: } 328: 329: /** 330: * Returns the preferred value of this Spring. 331: * 332: * @return the preferred value. 333: */ 334: public int getPreferredValue() 335: { 336: int pref1 = s1.getPreferredValue(); 337: int pref2 = s2.getPreferredValue(); 338: return pref1 + pref2; 339: } 340: 341: /** 342: * Returns the actual current value of this Spring. 343: * 344: * @return the current value of this Spring. 345: */ 346: public int getValue() 347: { 348: if (value == Spring.UNSET) 349: { 350: int val1 = s1.getValue(); 351: int val2 = s2.getValue(); 352: value = val1 + val2; 353: } 354: return value; 355: } 356: 357: /** 358: * Sets the current value. 359: * 360: * @param val the value to be set. 361: */ 362: public void setValue(int val) 363: { 364: 365: if (val > getMaximumValue()) 366: { 367: value = getMaximumValue(); 368: } 369: else if (val < getMinimumValue()) 370: { 371: value = getMinimumValue(); 372: } 373: else 374: { 375: value = val; 376: } 377: 378: } 379: 380: } 381: 382: 383: /** 384: * A Spring that is calculated as the negation of another Spring. 385: * 386: * @author Roman Kennke (roman@ontographics.com) 387: */ 388: private static final class MinusSpring extends Spring 389: { 390: 391: /** The Spring from which to calculate the negation. */ 392: private final Spring s; 393: 394: /** The current value of this Spring. */ 395: private int value; 396: 397: /** 398: * Creates a new MinusSpring object. 399: * @param s the Spring from which to calculate the negation. 400: */ 401: protected MinusSpring(Spring s) 402: { 403: super(); 404: this.s = s; 405: value = Spring.UNSET; 406: } 407: 408: /** Returns the maximum value of this Spring. 409: * 410: * @return the maximum value. 411: */ 412: public int getMaximumValue() 413: { 414: return -s.getMinimumValue(); 415: } 416: 417: /** 418: * Returns the minimum value of this Spring. 419: * 420: * @return the minimum value. 421: */ 422: public int getMinimumValue() 423: { 424: return -s.getMaximumValue(); 425: } 426: 427: /** 428: * Returns the preferred value of this Spring. 429: * 430: * @return the preferred value. 431: */ 432: public int getPreferredValue() 433: { 434: return -s.getPreferredValue(); 435: } 436: 437: /** 438: * Returns the current value of this Spring. 439: * 440: * @return the current value. 441: */ 442: public int getValue() 443: { 444: if (value == Spring.UNSET) 445: { 446: value = -s.getValue(); 447: } 448: return value; 449: } 450: 451: /** 452: * Sets the current value. 453: * 454: * @param val the value to be set. 455: */ 456: public void setValue(int val) 457: { 458: 459: if (val > getMaximumValue()) 460: { 461: value = getMaximumValue(); 462: } 463: else if (val < getMinimumValue()) 464: { 465: value = getMinimumValue(); 466: } 467: else 468: { 469: value = val; 470: } 471: 472: } 473: 474: } 475: 476: 477: /** 478: * A Spring, that is calculated as the maximum of two Springs. 479: * 480: * @author Roman Kennke (roman@ontographics.com) 481: */ 482: private static final class MaxSpring extends Spring 483: { 484: 485: /** The two other Springs from which to calculate the maximum. */ 486: private final Spring s1; 487: private final Spring s2; 488: 489: /** The current value of this Spring. */ 490: private int value; 491: 492: /** 493: * Creates a new MaxSpring object. 494: * 495: * @param s1 the 1st operand. 496: * @param s2 the 2nd operand. 497: */ 498: protected MaxSpring(Spring s1, Spring s2) 499: { 500: super(); 501: this.s1 = s1; 502: this.s2 = s2; 503: value = Spring.UNSET; 504: } 505: 506: 507: /** 508: * Returns the maximum value of this Spring. 509: * 510: * @return the maximum value. 511: */ 512: public int getMaximumValue() 513: { 514: int max1 = s1.getMaximumValue(); 515: int max2 = s2.getMaximumValue(); 516: return Math.max(max1, max2); 517: } 518: 519: /** 520: * Returns the minimum value of this Spring. 521: * 522: * @return the minimum value. 523: */ 524: public int getMinimumValue() 525: { 526: int min1 = s1.getMinimumValue(); 527: int min2 = s2.getMinimumValue(); 528: return Math.max(min1, min2); 529: } 530: 531: /** 532: * Returns the preferred value of this Spring. 533: * 534: * @return the preferred value. 535: */ 536: public int getPreferredValue() 537: { 538: int pref1 = s1.getPreferredValue(); 539: int pref2 = s2.getPreferredValue(); 540: return Math.max(pref1, pref2); 541: } 542: 543: /** 544: * Returns the actual value of this Spring. 545: * 546: * @return the current value. 547: */ 548: public int getValue() 549: { 550: if (value == Spring.UNSET) 551: { 552: int val1 = s1.getValue(); 553: int val2 = s2.getValue(); 554: value = Math.max(val1, val2); 555: } 556: return value; 557: } 558: 559: /** 560: * Sets the current value. 561: * 562: * @param val the value to be set. 563: */ 564: public void setValue(int val) 565: { 566: 567: if (val > getMaximumValue()) 568: { 569: value = getMaximumValue(); 570: } 571: else if (val < getMinimumValue()) 572: { 573: value = getMinimumValue(); 574: } 575: else 576: { 577: value = val; 578: } 579: } 580: } 581: }
GNU Classpath (0.20) |