package net.sf.jasperreports.engine.base;

import java.io.Serializable;
import net.sf.jasperreports.engine.JRRuntimeException;
import net.sf.jasperreports.engine.JRSortField;
import net.sf.jasperreports.engine.design.events.JRChangeEventsSupport;
import net.sf.jasperreports.engine.design.events.JRPropertyChangeSupport;

public class JRBaseSortField implements JRSortField, Serializable, JRChangeEventsSupport {
  private static final long serialVersionUID = 10200L;
  
  public static final String PROPERTY_ORDER = "order";
  
  protected String name = null;
  
  protected byte order = 0;
  
  private transient JRPropertyChangeSupport eventSupport;
  
  protected JRBaseSortField() {}
  
  protected JRBaseSortField(JRSortField sortField, JRBaseObjectFactory factory) {
    factory.put(sortField, this);
    this.name = sortField.getName();
    this.order = sortField.getOrder();
  }
  
  public String getName() {
    return this.name;
  }
  
  public byte getOrder() {
    return this.order;
  }
  
  public void setOrder(byte order) {
    byte old = this.order;
    this.order = order;
    getEventSupport().firePropertyChange("order", old, this.order);
  }
  
  public Object clone() {
    try {
      return super.clone();
    } catch (CloneNotSupportedException e) {
      throw new JRRuntimeException(e);
    } 
  }
  
  public JRPropertyChangeSupport getEventSupport() {
    synchronized (this) {
      if (this.eventSupport == null)
        this.eventSupport = new JRPropertyChangeSupport(this); 
    } 
    return this.eventSupport;
  }
}
