KickJava   Java API By Example, From Geeks To Geeks.

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


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: KnuthSequence.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.layoutmgr;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.ListIterator JavaDoc;
25
26 /**
27  * Represents a list of Knuth elements.
28  */

29 /**
30  *
31  */

32 public abstract class KnuthSequence extends ArrayList JavaDoc {
33     /**
34      * Creates a new and empty list.
35      */

36     public KnuthSequence() {
37         super();
38     }
39
40     /**
41      * Creates a new list from an existing list.
42      * @param list The list from which to create the new list.
43      */

44     public KnuthSequence(List JavaDoc list) {
45         super(list);
46     }
47
48     /**
49      * Marks the start of the sequence.
50      */

51     public void startSequence() {
52     }
53
54     /**
55      * Finalizes a Knuth sequence.
56      * @return a finalized sequence.
57      */

58     public abstract KnuthSequence endSequence();
59
60     /**
61      * Can sequence be appended to this sequence?
62      * @param sequence The sequence that may be appended.
63      * @return whether the sequence can be appended to this sequence.
64      */

65     public abstract boolean canAppendSequence(KnuthSequence sequence);
66
67     /**
68      * Append sequence to this sequence if it can be appended.
69      * @param sequence The sequence that is to be appended.
70      * @param keepTogether Whether the two sequences must be kept together.
71      * @param breakElement The BreakElement that may be inserted between the two sequences.
72      * @return whether the sequence was succesfully appended to this sequence.
73      */

74     public abstract boolean appendSequence(KnuthSequence sequence, boolean keepTogether,
75                                            BreakElement breakElement);
76     
77     /**
78      * Append sequence to this sequence if it can be appended.
79      * @param sequence The sequence that is to be appended.
80      * @return whether the sequence was succesfully appended to this sequence.
81      */

82     public abstract boolean appendSequence(KnuthSequence sequence);
83     
84     /**
85      * Append sequence to this sequence if it can be appended.
86      * If that is not possible, close this sequence.
87      * @param sequence The sequence that is to be appended.
88      * @return whether the sequence was succesfully appended to this sequence.
89      */

90     public boolean appendSequenceOrClose(KnuthSequence sequence) {
91         if (!appendSequence(sequence)) {
92             endSequence();
93             return false;
94         } else {
95             return true;
96         }
97     }
98     
99     /**
100      * Append sequence to this sequence if it can be appended.
101      * If that is not possible, close this sequence.
102      * @param sequence The sequence that is to be appended.
103      * @param keepTogether Whether the two sequences must be kept together.
104      * @param breakElement The BreakElement that may be inserted between the two sequences.
105      * @return whether the sequence was succesfully appended to this sequence.
106      */

107     public boolean appendSequenceOrClose(KnuthSequence sequence, boolean keepTogether,
108                                          BreakElement breakElement) {
109         if (!appendSequence(sequence, keepTogether, breakElement)) {
110             endSequence();
111             return false;
112         } else {
113             return true;
114         }
115     }
116     
117     /**
118      * Wrap the Positions of the elements of this sequence in a Position for LayoutManager lm.
119      * @param lm The LayoutManager for the Positions that will be created.
120      */

121     public void wrapPositions(LayoutManager lm) {
122         ListIterator JavaDoc listIter = listIterator();
123         ListElement element;
124         while (listIter.hasNext()) {
125             element = (ListElement) listIter.next();
126             element.setPosition
127             (lm.notifyPos(new NonLeafPosition(lm, element.getPosition())));
128         }
129     }
130     
131     /**
132      * @return the last element of this sequence.
133      */

134     public ListElement getLast() {
135         int idx = size();
136         if (idx == 0) {
137             return null;
138         }
139         return (ListElement) get(idx - 1);
140     }
141
142     /**
143      * Remove the last element of this sequence.
144      * @return the removed element.
145      */

146     public ListElement removeLast() {
147         int idx = size();
148         if (idx == 0) {
149             return null;
150         }
151         return (ListElement) remove(idx - 1);
152     }
153
154     /**
155      * @param index The index of the element to be returned
156      * @return the element at index index.
157      */

158     public ListElement getElement(int index) {
159         return (ListElement) get(index);
160     }
161
162     /**
163      * Is this an inline or a block sequence?
164      * @return true if this is an inline sequence
165      */

166     public abstract boolean isInlineSequence();
167
168 }
169
Popular Tags