package net.sf.jasperreports.engine.design;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import net.sf.jasperreports.engine.JRDefaultStyleProvider;
import net.sf.jasperreports.engine.JRExpression;
import net.sf.jasperreports.engine.JRExpressionCollector;
import net.sf.jasperreports.engine.JRGroup;
import net.sf.jasperreports.engine.JRHyperlink;
import net.sf.jasperreports.engine.JRHyperlinkHelper;
import net.sf.jasperreports.engine.JRHyperlinkParameter;
import net.sf.jasperreports.engine.JRTextField;
import net.sf.jasperreports.engine.JRVisitor;
import net.sf.jasperreports.engine.util.JRStyleResolver;

public class JRDesignTextField extends JRDesignTextElement implements JRTextField {
  private static final long serialVersionUID = 10200L;
  
  public static final String PROPERTY_ANCHOR_NAME_EXPRESSION = "anchorNameExpression";
  
  public static final String PROPERTY_BOOKMARK_LEVEL = "bookmarkLevel";
  
  public static final String PROPERTY_EVALUATION_GROUP = "evaluationGroup";
  
  public static final String PROPERTY_EVALUATION_TIME = "evaluationTime";
  
  public static final String PROPERTY_EXPRESSION = "expression";
  
  protected boolean isStretchWithOverflow = false;
  
  protected byte evaluationTime = 1;
  
  protected String pattern = null;
  
  protected Boolean isBlankWhenNull = null;
  
  protected byte hyperlinkType = 0;
  
  protected String linkType;
  
  protected byte hyperlinkTarget = 1;
  
  private List hyperlinkParameters;
  
  protected JRGroup evaluationGroup = null;
  
  protected JRExpression expression = null;
  
  protected JRExpression anchorNameExpression = null;
  
  protected JRExpression hyperlinkReferenceExpression = null;
  
  protected JRExpression hyperlinkAnchorExpression = null;
  
  protected JRExpression hyperlinkPageExpression = null;
  
  private JRExpression hyperlinkTooltipExpression;
  
  protected int bookmarkLevel = 0;
  
  public JRDesignTextField() {
    super((JRDefaultStyleProvider)null);
    this.hyperlinkParameters = new ArrayList();
  }
  
  public JRDesignTextField(JRDefaultStyleProvider defaultStyleProvider) {
    super(defaultStyleProvider);
    this.hyperlinkParameters = new ArrayList();
  }
  
  public boolean isStretchWithOverflow() {
    return this.isStretchWithOverflow;
  }
  
  public byte getEvaluationTime() {
    return this.evaluationTime;
  }
  
  public String getPattern() {
    return JRStyleResolver.getPattern(this);
  }
  
  public String getOwnPattern() {
    return this.pattern;
  }
  
  public boolean isBlankWhenNull() {
    return JRStyleResolver.isBlankWhenNull(this);
  }
  
  public Boolean isOwnBlankWhenNull() {
    return this.isBlankWhenNull;
  }
  
  public byte getHyperlinkType() {
    return JRHyperlinkHelper.getHyperlinkType((JRHyperlink)this);
  }
  
  public byte getHyperlinkTarget() {
    return this.hyperlinkTarget;
  }
  
  public JRGroup getEvaluationGroup() {
    return this.evaluationGroup;
  }
  
  public JRExpression getExpression() {
    return this.expression;
  }
  
  public JRExpression getAnchorNameExpression() {
    return this.anchorNameExpression;
  }
  
  public JRExpression getHyperlinkReferenceExpression() {
    return this.hyperlinkReferenceExpression;
  }
  
  public JRExpression getHyperlinkAnchorExpression() {
    return this.hyperlinkAnchorExpression;
  }
  
  public JRExpression getHyperlinkPageExpression() {
    return this.hyperlinkPageExpression;
  }
  
