package net.sf.jasperreports.engine.design;

import java.awt.Color;
import java.io.IOException;
import java.io.ObjectInputStream;
import net.sf.jasperreports.engine.JRAlignment;
import net.sf.jasperreports.engine.JRBox;
import net.sf.jasperreports.engine.JRBoxContainer;
import net.sf.jasperreports.engine.JRCommonElement;
import net.sf.jasperreports.engine.JRCommonText;
import net.sf.jasperreports.engine.JRDefaultStyleProvider;
import net.sf.jasperreports.engine.JRFont;
import net.sf.jasperreports.engine.JRLineBox;
import net.sf.jasperreports.engine.JRPen;
import net.sf.jasperreports.engine.JRReportFont;
import net.sf.jasperreports.engine.JRStyle;
import net.sf.jasperreports.engine.JRTextElement;
import net.sf.jasperreports.engine.base.JRBaseLineBox;
import net.sf.jasperreports.engine.util.JRBoxUtil;
import net.sf.jasperreports.engine.util.JRPenUtil;
import net.sf.jasperreports.engine.util.JRStyleResolver;
import net.sf.jasperreports.engine.util.LineBoxWrapper;

public abstract class JRDesignTextElement extends JRDesignElement implements JRTextElement {
  private static final long serialVersionUID = 10200L;
  
  protected Byte horizontalAlignment;
  
  protected Byte verticalAlignment;
  
  protected Byte rotation;
  
  protected Byte lineSpacing;
  
  protected String markup;
  
  protected JRLineBox lineBox;
  
  protected JRReportFont reportFont = null;
  
  protected String fontName = null;
  
  protected Boolean isBold = null;
  
  protected Boolean isItalic = null;
  
  protected Boolean isUnderline = null;
  
  protected Boolean isStrikeThrough = null;
  
  protected Integer fontSize = null;
  
  protected String pdfFontName = null;
  
  protected String pdfEncoding = null;
  
  protected Boolean isPdfEmbedded = null;
  
  private Byte border;
  
  private Byte topBorder;
  
  private Byte leftBorder;
  
  private Byte bottomBorder;
  
  private Byte rightBorder;
  
  private Color borderColor;
  
  private Color topBorderColor;
  
  private Color leftBorderColor;
  
  private Color bottomBorderColor;
  
  private Color rightBorderColor;
  
  private Integer padding;
  
  private Integer topPadding;
  
  private Integer leftPadding;
  
  private Integer bottomPadding;
  
  private Integer rightPadding;
  
  private Boolean isStyledText;
  
  protected JRDesignTextElement(JRDefaultStyleProvider defaultStyleProvider) {
    super(defaultStyleProvider);
    this.border = null;
    this.topBorder = null;
    this.leftBorder = null;
    this.bottomBorder = null;
    this.rightBorder = null;
    this.borderColor = null;
    this.topBorderColor = null;
    this.leftBorderColor = null;
    this.bottomBorderColor = null;
    this.rightBorderColor = null;
    this.padding = null;
    this.topPadding = null;
    this.leftPadding = null;
    this.bottomPadding = null;
    this.rightPadding = null;
    this.isStyledText = null;
    this.lineBox = (JRLineBox)new JRBaseLineBox((JRBoxContainer)this);
  }
  
  protected JRFont getBaseFont() {
    if (this.reportFont != null)
      return (JRFont)this.reportFont; 
    if (this.defaultStyleProvider != null)
      return (JRFont)this.defaultStyleProvider.getDefaultFont(); 
    return null;
  }
  
  public byte getMode() {
    return JRStyleResolver.getMode((JRCommonElement)this, (byte)2);
  }
  
  public byte getTextAlignment() {
    if (this.horizontalAlignment == null) {
      JRStyle style = getBaseStyle();
      if (style != null && style.getHorizontalAlignment() != null)
        return style.getHorizontalAlignment().byteValue(); 
      return 1;
    } 
    return this.horizontalAlignment.byteValue();
  }
  
  public void setTextAlignment(byte horizontalAlignment) {
    setHorizontalAlignment(new Byte(horizontalAlignment));
  }
  
  public byte getHorizontalAlignment() {
    return JRStyleResolver.getHorizontalAlignment((JRAlignment)this);
  }
  
  public Byte getOwnHorizontalAlignment() {
    return this.horizontalAlignment;
  }
  
  public void setHorizontalAlignment(byte horizontalAlignment) {
    setHorizontalAlignment(new Byte(horizontalAlignment));
  }
  
