KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > fo > properties > NumberProperty


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /* $Id: NumberProperty.java 488960 2006-12-20 08:34:28Z spepping $ */
19
20 package org.apache.fop.fo.properties;
21
22 import java.awt.Color JavaDoc;
23
24 import org.apache.fop.apps.FOUserAgent;
25 import org.apache.fop.datatypes.Length;
26 import org.apache.fop.datatypes.Numeric;
27 import org.apache.fop.datatypes.PercentBaseContext;
28 import org.apache.fop.fo.FObj;
29 import org.apache.fop.fo.PropertyList;
30 import org.apache.fop.fo.expr.PropertyException;
31
32 /**
33  * Class for handling numeric properties
34  */

35 public class NumberProperty extends Property implements Numeric {
36
37     /**
38      * Inner class for making NumberProperty objects
39      */

40     public static class Maker extends PropertyMaker {
41
42         /**
43          * Constructor for NumberProperty.Maker
44          * @param propId the id of the property for which a Maker should be created
45          */

46         public Maker(int propId) {
47             super(propId);
48         }
49
50         /**
51          * @throws PropertyException
52          * @see PropertyMaker#convertProperty
53          */

54         public Property convertProperty(Property p,
55                                         PropertyList propertyList, FObj fo)
56                     throws PropertyException {
57             if (p instanceof NumberProperty) {
58                 return p;
59             }
60             if (p instanceof EnumProperty) {
61                 return EnumNumber.getInstance(p);
62             }
63             Number JavaDoc val = p.getNumber();
64             if (val != null) {
65                 return new NumberProperty(val);
66             }
67             return convertPropertyDatatype(p, propertyList, fo);
68         }
69
70     }
71
72     private Number JavaDoc number;
73
74     /**
75      * Constructor for Number input
76      * @param num Number object value for property
77      */

78     public NumberProperty(Number JavaDoc num) {
79         this.number = num;
80     }
81
82     /**
83      * Constructor for double input
84      * @param num double numeric value for property
85      */

86     public NumberProperty(double num) {
87         this.number = new Double JavaDoc(num);
88     }
89
90     /**
91      * Constructor for integer input
92      * @param num integer numeric value for property
93      */

94     public NumberProperty(int num) {
95         this.number = new Integer JavaDoc(num);
96     }
97     
98     /**
99      * Plain number always has a dimension of 0.
100      * @return a dimension of 0.
101      * @see Numeric#getDimension()
102      */

103     public int getDimension() {
104         return 0;
105     }
106
107     /**
108      * Return the value of this Numeric.
109      * @return The value as a double.
110      * @see Numeric#getNumericValue()
111      */

112     public double getNumericValue() {
113         return number.doubleValue();
114     }
115
116     /**
117      * Return the value of this Numeric.
118      * @param context Evaluation context
119      * @return The value as a double.
120      * @see Numeric#getNumericValue(PercentBaseContext)
121      */

122     public double getNumericValue(PercentBaseContext context) {
123         return getNumericValue();
124     }
125
126     /** @see org.apache.fop.datatypes.Numeric#getValue() */
127     public int getValue() {
128         return number.intValue();
129     }
130
131     /**
132      * Return the value
133      * @param context Evaluation context
134      * @return The value as an int.
135      * @see Numeric#getValue(PercentBaseContext)
136      */

137     public int getValue(PercentBaseContext context) {
138         return getValue();
139     }
140
141     /**
142      * Return true because all numbers are absolute.
143      * @return true.
144      * @see Numeric#isAbsolute()
145      */

146     public boolean isAbsolute() {
147         return true;
148     }
149
150     /**
151      * @return this.number cast as a Number
152      */

153     public Number JavaDoc getNumber() {
154         return this.number;
155     }
156
157     /**
158      * @return this.number cast as an Object
159      */

160     public Object JavaDoc getObject() {
161         return this.number;
162     }
163
164     /**
165      * Convert NumberProperty to Numeric object
166      * @return Numeric object corresponding to this
167      */

168     public Numeric getNumeric() {
169         return this;
170     }
171
172     /** @see org.apache.fop.fo.properties.Property#getLength() */
173     public Length getLength() {
174         //Assume pixels (like in HTML) when there's no unit
175
return new FixedLength(getNumericValue(), "px");
176     }
177
178     /**
179      * Convert NumberProperty to a Color. Not sure why this is needed.
180      * @param foUserAgent FOUserAgent
181      * @return Color that corresponds to black
182      */

183     public Color JavaDoc getColor(FOUserAgent foUserAgent) {
184         // TODO: Implement somehow
185
// Convert numeric value to color ???
186
// Convert to hexadecimal and then try to make it into a color?
187
return Color.black;
188     }
189
190 }
191
Popular Tags