package net.sf.jasperreports.engine.base;

import java.io.Serializable;
import net.sf.jasperreports.engine.JRExpression;
import net.sf.jasperreports.engine.JRPropertyExpression;
import net.sf.jasperreports.engine.design.events.JRChangeEventsSupport;
import net.sf.jasperreports.engine.design.events.JRPropertyChangeSupport;

public class JRBasePropertyExpression implements JRPropertyExpression, Serializable, JRChangeEventsSupport {
  private static final long serialVersionUID = 10200L;
  
  public static final String PROPERTY_NAME = "name";
  
  public static final String pROPERTY_VALUE_EXPRESSION = "valueExpression";
  
  private String name;
  
  private JRExpression valueExpression;
  
  private transient JRPropertyChangeSupport eventSupport;
  
  protected JRBasePropertyExpression() {}
  
  public JRBasePropertyExpression(JRPropertyExpression propertyExpression, JRBaseObjectFactory factory) {
    this.name = propertyExpression.getName();
    this.valueExpression = factory.getExpression(propertyExpression.getValueExpression());
  }
  
  public String getName() {
    return this.name;
  }
  
  public void setName(String name) {
    Object old = this.name;
    this.name = name;
    getEventSupport().firePropertyChange("name", old, this.name);
  }
  
  public JRExpression getValueExpression() {
    return this.valueExpression;
  }
  
  protected void setValueExpression(JRExpression valueExpression) {
    Object old = this.valueExpression;
    this.valueExpression = valueExpression;
    getEventSupport().firePropertyChange("valueExpression", old, this.valueExpression);
  }
  
  public JRPropertyChangeSupport getEventSupport() {
    synchronized (this) {
      if (this.eventSupport == null)
        this.eventSupport = new JRPropertyChangeSupport(this); 
    } 
    return this.eventSupport;
  }
}
