Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

KnuthSequence.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. /* $Id$ */
  18. package org.apache.fop.layoutmgr;
  19. import java.util.ArrayList;
  20. import java.util.Iterator;
  21. import java.util.List;
  22. import java.util.ListIterator;
  23. import org.apache.fop.util.ListUtil;
  24. /**
  25. * Represents a list of {@link KnuthElement Knuth elements}.
  26. */
  27. public abstract class KnuthSequence extends ArrayList {
  28. //TODO: do not extend ArrayList
  29. /**
  30. * Creates a new and empty list.
  31. */
  32. public KnuthSequence() {
  33. super();
  34. }
  35. /**
  36. * Creates a new list from an existing list.
  37. * @param list The list from which to create the new list.
  38. */
  39. public KnuthSequence(List list) {
  40. super(list);
  41. }
  42. /**
  43. * Marks the start of the sequence.
  44. */
  45. public void startSequence() {
  46. }
  47. /**
  48. * Finalizes a Knuth sequence.
  49. * @return a finalized sequence.
  50. */
  51. public abstract KnuthSequence endSequence();
  52. /**
  53. * Can sequence be appended to this sequence?
  54. * @param sequence The sequence that may be appended.
  55. * @return whether the sequence can be appended to this sequence.
  56. */
  57. public abstract boolean canAppendSequence(KnuthSequence sequence);
  58. /**
  59. * Append sequence to this sequence if it can be appended.
  60. * @param sequence The sequence that is to be appended.
  61. * @param keepTogether Whether the two sequences must be kept together.
  62. * @param breakElement The BreakElement that may be inserted between the two sequences.
  63. * @return whether the sequence was succesfully appended to this sequence.
  64. */
  65. public abstract boolean appendSequence(KnuthSequence sequence, boolean keepTogether,
  66. BreakElement breakElement);
  67. /**
  68. * Append sequence to this sequence if it can be appended.
  69. * @param sequence The sequence that is to be appended.
  70. * @return whether the sequence was succesfully appended to this sequence.
  71. */
  72. public abstract boolean appendSequence(KnuthSequence sequence);
  73. /**
  74. * Append sequence to this sequence if it can be appended.
  75. * If that is not possible, close this sequence.
  76. * @param sequence The sequence that is to be appended.
  77. * @return whether the sequence was succesfully appended to this sequence.
  78. */
  79. public boolean appendSequenceOrClose(KnuthSequence sequence) {
  80. if (!appendSequence(sequence)) {
  81. endSequence();
  82. return false;
  83. } else {
  84. return true;
  85. }
  86. }
  87. /**
  88. * Append sequence to this sequence if it can be appended.
  89. * If that is not possible, close this sequence.
  90. * @param sequence The sequence that is to be appended.
  91. * @param keepTogether Whether the two sequences must be kept together.
  92. * @param breakElement The BreakElement that may be inserted between the two sequences.
  93. * @return whether the sequence was succesfully appended to this sequence.
  94. */
  95. public boolean appendSequenceOrClose(KnuthSequence sequence, boolean keepTogether,
  96. BreakElement breakElement) {
  97. if (!appendSequence(sequence, keepTogether, breakElement)) {
  98. endSequence();
  99. return false;
  100. } else {
  101. return true;
  102. }
  103. }
  104. /**
  105. * Wrap the Positions of the elements of this sequence in a Position for LayoutManager lm.
  106. * @param lm The LayoutManager for the Positions that will be created.
  107. */
  108. public void wrapPositions(LayoutManager lm) {
  109. ListIterator listIter = listIterator();
  110. ListElement element;
  111. while (listIter.hasNext()) {
  112. element = (ListElement) listIter.next();
  113. element.setPosition(
  114. lm.notifyPos(new NonLeafPosition(lm, element.getPosition())));
  115. }
  116. }
  117. /**
  118. * @return the last element of this sequence.
  119. */
  120. public ListElement getLast() {
  121. return (isEmpty()
  122. ? null
  123. : (ListElement) ListUtil.getLast(this));
  124. }
  125. /**
  126. * Remove the last element of this sequence.
  127. * @return the removed element.
  128. */
  129. public ListElement removeLast() {
  130. return (isEmpty()
  131. ? null
  132. : (ListElement) ListUtil.removeLast(this));
  133. }
  134. /**
  135. * @param index The index of the element to be returned
  136. * @return the element at index index.
  137. */
  138. public ListElement getElement(int index) {
  139. return (index >= size() || index < 0)
  140. ? null
  141. : (ListElement) get(index);
  142. }
  143. /**
  144. * Returns the position index of the first box in this sequence, starting at the given
  145. * index. If {@code startIndex} is outside the bounds of this sequence, it is
  146. * returned.
  147. *
  148. * @param startIndex the index from which to start the lookup
  149. * @return the index of the next box element, {@link #size()} if there is no such
  150. * element, {@code startIndex} if {@code (startIndex < 0 || startIndex >= size())}
  151. */
  152. protected int getFirstBoxIndex(int startIndex) {
  153. if (startIndex < 0 || startIndex >= size()) {
  154. return startIndex;
  155. } else {
  156. int boxIndex = startIndex;
  157. @SuppressWarnings("unchecked")
  158. Iterator<ListElement> iter = listIterator(startIndex);
  159. while (iter.hasNext() && !iter.next().isBox()) {
  160. boxIndex++;
  161. }
  162. return boxIndex;
  163. }
  164. }
  165. /**
  166. * Is this an inline or a block sequence?
  167. * @return true if this is an inline sequence
  168. */
  169. public abstract boolean isInlineSequence();
  170. /** {@inheritDoc} */
  171. public String toString() {
  172. return "<KnuthSequence " + super.toString() + ">";
  173. }
  174. }