KickJava   Java API By Example, From Geeks To Geeks.

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


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: LengthRangeProperty.java 454018 2006-10-07 21:00:13Z pietsch $ */
19
20 package org.apache.fop.fo.properties;
21
22 import org.apache.fop.datatypes.CompoundDatatype;
23 import org.apache.fop.datatypes.Length;
24 import org.apache.fop.datatypes.PercentBaseContext;
25 import org.apache.fop.fo.FObj;
26 import org.apache.fop.fo.PropertyList;
27 import org.apache.fop.fo.expr.PropertyException;
28
29 /**
30  * Superclass for properties that contain LengthRange values
31  */

32 public class LengthRangeProperty extends Property implements CompoundDatatype {
33     private Property minimum;
34     private Property optimum;
35     private Property maximum;
36     private static final int MINSET = 1;
37     private static final int OPTSET = 2;
38     private static final int MAXSET = 4;
39     private int bfSet = 0; // bit field
40
private boolean consistent = false;
41
42     /**
43      * Inner class for a Maker for LengthProperty objects
44      */

45     public static class Maker extends CompoundPropertyMaker {
46
47         /**
48          * @param propId the id of the property for which a Maker should be created
49          */

50         public Maker(int propId) {
51             super(propId);
52         }
53
54         /**
55          * Create a new empty instance of LengthRangeProperty.
56          * @return the new instance.
57          */

58         public Property makeNewProperty() {
59             return new LengthRangeProperty();
60         }
61
62         private boolean isNegativeLength(Length len) {
63             return ((len instanceof PercentLength
64                         && ((PercentLength) len).getPercentage() < 0)
65                     || (len.isAbsolute() && len.getValue() < 0));
66         }
67         
68         /**
69          * @see CompoundPropertyMaker#convertProperty
70          */

71         public Property convertProperty(Property p,
72                                 PropertyList propertyList, FObj fo)
73                         throws PropertyException {
74             
75             if (p instanceof LengthRangeProperty) {
76                 return p;
77             }
78             
79             if (this.propId == PR_BLOCK_PROGRESSION_DIMENSION
80                     || this.propId == PR_INLINE_PROGRESSION_DIMENSION) {
81                 Length len = p.getLength();
82                 if (len != null) {
83                     if (isNegativeLength(len)) {
84                         log.warn(FObj.decorateWithContextInfo(
85                                 "Replaced negative value (" + len + ") for " + getName()
86                                 + " with 0mpt", fo));
87                         p = new FixedLength(0);
88                     }
89                 }
90             }
91             
92             return super.convertProperty(p, propertyList, fo);
93         }
94         
95         
96         /**
97          * @see org.apache.fop.fo.properties.PropertyMaker#getSubprop(Property, int, Property)
98          */

99         protected Property setSubprop(Property baseProperty, int subpropertyId,
100                                         Property subproperty) {
101             CompoundDatatype val = (CompoundDatatype) baseProperty.getObject();
102             if (this.propId == PR_BLOCK_PROGRESSION_DIMENSION
103                     || this.propId == PR_INLINE_PROGRESSION_DIMENSION) {
104                 Length len = subproperty.getLength();
105                 if (len != null) {
106                     if (isNegativeLength(len)) {
107                         log.warn("Replaced negative value (" + len + ") for " + getName()
108                                 + " with 0mpt");
109                         val.setComponent(subpropertyId,
110                                 new FixedLength(0), false);
111                         return baseProperty;
112                     }
113                 }
114             }
115             val.setComponent(subpropertyId, subproperty, false);
116             return baseProperty;
117         }
118
119     }
120
121
122
123     /**
124      * @see org.apache.fop.datatypes.CompoundDatatype#setComponent(int, Property, boolean)
125      */

126     public void setComponent(int cmpId, Property cmpnValue,
127                              boolean bIsDefault) {
128         if (cmpId == CP_MINIMUM) {
129             setMinimum(cmpnValue, bIsDefault);
130         } else if (cmpId == CP_OPTIMUM) {
131             setOptimum(cmpnValue, bIsDefault);
132         } else if (cmpId == CP_MAXIMUM) {
133             setMaximum(cmpnValue, bIsDefault);
134         }
135     }
136
137     /**
138      * @see org.apache.fop.datatypes.CompoundDatatype#getComponent(int)
139      */

140     public Property getComponent(int cmpId) {
141         if (cmpId == CP_MINIMUM) {
142             return getMinimum(null);
143         } else if (cmpId == CP_OPTIMUM) {
144             return getOptimum(null);
145         } else if (cmpId == CP_MAXIMUM) {
146             return getMaximum(null);
147         } else {
148             return null; // SHOULDN'T HAPPEN
149
}
150     }
151
152     /**
153      * Set minimum value to min.
154      * @param minimum A Length value specifying the minimum value for this
155      * LengthRange.
156      * @param bIsDefault If true, this is set as a "default" value
157      * and not a user-specified explicit value.
158      */

159     protected void setMinimum(Property minimum, boolean bIsDefault) {
160         this.minimum = minimum;
161         if (!bIsDefault) {
162             bfSet |= MINSET;
163         }
164         consistent = false;
165     }
166
167
168     /**
169      * Set maximum value to max if it is >= optimum or optimum isn't set.
170      * @param max A Length value specifying the maximum value for this
171      * @param bIsDefault If true, this is set as a "default" value
172      * and not a user-specified explicit value.
173      */

174     protected void setMaximum(Property max, boolean bIsDefault) {
175         maximum = max;
176         if (!bIsDefault) {
177             bfSet |= MAXSET;
178         }
179         consistent = false;
180     }
181
182
183     /**
184      * Set the optimum value.
185      * @param opt A Length value specifying the optimum value for this
186      * @param bIsDefault If true, this is set as a "default" value
187      * and not a user-specified explicit value.
188      */

189     protected void setOptimum(Property opt, boolean bIsDefault) {
190         optimum = opt;
191         if (!bIsDefault) {
192             bfSet |= OPTSET;
193         }
194         consistent = false;
195     }
196     
197     // Minimum is prioritaire, if explicit
198
private void checkConsistency(PercentBaseContext context) {
199         if (consistent) {
200             return;
201         }
202         if (context == null) {
203             return;
204         }
205         // Make sure max >= min
206
// Must also control if have any allowed enum values!
207

208         if (!minimum.isAuto() && !maximum.isAuto()
209                 && minimum.getLength().getValue(context) > maximum.getLength().getValue(context)) {
210             if ((bfSet & MINSET) != 0) {
211                 // if minimum is explicit, force max to min
212
if ((bfSet & MAXSET) != 0) {
213                     // Warning: min>max, resetting max to min
214
log.error("forcing max to min in LengthRange");
215                 }
216                 maximum = minimum;
217             } else {
218                 minimum = maximum; // minimum was default value
219
}
220         }
221         // Now make sure opt <= max and opt >= min
222
if (!optimum.isAuto() && !maximum.isAuto()
223                 && optimum.getLength().getValue(context) > maximum.getLength().getValue(context)) {
224             if ((bfSet & OPTSET) != 0) {
225                 if ((bfSet & MAXSET) != 0) {
226                     // Warning: opt > max, resetting opt to max
227
log.error("forcing opt to max in LengthRange");
228                     optimum = maximum;
229                 } else {
230                     maximum = optimum; // maximum was default value
231
}
232             } else {
233                 // opt is default and max is explicit or default
234
optimum = maximum;
235             }
236         } else if (!optimum.isAuto() && !minimum.isAuto()
237                     && optimum.getLength().getValue(context)
238                         < minimum.getLength().getValue(context)) {
239             if ((bfSet & MINSET) != 0) {
240                 // if minimum is explicit, force opt to min
241
if ((bfSet & OPTSET) != 0) {
242                     log.error("forcing opt to min in LengthRange");
243                 }
244                 optimum = minimum;
245             } else {
246                 minimum = optimum; // minimum was default value
247
}
248         }
249         
250         consistent = true;
251     }
252
253     /**
254      * @param context Percentage evaluation context
255      * @return minimum length
256      */

257     public Property getMinimum(PercentBaseContext context) {
258         checkConsistency(context);
259         return this.minimum;
260     }
261
262     /**
263      * @param context Percentage evaluation context
264      * @return maximum length
265      */

266     public Property getMaximum(PercentBaseContext context) {
267         checkConsistency(context);
268         return this.maximum;
269     }
270
271     /**
272      * @param context Percentage evaluation context
273      * @return optimum length
274      */

275     public Property getOptimum(PercentBaseContext context) {
276         checkConsistency(context);
277         return this.optimum;
278     }
279
280     /** @see java.lang.Object#toString() */
281     public String JavaDoc toString() {
282         return "LengthRange["
283             + "min:" + getMinimum(null).getObject()
284             + ", max:" + getMaximum(null).getObject()
285             + ", opt:" + getOptimum(null).getObject() + "]";
286     }
287
288     /**
289      * @return this.lengthRange
290      */

291     public LengthRangeProperty getLengthRange() {
292         return this;
293     }
294
295     /**
296      * @return this.lengthRange cast as an Object
297      */

298     public Object JavaDoc getObject() {
299         return this;
300     }
301
302 }
303
Popular Tags