You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AbstractBaseLayoutManager.java 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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.List;
  20. import java.util.Stack;
  21. import org.apache.commons.logging.Log;
  22. import org.apache.commons.logging.LogFactory;
  23. import org.apache.fop.datatypes.LengthBase;
  24. import org.apache.fop.datatypes.PercentBaseContext;
  25. import org.apache.fop.fo.FObj;
  26. import org.apache.fop.fo.flow.ChangeBar;
  27. /**
  28. * The base class for nearly all LayoutManagers.
  29. * Provides the functionality for merging the {@link LayoutManager}
  30. * and the {@link org.apache.fop.datatypes.PercentBaseContext} interfaces
  31. * into a common base class for all higher LayoutManagers.
  32. */
  33. public abstract class AbstractBaseLayoutManager
  34. implements LayoutManager, PercentBaseContext {
  35. /** Indicator if this LM generates reference areas. */
  36. protected boolean generatesReferenceArea;
  37. /** Indicator if this LM generates block areas. */
  38. protected boolean generatesBlockArea;
  39. /** The formatting object for this LM. */
  40. protected final FObj fobj;
  41. /**
  42. * logging instance
  43. */
  44. private static final Log LOG = LogFactory.getLog(AbstractBaseLayoutManager.class);
  45. /**
  46. * Abstract base layout manager.
  47. */
  48. public AbstractBaseLayoutManager() {
  49. fobj = null;
  50. }
  51. /**
  52. * Abstract base layout manager.
  53. *
  54. * @param fo the formatting object for this layout manager
  55. */
  56. public AbstractBaseLayoutManager(FObj fo) {
  57. if (fo == null) {
  58. throw new IllegalStateException("Null formatting object found.");
  59. }
  60. this.fobj = fo;
  61. setGeneratesReferenceArea(fo.generatesReferenceAreas());
  62. }
  63. // --------- Property Resolution related functions --------- //
  64. /** {@inheritDoc} */
  65. public int getBaseLength(int lengthBase, FObj fobjx) {
  66. if (fobjx == this.fobj) {
  67. switch (lengthBase) {
  68. case LengthBase.CONTAINING_BLOCK_WIDTH:
  69. return getAncestorBlockAreaIPD();
  70. case LengthBase.CONTAINING_BLOCK_HEIGHT:
  71. return getAncestorBlockAreaBPD();
  72. case LengthBase.PARENT_AREA_WIDTH:
  73. return getParentAreaIPD();
  74. case LengthBase.CONTAINING_REFAREA_WIDTH:
  75. return getReferenceAreaIPD();
  76. default:
  77. LOG.error("Unknown base type for LengthBase:" + lengthBase);
  78. return 0;
  79. }
  80. } else {
  81. LayoutManager lm = getParent();
  82. while (lm != null && fobjx != lm.getFObj()) {
  83. lm = lm.getParent();
  84. }
  85. if (lm != null) {
  86. return lm.getBaseLength(lengthBase, fobjx);
  87. }
  88. }
  89. LOG.error("Cannot find LM to handle given FO for LengthBase. ("
  90. + fobjx.getContextInfo() + ")");
  91. return 0;
  92. }
  93. /**
  94. * Find the first ancestor area that is a block area
  95. * and returns its IPD.
  96. * @return the ipd of the ancestor block area
  97. */
  98. protected int getAncestorBlockAreaIPD() {
  99. LayoutManager lm = getParent();
  100. while (lm != null) {
  101. if (lm.getGeneratesBlockArea() && !lm.getGeneratesLineArea()) {
  102. return lm.getContentAreaIPD();
  103. }
  104. lm = lm.getParent();
  105. }
  106. LOG.error("No parent LM found");
  107. return 0;
  108. }
  109. /**
  110. * Find the first ancestor area that is a block area
  111. * and returns its BPD.
  112. * @return the bpd of the ancestor block area
  113. */
  114. protected int getAncestorBlockAreaBPD() {
  115. LayoutManager lm = getParent();
  116. while (lm != null) {
  117. if (lm.getGeneratesBlockArea() && !lm.getGeneratesLineArea()) {
  118. return lm.getContentAreaBPD();
  119. }
  120. lm = lm.getParent();
  121. }
  122. LOG.error("No parent LM found");
  123. return 0;
  124. }
  125. /**
  126. * Find the parent area and returns its IPD.
  127. * @return the ipd of the parent area
  128. */
  129. protected int getParentAreaIPD() {
  130. LayoutManager lm = getParent();
  131. if (lm != null) {
  132. return lm.getContentAreaIPD();
  133. }
  134. LOG.error("No parent LM found");
  135. return 0;
  136. }
  137. /**
  138. * Find the parent area and returns its BPD.
  139. * @return the bpd of the parent area
  140. */
  141. protected int getParentAreaBPD() {
  142. LayoutManager lm = getParent();
  143. if (lm != null) {
  144. return lm.getContentAreaBPD();
  145. }
  146. LOG.error("No parent LM found");
  147. return 0;
  148. }
  149. /**
  150. * Find the first ancestor area that is a reference area
  151. * and returns its IPD.
  152. * @return the ipd of the ancestor reference area
  153. */
  154. public int getReferenceAreaIPD() {
  155. LayoutManager lm = getParent();
  156. while (lm != null) {
  157. if (lm.getGeneratesReferenceArea()) {
  158. return lm.getContentAreaIPD();
  159. }
  160. lm = lm.getParent();
  161. }
  162. LOG.error("No parent LM found");
  163. return 0;
  164. }
  165. /**
  166. * Find the first ancestor area that is a reference area
  167. * and returns its BPD.
  168. * @return the bpd of the ancestor reference area
  169. */
  170. protected int getReferenceAreaBPD() {
  171. LayoutManager lm = getParent();
  172. while (lm != null) {
  173. if (lm.getGeneratesReferenceArea()) {
  174. return lm.getContentAreaBPD();
  175. }
  176. lm = lm.getParent();
  177. }
  178. LOG.error("No parent LM found");
  179. return 0;
  180. }
  181. /**
  182. * {@inheritDoc}
  183. * <i>NOTE: Should be overridden by subclasses.
  184. * Default implementation throws an <code>UnsupportedOperationException</code>.</i>
  185. */
  186. public int getContentAreaIPD() {
  187. throw new UnsupportedOperationException(
  188. "getContentAreaIPD() called when it should have been overridden");
  189. }
  190. /**
  191. * {@inheritDoc}
  192. * <i>NOTE: Should be overridden by subclasses.
  193. * Default implementation throws an <code>UnsupportedOperationException</code>.</i>
  194. */
  195. public int getContentAreaBPD() {
  196. throw new UnsupportedOperationException(
  197. "getContentAreaBPD() called when it should have been overridden");
  198. }
  199. /** {@inheritDoc} */
  200. public boolean getGeneratesReferenceArea() {
  201. return generatesReferenceArea;
  202. }
  203. /**
  204. * Lets implementing LM set the flag indicating if they
  205. * generate reference areas.
  206. * @param generatesReferenceArea if true the areas generates by this LM are
  207. * reference areas.
  208. */
  209. protected void setGeneratesReferenceArea(boolean generatesReferenceArea) {
  210. this.generatesReferenceArea = generatesReferenceArea;
  211. }
  212. /** {@inheritDoc} */
  213. public boolean getGeneratesBlockArea() {
  214. return generatesBlockArea;
  215. }
  216. /**
  217. * Lets implementing LM set the flag indicating if they
  218. * generate block areas.
  219. * @param generatesBlockArea if true the areas generates by this LM are block areas.
  220. */
  221. protected void setGeneratesBlockArea(boolean generatesBlockArea) {
  222. this.generatesBlockArea = generatesBlockArea;
  223. }
  224. /** {@inheritDoc} */
  225. public boolean getGeneratesLineArea() {
  226. return false;
  227. }
  228. /**
  229. * {@inheritDoc}
  230. */
  231. public FObj getFObj() {
  232. return fobj;
  233. }
  234. /**
  235. * Returns the active change bar list.
  236. *
  237. * @return The active change bar list
  238. */
  239. public List<ChangeBar> getChangeBarList() {
  240. if (fobj == null) {
  241. return null;
  242. } else {
  243. return fobj.getChangeBarList();
  244. }
  245. }
  246. /** {@inheritDoc} */
  247. public void reset() {
  248. throw new UnsupportedOperationException("Not implemented");
  249. }
  250. /** {@inheritDoc} */
  251. public boolean isRestartable() {
  252. return false;
  253. }
  254. /** {@inheritDoc} */
  255. public List getNextKnuthElements(LayoutContext context, int alignment, Stack lmStack,
  256. Position positionAtIPDChange, LayoutManager restartAtLM) {
  257. throw new UnsupportedOperationException("Not implemented");
  258. }
  259. public void preserveChildrenAtEndOfLayout() {
  260. }
  261. public void recreateChildrenLMs() {
  262. }
  263. }