1 7 package javax.swing; 8 9 import java.awt.Component ; 10 import java.util.*; 11 12 114 public abstract class Spring { 115 116 119 public static final int UNSET = Integer.MIN_VALUE; 120 121 131 protected Spring() {} 132 133 138 public abstract int getMinimumValue(); 139 140 145 public abstract int getPreferredValue(); 146 147 152 public abstract int getMaximumValue(); 153 154 161 public abstract int getValue(); 162 163 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 double getStrain() { 178 double delta = (getValue() - getPreferredValue()); 179 return delta/range(getValue() < getPreferredValue()); 180 } 181 182 void setStrain(double strain) { 183 setValue(getPreferredValue() + (int)(strain * range(strain < 0))); 184 } 185 186 boolean isCyclic(SpringLayout l) { 187 return false; 188 } 189 190 static abstract class AbstractSpring extends Spring { 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 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 { 246 private Spring s; 247 248 public NegativeSpring(Spring s) { 249 this.s = s; 250 } 251 252 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 s.setValue(-size); 275 } 276 277 boolean isCyclic(SpringLayout l) { 278 return s.isCyclic(l); 279 } 280 } 281 282 private static class ScaleSpring extends Spring { 283 private Spring s; 284 private float factor; 285 286 private ScaleSpring(Spring 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 boolean isCyclic(SpringLayout l) { 316 return s.isCyclic(l); 317 } 318 } 319 320 static class WidthSpring extends AbstractSpring { 321 Component c; 322 323 public WidthSpring(Component 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 return Math.min(Short.MAX_VALUE, c.getMaximumSize().width); 340 } 341 } 342 343 static class HeightSpring extends AbstractSpring { 344 Component c; 345 346 public HeightSpring(Component 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 static abstract class CompoundSpring extends StaticSpring { 366 protected Spring s1; 367 protected Spring s2; 368 369 public CompoundSpring(Spring s1, Spring s2) { 370 clear(); 371 this.s1 = s1; 372 this.s2 = s2; 373 } 374 375 public String 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 boolean isCyclic(SpringLayout l) { 426 return l.isCyclic(s1) || l.isCyclic(s2); 427 } 428 }; 429 430 private static class SumSpring extends CompoundSpring { 431 public SumSpring(Spring s1, Spring 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 s1, Spring 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 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 487 public static Spring constant(int pref) { 488 return constant(pref, pref, pref); 489 } 490 491 505 public static Spring constant(int min, int pref, int max) { 506 return new StaticSpring(min, pref, max); 507 } 508 509 510 517 public static Spring minus(Spring s) { 518 return new NegativeSpring(s); 519 } 520 521 550 public static Spring sum(Spring s1, Spring s2) { 551 return new SumSpring(s1, s2); 552 } 553 554 562 public static Spring max(Spring s1, Spring s2) { 563 return new MaxSpring(s1, s2); 564 } 565 566 569 static Spring difference(Spring s1, Spring s2) { 570 return sum(s1, minus(s2)); 571 } 572 573 578 579 597 public static Spring scale(Spring s, float factor) { 598 checkArg(s); 599 return new ScaleSpring(s, factor); 600 } 601 602 618 public static Spring width(Component c) { 619 checkArg(c); 620 return new WidthSpring(c); 621 } 622 623 639 public static Spring height(Component c) { 640 checkArg(c); 641 return new HeightSpring(c); 642 } 643 644 645 648 private static void checkArg(Object s) { 649 if (s == null) { 650 throw new NullPointerException ("Argument must not be null"); 651 } 652 } 653 } 654 | Popular Tags |