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: SpaceElement.java 893238 2009-12-22 17:20:51Z vhennebert $ */
19 
20 package org.apache.fop.layoutmgr;
21 
22 import org.apache.fop.datatypes.PercentBaseContext;
23 import org.apache.fop.fo.Constants;
24 import org.apache.fop.fo.properties.SpaceProperty;
25 import org.apache.fop.traits.MinOptMax;
26 
27 /**
28  * This class represents an unresolved space element.
29  */
30 public class SpaceElement extends UnresolvedListElementWithLength {
31 
32     private int precedence;
33 
34     /**
35      * Main constructor
36      * @param position the Position instance needed by the addAreas stage of the LMs.
37      * @param space the space property
38      * @param side the side to which this space element applies.
39      * @param isFirst true if this is a space-before of the first area generated.
40      * @param isLast true if this is a space-after of the last area generated.
41      * @param context the property evaluation context
42      */
SpaceElement(Position position, SpaceProperty space, RelSide side, boolean isFirst, boolean isLast, PercentBaseContext context)43     public SpaceElement(Position position, SpaceProperty space, RelSide side, boolean isFirst,
44                         boolean isLast, PercentBaseContext context) {
45         super(position, space.getSpace().getLengthRange().toMinOptMax(context), side,
46                 space.isDiscard(), isFirst, isLast);
47         int en = space.getSpace().getPrecedence().getEnum();
48         if (en == Constants.EN_FORCE) {
49             this.precedence = Integer.MAX_VALUE;
50         } else {
51             this.precedence = space.getSpace().getPrecedence().getNumber().intValue();
52         }
53     }
54 
55     /** @return true if the space is forcing. */
isForcing()56     public boolean isForcing() {
57         return this.precedence == Integer.MAX_VALUE;
58     }
59 
60     /** @return the precedence of the space */
getPrecedence()61     public int getPrecedence() {
62         return this.precedence;
63     }
64 
65     /** {@inheritDoc} */
notifyLayoutManager(MinOptMax effectiveLength)66     public void notifyLayoutManager(MinOptMax effectiveLength) {
67         LayoutManager lm = getOriginatingLayoutManager();
68         if (lm instanceof ConditionalElementListener) {
69             ((ConditionalElementListener)lm).notifySpace(
70                     getSide(), effectiveLength);
71         } else {
72             log.warn("Cannot notify LM. It does not implement ConditionalElementListener:"
73                     + lm.getClass().getName());
74         }
75     }
76 
77     /** {@inheritDoc} */
toString()78     public String toString() {
79         StringBuffer sb = new StringBuffer("Space[");
80         sb.append(super.toString());
81         sb.append(", precedence=");
82         if (isForcing()) {
83             sb.append("forcing");
84         } else {
85             sb.append(getPrecedence());
86         }
87         sb.append("]");
88         return sb.toString();
89     }
90 
91 }
92