KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > layoutmgr > inline > FootnoteLayoutManager


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: FootnoteLayoutManager.java 453310 2006-10-05 18:44:15Z spepping $ */
19
20 package org.apache.fop.layoutmgr.inline;
21
22 import java.util.LinkedList JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.ListIterator JavaDoc;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.fop.fo.flow.Footnote;
29 import org.apache.fop.layoutmgr.AbstractLayoutManager;
30 import org.apache.fop.layoutmgr.FootnoteBodyLayoutManager;
31 import org.apache.fop.layoutmgr.InlineKnuthSequence;
32 import org.apache.fop.layoutmgr.KnuthElement;
33 import org.apache.fop.layoutmgr.KnuthSequence;
34 import org.apache.fop.layoutmgr.LayoutContext;
35 import org.apache.fop.layoutmgr.Position;
36
37 /**
38  * Layout manager for fo:footnote.
39  */

40 public class FootnoteLayoutManager extends AbstractLayoutManager
41                                    implements InlineLevelLayoutManager {
42
43     /**
44      * logging instance
45      */

46     private static Log log = LogFactory.getLog(FootnoteLayoutManager.class);
47
48     private Footnote footnote;
49     private InlineStackingLayoutManager citationLM;
50     private FootnoteBodyLayoutManager bodyLM;
51     /** Represents the footnote citation **/
52     private KnuthElement forcedAnchor;
53
54     /**
55      * Create a new footnote layout manager.
56      * @param node footnote to create the layout manager for
57      */

58     public FootnoteLayoutManager(Footnote node) {
59         super(node);
60         footnote = node;
61     }
62     
63     /** @see org.apache.fop.layoutmgr.LayoutManager#initialize() */
64     public void initialize() {
65         // create an InlineStackingLM handling the fo:inline child of fo:footnote
66
citationLM = new InlineLayoutManager(footnote.getFootnoteCitation());
67
68         // create a FootnoteBodyLM handling the fo:footnote-body child of fo:footnote
69
bodyLM = new FootnoteBodyLayoutManager(footnote.getFootnoteBody());
70     }
71
72     /** @see org.apache.fop.layoutmgr.LayoutManager */
73     public LinkedList JavaDoc getNextKnuthElements(LayoutContext context,
74                                            int alignment) {
75         // this is the only method that must be implemented:
76
// all other methods will never be called, as the returned elements
77
// contain Positions created by the citationLM, so its methods will
78
// be called instead
79

80         // set the citationLM parent to be this LM's parent
81
citationLM.setParent(getParent());
82         citationLM.initialize();
83         bodyLM.setParent(this);
84         bodyLM.initialize();
85
86         // get Knuth elements representing the footnote citation
87
LinkedList JavaDoc returnedList = new LinkedList JavaDoc();
88         while (!citationLM.isFinished()) {
89             LinkedList JavaDoc partialList = citationLM.getNextKnuthElements(context, alignment);
90             if (partialList != null) {
91                 returnedList.addAll(partialList);
92             }
93         }
94         if (returnedList.size() == 0) {
95             //Inline part of the footnote is empty. Need to send back an auxiliary
96
//zero-width, zero-height inline box so the footnote gets painted.
97
KnuthSequence seq = new InlineKnuthSequence();
98             //Need to use an aux. box, otherwise, the line height can't be forced to zero height.
99
forcedAnchor = new KnuthInlineBox(0, null, null, true);
100             seq.add(forcedAnchor);
101             returnedList.add(seq);
102         }
103         setFinished(true);
104
105         addAnchor(returnedList);
106
107         return returnedList;
108     }
109
110     private void addAnchor(LinkedList JavaDoc citationList) {
111         // find the last box in the sequence, and add a reference
112
// to the FootnoteBodyLM
113
KnuthInlineBox lastBox = null;
114         ListIterator JavaDoc citationIterator = citationList.listIterator(citationList.size());
115         while (citationIterator.hasPrevious() && lastBox == null) {
116             Object JavaDoc obj = citationIterator.previous();
117             if (obj instanceof KnuthElement) {
118                 KnuthElement element = (KnuthElement)obj;
119                 if (element instanceof KnuthInlineBox) {
120                     lastBox = (KnuthInlineBox) element;
121                 }
122             } else {
123                 KnuthSequence seq = (KnuthSequence)obj;
124                 ListIterator JavaDoc nestedIterator = seq.listIterator(seq.size());
125                 while (nestedIterator.hasPrevious() && lastBox == null) {
126                     KnuthElement element = (KnuthElement)nestedIterator.previous();
127                     if (element instanceof KnuthInlineBox && !element.isAuxiliary()
128                             || element == forcedAnchor) {
129                         lastBox = (KnuthInlineBox) element;
130                     }
131                 }
132             }
133         }
134         if (lastBox != null) {
135             lastBox.setFootnoteBodyLM(bodyLM);
136         } else {
137             //throw new IllegalStateException("No anchor box was found for a footnote.");
138
}
139     }
140
141     /** @see org.apache.fop.layoutmgr.inline.InlineLevelLayoutManager */
142     public List JavaDoc addALetterSpaceTo(List JavaDoc oldList) {
143         log.warn("null implementation of addALetterSpaceTo() called!");
144         return oldList;
145     }
146
147     /**
148      * Remove the word space represented by the given elements
149      *
150      * @param oldList the elements representing the word space
151      */

152     public void removeWordSpace(List JavaDoc oldList) {
153         // do nothing
154
log.warn(this.getClass().getName() + " should not receive a call to removeWordSpace(list)");
155     }
156
157     /** @see org.apache.fop.layoutmgr.inline.InlineLevelLayoutManager */
158     public void getWordChars(StringBuffer JavaDoc sbChars, Position pos) {
159         log.warn("null implementation of getWordChars() called!");
160     }
161
162     /** @see org.apache.fop.layoutmgr.inline.InlineLevelLayoutManager */
163     public void hyphenate(Position pos, HyphContext hc) {
164         log.warn("null implementation of hyphenate called!");
165     }
166
167     /** @see org.apache.fop.layoutmgr.inline.InlineLevelLayoutManager */
168     public boolean applyChanges(List JavaDoc oldList) {
169         log.warn("null implementation of applyChanges() called!");
170         return false;
171     }
172
173     /**
174      * @see org.apache.fop.layoutmgr.LayoutManager#getChangedKnuthElements(java.util.List, int)
175      */

176     public LinkedList JavaDoc getChangedKnuthElements(List JavaDoc oldList,
177                                               int alignment) {
178         log.warn("null implementation of getChangeKnuthElement() called!");
179         return null;
180     }
181 }
182
Popular Tags