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: CondLengthProperty.java 426576 2006-07-28 15:44:37Z jeremias $ */
019:
020: package org.apache.fop.fo.properties;
021:
022: import org.apache.fop.datatypes.CompoundDatatype;
023: import org.apache.fop.datatypes.Length;
024: import org.apache.fop.datatypes.PercentBaseContext;
025: import org.apache.fop.fo.Constants;
026: import org.apache.fop.fo.FObj;
027: import org.apache.fop.fo.PropertyList;
028: import org.apache.fop.fo.expr.PropertyException;
029:
030: /**
031: * Superclass for properties that have conditional lengths
032: */
033: public class CondLengthProperty extends Property implements
034: CompoundDatatype {
035: private Property length;
036: private Property conditionality;
037:
038: /**
039: * Inner class for creating instances of CondLengthProperty
040: */
041: public static class Maker extends CompoundPropertyMaker {
042:
043: /**
044: * @param propId the id of the property for which a Maker should be created
045: */
046: public Maker(int propId) {
047: super (propId);
048: }
049:
050: /**
051: * Create a new empty instance of CondLengthProperty.
052: * @return the new instance.
053: */
054: public Property makeNewProperty() {
055: return new CondLengthProperty();
056: }
057:
058: /**
059: * @see CompoundPropertyMaker#convertProperty
060: */
061: public Property convertProperty(Property p,
062: PropertyList propertyList, FObj fo)
063: throws PropertyException {
064: if (p instanceof KeepProperty) {
065: return p;
066: }
067: return super .convertProperty(p, propertyList, fo);
068: }
069: }
070:
071: /**
072: * @see org.apache.fop.datatypes.CompoundDatatype#setComponent(int, Property, boolean)
073: */
074: public void setComponent(int cmpId, Property cmpnValue,
075: boolean bIsDefault) {
076: if (cmpId == CP_LENGTH) {
077: length = cmpnValue;
078: } else if (cmpId == CP_CONDITIONALITY) {
079: conditionality = cmpnValue;
080: }
081: }
082:
083: /**
084: * @see org.apache.fop.datatypes.CompoundDatatype#getComponent(int)
085: */
086: public Property getComponent(int cmpId) {
087: if (cmpId == CP_LENGTH) {
088: return length;
089: } else if (cmpId == CP_CONDITIONALITY) {
090: return conditionality;
091: } else {
092: return null;
093: }
094: }
095:
096: /**
097: * Returns the conditionality.
098: * @return the conditionality
099: */
100: public Property getConditionality() {
101: return this .conditionality;
102: }
103:
104: /**
105: * Returns the length.
106: * @return the length
107: */
108: public Property getLengthComponent() {
109: return this .length;
110: }
111:
112: /**
113: * Indicates if the length can be discarded on certain conditions.
114: * @return true if the length can be discarded.
115: */
116: public boolean isDiscard() {
117: return this .conditionality.getEnum() == Constants.EN_DISCARD;
118: }
119:
120: /**
121: * Returns the computed length value.
122: * @return the length in millipoints
123: */
124: public int getLengthValue() {
125: return this .length.getLength().getValue();
126: }
127:
128: /**
129: * Returns the computed length value.
130: * @param context The context for the length calculation (for percentage based lengths)
131: * @return the length in millipoints
132: */
133: public int getLengthValue(PercentBaseContext context) {
134: return this .length.getLength().getValue(context);
135: }
136:
137: /** @see java.lang.Object#toString() */
138: public String toString() {
139: return "CondLength["
140: + length.getObject().toString()
141: + ", "
142: + (isDiscard() ? conditionality.toString()
143: .toLowerCase() : conditionality.toString())
144: + "]";
145: }
146:
147: /**
148: * @return this.condLength
149: */
150: public CondLengthProperty getCondLength() {
151: return this ;
152: }
153:
154: /**
155: * TODO: Should we allow this?
156: * @return this.condLength cast as a Length
157: */
158: public Length getLength() {
159: return length.getLength();
160: }
161:
162: /**
163: * @return this.condLength cast as an Object
164: */
165: public Object getObject() {
166: return this;
167: }
168:
169: }
|