1 17 18 19 20 package org.apache.fop.layoutmgr; 21 22 import org.apache.commons.logging.Log; 23 import org.apache.commons.logging.LogFactory; 24 import org.apache.fop.fo.FObj; 25 import org.apache.fop.fo.FONode; 26 import org.apache.fop.area.Area; 27 import org.apache.fop.area.PageViewport; 28 import org.apache.fop.fo.Constants; 29 import org.apache.fop.fo.flow.RetrieveMarker; 30 31 import java.util.LinkedList ; 32 import java.util.List ; 33 import java.util.ArrayList ; 34 import java.util.ListIterator ; 35 import java.util.Map ; 36 37 40 public abstract class AbstractLayoutManager extends AbstractBaseLayoutManager 41 implements Constants { 42 43 46 private static Log log = LogFactory.getLog(AbstractLayoutManager.class); 47 48 49 protected LayoutManager parentLM = null; 50 51 protected List childLMs = null; 52 53 protected ListIterator fobjIter = null; 54 55 protected Map markers = null; 56 57 58 private boolean bFinished = false; 59 60 61 protected LayoutManager curChildLM = null; 62 63 protected ListIterator childLMiter = null; 64 65 private int lastGeneratedPosition = -1; 66 private int smallestPosNumberChecked = Integer.MAX_VALUE; 67 68 71 public AbstractLayoutManager() { 72 } 73 74 79 public AbstractLayoutManager(FObj fo) { 80 super(fo); 81 if (fo == null) { 82 throw new IllegalStateException ("Null formatting object found."); 83 } 84 markers = fo.getMarkers(); 85 fobjIter = fo.getChildNodes(); 86 childLMiter = new LMiter(this); 87 } 88 89 90 public void setParent(LayoutManager lm) { 91 this.parentLM = lm; 92 } 93 94 95 public LayoutManager getParent() { 96 return this.parentLM; 97 } 98 99 100 public void initialize() { 101 } 103 104 111 protected LayoutManager getChildLM() { 112 if (curChildLM != null && !curChildLM.isFinished()) { 113 return curChildLM; 114 } 115 while (childLMiter.hasNext()) { 116 curChildLM = (LayoutManager) childLMiter.next(); 117 curChildLM.initialize(); 118 return curChildLM; 119 } 120 return null; 121 } 122 123 127 protected boolean hasNextChildLM() { 128 return childLMiter.hasNext(); 129 } 130 131 139 protected void reset(org.apache.fop.layoutmgr.Position pos) { 140 LayoutManager lm = (pos != null) ? pos.getLM() : null; 142 if (curChildLM != lm) { 143 if (childLMiter.hasPrevious() && curChildLM 145 != (LayoutManager) childLMiter.previous()) { 146 } 148 while (curChildLM != lm && childLMiter.hasPrevious()) { 149 curChildLM.resetPosition(null); 150 curChildLM = (LayoutManager) childLMiter.previous(); 151 } 152 childLMiter.next(); 154 } 155 if (curChildLM != null) { 156 curChildLM.resetPosition(pos); 157 } 158 if (isFinished()) { 159 setFinished(false); 160 } 161 } 162 163 164 public void resetPosition(Position resetPos) { 165 } 169 170 175 public boolean isFinished() { 176 return bFinished; 177 } 178 179 183 public void setFinished(boolean fin) { 184 bFinished = fin; 185 } 186 187 192 public void addAreas(PositionIterator posIter, LayoutContext context) { 193 } 194 195 198 public LinkedList getNextKnuthElements(LayoutContext context, 199 int alignment) { 200 log.warn("null implementation of getNextKnuthElements() called!"); 201 setFinished(true); 202 return null; 203 } 204 205 208 public LinkedList getChangedKnuthElements(List oldList, 209 int alignment) { 210 log.warn("null implementation of getChangeKnuthElement() called!"); 211 return null; 212 } 213 214 226 public Area getParentArea(Area childArea) { 227 return null; 228 } 229 230 236 public void addChildArea(Area childArea) { 237 } 238 239 245 protected List createChildLMs(int size) { 246 if (fobjIter == null) { 247 return null; 248 } 249 List newLMs = new ArrayList (size); 250 while (fobjIter.hasNext() && newLMs.size() < size ) { 251 Object theobj = fobjIter.next(); 252 if (theobj instanceof FONode) { 253 FONode foNode = (FONode) theobj; 254 if (foNode instanceof RetrieveMarker) { 255 foNode = getPSLM().resolveRetrieveMarker( 256 (RetrieveMarker) foNode); 257 } 258 if (foNode != null) { 259 getPSLM().getLayoutManagerMaker(). 260 makeLayoutManagers(foNode, newLMs); 261 } 262 } 263 } 264 return newLMs; 265 } 266 267 270 public PageSequenceLayoutManager getPSLM() { 271 return parentLM.getPSLM(); 272 } 273 274 277 public Page getCurrentPage() { 278 return getPSLM().getCurrentPage(); 279 } 280 281 282 public PageViewport getCurrentPV() { 283 return getPSLM().getCurrentPage().getPageViewport(); 284 } 285 286 289 public boolean createNextChildLMs(int pos) { 290 List newLMs = createChildLMs(pos + 1 - childLMs.size()); 291 addChildLMs(newLMs); 292 return pos < childLMs.size(); 293 } 294 295 298 public List getChildLMs() { 299 if (childLMs == null) { 300 childLMs = new java.util.ArrayList (10); 301 } 302 return childLMs; 303 } 304 305 308 public void addChildLM(LayoutManager lm) { 309 if (lm == null) { 310 return; 311 } 312 lm.setParent(this); 313 if (childLMs == null) { 314 childLMs = new java.util.ArrayList (10); 315 } 316 childLMs.add(lm); 317 log.trace(this.getClass().getName() 318 + ": Adding child LM " + lm.getClass().getName()); 319 } 320 321 324 public void addChildLMs(List newLMs) { 325 if (newLMs == null || newLMs.size() == 0) { 326 return; 327 } 328 ListIterator iter = newLMs.listIterator(); 329 while (iter.hasNext()) { 330 LayoutManager lm = (LayoutManager) iter.next(); 331 addChildLM(lm); 332 } 333 } 334 335 341 public Position notifyPos(Position pos) { 342 if (pos.getIndex() >= 0) { 343 throw new IllegalStateException ("Position already got its index"); 344 } 345 lastGeneratedPosition++; 346 pos.setIndex(lastGeneratedPosition); 347 return pos; 348 } 349 350 355 public boolean isFirst(Position pos) { 356 if (pos.getIndex() < 0) { 358 throw new IllegalArgumentException ("Only Positions with an index can be checked"); 359 } 360 if (pos.getIndex() == this.smallestPosNumberChecked) { 361 return true; 362 } else if (pos.getIndex() < this.smallestPosNumberChecked) { 363 this.smallestPosNumberChecked = pos.getIndex(); 364 return true; 365 } else { 366 return false; 367 } 368 } 369 370 375 public boolean isLast(Position pos) { 376 if (pos.getIndex() < 0) { 378 throw new IllegalArgumentException ("Only Positions with an index can be checked"); 379 } 380 return (pos.getIndex() == this.lastGeneratedPosition 381 && isFinished()); 382 } 383 384 388 protected void transferForeignAttributes(Area targetArea) { 389 Map atts = getFObj().getForeignAttributes(); 390 targetArea.setForeignAttributes(atts); 391 } 392 } 393 | Popular Tags |