package net.sf.jasperreports.engine.fill;

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.JRCommonText;
import net.sf.jasperreports.engine.JRDefaultStyleProvider;
import net.sf.jasperreports.engine.JRElement;
import net.sf.jasperreports.engine.JRFont;
import net.sf.jasperreports.engine.JRHyperlinkHelper;
import net.sf.jasperreports.engine.JRLineBox;
import net.sf.jasperreports.engine.JROrigin;
import net.sf.jasperreports.engine.JRPen;
import net.sf.jasperreports.engine.JRReportFont;
import net.sf.jasperreports.engine.JRRuntimeException;
import net.sf.jasperreports.engine.JRStaticText;
import net.sf.jasperreports.engine.JRStyle;
import net.sf.jasperreports.engine.JRTextElement;
import net.sf.jasperreports.engine.JRTextField;
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 class JRTemplateText extends JRTemplateElement implements JRAlignment, JRBox, JRFont, JRCommonText {
  private static final long serialVersionUID = 10200L;
  
  private Byte horizontalAlignment = null;
  
  private Byte verticalAlignment = null;
  
  private Byte rotation = null;
  
  private Byte lineSpacing = null;
  
  private String markup = null;
  
  private byte hyperlinkType = 0;
  
  private String linkType;
  
  private byte hyperlinkTarget = 1;
  
  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;
  
  protected String valueClassName;
  
  protected String pattern;
  
  protected String formatFactoryClass;
  
  protected String localeCode;
  
  protected String timeZoneId;
  
  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 JRTemplateText(JROrigin origin, JRDefaultStyleProvider defaultStyleProvider, JRStaticText staticText) {
    super(origin, 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.topPadding = null;
    this.leftPadding = null;
    this.bottomPadding = null;
    this.rightPadding = null;
    this.isStyledText = null;
    setStaticText(staticText);
  }
  
  protected JRTemplateText(JROrigin origin, JRDefaultStyleProvider defaultStyleProvider, JRTextField textField) {
    super(origin, 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.topPadding = null;
    this.leftPadding = null;
    this.bottomPadding = null;
    this.rightPadding = null;
    this.isStyledText = null;
    setTextField(textField);
  }
  
  protected void setStaticText(JRStaticText staticText) {
    setTextElement((JRTextElement)staticText);
  }
  
  protected void setTextField(JRTextField textField) {
    setTextElement((JRTextElement)textField);
    setLinkType(textField.getLinkType());
    this.hyperlinkTarget = textField.getHyperlinkTarget();
  }
  
  protected void setTextElement(JRTextElement textElement) {
    setElement((JRElement)textElement);
    this.lineBox = textElement.getLineBox().clone((JRBoxContainer)this);
    this.reportFont = textElement.getReportFont();
    this.fontName = textElement.getOwnFontName();
    this.isBold = textElement.isOwnBold();
    this.isItalic = textElement.isOwnItalic();
    this.isUnderline = textElement.isOwnUnderline();
    this.isStrikeThrough = textElement.isOwnStrikeThrough();
    this.fontSize = textElement.getOwnFontSize();
    this.pdfFontName = textElement.getOwnPdfFontName();
    this.pdfEncoding = textElement.getOwnPdfEncoding();
    this.isPdfEmbedded = textElement.isOwnPdfEmbedded();
    this.horizontalAlignment = textElement.getOwnHorizontalAlignment();
    this.verticalAlignment = textElement.getOwnVerticalAlignment();
    this.rotation = textElement.getOwnRotation();
    this.lineSpacing = textElement.getOwnLineSpacing();
    this.markup = textElement.getOwnMarkup();
  }
  
  protected JRFont getBaseFont() {
    if (this.reportFont != null)
      return (JRFont)this.reportFont; 
    if (this.defaultStyleProvider != null)
      return (JRFont)this.defaultStyleProvider.getDefaultFont(); 
    return null;
  }
  
  public int getWidth() {
    throw new JRRuntimeException("This method should never be called.");
  }
  
  public int getHeight() {
    throw new JRRuntimeException("This method should never be called.");
  }
  
  public byte getMode() {
    return JRStyleResolver.getMode(this, (byte)2);
  }
  
  public byte getTextAlignment() {
    return getHorizontalAlignment();
  }
  
  public byte getHorizontalAlignment() {
    return JRStyleResolver.getHorizontalAlignment(this);
  }
  
  public Byte getOwnHorizontalAlignment() {
    return this.horizontalAlignment;
  }
  
  public void setHorizontalAlignment(byte horizontalAlignment) {
    this.horizontalAlignment = new Byte(horizontalAlignment);
  }
  
  public void setHorizontalAlignment(Byte horizontalAlignment) {
    this.horizontalAlignment = horizontalAlignment;
  }
  
  public byte getVerticalAlignment() {
    return JRStyleResolver.getVerticalAlignment(this);
  }
  
  public Byte getOwnVerticalAlignment() {
    return this.verticalAlignment;
  }
  
  public void setVerticalAlignment(byte verticalAlignment) {
    this.verticalAlignment = new Byte(verticalAlignment);
  }
  
  public void setVerticalAlignment(Byte verticalAlignment) {
    this.verticalAlignment = verticalAlignment;
  }
  
  public byte getRotation() {
    return JRStyleResolver.getRotation(this);
  }
  
  public Byte getOwnRotation() {
    return this.rotation;
  }
  
  public byte getLineSpacing() {
    return JRStyleResolver.getLineSpacing(this);
  }
  
  public Byte getOwnLineSpacing() {
    return 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 String getMarkup() {
    return JRStyleResolver.getMarkup(this);
  }
  
  public String getOwnMarkup() {
    return this.markup;
  }
  
  public JRBox getBox() {
    return (JRBox)new LineBoxWrapper(getLineBox());
  }
  
  public JRLineBox getLineBox() {
    return this.lineBox;
  }
  
  public JRFont getFont() {
    return this;
  }
  
  public byte getHyperlinkType() {
    return JRHyperlinkHelper.getHyperlinkType(getLinkType());
  }
  
  public byte getHyperlinkTarget() {
    return this.hyperlinkTarget;
  }
  
  public JRReportFont getReportFont() {
    return this.reportFont;
  }
  
  public void setReportFont(JRReportFont reportFont) {
    this.reportFont = reportFont;
  }
  
  public String getFontName() {
    return JRStyleResolver.getFontName(this);
  }
  
  public String getOwnFontName() {
    return this.fontName;
  }
  
  public void setFontName(String fontName) {
    this.fontName = fontName;
  }
  
  public boolean isBold() {
    return JRStyleResolver.isBold(this);
  }
  
  public Boolean isOwnBold() {
    return this.isBold;
  }
  
  public void setBold(boolean isBold) {
    setBold(isBold ? Boolean.TRUE : Boolean.FALSE);
  }
  
  public void setBold(Boolean isBold) {
    this.isBold = isBold;
  }
  
  public boolean isItalic() {
    return JRStyleResolver.isItalic(this);
  }
  
  public Boolean isOwnItalic() {
    return this.isItalic;
  }
  
  public void setItalic(boolean isItalic) {
    setItalic(isItalic ? Boolean.TRUE : Boolean.FALSE);
  }
  
  public void setItalic(Boolean isItalic) {
    this.isItalic = isItalic;
  }
  
  public boolean isUnderline() {
    return JRStyleResolver.isUnderline(this);
  }
  
  public Boolean isOwnUnderline() {
    return this.isUnderline;
  }
  
  public void setUnderline(boolean isUnderline) {
    setUnderline(isUnderline ? Boolean.TRUE : Boolean.FALSE);
  }
  
  public void setUnderline(Boolean isUnderline) {
    this.isUnderline = isUnderline;
  }
  
  public boolean isStrikeThrough() {
    return JRStyleResolver.isStrikeThrough(this);
  }
  
  public Boolean isOwnStrikeThrough() {
    return this.isStrikeThrough;
  }
  
  public void setStrikeThrough(boolean isStrikeThrough) {
    setStrikeThrough(isStrikeThrough ? Boolean.TRUE : Boolean.FALSE);
  }
  
  public void setStrikeThrough(Boolean isStrikeThrough) {
    this.isStrikeThrough = isStrikeThrough;
  }
  
  public int getFontSize() {
    return JRStyleResolver.getFontSize(this);
  }
  
  public Integer getOwnFontSize() {
    return this.fontSize;
  }
  
  public void setFontSize(int fontSize) {
    setFontSize(new Integer(fontSize));
  }
  
  public void setFontSize(Integer fontSize) {
    this.fontSize = 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(this);
  }
  
  public String getOwnPdfFontName() {
    return this.pdfFontName;
  }
  
  public void setPdfFontName(String pdfFontName) {
    this.pdfFontName = pdfFontName;
  }
  
  public String getPdfEncoding() {
    return JRStyleResolver.getPdfEncoding(this);
  }
  
  public String getOwnPdfEncoding() {
    return this.pdfEncoding;
  }
  
  public void setPdfEncoding(String pdfEncoding) {
    this.pdfEncoding = pdfEncoding;
  }
  
  public boolean isPdfEmbedded() {
    return JRStyleResolver.isPdfEmbedded(this);
  }
  
  public Boolean isOwnPdfEmbedded() {
    return this.isPdfEmbedded;
  }
  
  public void setPdfEmbedded(boolean isPdfEmbedded) {
    setPdfEmbedded(isPdfEmbedded ? Boolean.TRUE : Boolean.FALSE);
  }
  
  public void setPdfEmbedded(Boolean isPdfEmbedded) {
    this.isPdfEmbedded = isPdfEmbedded;
  }
  
  public JRStyle getStyle() {
    return this.parentStyle;
  }
  
  public String getPattern() {
    return this.pattern;
  }
  
  public void setPattern(String pattern) {
    this.pattern = pattern;
  }
  
  public String getValueClassName() {
    return this.valueClassName;
  }
  
  public void setValueClassName(String valueClassName) {
    this.valueClassName = valueClassName;
  }
  
  public String getFormatFactoryClass() {
    return this.formatFactoryClass;
  }
  
  public void setFormatFactoryClass(String formatFactoryClass) {
    this.formatFactoryClass = formatFactoryClass;
  }
  
  public String getLocaleCode() {
    return this.localeCode;
  }
  
  public void setLocaleCode(String localeCode) {
    this.localeCode = localeCode;
  }
  
  public String getTimeZoneId() {
    return this.timeZoneId;
  }
  
  public void setTimeZoneId(String timeZoneId) {
    this.timeZoneId = timeZoneId;
  }
  
  public String getLinkType() {
    return this.linkType;
  }
  
  public void setLinkType(String linkType) {
    this.linkType = linkType;
  }
  
  protected void normalizeLinkType() {
    if (this.linkType == null)
      this.linkType = JRHyperlinkHelper.getLinkType(this.hyperlinkType); 
    this.hyperlinkType = 0;
  }
  
  public Color getDefaultLineColor() {
    return getForecolor();
  }
  
  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);
  }
  
  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;
    } 
    normalizeLinkType();
  }
}
