1:
47:
48: package ;
49:
50: import ;
51: import ;
52:
53: import ;
54:
55:
61: public class RectangleInsets implements Serializable {
62:
63:
64: private static final long serialVersionUID = 1902273207559319996L;
65:
66:
69: public static final RectangleInsets ZERO_INSETS = new RectangleInsets(
70: UnitType.ABSOLUTE, 0.0, 0.0, 0.0, 0.0);
71:
72:
73: private UnitType unitType;
74:
75:
76: private double top;
77:
78:
79: private double left;
80:
81:
82: private double bottom;
83:
84:
85: private double right;
86:
87:
95: public RectangleInsets(final double top, final double left,
96: final double bottom, final double right) {
97: this(UnitType.ABSOLUTE, top, left, bottom, right);
98: }
99:
100:
110: public RectangleInsets(final UnitType unitType,
111: final double top, final double left,
112: final double bottom, final double right) {
113: if (unitType == null) {
114: throw new IllegalArgumentException("Null 'unitType' argument.");
115: }
116: this.unitType = unitType;
117: this.top = top;
118: this.bottom = bottom;
119: this.left = left;
120: this.right = right;
121: }
122:
123:
129: public UnitType getUnitType() {
130: return this.unitType;
131: }
132:
133:
138: public double getTop() {
139: return this.top;
140: }
141:
142:
147: public double getBottom() {
148: return this.bottom;
149: }
150:
151:
156: public double getLeft() {
157: return this.left;
158: }
159:
160:
165: public double getRight() {
166: return this.right;
167: }
168:
169:
176: public boolean equals(final Object obj) {
177: if (obj == this) {
178: return true;
179: }
180: if (!(obj instanceof RectangleInsets)) {
181: return false;
182: }
183: final RectangleInsets that = (RectangleInsets) obj;
184: if (that.unitType != this.unitType) {
185: return false;
186: }
187: if (this.left != that.left) {
188: return false;
189: }
190: if (this.right != that.right) {
191: return false;
192: }
193: if (this.top != that.top) {
194: return false;
195: }
196: if (this.bottom != that.bottom) {
197: return false;
198: }
199: return true;
200: }
201:
202:
207: public int hashCode() {
208: int result;
209: long temp;
210: result = (this.unitType != null ? this.unitType.hashCode() : 0);
211: temp = this.top != +0.0d ? Double.doubleToLongBits(this.top) : 0L;
212: result = 29 * result + (int) (temp ^ (temp >>> 32));
213: temp = this.bottom != +0.0d ? Double.doubleToLongBits(this.bottom) : 0L;
214: result = 29 * result + (int) (temp ^ (temp >>> 32));
215: temp = this.left != +0.0d ? Double.doubleToLongBits(this.left) : 0L;
216: result = 29 * result + (int) (temp ^ (temp >>> 32));
217: temp = this.right != +0.0d ? Double.doubleToLongBits(this.right) : 0L;
218: result = 29 * result + (int) (temp ^ (temp >>> 32));
219: return result;
220: }
221:
222:
228: public String toString() {
229: return "RectangleInsets[t=" + this.top + ",l=" + this.left
230: + ",b=" + this.bottom + ",r=" + this.right + "]";
231: }
232:
233:
246: public Rectangle2D createAdjustedRectangle(final Rectangle2D base,
247: final LengthAdjustmentType horizontal,
248: final LengthAdjustmentType vertical) {
249: if (base == null) {
250: throw new IllegalArgumentException("Null 'base' argument.");
251: }
252: double x = base.getX();
253: double y = base.getY();
254: double w = base.getWidth();
255: double h = base.getHeight();
256: if (horizontal == LengthAdjustmentType.EXPAND) {
257: final double leftOutset = calculateLeftOutset(w);
258: x = x - leftOutset;
259: w = w + leftOutset + calculateRightOutset(w);
260: }
261: else if (horizontal == LengthAdjustmentType.CONTRACT) {
262: final double leftMargin = calculateLeftInset(w);
263: x = x + leftMargin;
264: w = w - leftMargin - calculateRightInset(w);
265: }
266: if (vertical == LengthAdjustmentType.EXPAND) {
267: final double topMargin = calculateTopOutset(h);
268: y = y - topMargin;
269: h = h + topMargin + calculateBottomOutset(h);
270: }
271: else if (vertical == LengthAdjustmentType.CONTRACT) {
272: final double topMargin = calculateTopInset(h);
273: y = y + topMargin;
274: h = h - topMargin - calculateBottomInset(h);
275: }
276: return new Rectangle2D.Double(x, y, w, h);
277: }
278:
279:
286: public Rectangle2D createInsetRectangle(final Rectangle2D base) {
287: return createInsetRectangle(base, true, true);
288: }
289:
290:
299: public Rectangle2D createInsetRectangle(final Rectangle2D base,
300: final boolean horizontal,
301: final boolean vertical) {
302: if (base == null) {
303: throw new IllegalArgumentException("Null 'base' argument.");
304: }
305: double topMargin = 0.0;
306: double bottomMargin = 0.0;
307: if (vertical) {
308: topMargin = calculateTopInset(base.getHeight());
309: bottomMargin = calculateBottomInset(base.getHeight());
310: }
311: double leftMargin = 0.0;
312: double rightMargin = 0.0;
313: if (horizontal) {
314: leftMargin = calculateLeftInset(base.getWidth());
315: rightMargin = calculateRightInset(base.getWidth());
316: }
317: return new Rectangle2D.Double(
318: base.getX() + leftMargin,
319: base.getY() + topMargin,
320: base.getWidth() - leftMargin - rightMargin,
321: base.getHeight() - topMargin - bottomMargin
322: );
323: }
324:
325:
332: public Rectangle2D createOutsetRectangle(final Rectangle2D base) {
333: return createOutsetRectangle(base, true, true);
334: }
335:
336:
345: public Rectangle2D createOutsetRectangle(final Rectangle2D base,
346: final boolean horizontal,
347: final boolean vertical) {
348: if (base == null) {
349: throw new IllegalArgumentException("Null 'base' argument.");
350: }
351: double topMargin = 0.0;
352: double bottomMargin = 0.0;
353: if (vertical) {
354: topMargin = calculateTopOutset(base.getHeight());
355: bottomMargin = calculateBottomOutset(base.getHeight());
356: }
357: double leftMargin = 0.0;
358: double rightMargin = 0.0;
359: if (horizontal) {
360: leftMargin = calculateLeftOutset(base.getWidth());
361: rightMargin = calculateRightOutset(base.getWidth());
362: }
363: return new Rectangle2D.Double(
364: base.getX() - leftMargin,
365: base.getY() - topMargin,
366: base.getWidth() + leftMargin + rightMargin,
367: base.getHeight() + topMargin + bottomMargin
368: );
369: }
370:
371:
378: public double calculateTopInset(final double height) {
379: double result = this.top;
380: if (this.unitType == UnitType.RELATIVE) {
381: result = (this.top * height);
382: }
383: return result;
384: }
385:
386:
393: public double calculateTopOutset(final double height) {
394: double result = this.top;
395: if (this.unitType == UnitType.RELATIVE) {
396: result = (height / (1 - this.top - this.bottom)) * this.top;
397: }
398: return result;
399: }
400:
401:
408: public double calculateBottomInset(final double height) {
409: double result = this.bottom;
410: if (this.unitType == UnitType.RELATIVE) {
411: result = (this.bottom * height);
412: }
413: return result;
414: }
415:
416:
423: public double calculateBottomOutset(final double height) {
424: double result = this.bottom;
425: if (this.unitType == UnitType.RELATIVE) {
426: result = (height / (1 - this.top - this.bottom)) * this.bottom;
427: }
428: return result;
429: }
430:
431:
438: public double calculateLeftInset(final double width) {
439: double result = this.left;
440: if (this.unitType == UnitType.RELATIVE) {
441: result = (this.left * width);
442: }
443: return result;
444: }
445:
446:
453: public double calculateLeftOutset(final double width) {
454: double result = this.left;
455: if (this.unitType == UnitType.RELATIVE) {
456: result = (width / (1 - this.left - this.right)) * this.left;
457: }
458: return result;
459: }
460:
461:
468: public double calculateRightInset(final double width) {
469: double result = this.right;
470: if (this.unitType == UnitType.RELATIVE) {
471: result = (this.right * width);
472: }
473: return result;
474: }
475:
476:
483: public double calculateRightOutset(final double width) {
484: double result = this.right;
485: if (this.unitType == UnitType.RELATIVE) {
486: result = (width / (1 - this.left - this.right)) * this.right;
487: }
488: return result;
489: }
490:
491:
498: public double trimWidth(final double width) {
499: return width - calculateLeftInset(width) - calculateRightInset(width);
500: }
501:
502:
509: public double extendWidth(final double width) {
510: return width + calculateLeftOutset(width) + calculateRightOutset(width);
511: }
512:
513:
520: public double trimHeight(final double height) {
521: return height
522: - calculateTopInset(height) - calculateBottomInset(height);
523: }
524:
525:
532: public double extendHeight(final double height) {
533: return height
534: + calculateTopOutset(height) + calculateBottomOutset(height);
535: }
536:
537:
542: public void trim(final Rectangle2D area) {
543: final double w = area.getWidth();
544: final double h = area.getHeight();
545: final double l = calculateLeftInset(w);
546: final double r = calculateRightInset(w);
547: final double t = calculateTopInset(h);
548: final double b = calculateBottomInset(h);
549: area.setRect(area.getX() + l, area.getY() + t, w - l - r, h - t - b);
550: }
551:
552: }