001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Roman I. Chernyatchik
019: * @version $Revision$
020: */package javax.swing;
021:
022: import java.awt.Component;
023:
024: public abstract class Spring {
025:
026: static final class HeightSpring extends Spring {
027: final Component component;
028: private int value = Spring.UNSET;
029:
030: public int getMinimumValue() {
031: return trim(component.getMinimumSize().height);
032: }
033:
034: public int getPreferredValue() {
035: return trim(component.getPreferredSize().height);
036: }
037:
038: public int getMaximumValue() {
039: return trim(component.getMaximumSize().height);
040: }
041:
042: public int getValue() {
043: return (value != Spring.UNSET ? value : getPreferredValue());
044: }
045:
046: public void setValue(final int value) {
047: this .value = value;
048: }
049:
050: public String toString() {
051: return "[Height of " + component.getClass().getName()
052: + ": (" + getMinimumValue() + ", "
053: + getPreferredValue() + ", " + getMaximumValue()
054: + ")]";
055: }
056:
057: private HeightSpring(final Component component) {
058: this .component = component;
059: }
060: }
061:
062: static final class WidthSpring extends Spring {
063: final Component component;
064: private int value = Spring.UNSET;
065:
066: public int getMinimumValue() {
067: return trim(component.getMinimumSize().width);
068: }
069:
070: public int getPreferredValue() {
071: return trim(component.getPreferredSize().width);
072: }
073:
074: public int getMaximumValue() {
075: return trim(component.getMaximumSize().width);
076: }
077:
078: public int getValue() {
079: return (value != Spring.UNSET ? value : getPreferredValue());
080: }
081:
082: public void setValue(final int value) {
083: this .value = value;
084: }
085:
086: public String toString() {
087: return "[Width of " + component.getClass().getName()
088: + ": (" + getMinimumValue() + ", "
089: + getPreferredValue() + ", " + getMaximumValue()
090: + ")]";
091: }
092:
093: private WidthSpring(final Component component) {
094: this .component = component;
095: }
096: }
097:
098: private static final class ConstantSpring extends Spring {
099: private final int minValue;
100: private final int prefValue;
101: private final int maxValue;
102: private int value;
103:
104: public int getMinimumValue() {
105: return minValue;
106: }
107:
108: public int getPreferredValue() {
109: return prefValue;
110: }
111:
112: public int getMaximumValue() {
113: return maxValue;
114: }
115:
116: public int getValue() {
117: return (value != Spring.UNSET ? value : getPreferredValue());
118: }
119:
120: public void setValue(final int value) {
121: this .value = value;
122: }
123:
124: public String toString() {
125: return "[" + minValue + ", " + prefValue + ", " + maxValue
126: + "]";
127: }
128:
129: private ConstantSpring(final int pref) {
130: this (pref, pref, pref);
131: }
132:
133: private ConstantSpring(final int min, final int pref,
134: final int max) {
135: this .maxValue = max;
136: this .minValue = min;
137: this .prefValue = pref;
138: value = pref;
139: }
140: }
141:
142: private static final class MinusSpring extends Spring {
143: private final Spring sourceSpring;
144:
145: public int getMinimumValue() {
146: int val = sourceSpring.getMaximumValue();
147: return negativeValue(val);
148: }
149:
150: public int getPreferredValue() {
151: int val = sourceSpring.getPreferredValue();
152: return negativeValue(val);
153: }
154:
155: public int getMaximumValue() {
156: int val = sourceSpring.getMinimumValue();
157: return negativeValue(val);
158: }
159:
160: public int getValue() {
161: int val = sourceSpring.getValue();
162: return negativeValue(val);
163: }
164:
165: public void setValue(final int value) {
166: sourceSpring.setValue(value != Spring.UNSET ? (-1) * value
167: : Spring.UNSET);
168: }
169:
170: public String toString() {
171: return "(-" + sourceSpring + ")";
172: }
173:
174: private int negativeValue(final int val) {
175: int value = trim(val);
176: switch (value) {
177: case TRIMMED_MAX_VALUE:
178: return TRIMMED_MIN_VALUE;
179:
180: case TRIMMED_MIN_VALUE:
181: return TRIMMED_MAX_VALUE;
182:
183: default:
184: return (-1) * value;
185: }
186: }
187:
188: private MinusSpring(final Spring spring) {
189: sourceSpring = spring;
190: }
191: }
192:
193: private static final class MaxSpring extends Spring {
194: private final Spring sourceSpring1;
195: private final Spring sourceSpring2;
196:
197: private int minValue = Spring.UNSET;
198: private int prefValue = Spring.UNSET;
199: private int maxValue = Spring.UNSET;
200: private int value = Spring.UNSET;
201:
202: public int getMinimumValue() {
203: if (minValue == Spring.UNSET) {
204: minValue = trim(Math.max(sourceSpring1
205: .getMinimumValue(), sourceSpring2
206: .getMinimumValue()));
207: }
208: return minValue;
209: }
210:
211: public int getPreferredValue() {
212: if (prefValue == Spring.UNSET) {
213: prefValue = trim(Math.max(sourceSpring1
214: .getPreferredValue(), sourceSpring2
215: .getPreferredValue()));
216: }
217: return prefValue;
218: }
219:
220: public int getMaximumValue() {
221: if (maxValue == Spring.UNSET) {
222: maxValue = trim(Math.max(sourceSpring1
223: .getMaximumValue(), sourceSpring2
224: .getMaximumValue()));
225: }
226: return maxValue;
227: }
228:
229: public int getValue() {
230: if (value == Spring.UNSET) {
231: value = Math.max(sourceSpring1.getValue(),
232: sourceSpring2.getValue());
233: }
234: return value;
235: }
236:
237: public void setValue(final int value) {
238: if (value == Spring.UNSET) {
239: minValue = Spring.UNSET;
240: prefValue = Spring.UNSET;
241: maxValue = Spring.UNSET;
242: if (this .value != Spring.UNSET) {
243: sourceSpring1.setValue(value);
244: sourceSpring2.setValue(value);
245: }
246: this .value = Spring.UNSET;
247: return;
248: }
249:
250: this .value = value;
251: final int pref1 = sourceSpring1.getPreferredValue();
252: final int pref2 = sourceSpring2.getPreferredValue();
253:
254: if (pref1 < pref2) {
255: sourceSpring1.setValue(Math.min(value, pref1));
256: sourceSpring2.setValue(value);
257: } else {
258: sourceSpring1.setValue(value);
259: sourceSpring2.setValue(Math.min(value, pref2));
260: }
261: }
262:
263: public String toString() {
264: return "max(" + sourceSpring1 + ", " + sourceSpring2 + ")";
265: }
266:
267: private MaxSpring(final Spring spring1, final Spring spring2) {
268: sourceSpring1 = spring1;
269: sourceSpring2 = spring2;
270: }
271: }
272:
273: private static final class SumSpring extends Spring {
274: private final Spring sourceSpring1;
275: private final Spring sourceSpring2;
276:
277: private int minValue = Spring.UNSET;
278: private int prefValue = Spring.UNSET;
279: private int maxValue = Spring.UNSET;
280: private int value = Spring.UNSET;
281:
282: public int getMinimumValue() {
283: if (minValue == Spring.UNSET) {
284: minValue = sourceSpring1.getMinimumValue()
285: + sourceSpring2.getMinimumValue();
286: }
287: return minValue;
288: }
289:
290: public int getPreferredValue() {
291: if (prefValue == Spring.UNSET) {
292: prefValue = sourceSpring1.getPreferredValue()
293: + sourceSpring2.getPreferredValue();
294: }
295: return prefValue;
296: }
297:
298: public int getMaximumValue() {
299: if (maxValue == Spring.UNSET) {
300: maxValue = sourceSpring1.getMaximumValue()
301: + sourceSpring2.getMaximumValue();
302: }
303: return maxValue;
304: }
305:
306: public int getValue() {
307: if (value == Spring.UNSET) {
308: value = trim(trim(sourceSpring1.getValue())
309: + trim(sourceSpring2.getValue()));
310: }
311: return value;
312: }
313:
314: public void setValue(final int value) {
315: if (value == Spring.UNSET) {
316: minValue = Spring.UNSET;
317: prefValue = Spring.UNSET;
318: maxValue = Spring.UNSET;
319: if (this .value != Spring.UNSET) {
320: sourceSpring1.setValue(value);
321: sourceSpring2.setValue(value);
322: }
323: this .value = Spring.UNSET;
324: return;
325: }
326:
327: final int c;
328: final int c1;
329: // final int c2;
330: int val1;
331:
332: boolean compression = (value <= getPreferredValue());
333: this .value = value;
334:
335: if (compression) {
336: c = getPreferredValue() - getMinimumValue();
337: c1 = sourceSpring1.getPreferredValue()
338: - sourceSpring1.getMinimumValue();
339: // c2 = sourceSpring2.getPreferredValue()
340: // - sourceSpring2.getMinimumValue();
341: } else {
342: c = getMaximumValue() - getPreferredValue();
343: c1 = sourceSpring1.getMaximumValue()
344: - sourceSpring1.getPreferredValue();
345: // c2 = sourceSpring2.getMaximumValue()
346: // - sourceSpring2.getPreferredValue();
347:
348: }
349:
350: val1 = (value - getPreferredValue());
351: if (c == 0) {
352: if ((val1 * c1 >= 0)) {
353: val1 = sourceSpring1.getPreferredValue();
354: } else {
355: val1 = Spring.TRIMMED_MAX_VALUE
356: + sourceSpring1.getPreferredValue();
357: }
358: } else {
359: val1 = val1 * c1 / c
360: + sourceSpring1.getPreferredValue();
361: }
362:
363: sourceSpring1.setValue(val1);
364: sourceSpring2.setValue(value - val1);
365: }
366:
367: public String toString() {
368: return "(" + sourceSpring1 + " + " + sourceSpring2 + ")";
369: }
370:
371: private SumSpring(final Spring spring1, final Spring spring2) {
372: sourceSpring1 = spring1;
373: sourceSpring2 = spring2;
374: }
375: }
376:
377: private static final class ScaleSpring extends Spring {
378: private final Spring sourceSpring;
379: private float factor;
380:
381: private ScaleSpring(final Spring spring, final float factor) {
382: this .factor = factor;
383: sourceSpring = spring;
384: }
385:
386: public int getMinimumValue() {
387: int val = factor > 0 ? sourceSpring.getMinimumValue()
388: : sourceSpring.getMaximumValue();
389: return trim(Math.round(trim(val) * factor));
390: }
391:
392: public int getPreferredValue() {
393: return trim(Math.round(trim(sourceSpring
394: .getPreferredValue())
395: * factor));
396: }
397:
398: public int getMaximumValue() {
399: int val = factor > 0 ? sourceSpring.getMaximumValue()
400: : sourceSpring.getMinimumValue();
401: return trim(Math.round(trim(val) * factor));
402: }
403:
404: public int getValue() {
405: return trim(Math.round(trim(sourceSpring.getValue())
406: * factor));
407: }
408:
409: public void setValue(final int value) {
410: int newValue;
411: if (value == Spring.UNSET || value == 0) {
412: newValue = value;
413: } else {
414: if (factor != 0) {
415: newValue = Math.round(value / factor);
416: } else {
417: newValue = sourceSpring.getPreferredValue();
418: newValue = value > 0 ? Integer.MAX_VALUE
419: : sourceSpring.getPreferredValue();
420: }
421:
422: }
423: sourceSpring.setValue(newValue);
424: }
425:
426: public String toString() {
427: return "(" + factor + " * " + sourceSpring + ")";
428: }
429:
430: }
431:
432: public static final int UNSET = -2147483648;
433:
434: static final int TRIMMED_MAX_VALUE = Short.MAX_VALUE;
435: static final int TRIMMED_MIN_VALUE = Short.MIN_VALUE;
436:
437: protected Spring() {
438: }
439:
440: public abstract int getMinimumValue();
441:
442: public abstract int getPreferredValue();
443:
444: public abstract int getMaximumValue();
445:
446: public abstract void setValue(int value);
447:
448: public abstract int getValue();
449:
450: public static Spring constant(final int pref) {
451: return new ConstantSpring(pref);
452: }
453:
454: public static Spring constant(final int min, final int pref,
455: final int max) {
456: return new ConstantSpring(min, pref, max);
457: }
458:
459: public static Spring minus(final Spring spring) {
460: return new MinusSpring(spring);
461: }
462:
463: public static Spring sum(final Spring s1, final Spring s2) {
464: return new SumSpring(s1, s2);
465: }
466:
467: public static Spring max(final Spring s1, final Spring s2) {
468: return new MaxSpring(s1, s2);
469: }
470:
471: public static Spring scale(final Spring s, final float factor) {
472: return new ScaleSpring(s, factor);
473: }
474:
475: public static Spring width(final Component c) {
476: return new WidthSpring(c);
477: }
478:
479: public static Spring height(final Component c) {
480: return new HeightSpring(c);
481: }
482:
483: private static int trim(final int value) {
484: if (value > TRIMMED_MAX_VALUE) {
485: return TRIMMED_MAX_VALUE;
486: } else if (value < TRIMMED_MIN_VALUE) {
487: return TRIMMED_MIN_VALUE;
488: }
489: return value;
490: }
491: }
|