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: * RenderLength.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report.layout.model;
030:
031: import org.jfree.report.util.geom.StrictGeomUtility;
032:
033: /**
034: * Creation-Date: 09.07.2006, 21:03:12
035: *
036: * @author Thomas Morgner
037: */
038: public class RenderLength {
039: public static final RenderLength AUTO = new RenderLength(
040: Long.MIN_VALUE, false);
041: public static final RenderLength EMPTY = new RenderLength(0, false);
042:
043: private long value;
044: private boolean percentage;
045:
046: public RenderLength(final long value, final boolean percentage) {
047: this .value = value;
048: this .percentage = percentage;
049: }
050:
051: public long getValue() {
052: return value;
053: }
054:
055: public boolean isPercentage() {
056: return percentage;
057: }
058:
059: public boolean equals(final Object o) {
060: if (this == o) {
061: return true;
062: }
063: if (o == null || getClass() != o.getClass()) {
064: return false;
065: }
066:
067: final RenderLength that = (RenderLength) o;
068:
069: if (percentage != that.percentage) {
070: return false;
071: }
072: if (value != that.value) {
073: return false;
074: }
075:
076: return true;
077: }
078:
079: public int hashCode() {
080: int result = (int) (value ^ (value >>> 32));
081: result = 29 * result + (percentage ? 1 : 0);
082: return result;
083: }
084:
085: public long resolve(final long parent) {
086: if (isPercentage()) {
087: return StrictGeomUtility.multiply(value, parent) / 100;
088: } else if (value == Long.MIN_VALUE) {
089: return 0;
090: } else {
091: return value;
092: }
093: }
094:
095: public long resolve(final long parent, final long auto) {
096: if (isPercentage()) {
097: return StrictGeomUtility.multiply(value, parent) / 100;
098: } else if (value == Long.MIN_VALUE) {
099: return auto;
100: } else {
101: return value;
102: }
103: }
104:
105: public static long resolveLength(final long parent,
106: final double rawvalue) {
107: final long value = StrictGeomUtility.toInternalValue(rawvalue);
108: if (value == Long.MIN_VALUE) {
109: return 0;
110: }
111: if (value < 0) {
112: return -StrictGeomUtility.multiply(value, parent) / 100;
113: } else {
114: return value;
115: }
116: }
117:
118: public static RenderLength createFromRaw(final double rawValue) {
119: if (rawValue < 0) {
120: return new RenderLength(StrictGeomUtility
121: .toInternalValue(-rawValue), true);
122: }
123: if (rawValue == 0) {
124: return EMPTY;
125: }
126: return new RenderLength(StrictGeomUtility
127: .toInternalValue(rawValue), false);
128: }
129:
130: public RenderLength resolveToRenderLength(final long parent) {
131: if (isPercentage()) {
132: if (parent <= 0) {
133: // An unresolvable parent ...
134: return RenderLength.AUTO;
135: }
136: // This may resolve to zero - which is valid
137: final long value = StrictGeomUtility.multiply(this .value,
138: parent) / 100;
139: return new RenderLength(value, false);
140: } else if (value <= 0) {
141: return RenderLength.AUTO;
142: } else {
143: return new RenderLength(value, false);
144: }
145: }
146:
147: public String toString() {
148: if (value == Long.MIN_VALUE) {
149: return "RenderLength{value=AUTO}";
150: }
151: if (isPercentage()) {
152: return "RenderLength{" + "value="
153: + StrictGeomUtility.toExternalValue(value) + "% }";
154: } else {
155: return "RenderLength{" + "value=" + value + "micro-pt }";
156: }
157: }
158: }
|