1:
46:
47: package ;
48:
49: import ;
50: import ;
51:
52:
56: public class RectangleConstraint {
57:
58:
61: public static final RectangleConstraint NONE = new RectangleConstraint(
62: 0.0, null, LengthConstraintType.NONE,
63: 0.0, null, LengthConstraintType.NONE
64: );
65:
66:
67: private double width;
68:
69:
70: private Range widthRange;
71:
72:
73: private LengthConstraintType widthConstraintType;
74:
75:
76: private double height;
77:
78: private Range heightRange;
79:
80:
81: private LengthConstraintType heightConstraintType;
82:
83:
89: public RectangleConstraint(double w, double h) {
90: this(
91: w, null, LengthConstraintType.FIXED,
92: h, null, LengthConstraintType.FIXED
93: );
94: }
95:
96:
102: public RectangleConstraint(Range w, Range h) {
103: this(
104: 0.0, w, LengthConstraintType.RANGE,
105: 0.0, h, LengthConstraintType.RANGE
106: );
107: }
108:
109:
116: public RectangleConstraint(Range w, double h) {
117: this(
118: 0.0, w, LengthConstraintType.RANGE,
119: h, null, LengthConstraintType.FIXED
120: );
121: }
122:
123:
130: public RectangleConstraint(double w, Range h) {
131: this(
132: w, null, LengthConstraintType.FIXED,
133: 0.0, h, LengthConstraintType.RANGE
134: );
135: }
136:
137:
147: public RectangleConstraint(double w, Range widthRange,
148: LengthConstraintType widthConstraintType,
149: double h, Range heightRange,
150: LengthConstraintType heightConstraintType) {
151: if (widthConstraintType == null) {
152: throw new IllegalArgumentException("Null 'widthType' argument.");
153: }
154: if (heightConstraintType == null) {
155: throw new IllegalArgumentException("Null 'heightType' argument.");
156: }
157: this.width = w;
158: this.widthRange = widthRange;
159: this.widthConstraintType = widthConstraintType;
160: this.height = h;
161: this.heightRange = heightRange;
162: this.heightConstraintType = heightConstraintType;
163: }
164:
165:
170: public double getWidth() {
171: return this.width;
172: }
173:
174:
179: public Range getWidthRange() {
180: return this.widthRange;
181: }
182:
183:
188: public LengthConstraintType getWidthConstraintType() {
189: return this.widthConstraintType;
190: }
191:
192:
197: public double getHeight() {
198: return this.height;
199: }
200:
201:
206: public Range getHeightRange() {
207: return this.heightRange;
208: }
209:
210:
215: public LengthConstraintType getHeightConstraintType() {
216: return this.heightConstraintType;
217: }
218:
219:
225: public RectangleConstraint toUnconstrainedWidth() {
226: if (this.widthConstraintType == LengthConstraintType.NONE) {
227: return this;
228: }
229: else {
230: return new RectangleConstraint(
231: this.width, this.widthRange, LengthConstraintType.NONE,
232: this.height, this.heightRange, this.heightConstraintType
233: );
234: }
235: }
236:
237:
243: public RectangleConstraint toUnconstrainedHeight() {
244: if (this.heightConstraintType == LengthConstraintType.NONE) {
245: return this;
246: }
247: else {
248: return new RectangleConstraint(
249: this.width, this.widthRange, this.widthConstraintType,
250: 0.0, this.heightRange, LengthConstraintType.NONE
251: );
252: }
253: }
254:
255:
263: public RectangleConstraint toFixedWidth(double width) {
264: return new RectangleConstraint(
265: width, this.widthRange, LengthConstraintType.FIXED,
266: this.height, this.heightRange, this.heightConstraintType
267: );
268: }
269:
270:
278: public RectangleConstraint toFixedHeight(double height) {
279: return new RectangleConstraint(
280: this.width, this.widthRange, this.widthConstraintType,
281: height, this.heightRange, LengthConstraintType.FIXED
282: );
283: }
284:
285:
293: public RectangleConstraint toRangeWidth(Range range) {
294: if (range == null) {
295: throw new IllegalArgumentException("Null 'range' argument.");
296: }
297: return new RectangleConstraint(
298: range.getUpperBound(), range, LengthConstraintType.RANGE,
299: this.height, this.heightRange, this.heightConstraintType
300: );
301: }
302:
303:
311: public RectangleConstraint toRangeHeight(Range range) {
312: if (range == null) {
313: throw new IllegalArgumentException("Null 'range' argument.");
314: }
315: return new RectangleConstraint(
316: this.width, this.widthRange, this.widthConstraintType,
317: range.getUpperBound(), range, LengthConstraintType.RANGE
318: );
319: }
320:
321:
327: public String toString() {
328: return "RectangleConstraint["
329: + this.widthConstraintType.toString() + ": width="
330: + this.width + ", height=" + this.height + "]";
331: }
332:
333:
341: public Size2D calculateConstrainedSize(Size2D base) {
342: Size2D result = new Size2D();
343: if (this.widthConstraintType == LengthConstraintType.NONE) {
344: result.width = base.width;
345: if (this.heightConstraintType == LengthConstraintType.NONE) {
346: result.height = base.height;
347: }
348: else if (this.heightConstraintType == LengthConstraintType.RANGE) {
349: result.height = this.heightRange.constrain(base.height);
350: }
351: else if (this.heightConstraintType == LengthConstraintType.FIXED) {
352: result.height = this.height;
353: }
354: }
355: else if (this.widthConstraintType == LengthConstraintType.RANGE) {
356: result.width = this.widthRange.constrain(base.width);
357: if (this.heightConstraintType == LengthConstraintType.NONE) {
358: result.height = base.height;
359: }
360: else if (this.heightConstraintType == LengthConstraintType.RANGE) {
361: result.height = this.heightRange.constrain(base.height);
362: }
363: else if (this.heightConstraintType == LengthConstraintType.FIXED) {
364: result.height = this.height;
365: }
366: }
367: else if (this.widthConstraintType == LengthConstraintType.FIXED) {
368: result.width = this.width;
369: if (this.heightConstraintType == LengthConstraintType.NONE) {
370: result.height = base.height;
371: }
372: else if (this.heightConstraintType == LengthConstraintType.RANGE) {
373: result.height = this.heightRange.constrain(base.height);
374: }
375: else if (this.heightConstraintType == LengthConstraintType.FIXED) {
376: result.height = this.height;
377: }
378: }
379: return result;
380: }
381:
382: }