001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /* $Id: NumericProperty.java 474225 2006-11-13 10:08:19Z jeremias $ */
019:
020: package org.apache.fop.fo.expr;
021:
022: import java.awt.Color;
023:
024: import org.apache.fop.apps.FOUserAgent;
025: import org.apache.fop.datatypes.Length;
026: import org.apache.fop.datatypes.PercentBaseContext;
027: import org.apache.fop.datatypes.Numeric;
028: import org.apache.fop.fo.properties.Property;
029:
030: /**
031: * A numeric property which hold the final absolute result of an expression
032: * calculations.
033: */
034: public class NumericProperty extends Property implements Numeric,
035: Length {
036: private double value;
037: private int dim;
038:
039: /**
040: * Construct a Numeric object by specifying one or more components,
041: * including absolute length, percent length, table units.
042: * @param value The value of the numeric.
043: * @param dim The dimension of the value. 0 for a Number, 1 for a Length
044: * (any type), >1, <0 if Lengths have been multiplied or divided.
045: */
046: protected NumericProperty(double value, int dim) {
047: this .value = value;
048: this .dim = dim;
049: }
050:
051: /**
052: * Return the dimension.
053: * @see Numeric#getDimension()
054: */
055: public int getDimension() {
056: return dim;
057: }
058:
059: /**
060: * Return the value.
061: * @see Numeric#getNumericValue()
062: */
063: public double getNumericValue() {
064: return value;
065: }
066:
067: /**
068: * @see Numeric#getNumericValue(PercentBaseContext)
069: */
070: public double getNumericValue(PercentBaseContext context) {
071: return value;
072: }
073:
074: /**
075: * Return true of the numeric is absolute.
076: * @see Numeric#isAbsolute()
077: */
078: public boolean isAbsolute() {
079: return true;
080: }
081:
082: /** @see org.apache.fop.fo.properties.Property#getNumeric() */
083: public Numeric getNumeric() {
084: return this ;
085: }
086:
087: /** @see org.apache.fop.fo.properties.Property#getNumber() */
088: public Number getNumber() {
089: return new Double(value);
090: }
091:
092: /** @see org.apache.fop.datatypes.Numeric#getValue() */
093: public int getValue() {
094: return (int) value;
095: }
096:
097: /** @see org.apache.fop.datatypes.Numeric#getValue(PercentBaseContext) */
098: public int getValue(PercentBaseContext context) {
099: return (int) value;
100: }
101:
102: /** @see org.apache.fop.fo.properties.Property#getLength() */
103: public Length getLength() {
104: if (dim == 1) {
105: return this ;
106: }
107: log.error("Can't create length with dimension " + dim);
108: return null;
109: }
110:
111: /** @see org.apache.fop.fo.properties.Property#getColor(FOUserAgent) */
112: public Color getColor(FOUserAgent foUserAgent) {
113: // TODO: try converting to numeric number and then to color
114: return null;
115: }
116:
117: /** @see org.apache.fop.fo.properties.Property#getObject() */
118: public Object getObject() {
119: return this ;
120: }
121:
122: /** @see java.lang.Object#toString() */
123: public String toString() {
124: if (dim == 1) {
125: return (int) value + "mpt";
126: } else {
127: return value + "^" + dim;
128: }
129: }
130: }
|