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