KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > layoutmgr > SpaceSpecifier


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: SpaceSpecifier.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.layoutmgr;
21
22 import org.apache.fop.traits.SpaceVal;
23 import java.util.ArrayList JavaDoc;
24 import java.util.List JavaDoc;
25 import org.apache.fop.traits.MinOptMax;
26
27 /**
28  * Accumulate a sequence of space-specifiers (XSL space type) on
29  * areas with a stacking constraint. Provide a way to resolve these into
30  * a single MinOptMax value.
31  */

32 public class SpaceSpecifier implements Cloneable JavaDoc {
33
34
35     private boolean startsReferenceArea;
36     private boolean hasForcing = false;
37     private List JavaDoc spaceVals = new ArrayList JavaDoc();
38
39
40     /**
41      * Creates a new SpaceSpecifier.
42      * @param startsReferenceArea true if it starts a new reference area
43      */

44     public SpaceSpecifier(boolean startsReferenceArea) {
45         this.startsReferenceArea = startsReferenceArea;
46     }
47
48     /**
49      * @see java.lang.Object#clone()
50      */

51     public Object JavaDoc clone() {
52         try {
53             SpaceSpecifier ss = (SpaceSpecifier) super.clone();
54             ss.startsReferenceArea = startsReferenceArea;
55             ss.hasForcing = hasForcing;
56             // Clone the vector, but share the objects in it!
57
ss.spaceVals = new ArrayList JavaDoc();
58             ss.spaceVals.addAll(spaceVals);
59             return ss;
60         } catch (CloneNotSupportedException JavaDoc cnse) {
61             return null;
62         }
63
64     }
65
66     /**
67      * Clear all space specifiers
68      */

69     public void clear() {
70         hasForcing = false;
71         spaceVals.clear();
72     }
73
74     /**
75      * Indicates whether any space-specifiers have been added.
76      * @return true if any space-specifiers have been added.
77      */

78     public boolean hasSpaces() {
79         return (spaceVals.size() > 0);
80     }
81
82     /**
83      * Add a new space to the sequence. If this sequence starts a reference
84      * area, and the added space is conditional, and there are no
85      * non-conditional values in the sequence yet, then ignore it. Otherwise
86      * add it to the sequence.
87      */

88     public void addSpace(SpaceVal moreSpace) {
89         if (!startsReferenceArea
90                 || !moreSpace.isConditional()
91                 || !spaceVals.isEmpty()) {
92             if (moreSpace.isForcing()) {
93                 if (!hasForcing) {
94                     // Remove all other values (must all be non-forcing)
95
spaceVals.clear();
96                     hasForcing = true;
97                 }
98                 spaceVals.add(moreSpace);
99             } else if (!hasForcing) {
100                 // Don't bother adding all 0 space-specifier if not forcing
101
if (moreSpace.getSpace().min != 0
102                         || moreSpace.getSpace().opt != 0
103                         || moreSpace.getSpace().max != 0) {
104                     spaceVals.add(moreSpace);
105                 }
106             }
107         }
108     }
109
110
111     /**
112      * Resolve the current sequence of space-specifiers, accounting for
113      * forcing values.
114      * @param endsReferenceArea True if the sequence should be resolved
115      * at the trailing edge of reference area.
116      * @return The resolved value as a min/opt/max triple.
117      */

118     public MinOptMax resolve(boolean endsReferenceArea) {
119         int lastIndex = spaceVals.size();
120         if (endsReferenceArea) {
121             // Start from the end and count conditional specifiers
122
// Stop at first non-conditional
123
for (; lastIndex > 0; --lastIndex) {
124                 SpaceVal spaceVal = (SpaceVal) spaceVals.get(lastIndex - 1);
125                 if (!spaceVal.isConditional()) {
126                     break;
127                 }
128             }
129         }
130         MinOptMax resolvedSpace = new MinOptMax(0);
131         int maxPrecedence = -1;
132         for (int index = 0; index < lastIndex; index++) {
133             SpaceVal spaceVal = (SpaceVal) spaceVals.get(index);
134             if (hasForcing) {
135                 resolvedSpace.add(spaceVal.getSpace());
136             } else if (spaceVal.getPrecedence() > maxPrecedence) {
137                 maxPrecedence = spaceVal.getPrecedence();
138                 resolvedSpace = spaceVal.getSpace();
139             } else if (spaceVal.getPrecedence() == maxPrecedence) {
140                 if (spaceVal.getSpace().opt > resolvedSpace.opt) {
141                     resolvedSpace = spaceVal.getSpace();
142                 } else if (spaceVal.getSpace().opt == resolvedSpace.opt) {
143                     if (resolvedSpace.min < spaceVal.getSpace().min) {
144                         resolvedSpace.min = spaceVal.getSpace().min;
145                     }
146                     if (resolvedSpace.max > spaceVal.getSpace().max) {
147                         resolvedSpace.max = spaceVal.getSpace().max;
148                     }
149                 }
150             }
151
152         }
153         return resolvedSpace;
154     }
155     
156     public String JavaDoc toString() {
157         return "Space Specifier (resolved at begin/end of ref. area:):\n" +
158             resolve(false).toString() + "\n" +
159             resolve(true).toString();
160     }
161 }
162
Popular Tags