package net.sf.jasperreports.engine.design;

import net.sf.jasperreports.engine.JRBand;
import net.sf.jasperreports.engine.JRExpression;
import net.sf.jasperreports.engine.JROrigin;
import net.sf.jasperreports.engine.JRVariable;
import net.sf.jasperreports.engine.base.JRBaseGroup;

public class JRDesignGroup extends JRBaseGroup {
  private static final long serialVersionUID = 10200L;
  
  public static final String PROPERTY_COUNT_VARIABLE = "countVariable";
  
  public static final String PROPERTY_EXPRESSION = "expression";
  
  public static final String PROPERTY_GROUP_FOOTER = "groupFooter";
  
  public static final String PROPERTY_GROUP_HEADER = "groupHeader";
  
  public static final String PROPERTY_NAME = "name";
  
  public void setName(String name) {
    Object old = this.name;
    this.name = name;
    updateBandOrigins();
    getEventSupport().firePropertyChange("name", old, this.name);
  }
  
  public void setExpression(JRExpression expression) {
    Object old = this.expression;
    this.expression = expression;
    getEventSupport().firePropertyChange("expression", old, this.expression);
  }
  
  public void setGroupHeader(JRBand groupHeader) {
    Object old = this.groupHeader;
    this.groupHeader = groupHeader;
    setBandOrigin(this.groupHeader, (byte)5);
    getEventSupport().firePropertyChange("groupHeader", old, this.groupHeader);
  }
  
  public void setGroupFooter(JRBand groupFooter) {
    Object old = this.groupFooter;
    this.groupFooter = groupFooter;
    setBandOrigin(this.groupFooter, (byte)7);
    getEventSupport().firePropertyChange("groupFooter", old, this.groupFooter);
  }
  
  public void setCountVariable(JRVariable countVariable) {
    Object old = this.countVariable;
    this.countVariable = countVariable;
    getEventSupport().firePropertyChange("countVariable", old, this.countVariable);
  }
  
  protected void setBandOrigin(JRBand band, byte type) {
    if (band instanceof JRDesignBand) {
      JROrigin origin = new JROrigin(null, getName(), type);
      ((JRDesignBand)band).setOrigin(origin);
    } 
  }
  
  protected void updateBandOrigins() {
    setBandOrigin(getGroupHeader(), (byte)5);
    setBandOrigin(getGroupFooter(), (byte)7);
  }
}
