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: KnuthSequence.java 426576 2006-07-28 15:44:37Z jeremias $ */
019:
020: package org.apache.fop.layoutmgr;
021:
022: import java.util.ArrayList;
023: import java.util.List;
024: import java.util.ListIterator;
025:
026: /**
027: * Represents a list of Knuth elements.
028: */
029: /**
030: *
031: */
032: public abstract class KnuthSequence extends ArrayList {
033: /**
034: * Creates a new and empty list.
035: */
036: public KnuthSequence() {
037: super ();
038: }
039:
040: /**
041: * Creates a new list from an existing list.
042: * @param list The list from which to create the new list.
043: */
044: public KnuthSequence(List list) {
045: super (list);
046: }
047:
048: /**
049: * Marks the start of the sequence.
050: */
051: public void startSequence() {
052: }
053:
054: /**
055: * Finalizes a Knuth sequence.
056: * @return a finalized sequence.
057: */
058: public abstract KnuthSequence endSequence();
059:
060: /**
061: * Can sequence be appended to this sequence?
062: * @param sequence The sequence that may be appended.
063: * @return whether the sequence can be appended to this sequence.
064: */
065: public abstract boolean canAppendSequence(KnuthSequence sequence);
066:
067: /**
068: * Append sequence to this sequence if it can be appended.
069: * @param sequence The sequence that is to be appended.
070: * @param keepTogether Whether the two sequences must be kept together.
071: * @param breakElement The BreakElement that may be inserted between the two sequences.
072: * @return whether the sequence was succesfully appended to this sequence.
073: */
074: public abstract boolean appendSequence(KnuthSequence sequence,
075: boolean keepTogether, BreakElement breakElement);
076:
077: /**
078: * Append sequence to this sequence if it can be appended.
079: * @param sequence The sequence that is to be appended.
080: * @return whether the sequence was succesfully appended to this sequence.
081: */
082: public abstract boolean appendSequence(KnuthSequence sequence);
083:
084: /**
085: * Append sequence to this sequence if it can be appended.
086: * If that is not possible, close this sequence.
087: * @param sequence The sequence that is to be appended.
088: * @return whether the sequence was succesfully appended to this sequence.
089: */
090: public boolean appendSequenceOrClose(KnuthSequence sequence) {
091: if (!appendSequence(sequence)) {
092: endSequence();
093: return false;
094: } else {
095: return true;
096: }
097: }
098:
099: /**
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,
108: boolean keepTogether, 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 listIter = listIterator();
123: ListElement element;
124: while (listIter.hasNext()) {
125: element = (ListElement) listIter.next();
126: element.setPosition(lm.notifyPos(new NonLeafPosition(lm,
127: 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: }
|