  public void setHorizontalAlignment(Byte horizontalAlignment) {
    Object old = this.horizontalAlignment;
    this.horizontalAlignment = horizontalAlignment;
    getEventSupport().firePropertyChange("horizontalAlignment", old, this.horizontalAlignment);
  }
  
  public byte getVerticalAlignment() {
    return JRStyleResolver.getVerticalAlignment((JRAlignment)this);
  }
  
  public Byte getOwnVerticalAlignment() {
    return this.verticalAlignment;
  }
  
  public void setVerticalAlignment(byte verticalAlignment) {
    setVerticalAlignment(new Byte(verticalAlignment));
  }
  
  public void setVerticalAlignment(Byte verticalAlignment) {
    Object old = this.verticalAlignment;
    this.verticalAlignment = verticalAlignment;
    getEventSupport().firePropertyChange("verticalAlignment", old, this.verticalAlignment);
  }
  
  public byte getRotation() {
    return JRStyleResolver.getRotation((JRCommonText)this);
  }
  
  public Byte getOwnRotation() {
    return this.rotation;
  }
  
  public void setRotation(byte rotation) {
    setRotation(new Byte(rotation));
  }
  
  public void setRotation(Byte rotation) {
    Object old = this.rotation;
    this.rotation = rotation;
    getEventSupport().firePropertyChange("rotation", old, this.rotation);
  }
  
  public byte getLineSpacing() {
    return JRStyleResolver.getLineSpacing((JRCommonText)this);
  }
  
  public Byte getOwnLineSpacing() {
    return this.lineSpacing;
  }
  
  public void setLineSpacing(byte lineSpacing) {
    setLineSpacing(new Byte(lineSpacing));
  }
  
  public void setLineSpacing(Byte lineSpacing) {
    Object old = this.lineSpacing;
    this.lineSpacing = lineSpacing;
    getEventSupport().firePropertyChange("lineSpacing", old, this.lineSpacing);
  }
  
  public boolean isStyledText() {
    return "styled".equals(getMarkup());
  }
  
  public Boolean isOwnStyledText() {
    String mkp = getOwnMarkup();
    return "styled".equals(mkp) ? Boolean.TRUE : ((mkp == null) ? null : Boolean.FALSE);
  }
  
  public void setStyledText(boolean isStyledText) {
    setStyledText(isStyledText ? Boolean.TRUE : Boolean.FALSE);
  }
  
  public void setStyledText(Boolean isStyledText) {
    if (isStyledText == null) {
      setMarkup((String)null);
    } else {
      setMarkup(isStyledText.booleanValue() ? "styled" : "none");
    } 
  }
  
  public String getMarkup() {
    return JRStyleResolver.getMarkup((JRCommonText)this);
  }
  
  public String getOwnMarkup() {
    return this.markup;
  }
  
  public void setMarkup(String markup) {
    Object old = this.markup;
    this.markup = markup;
    getEventSupport().firePropertyChange("markup", old, this.markup);
  }
  
  public JRBox getBox() {
    return (JRBox)new LineBoxWrapper(getLineBox());
  }
  
  public JRLineBox getLineBox() {
    return this.lineBox;
  }
  
  public void setBox(JRBox box) {
    JRBoxUtil.setBoxToLineBox(box, this.lineBox);
  }
  
  public JRFont getFont() {
    return (JRFont)this;
  }
  
  public void setFont(JRFont font) {
    setReportFont(font.getReportFont());
    setFontName(font.getOwnFontName());
    setBold(font.isOwnBold());
    setItalic(font.isOwnItalic());
    setUnderline(font.isOwnUnderline());
    setStrikeThrough(font.isOwnStrikeThrough());
    setFontSize(font.getOwnSize());
    setPdfFontName(font.getOwnPdfFontName());
    setPdfEncoding(font.getOwnPdfEncoding());
    setPdfEmbedded(font.isOwnPdfEmbedded());
  }
  
  public byte getBorder() {
    return JRPenUtil.getPenFromLinePen((JRPen)this.lineBox.getPen());
  }
  
  public Byte getOwnBorder() {
    return JRPenUtil.getOwnPenFromLinePen((JRPen)this.lineBox.getPen());
  }
  
  public void setBorder(byte border) {
    JRPenUtil.setLinePenFromPen(border, (JRPen)this.lineBox.getPen());
  }
  
