KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > css > engine > value > FloatValue


1 /*
2
3    Copyright 2002 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    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 package org.apache.batik.css.engine.value;
19
20 import org.w3c.dom.DOMException JavaDoc;
21 import org.w3c.dom.css.CSSPrimitiveValue;
22
23 /**
24  * This class represents float values.
25  *
26  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
27  * @version $Id: FloatValue.java,v 1.4 2004/08/18 07:12:53 vhardy Exp $
28  */

29 public class FloatValue extends AbstractValue {
30     
31     /**
32      * Returns the CSS text associated with the given type/value pair.
33      */

34     public static String JavaDoc getCssText(short unit, float value) {
35         if (unit < 0 || unit >= UNITS.length) {
36             throw new DOMException JavaDoc(DOMException.SYNTAX_ERR, "");
37         }
38         String JavaDoc s = String.valueOf(value);
39         if (s.endsWith(".0")) {
40             s = s.substring(0, s.length() - 2);
41         }
42     return s + UNITS[unit - CSSPrimitiveValue.CSS_NUMBER];
43     }
44
45     /**
46      * The unit types representations
47      */

48     protected final static String JavaDoc[] UNITS = {
49         "", "%", "em", "ex", "px", "cm", "mm", "in", "pt",
50         "pc", "deg", "rad", "grad", "ms", "s", "Hz", "kHz", ""
51     };
52
53     /**
54      * The float value
55      */

56     protected float floatValue;
57
58     /**
59      * The unit type
60      */

61     protected short unitType;
62
63     /**
64      * Creates a new value.
65      */

66     public FloatValue(short unitType, float floatValue) {
67     this.unitType = unitType;
68     this.floatValue = floatValue;
69     }
70
71     /**
72      * The type of the value.
73      */

74     public short getPrimitiveType() {
75         return unitType;
76     }
77
78     /**
79      * Returns the float value.
80      */

81     public float getFloatValue() {
82         return floatValue;
83     }
84
85     /**
86      * A string representation of the current value.
87      */

88     public String JavaDoc getCssText() {
89     return getCssText(unitType, floatValue);
90     }
91
92     /**
93      * Returns a printable representation of this value.
94      */

95     public String JavaDoc toString() {
96         return getCssText();
97     }
98 }
99
Popular Tags