001: /**
002: * ===========================================
003: * JFreeReport : a free Java reporting library
004: * ===========================================
005: *
006: * Project Info: http://reporting.pentaho.org/
007: *
008: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
009: *
010: * This library is free software; you can redistribute it and/or modify it under the terms
011: * of the GNU Lesser General Public License as published by the Free Software Foundation;
012: * either version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
015: * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016: * See the GNU Lesser General Public License for more details.
017: *
018: * You should have received a copy of the GNU Lesser General Public License along with this
019: * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: *
022: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
023: * in the United States and other countries.]
024: *
025: * ------------
026: * BoxDefinition.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report.layout.model.context;
030:
031: import org.jfree.report.layout.model.Border;
032: import org.jfree.report.layout.model.RenderLength;
033: import org.jfree.report.layout.model.RenderNode;
034: import org.jfree.report.util.geom.StrictGeomUtility;
035:
036: /**
037: * A box definition. The paddings or maximum height/width cannot be percentages or AUTO.
038: *
039: * @author Thomas Morgner
040: */
041: public final class BoxDefinition {
042: public static final BoxDefinition EMPTY = new BoxDefinition()
043: .lock();
044: public static final RenderLength DEFAULT_PREFERRED_WIDTH = new RenderLength(
045: StrictGeomUtility.toInternalValue(100), true);
046:
047: private Boolean empty;
048: private long paddingTop;
049: private long paddingLeft;
050: private long paddingBottom;
051: private long paddingRight;
052: private Border border;
053: private RenderLength preferredHeight;
054: private RenderLength preferredWidth;
055: private RenderLength minimumHeight;
056: private RenderLength minimumWidth;
057: private RenderLength marginTop;
058: private RenderLength marginBottom;
059: private RenderLength marginLeft;
060: private RenderLength marginRight;
061: private RenderLength maximumHeight;
062: private RenderLength maximumWidth;
063: private RenderLength positionX;
064: private RenderLength positionY;
065: private RenderLength fixedPosition;
066: private boolean locked;
067: private boolean sizeSpecifiesBorderBox;
068:
069: public BoxDefinition() {
070: border = Border.EMPTY_BORDER;
071: preferredWidth = RenderLength.AUTO;
072: preferredHeight = RenderLength.AUTO;
073: minimumHeight = RenderLength.EMPTY;
074: minimumWidth = RenderLength.EMPTY;
075: marginTop = RenderLength.EMPTY;
076: marginLeft = RenderLength.EMPTY;
077: marginBottom = RenderLength.EMPTY;
078: marginRight = RenderLength.EMPTY;
079: maximumWidth = RenderLength.AUTO;
080: maximumHeight = RenderLength.AUTO;
081: positionX = RenderLength.AUTO;
082: positionY = RenderLength.AUTO;
083: fixedPosition = RenderLength.AUTO;
084: sizeSpecifiesBorderBox = true;
085: }
086:
087: public void setSizeSpecifiesBorderBox(
088: final boolean sizeSpecifiesBorderBox) {
089: this .sizeSpecifiesBorderBox = sizeSpecifiesBorderBox;
090: }
091:
092: public boolean isSizeSpecifiesBorderBox() {
093: return sizeSpecifiesBorderBox;
094: }
095:
096: public BoxDefinition lock() {
097: locked = true;
098: return this ;
099: }
100:
101: public BoxDefinition derive() {
102: final BoxDefinition retval = new BoxDefinition();
103: retval.border = border;
104: retval.preferredWidth = preferredWidth;
105: retval.preferredHeight = preferredHeight;
106: retval.minimumHeight = minimumHeight;
107: retval.minimumWidth = minimumWidth;
108: retval.marginTop = marginTop;
109: retval.marginLeft = marginLeft;
110: retval.marginBottom = marginBottom;
111: retval.marginRight = marginRight;
112: retval.maximumWidth = maximumWidth;
113: retval.maximumHeight = maximumHeight;
114: retval.positionX = positionX;
115: retval.positionY = positionY;
116: retval.fixedPosition = fixedPosition;
117: retval.locked = locked;
118: return retval;
119: }
120:
121: public RenderLength getFixedPosition() {
122: return fixedPosition;
123: }
124:
125: public void setFixedPosition(final RenderLength fixedPosition) {
126: if (locked) {
127: throw new IllegalStateException();
128: }
129:
130: if (fixedPosition == null) {
131: throw new NullPointerException();
132: }
133:
134: this .fixedPosition = fixedPosition;
135: }
136:
137: //
138: // public RenderLength getPositionX()
139: // {
140: // return positionX;
141: // }
142: //
143: // public void setPositionX(final RenderLength positionX)
144: // {
145: // if (locked)
146: // {
147: // throw new IllegalStateException();
148: // }
149: //
150: // if (positionX == null)
151: // {
152: // throw new NullPointerException();
153: // }
154: //
155: // this.positionX = positionX;
156: // }
157: //
158: // public RenderLength getPositionY()
159: // {
160: // return positionY;
161: // }
162: //
163: // public void setPositionY(final RenderLength positionY)
164: // {
165: // if (locked)
166: // {
167: // throw new IllegalStateException();
168: // }
169: //
170: // if (positionY == null)
171: // {
172: // throw new NullPointerException();
173: // }
174: //
175: // this.positionY = positionY;
176: // }
177:
178: public Border getBorder() {
179: return border;
180: }
181:
182: public void setBorder(final Border border) {
183: if (locked) {
184: throw new IllegalStateException();
185: }
186:
187: if (border == null) {
188: throw new NullPointerException();
189: }
190:
191: this .border = border;
192: }
193:
194: public long getPaddingTop() {
195: return paddingTop;
196: }
197:
198: public void setPaddingTop(final long paddingTop) {
199: if (locked) {
200: throw new IllegalStateException();
201: }
202:
203: this .paddingTop = paddingTop;
204: }
205:
206: public long getPaddingLeft() {
207: return paddingLeft;
208: }
209:
210: public void setPaddingLeft(final long paddingLeft) {
211: if (locked) {
212: throw new IllegalStateException();
213: }
214:
215: this .paddingLeft = paddingLeft;
216: }
217:
218: public long getPaddingBottom() {
219: return paddingBottom;
220: }
221:
222: public void setPaddingBottom(final long paddingBottom) {
223: if (locked) {
224: throw new IllegalStateException();
225: }
226:
227: this .paddingBottom = paddingBottom;
228: }
229:
230: public long getPaddingRight() {
231: return paddingRight;
232: }
233:
234: public void setPaddingRight(final long paddingRight) {
235: if (locked) {
236: throw new IllegalStateException();
237: }
238: this .paddingRight = paddingRight;
239: }
240:
241: public RenderLength getPreferredHeight() {
242: return preferredHeight;
243: }
244:
245: public void setPreferredHeight(final RenderLength preferredHeight) {
246: if (locked) {
247: throw new IllegalStateException();
248: }
249: if (preferredHeight == null) {
250: throw new NullPointerException();
251: }
252:
253: this .preferredHeight = preferredHeight;
254: }
255:
256: public RenderLength getPreferredWidth() {
257: return preferredWidth;
258: }
259:
260: public void setPreferredWidth(final RenderLength preferredWidth) {
261: if (locked) {
262: throw new IllegalStateException();
263: }
264: if (preferredWidth == null) {
265: throw new NullPointerException();
266: }
267:
268: this .preferredWidth = preferredWidth;
269: }
270:
271: public RenderLength getMinimumHeight() {
272: return minimumHeight;
273: }
274:
275: public void setMinimumHeight(final RenderLength minimumHeight) {
276: if (locked) {
277: throw new IllegalStateException();
278: }
279: if (minimumHeight == null) {
280: throw new NullPointerException();
281: }
282: this .minimumHeight = minimumHeight;
283: }
284:
285: public RenderLength getMinimumWidth() {
286: return minimumWidth;
287: }
288:
289: public void setMinimumWidth(final RenderLength minimumWidth) {
290: if (locked) {
291: throw new IllegalStateException();
292: }
293: if (minimumWidth == null) {
294: throw new NullPointerException();
295: }
296: this .minimumWidth = minimumWidth;
297: }
298:
299: public RenderLength getMaximumHeight() {
300: return maximumHeight;
301: }
302:
303: public void setMaximumHeight(final RenderLength maximumHeight) {
304: if (locked) {
305: throw new IllegalStateException();
306: }
307: if (maximumHeight == null) {
308: throw new NullPointerException();
309: }
310:
311: this .maximumHeight = maximumHeight;
312: }
313:
314: public RenderLength getMaximumWidth() {
315: return maximumWidth;
316: }
317:
318: public void setMaximumWidth(final RenderLength maximumWidth) {
319: if (locked) {
320: throw new IllegalStateException();
321: }
322: if (maximumWidth == null) {
323: throw new NullPointerException();
324: }
325:
326: this .maximumWidth = maximumWidth;
327: }
328:
329: public RenderLength getMarginTop() {
330: return marginTop;
331: }
332:
333: public void setMarginTop(final RenderLength marginTop) {
334: if (locked) {
335: throw new IllegalStateException();
336: }
337: if (marginTop == null) {
338: throw new NullPointerException();
339: }
340:
341: this .marginTop = marginTop;
342: }
343:
344: public RenderLength getMarginBottom() {
345: return marginBottom;
346: }
347:
348: public void setMarginBottom(final RenderLength marginBottom) {
349: if (locked) {
350: throw new IllegalStateException();
351: }
352: if (marginBottom == null) {
353: throw new NullPointerException();
354: }
355:
356: this .marginBottom = marginBottom;
357: }
358:
359: public RenderLength getMarginLeft() {
360: return marginLeft;
361: }
362:
363: public void setMarginLeft(final RenderLength marginLeft) {
364: if (locked) {
365: throw new IllegalStateException();
366: }
367: if (marginLeft == null) {
368: throw new NullPointerException();
369: }
370: this .marginLeft = marginLeft;
371: }
372:
373: public RenderLength getMarginRight() {
374: return marginRight;
375: }
376:
377: public void setMarginRight(final RenderLength marginRight) {
378: if (locked) {
379: throw new IllegalStateException();
380: }
381: if (marginRight == null) {
382: throw new NullPointerException();
383: }
384: this .marginRight = marginRight;
385: }
386:
387: public boolean isEmpty() {
388: if (empty != null) {
389: return empty.booleanValue();
390: }
391:
392: if (paddingTop != 0) {
393: empty = Boolean.FALSE;
394: return false;
395: }
396: if (paddingLeft != 0) {
397: empty = Boolean.FALSE;
398: return false;
399: }
400: if (paddingBottom != 0) {
401: empty = Boolean.FALSE;
402: return false;
403: }
404: if (paddingRight != 0) {
405: empty = Boolean.FALSE;
406: return false;
407: }
408:
409: if (border.isEmpty()) {
410: empty = Boolean.FALSE;
411: return false;
412: }
413:
414: empty = Boolean.TRUE;
415: return true;
416: }
417:
418: /**
419: * Split the box definition for the given major axis. A horizontal axis will perform vertical splits (resulting in a
420: * left and right box definition) and a given vertical axis will split the box into a top and bottom box.
421: *
422: * @param axis
423: * @return
424: */
425: public BoxDefinition[] split(final int axis) {
426: if (axis == RenderNode.HORIZONTAL_AXIS) {
427: return splitVertically();
428: }
429: return splitHorizontally();
430: }
431:
432: private BoxDefinition[] splitVertically() {
433: final Border[] borders = border.splitVertically(null);
434: final BoxDefinition first = new BoxDefinition();
435: first.marginTop = marginTop;
436: first.marginLeft = marginLeft;
437: first.marginBottom = marginBottom;
438: first.marginRight = RenderLength.EMPTY;
439: first.paddingBottom = paddingBottom;
440: first.paddingTop = paddingTop;
441: first.paddingLeft = paddingLeft;
442: first.paddingRight = 0;
443: first.border = borders[0];
444: first.preferredHeight = preferredHeight;
445: first.preferredWidth = preferredWidth;
446: first.minimumHeight = minimumHeight;
447: first.minimumWidth = minimumWidth;
448: first.maximumHeight = maximumHeight;
449: first.maximumWidth = maximumWidth;
450: first.positionX = RenderLength.AUTO;
451: first.positionY = positionY;
452:
453: final BoxDefinition second = new BoxDefinition();
454: second.marginTop = marginTop;
455: second.marginLeft = RenderLength.EMPTY;
456: second.marginBottom = marginBottom;
457: second.marginRight = marginRight;
458: second.paddingBottom = paddingBottom;
459: second.paddingTop = paddingTop;
460: second.paddingLeft = 0;
461: second.paddingRight = paddingRight;
462: second.border = borders[1];
463: second.preferredHeight = preferredHeight;
464: second.preferredWidth = preferredWidth;
465: second.minimumHeight = minimumHeight;
466: second.minimumWidth = minimumWidth;
467: second.maximumHeight = maximumHeight;
468: second.maximumWidth = maximumWidth;
469: second.positionX = RenderLength.AUTO;
470: second.positionY = positionY;
471:
472: final BoxDefinition[] boxes = new BoxDefinition[2];
473: boxes[0] = first;
474: boxes[1] = second;
475: return boxes;
476: }
477:
478: private BoxDefinition[] splitHorizontally() {
479: final Border[] borders = border.splitHorizontally(null);
480:
481: final BoxDefinition first = new BoxDefinition();
482: first.marginTop = marginTop;
483: first.marginLeft = marginLeft;
484: first.marginBottom = RenderLength.EMPTY;
485: first.marginRight = marginRight;
486: first.paddingBottom = 0;
487: first.paddingTop = paddingTop;
488: first.paddingLeft = paddingLeft;
489: first.paddingRight = paddingRight;
490: first.border = borders[0];
491: first.preferredHeight = preferredHeight;
492: first.preferredWidth = preferredWidth;
493: first.minimumHeight = minimumHeight;
494: first.minimumWidth = minimumWidth;
495: first.maximumHeight = maximumHeight;
496: first.maximumWidth = maximumWidth;
497: first.positionX = positionX;
498: first.positionY = RenderLength.AUTO;
499:
500: final BoxDefinition second = new BoxDefinition();
501: second.marginTop = RenderLength.EMPTY;
502: second.marginLeft = marginLeft;
503: second.marginBottom = marginBottom;
504: second.marginRight = marginRight;
505: second.paddingBottom = paddingBottom;
506: second.paddingTop = 0;
507: second.paddingLeft = paddingLeft;
508: second.paddingRight = paddingRight;
509: second.border = borders[1];
510: second.preferredHeight = preferredHeight;
511: second.preferredWidth = preferredWidth;
512: second.minimumHeight = minimumHeight;
513: second.minimumWidth = minimumWidth;
514: second.maximumHeight = maximumHeight;
515: second.maximumWidth = maximumWidth;
516: second.positionX = positionX;
517: second.positionY = RenderLength.AUTO;
518:
519: final BoxDefinition[] boxes = new BoxDefinition[2];
520: boxes[0] = first;
521: boxes[1] = second;
522: return boxes;
523: }
524:
525: public String toString() {
526: return "BoxDefinition{" + "paddingTop=" + paddingTop
527: + ", paddingLeft=" + paddingLeft + ", paddingBottom="
528: + paddingBottom + ", paddingRight=" + paddingRight
529: + ", border=" + border + ", preferredHeight="
530: + preferredHeight + ", preferredWidth="
531: + preferredWidth + ", minimumHeight=" + minimumHeight
532: + ", minimumWidth=" + minimumWidth + ", marginTop="
533: + marginTop + ", marginBottom=" + marginBottom
534: + ", marginLeft=" + marginLeft + ", marginRight="
535: + marginRight + ", maximumHeight=" + maximumHeight
536: + ", maximumWidth=" + maximumWidth + ", positionX="
537: + positionX + ", positionY=" + positionY + '}';
538: }
539: }
|