  public void setBorder(Byte border) {
    JRPenUtil.setLinePenFromPen(border, (JRPen)this.lineBox.getPen());
  }
  
  public Color getBorderColor() {
    return this.lineBox.getPen().getLineColor();
  }
  
  public Color getOwnBorderColor() {
    return this.lineBox.getPen().getOwnLineColor();
  }
  
  public void setBorderColor(Color borderColor) {
    this.lineBox.getPen().setLineColor(borderColor);
  }
  
  public int getPadding() {
    return this.lineBox.getPadding().intValue();
  }
  
  public Integer getOwnPadding() {
    return this.lineBox.getOwnPadding();
  }
  
  public void setPadding(int padding) {
    this.lineBox.setPadding(padding);
  }
  
  public void setPadding(Integer padding) {
    this.lineBox.setPadding(padding);
  }
  
  public byte getTopBorder() {
    return JRPenUtil.getPenFromLinePen((JRPen)this.lineBox.getTopPen());
  }
  
  public Byte getOwnTopBorder() {
    return JRPenUtil.getOwnPenFromLinePen((JRPen)this.lineBox.getTopPen());
  }
  
  public void setTopBorder(byte topBorder) {
    JRPenUtil.setLinePenFromPen(topBorder, (JRPen)this.lineBox.getTopPen());
  }
  
  public void setTopBorder(Byte topBorder) {
    JRPenUtil.setLinePenFromPen(topBorder, (JRPen)this.lineBox.getTopPen());
  }
  
  public Color getTopBorderColor() {
    return this.lineBox.getTopPen().getLineColor();
  }
  
  public Color getOwnTopBorderColor() {
    return this.lineBox.getTopPen().getOwnLineColor();
  }
  
  public void setTopBorderColor(Color topBorderColor) {
    this.lineBox.getTopPen().setLineColor(topBorderColor);
  }
  
  public int getTopPadding() {
    return this.lineBox.getTopPadding().intValue();
  }
  
  public Integer getOwnTopPadding() {
    return this.lineBox.getOwnTopPadding();
  }
  
  public void setTopPadding(int topPadding) {
    this.lineBox.setTopPadding(topPadding);
  }
  
  public void setTopPadding(Integer topPadding) {
    this.lineBox.setTopPadding(topPadding);
  }
  
  public byte getLeftBorder() {
    return JRPenUtil.getPenFromLinePen((JRPen)this.lineBox.getLeftPen());
  }
  
  public Byte getOwnLeftBorder() {
    return JRPenUtil.getOwnPenFromLinePen((JRPen)this.lineBox.getLeftPen());
  }
  
  public void setLeftBorder(byte leftBorder) {
    JRPenUtil.setLinePenFromPen(leftBorder, (JRPen)this.lineBox.getLeftPen());
  }
  
  public void setLeftBorder(Byte leftBorder) {
    JRPenUtil.setLinePenFromPen(leftBorder, (JRPen)this.lineBox.getLeftPen());
  }
  
  public Color getLeftBorderColor() {
    return this.lineBox.getLeftPen().getLineColor();
  }
  
  public Color getOwnLeftBorderColor() {
    return this.lineBox.getLeftPen().getOwnLineColor();
  }
  
  public void setLeftBorderColor(Color leftBorderColor) {
    this.lineBox.getLeftPen().setLineColor(leftBorderColor);
  }
  
  public int getLeftPadding() {
    return this.lineBox.getLeftPadding().intValue();
  }
  
  public Integer getOwnLeftPadding() {
    return this.lineBox.getOwnLeftPadding();
  }
  
  public void setLeftPadding(int leftPadding) {
    this.lineBox.setLeftPadding(leftPadding);
  }
  
  public void setLeftPadding(Integer leftPadding) {
    this.lineBox.setLeftPadding(leftPadding);
  }
  
  public byte getBottomBorder() {
    return JRPenUtil.getPenFromLinePen((JRPen)this.lineBox.getBottomPen());
  }
  
  public Byte getOwnBottomBorder() {
    return JRPenUtil.getOwnPenFromLinePen((JRPen)this.lineBox.getBottomPen());
  }
  
  public void setBottomBorder(byte bottomBorder) {
    JRPenUtil.setLinePenFromPen(bottomBorder, (JRPen)this.lineBox.getBottomPen());
  }
  
  public void setBottomBorder(Byte bottomBorder) {
    JRPenUtil.setLinePenFromPen(bottomBorder, (JRPen)this.lineBox.getBottomPen());
  }
  
