package net.sf.jasperreports.engine.fill;

import net.sf.jasperreports.engine.JRBreak;
import net.sf.jasperreports.engine.JRElement;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExpressionCollector;
import net.sf.jasperreports.engine.JRPrintElement;
import net.sf.jasperreports.engine.JRVisitor;

public class JRFillBreak extends JRFillElement implements JRBreak {
  protected JRFillBreak(JRBaseFiller filler, JRBreak breakElement, JRFillObjectFactory factory) {
    super(filler, (JRElement)breakElement, factory);
  }
  
  protected JRFillBreak(JRFillBreak breakElement, JRFillCloneFactory factory) {
    super(breakElement, factory);
  }
  
  public int getWidth() {
    switch (getType()) {
      case 1:
        width = this.filler.pageWidth - this.filler.leftMargin - this.filler.rightMargin;
        return width;
    } 
    int width = this.filler.columnWidth;
    return width;
  }
  
  public byte getType() {
    return ((JRBreak)this.parent).getType();
  }
  
  public void setType(byte type) {}
  
  protected void evaluate(byte evaluation) throws JRException {
    reset();
    evaluatePrintWhenExpression(evaluation);
    evaluateProperties(evaluation);
    setValueRepeating(true);
  }
  
  protected JRPrintElement fill() {
    return null;
  }
  
  public void collectExpressions(JRExpressionCollector collector) {
    collector.collect(this);
  }
  
  public void visit(JRVisitor visitor) {
    visitor.visitBreak(this);
  }
  
  protected void resolveElement(JRPrintElement element, byte evaluation) {}
  
  public JRFillCloneable createClone(JRFillCloneFactory factory) {
    return new JRFillBreak(this, factory);
  }
  
  public void rewind() {}
  
  protected boolean prepare(int availableStretchHeight, boolean isOverflow) throws JRException {
    super.prepare(availableStretchHeight, isOverflow);
    if (!isToPrint())
      return false; 
    boolean isToPrint = true;
    if (isOverflow && isAlreadyPrinted())
      isToPrint = false; 
    if (isToPrint && availableStretchHeight < getRelativeY() - getY() - getBandBottomY())
      isToPrint = false; 
    if (isToPrint)
      if (getType() == 2) {
        if (!this.filler.isFirstColumnBand || this.band.firstYElement != null)
          setStretchHeight(getHeight() + availableStretchHeight - getRelativeY() + getY() + getBandBottomY()); 
      } else if (!this.filler.isFirstPageBand || this.band.firstYElement != null) {
        setStretchHeight(getHeight() + availableStretchHeight - getRelativeY() + getY() + getBandBottomY());
        this.filler.columnIndex = this.filler.columnCount - 1;
      }  
    setToPrint(isToPrint);
    setReprinted(false);
    return false;
  }
}
