package net.sf.jasperreports.crosstabs.design;

import net.sf.jasperreports.crosstabs.JRCellContents;
import net.sf.jasperreports.crosstabs.JRCrosstabBucket;
import net.sf.jasperreports.crosstabs.base.JRBaseCrosstabGroup;
import net.sf.jasperreports.engine.JRVariable;
import net.sf.jasperreports.engine.design.JRDesignVariable;
import net.sf.jasperreports.engine.design.events.JRChangeEventsSupport;
import net.sf.jasperreports.engine.design.events.JRPropertyChangeSupport;

public abstract class JRDesignCrosstabGroup extends JRBaseCrosstabGroup implements JRChangeEventsSupport {
  public static final String PROPERTY_BUCKET = "bucket";
  
  public static final String PROPERTY_HEADER = "header";
  
  public static final String PROPERTY_NAME = "name";
  
  public static final String PROPERTY_TOTAL_HEADER = "totalHeader";
  
  public static final String PROPERTY_TOTAL_POSITION = "totalPosition";
  
  protected JRDesignVariable designVariable;
  
  protected JRDesignCrosstab parent;
  
  private transient JRPropertyChangeSupport eventSupport;
  
  protected JRDesignCrosstabGroup() {
    this.variable = (JRVariable)(this.designVariable = new JRDesignVariable());
    this.designVariable.setCalculation((byte)8);
    this.designVariable.setSystemDefined(true);
    this.header = new JRDesignCellContents();
    this.totalHeader = new JRDesignCellContents();
  }
  
  public void setName(String name) {
    Object old = this.name;
    this.name = name;
    this.designVariable.setName(name);
    getEventSupport().firePropertyChange("name", old, this.name);
  }
  
  public void setTotalPosition(byte totalPosition) {
    byte old = this.totalPosition;
    this.totalPosition = totalPosition;
    getEventSupport().firePropertyChange("totalPosition", old, this.totalPosition);
  }
  
  public void setBucket(JRDesignCrosstabBucket bucket) {
    Object old = this.bucket;
    this.bucket = (JRCrosstabBucket)bucket;
    getEventSupport().firePropertyChange("bucket", old, this.bucket);
  }
  
  public void setHeader(JRDesignCellContents header) {
    Object old = this.header;
    if (header == null)
      header = new JRDesignCellContents(); 
    this.header = header;
    getEventSupport().firePropertyChange("header", old, this.header);
  }
  
  public void setTotalHeader(JRDesignCellContents totalHeader) {
    Object old = this.totalHeader;
    if (totalHeader == null)
      totalHeader = new JRDesignCellContents(); 
    this.totalHeader = totalHeader;
    getEventSupport().firePropertyChange("totalHeader", old, this.totalHeader);
  }
  
  public JRDesignCrosstab getParent() {
    return this.parent;
  }
  
  void setParent(JRDesignCrosstab parent) {
    this.parent = parent;
  }
  
  protected void setCellOrigin(JRCellContents cell, JRCrosstabOrigin origin) {
    if (cell instanceof JRDesignCellContents)
      ((JRDesignCellContents)cell).setOrigin(origin); 
  }
  
  public Object clone() {
    return null;
  }
  
  public JRPropertyChangeSupport getEventSupport() {
    synchronized (this) {
      if (this.eventSupport == null)
        this.eventSupport = new JRPropertyChangeSupport(this); 
    } 
    return this.eventSupport;
  }
}