  public Color getBottomBorderColor() {
    return this.lineBox.getBottomPen().getLineColor();
  }
  
  public Color getOwnBottomBorderColor() {
    return this.lineBox.getBottomPen().getOwnLineColor();
  }
  
  public void setBottomBorderColor(Color bottomBorderColor) {
    this.lineBox.getBottomPen().setLineColor(bottomBorderColor);
  }
  
  public int getBottomPadding() {
    return this.lineBox.getBottomPadding().intValue();
  }
  
  public Integer getOwnBottomPadding() {
    return this.lineBox.getOwnBottomPadding();
  }
  
  public void setBottomPadding(int bottomPadding) {
    this.lineBox.setBottomPadding(bottomPadding);
  }
  
  public void setBottomPadding(Integer bottomPadding) {
    this.lineBox.setBottomPadding(bottomPadding);
  }
  
  public byte getRightBorder() {
    return JRPenUtil.getPenFromLinePen((JRPen)this.lineBox.getRightPen());
  }
  
  public Byte getOwnRightBorder() {
    return JRPenUtil.getOwnPenFromLinePen((JRPen)this.lineBox.getRightPen());
  }
  
  public void setRightBorder(byte rightBorder) {
    JRPenUtil.setLinePenFromPen(rightBorder, (JRPen)this.lineBox.getRightPen());
  }
  
  public void setRightBorder(Byte rightBorder) {
    JRPenUtil.setLinePenFromPen(rightBorder, (JRPen)this.lineBox.getRightPen());
  }
  
  public Color getRightBorderColor() {
    return this.lineBox.getRightPen().getLineColor();
  }
  
  public Color getOwnRightBorderColor() {
    return this.lineBox.getRightPen().getOwnLineColor();
  }
  
  public void setRightBorderColor(Color rightBorderColor) {
    this.lineBox.getRightPen().setLineColor(rightBorderColor);
  }
  
  public int getRightPadding() {
    return this.lineBox.getRightPadding().intValue();
  }
  
  public Integer getOwnRightPadding() {
    return this.lineBox.getOwnRightPadding();
  }
  
  public void setRightPadding(int rightPadding) {
    this.lineBox.setRightPadding(rightPadding);
  }
  
  public void setRightPadding(Integer rightPadding) {
    this.lineBox.setRightPadding(rightPadding);
  }
  
  public JRReportFont getReportFont() {
    return this.reportFont;
  }
  
  public void setReportFont(JRReportFont reportFont) {
    Object old = this.reportFont;
    this.reportFont = reportFont;
    getEventSupport().firePropertyChange("reportFont", old, this.reportFont);
  }
  
  public String getFontName() {
    return JRStyleResolver.getFontName((JRFont)this);
  }
  
  public String getOwnFontName() {
    return this.fontName;
  }
  
  public void setFontName(String fontName) {
    Object old = this.fontName;
    this.fontName = fontName;
    getEventSupport().firePropertyChange("fontName", old, this.fontName);
  }
  
  public boolean isBold() {
    return JRStyleResolver.isBold((JRFont)this);
  }
  
  public Boolean isOwnBold() {
    return this.isBold;
  }
  
  public void setBold(boolean isBold) {
    setBold(isBold ? Boolean.TRUE : Boolean.FALSE);
  }
  
  public void setBold(Boolean isBold) {
    Object old = this.isBold;
    this.isBold = isBold;
    getEventSupport().firePropertyChange("bold", old, this.isBold);
  }
  
  public boolean isItalic() {
    return JRStyleResolver.isItalic((JRFont)this);
  }
  
  public Boolean isOwnItalic() {
    return this.isItalic;
  }
  
  public void setItalic(boolean isItalic) {
    setItalic(isItalic ? Boolean.TRUE : Boolean.FALSE);
  }
  
  public void setItalic(Boolean isItalic) {
    Object old = this.isItalic;
    this.isItalic = isItalic;
    getEventSupport().firePropertyChange("italic", old, this.isItalic);
  }
  
  public boolean isUnderline() {
    return JRStyleResolver.isUnderline((JRFont)this);
  }
  
  public Boolean isOwnUnderline() {
    return this.isUnderline;
  }
  
  public void setUnderline(boolean isUnderline) {
    setUnderline(isUnderline ? Boolean.TRUE : Boolean.FALSE);
  }
  