  public void setStretchWithOverflow(boolean isStretch) {
    boolean old = this.isStretchWithOverflow;
    this.isStretchWithOverflow = isStretch;
    getEventSupport().firePropertyChange("stretchWithOverflow", old, this.isStretchWithOverflow);
  }
  
  public void setEvaluationTime(byte evaluationTime) {
    byte old = this.evaluationTime;
    this.evaluationTime = evaluationTime;
    getEventSupport().firePropertyChange("evaluationTime", old, this.evaluationTime);
  }
  
  public void setPattern(String pattern) {
    Object old = this.pattern;
    this.pattern = pattern;
    getEventSupport().firePropertyChange("pattern", old, this.pattern);
  }
  
  public void setBlankWhenNull(boolean isBlank) {
    setBlankWhenNull(isBlank ? Boolean.TRUE : Boolean.FALSE);
  }
  
  public void setBlankWhenNull(Boolean isBlank) {
    Object old = this.isBlankWhenNull;
    this.isBlankWhenNull = isBlank;
    getEventSupport().firePropertyChange("blankWhenNull", old, this.isBlankWhenNull);
  }
  
  public void setHyperlinkType(byte hyperlinkType) {
    setLinkType(JRHyperlinkHelper.getLinkType(hyperlinkType));
  }
  
  public void setHyperlinkTarget(byte hyperlinkTarget) {
    byte old = this.hyperlinkTarget;
    this.hyperlinkTarget = hyperlinkTarget;
    getEventSupport().firePropertyChange("hyperlinkTarget", old, this.hyperlinkTarget);
  }
  
  public void setEvaluationGroup(JRGroup evaluationGroup) {
    Object old = this.evaluationGroup;
    this.evaluationGroup = evaluationGroup;
    getEventSupport().firePropertyChange("evaluationGroup", old, this.evaluationGroup);
  }
  
  public void setExpression(JRExpression expression) {
    Object old = this.expression;
    this.expression = expression;
    getEventSupport().firePropertyChange("expression", old, this.expression);
  }
  
  public void setAnchorNameExpression(JRExpression anchorNameExpression) {
    Object old = this.anchorNameExpression;
    this.anchorNameExpression = anchorNameExpression;
    getEventSupport().firePropertyChange("anchorNameExpression", old, this.anchorNameExpression);
  }
  
  public void setHyperlinkReferenceExpression(JRExpression hyperlinkReferenceExpression) {
    Object old = this.hyperlinkReferenceExpression;
    this.hyperlinkReferenceExpression = hyperlinkReferenceExpression;
    getEventSupport().firePropertyChange("hyperlinkReferenceExpression", old, this.hyperlinkReferenceExpression);
  }
  
  public void setHyperlinkAnchorExpression(JRExpression hyperlinkAnchorExpression) {
    Object old = this.hyperlinkAnchorExpression;
    this.hyperlinkAnchorExpression = hyperlinkAnchorExpression;
    getEventSupport().firePropertyChange("hyperlinkAnchorExpression", old, this.hyperlinkAnchorExpression);
  }
  
  public void setHyperlinkPageExpression(JRExpression hyperlinkPageExpression) {
    Object old = this.hyperlinkPageExpression;
    this.hyperlinkPageExpression = hyperlinkPageExpression;
    getEventSupport().firePropertyChange("hyperlinkPageExpression", old, this.hyperlinkPageExpression);
  }
  
  public void collectExpressions(JRExpressionCollector collector) {
    collector.collect(this);
  }
  
  public void visit(JRVisitor visitor) {
    visitor.visitTextField(this);
  }
  
  public int getBookmarkLevel() {
    return this.bookmarkLevel;
  }
  
  public void setBookmarkLevel(int bookmarkLevel) {
    int old = this.bookmarkLevel;
    this.bookmarkLevel = bookmarkLevel;
    getEventSupport().firePropertyChange("bookmarkLevel", old, this.bookmarkLevel);
  }
  
  public String getLinkType() {
    return this.linkType;
  }
  
