1:
44:
45: package ;
46:
47: import ;
48: import ;
49:
50:
54: public class RectangleConstraint {
55:
56:
59: public static final RectangleConstraint NONE = new RectangleConstraint(
60: 0.0, null, LengthConstraintType.NONE,
61: 0.0, null, LengthConstraintType.NONE);
62:
63:
64: private double width;
65:
66:
67: private Range widthRange;
68:
69:
70: private LengthConstraintType widthConstraintType;
71:
72:
73: private double height;
74:
75: private Range heightRange;
76:
77:
78: private LengthConstraintType heightConstraintType;
79:
80:
86: public RectangleConstraint(double w, double h) {
87: this(w, null, LengthConstraintType.FIXED,
88: h, null, LengthConstraintType.FIXED);
89: }
90:
91:
97: public RectangleConstraint(Range w, Range h) {
98: this(0.0, w, LengthConstraintType.RANGE,
99: 0.0, h, LengthConstraintType.RANGE);
100: }
101:
102:
109: public RectangleConstraint(Range w, double h) {
110: this(0.0, w, LengthConstraintType.RANGE,
111: h, null, LengthConstraintType.FIXED);
112: }
113:
114:
121: public RectangleConstraint(double w, Range h) {
122: this(w, null, LengthConstraintType.FIXED,
123: 0.0, h, LengthConstraintType.RANGE);
124: }
125:
126:
136: public RectangleConstraint(double w, Range widthRange,
137: LengthConstraintType widthConstraintType,
138: double h, Range heightRange,
139: LengthConstraintType heightConstraintType) {
140: if (widthConstraintType == null) {
141: throw new IllegalArgumentException("Null 'widthType' argument.");
142: }
143: if (heightConstraintType == null) {
144: throw new IllegalArgumentException("Null 'heightType' argument.");
145: }
146: this.width = w;
147: this.widthRange = widthRange;
148: this.widthConstraintType = widthConstraintType;
149: this.height = h;
150: this.heightRange = heightRange;
151: this.heightConstraintType = heightConstraintType;
152: }
153:
154:
159: public double getWidth() {
160: return this.width;
161: }
162:
163:
168: public Range getWidthRange() {
169: return this.widthRange;
170: }
171:
172:
177: public LengthConstraintType getWidthConstraintType() {
178: return this.widthConstraintType;
179: }
180:
181:
186: public double getHeight() {
187: return this.height;
188: }
189:
190:
195: public Range getHeightRange() {
196: return this.heightRange;
197: }
198:
199:
204: public LengthConstraintType getHeightConstraintType() {
205: return this.heightConstraintType;
206: }
207:
208:
214: public RectangleConstraint toUnconstrainedWidth() {
215: if (this.widthConstraintType == LengthConstraintType.NONE) {
216: return this;
217: }
218: else {
219: return new RectangleConstraint(this.width, this.widthRange,
220: LengthConstraintType.NONE, this.height, this.heightRange,
221: this.heightConstraintType);
222: }
223: }
224:
225:
231: public RectangleConstraint toUnconstrainedHeight() {
232: if (this.heightConstraintType == LengthConstraintType.NONE) {
233: return this;
234: }
235: else {
236: return new RectangleConstraint(this.width, this.widthRange,
237: this.widthConstraintType, 0.0, this.heightRange,
238: LengthConstraintType.NONE);
239: }
240: }
241:
242:
250: public RectangleConstraint toFixedWidth(double width) {
251: return new RectangleConstraint(width, this.widthRange,
252: LengthConstraintType.FIXED, this.height, this.heightRange,
253: this.heightConstraintType);
254: }
255:
256:
264: public RectangleConstraint toFixedHeight(double height) {
265: return new RectangleConstraint(this.width, this.widthRange,
266: this.widthConstraintType, height, this.heightRange,
267: LengthConstraintType.FIXED);
268: }
269:
270:
278: public RectangleConstraint toRangeWidth(Range range) {
279: if (range == null) {
280: throw new IllegalArgumentException("Null 'range' argument.");
281: }
282: return new RectangleConstraint(range.getUpperBound(), range,
283: LengthConstraintType.RANGE, this.height, this.heightRange,
284: this.heightConstraintType);
285: }
286:
287:
295: public RectangleConstraint toRangeHeight(Range range) {
296: if (range == null) {
297: throw new IllegalArgumentException("Null 'range' argument.");
298: }
299: return new RectangleConstraint(this.width, this.widthRange,
300: this.widthConstraintType, range.getUpperBound(), range,
301: LengthConstraintType.RANGE);
302: }
303:
304:
310: public String toString() {
311: return "RectangleConstraint["
312: + this.widthConstraintType.toString() + ": width="
313: + this.width + ", height=" + this.height + "]";
314: }
315:
316:
324: public Size2D calculateConstrainedSize(Size2D base) {
325: Size2D result = new Size2D();
326: if (this.widthConstraintType == LengthConstraintType.NONE) {
327: result.width = base.width;
328: if (this.heightConstraintType == LengthConstraintType.NONE) {
329: result.height = base.height;
330: }
331: else if (this.heightConstraintType == LengthConstraintType.RANGE) {
332: result.height = this.heightRange.constrain(base.height);
333: }
334: else if (this.heightConstraintType == LengthConstraintType.FIXED) {
335: result.height = this.height;
336: }
337: }
338: else if (this.widthConstraintType == LengthConstraintType.RANGE) {
339: result.width = this.widthRange.constrain(base.width);
340: if (this.heightConstraintType == LengthConstraintType.NONE) {
341: result.height = base.height;
342: }
343: else if (this.heightConstraintType == LengthConstraintType.RANGE) {
344: result.height = this.heightRange.constrain(base.height);
345: }
346: else if (this.heightConstraintType == LengthConstraintType.FIXED) {
347: result.height = this.height;
348: }
349: }
350: else if (this.widthConstraintType == LengthConstraintType.FIXED) {
351: result.width = this.width;
352: if (this.heightConstraintType == LengthConstraintType.NONE) {
353: result.height = base.height;
354: }
355: else if (this.heightConstraintType == LengthConstraintType.RANGE) {
356: result.height = this.heightRange.constrain(base.height);
357: }
358: else if (this.heightConstraintType == LengthConstraintType.FIXED) {
359: result.height = this.height;
360: }
361: }
362: return result;
363: }
364:
365: }