  public void setUnderline(Boolean isUnderline) {
    Object old = this.isUnderline;
    this.isUnderline = isUnderline;
    getEventSupport().firePropertyChange("underline", old, this.isUnderline);
  }
  
  public boolean isStrikeThrough() {
    return JRStyleResolver.isStrikeThrough((JRFont)this);
  }
  
  public Boolean isOwnStrikeThrough() {
    return this.isStrikeThrough;
  }
  
  public void setStrikeThrough(boolean isStrikeThrough) {
    setStrikeThrough(isStrikeThrough ? Boolean.TRUE : Boolean.FALSE);
  }
  
  public void setStrikeThrough(Boolean isStrikeThrough) {
    Object old = this.isStrikeThrough;
    this.isStrikeThrough = isStrikeThrough;
    getEventSupport().firePropertyChange("strikeThrough", old, this.isStrikeThrough);
  }
  
  public int getFontSize() {
    return JRStyleResolver.getFontSize((JRFont)this);
  }
  
  public Integer getOwnFontSize() {
    return this.fontSize;
  }
  
  public void setFontSize(int fontSize) {
    setFontSize(new Integer(fontSize));
  }
  
  public void setFontSize(Integer fontSize) {
    Object old = this.fontSize;
    this.fontSize = fontSize;
    getEventSupport().firePropertyChange("fontSize", old, this.fontSize);
  }
  
  public int getSize() {
    return getFontSize();
  }
  
  public Integer getOwnSize() {
    return getOwnFontSize();
  }
  
  public void setSize(int size) {
    setFontSize(size);
  }
  
  public void setSize(Integer size) {
    setFontSize(size);
  }
  
  public String getPdfFontName() {
    return JRStyleResolver.getPdfFontName((JRFont)this);
  }
  
  public String getOwnPdfFontName() {
    return this.pdfFontName;
  }
  
  public void setPdfFontName(String pdfFontName) {
    Object old = this.pdfFontName;
    this.pdfFontName = pdfFontName;
    getEventSupport().firePropertyChange("pdfFontName", old, this.pdfFontName);
  }
  
  public String getPdfEncoding() {
    return JRStyleResolver.getPdfEncoding((JRFont)this);
  }
  
  public String getOwnPdfEncoding() {
    return this.pdfEncoding;
  }
  
  public void setPdfEncoding(String pdfEncoding) {
    Object old = this.pdfEncoding;
    this.pdfEncoding = pdfEncoding;
    getEventSupport().firePropertyChange("pdfEncoding", old, this.pdfEncoding);
  }
  
  public boolean isPdfEmbedded() {
    return JRStyleResolver.isPdfEmbedded((JRFont)this);
  }
  
  public Boolean isOwnPdfEmbedded() {
    return this.isPdfEmbedded;
  }
  
  public void setPdfEmbedded(boolean isPdfEmbedded) {
    setPdfEmbedded(isPdfEmbedded ? Boolean.TRUE : Boolean.FALSE);
  }
  
  public void setPdfEmbedded(Boolean isPdfEmbedded) {
    Object old = this.isPdfEmbedded;
    this.isPdfEmbedded = isPdfEmbedded;
    getEventSupport().firePropertyChange("pdfEmbedded", old, this.isPdfEmbedded);
  }
  
  public Color getDefaultLineColor() {
    return getForecolor();
  }
  
  private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
    in.defaultReadObject();
    if (this.lineBox == null) {
      this.lineBox = (JRLineBox)new JRBaseLineBox((JRBoxContainer)this);
      JRBoxUtil.setToBox(this.border, this.topBorder, this.leftBorder, this.bottomBorder, this.rightBorder, this.borderColor, this.topBorderColor, this.leftBorderColor, this.bottomBorderColor, this.rightBorderColor, this.padding, this.topPadding, this.leftPadding, this.bottomPadding, this.rightPadding, this.lineBox);
      this.border = null;
      this.topBorder = null;
      this.leftBorder = null;
      this.bottomBorder = null;
      this.rightBorder = null;
      this.borderColor = null;
      this.topBorderColor = null;
      this.leftBorderColor = null;
      this.bottomBorderColor = null;
      this.rightBorderColor = null;
      this.padding = null;
      this.topPadding = null;
      this.leftPadding = null;
      this.bottomPadding = null;
      this.rightPadding = null;
    } 
    if (this.isStyledText != null) {
      this.markup = this.isStyledText.booleanValue() ? "styled" : "none";
      this.isStyledText = null;
    } 
  }
}
