package net.sf.jasperreports.engine.base;

import java.awt.Color;
import java.io.IOException;
import java.io.ObjectInputStream;
import net.sf.jasperreports.engine.JRCommonGraphicElement;
import net.sf.jasperreports.engine.JRElement;
import net.sf.jasperreports.engine.JRGraphicElement;
import net.sf.jasperreports.engine.JRPen;
import net.sf.jasperreports.engine.JRPenContainer;
import net.sf.jasperreports.engine.util.JRPenUtil;
import net.sf.jasperreports.engine.util.JRStyleResolver;

public abstract class JRBaseGraphicElement extends JRBaseElement implements JRGraphicElement {
  private static final long serialVersionUID = 10200L;
  
  protected JRPen linePen;
  
  protected Byte fill;
  
  private Byte pen;
  
  protected JRBaseGraphicElement(JRGraphicElement graphicElement, JRBaseObjectFactory factory) {
    super((JRElement)graphicElement, factory);
    this.linePen = graphicElement.getLinePen().clone((JRPenContainer)this);
    this.fill = graphicElement.getOwnFill();
  }
  
  public JRPen getLinePen() {
    return this.linePen;
  }
  
  public byte getPen() {
    return JRPenUtil.getPenFromLinePen(this.linePen);
  }
  
  public Byte getOwnPen() {
    return JRPenUtil.getOwnPenFromLinePen(this.linePen);
  }
  
  public void setPen(byte pen) {
    setPen(new Byte(pen));
  }
  
  public void setPen(Byte pen) {
    JRPenUtil.setLinePenFromPen(pen, this.linePen);
  }
  
  public byte getFill() {
    return JRStyleResolver.getFill((JRCommonGraphicElement)this);
  }
  
  public Byte getOwnFill() {
    return this.fill;
  }
  
  public void setFill(byte fill) {
    setFill(new Byte(fill));
  }
  
  public void setFill(Byte fill) {
    Object old = this.fill;
    this.fill = fill;
    getEventSupport().firePropertyChange("fill", old, this.fill);
  }
  
  public Float getDefaultLineWidth() {
    return JRPen.LINE_WIDTH_1;
  }
  
  public Color getDefaultLineColor() {
    return getForecolor();
  }
  
  private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
    in.defaultReadObject();
    if (this.linePen == null) {
      this.linePen = new JRBasePen((JRPenContainer)this);
      JRPenUtil.setLinePenFromPen(this.pen, this.linePen);
      this.pen = null;
    } 
  }
}
