/*
 * $Id$
 * Copyright (C) 2002 The Apache Software Foundation. All rights reserved.
 * For details on use and redistribution please refer to the
 * LICENSE file included with these sources.
 */

package org.apache.fop.layoutmgr;

import org.apache.fop.fo.FObj;
import org.apache.fop.area.RegionReference;
import org.apache.fop.area.Area;
import org.apache.fop.area.Block;

import java.util.ArrayList;
import java.util.List;

/**
 * LayoutManager for an fo:flow object.
 * Its parent LM is the PageLayoutManager.
 * This LM is responsible for getting columns of the appropriate size
 * and filling them with block-level areas generated by its children.
 */
public class StaticContentLayoutManager extends BlockStackingLayoutManager {

    private RegionReference region;
    private List blockBreaks = new ArrayList();      
  
    public StaticContentLayoutManager(FObj fobj) {
        super(fobj);
    }

    public void setRegionReference(RegionReference region) {
        this.region = region;
    }
   
    public BreakPoss getNextBreakPoss(LayoutContext context) {

        // currently active LM
        LayoutManager curLM;

        while ((curLM = getChildLM()) != null) {
            // Make break positions and return page break
            // Set up a LayoutContext
            BreakPoss bp;
            LayoutContext childLC = context;
            if (!curLM.isFinished()) {
                if ((bp = curLM.getNextBreakPoss(childLC)) != null) {
                    blockBreaks.add(bp);
                    if (bp.isForcedBreak()) {
                        getLogger().error("Forced breaks are not allowed in static content - ignoring");
                        return null;
                    }
                }
            }
        }
        setFinished(true);
        if (blockBreaks.size() > 0) {
            return new BreakPoss(
              new LeafPosition(this, blockBreaks.size() - 1));
        }
        return null;
    }

    public void addAreas(PositionIterator parentIter, LayoutContext layoutContext) {

        LayoutManager childLM;
        int iStartPos = 0;
        LayoutContext lc = new LayoutContext(0);
        while (parentIter.hasNext()) {
            LeafPosition lfp = (LeafPosition) parentIter.next();
            // Add the block areas to Area
            PositionIterator breakPosIter =
              new BreakPossPosIter(blockBreaks, iStartPos,
                                   lfp.getLeafPos() + 1);
            iStartPos = lfp.getLeafPos() + 1; 
            while ((childLM = breakPosIter.getNextChildLM()) != null) {
                childLM.addAreas(breakPosIter, lc);
            }
        }

        flush();
    }


    /**
     * Add child area to a the correct container, depending on its
     * area class. A Flow can fill at most one area container of any class
     * at any one time. The actual work is done by BlockStackingLM.
     */
    public void addChild(Area childArea) {
        region.addBlock((Block)childArea);
    }

    public Area getParentArea(Area childArea) {
        return region;
    }

    /**
     * Markers are not allowed in static areas so this reports an
     * error and does nothing.
     *
     * @see org.apache.fop.layoutmgr.LayoutManager
     */
    public void addMarker(String name, LayoutManager lm, boolean start) {
        // error markers not allowed in static
        getLogger().error("Cannot add marker to static areas");
    }
}


@@ -2654,15 +2654,67 @@
     
-flush()
+blockBreaks.clear();%0A        flush();%0A        region = null
 ;%0A  

clear old breaks