KickJava   Java API By Example, From Geeks To Geeks.

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


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: PercentLength.java 434497 2006-08-24 22:08:26 +0200 (Thu, 24 Aug 2006) adelmelle $ */
19
20 package org.apache.fop.fo.properties;
21
22 import org.apache.fop.datatypes.PercentBaseContext;
23 import org.apache.fop.datatypes.PercentBase;
24 import org.apache.fop.fo.expr.PropertyException;
25
26 /**
27  * a percent specified length quantity in XSL
28  */

29 public class PercentLength extends LengthProperty {
30     
31     /**
32      * The percentage itself, expressed as a decimal value, e.g. for 95%, set
33      * the value to .95
34      */

35     private double factor;
36
37     /**
38      * A PercentBase implementation that contains the base length to which the
39      * {@link #factor} should be applied to compute the actual length
40      */

41     private PercentBase lbase = null;
42     
43     private double resolvedValue;
44
45     /**
46      * Main constructor. Construct an object based on a factor (the percent,
47      * as a factor) and an object which has a method to return the Length which
48      * provides the "base" for the actual length that is modeled.
49      * @param factor the percentage factor, expressed as a decimal (e.g. use
50      * .95 to represent 95%)
51      * @param lbase base property to which the factor should be applied
52      */

53     public PercentLength(double factor, PercentBase lbase) {
54         this.factor = factor;
55         this.lbase = lbase;
56     }
57
58     /**
59      * @return the base
60      */

61     public PercentBase getBaseLength() {
62         return this.lbase;
63     }
64
65     /**
66      * Used during property resolution to check for
67      * negative percentages
68      *
69      * @return the percentage value
70      */

71     protected double getPercentage() {
72         return factor * 100;
73     }
74
75     /**
76      * Return false because percent-length are always relative.
77      * @see org.apache.fop.datatypes.Numeric#isAbsolute()
78      */

79     public boolean isAbsolute() {
80         return false;
81     }
82
83     /**
84      * @see org.apache.fop.datatypes.Numeric#getNumericValue()
85      */

86     public double getNumericValue() {
87         return getNumericValue(null);
88     }
89
90     /**
91      * @see org.apache.fop.datatypes.Numeric#getNumericValue(PercentBaseContext)
92      */

93     public double getNumericValue(PercentBaseContext context) {
94         try {
95             resolvedValue = factor * lbase.getBaseLength(context);
96             return resolvedValue;
97         } catch (PropertyException exc) {
98             log.error(exc);
99             return 0;
100         }
101     }
102
103     /**
104      * Return the length of this PercentLength.
105      * @see org.apache.fop.datatypes.Length#getValue()
106      */

107     public int getValue() {
108         return (int) getNumericValue();
109     }
110
111     /**
112      * @see org.apache.fop.datatypes.Numeric#getValue(PercentBaseContext)
113      */

114     public int getValue(PercentBaseContext context) {
115         return (int) getNumericValue(context);
116     }
117     
118     /**
119      * @return the String equivalent of this
120      */

121     public String JavaDoc toString() {
122         // TODO: What about the base value?
123
return (new Double JavaDoc(factor * 100.0).toString()) + "%";
124     }
125
126 }
127
Popular Tags