  public void setLinkType(String type) {
    Object old = this.linkType;
    this.linkType = type;
    getEventSupport().firePropertyChange("linkType", old, this.linkType);
  }
  
  public JRHyperlinkParameter[] getHyperlinkParameters() {
    JRHyperlinkParameter[] parameters;
    if (this.hyperlinkParameters.isEmpty()) {
      parameters = null;
    } else {
      parameters = new JRHyperlinkParameter[this.hyperlinkParameters.size()];
      this.hyperlinkParameters.toArray((Object[])parameters);
    } 
    return parameters;
  }
  
  public List getHyperlinkParametersList() {
    return this.hyperlinkParameters;
  }
  
  public void addHyperlinkParameter(JRHyperlinkParameter parameter) {
    this.hyperlinkParameters.add(parameter);
    getEventSupport().fireCollectionElementAddedEvent("hyperlinkParameters", parameter, this.hyperlinkParameters.size() - 1);
  }
  
  public void removeHyperlinkParameter(JRHyperlinkParameter parameter) {
    int idx = this.hyperlinkParameters.indexOf(parameter);
    if (idx >= 0) {
      this.hyperlinkParameters.remove(idx);
      getEventSupport().fireCollectionElementRemovedEvent("hyperlinkParameters", parameter, idx);
    } 
  }
  
  public void removeHyperlinkParameter(String parameterName) {
    for (ListIterator it = this.hyperlinkParameters.listIterator(); it.hasNext(); ) {
      JRHyperlinkParameter parameter = it.next();
      if (parameter.getName() != null && parameter.getName().equals(parameterName)) {
        it.remove();
        getEventSupport().fireCollectionElementRemovedEvent("hyperlinkParameters", parameter, it.nextIndex());
      } 
    } 
  }
  
  private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
    in.defaultReadObject();
    normalizeLinkType();
  }
  
  protected void normalizeLinkType() {
    if (this.linkType == null)
      this.linkType = JRHyperlinkHelper.getLinkType(this.hyperlinkType); 
    this.hyperlinkType = 0;
  }
  
  public JRExpression getHyperlinkTooltipExpression() {
    return this.hyperlinkTooltipExpression;
  }
  
  public void setHyperlinkTooltipExpression(JRExpression hyperlinkTooltipExpression) {
    Object old = this.hyperlinkTooltipExpression;
    this.hyperlinkTooltipExpression = hyperlinkTooltipExpression;
    getEventSupport().firePropertyChange("hyperlinkTooltipExpression", old, this.hyperlinkTooltipExpression);
  }
  
  public Object clone() {
    JRDesignTextField clone = (JRDesignTextField)super.clone();
    if (this.hyperlinkParameters != null) {
      clone.hyperlinkParameters = new ArrayList(this.hyperlinkParameters.size());
      for (int i = 0; i < this.hyperlinkParameters.size(); i++)
        clone.hyperlinkParameters.add(((JRHyperlinkParameter)this.hyperlinkParameters.get(i)).clone()); 
    } 
    if (this.expression != null)
      clone.expression = (JRExpression)this.expression.clone(); 
    if (this.anchorNameExpression != null)
      clone.anchorNameExpression = (JRExpression)this.anchorNameExpression.clone(); 
    if (this.hyperlinkReferenceExpression != null)
      clone.hyperlinkReferenceExpression = (JRExpression)this.hyperlinkReferenceExpression.clone(); 
    if (this.hyperlinkAnchorExpression != null)
      clone.hyperlinkAnchorExpression = (JRExpression)this.hyperlinkAnchorExpression.clone(); 
    if (this.hyperlinkPageExpression != null)
      clone.hyperlinkPageExpression = (JRExpression)this.hyperlinkPageExpression.clone(); 
    if (this.hyperlinkTooltipExpression != null)
      clone.hyperlinkTooltipExpression = (JRExpression)this.hyperlinkTooltipExpression.clone(); 
    return clone;
  }
}
