package hep.aida.ref.plotter.adapter;

import jas.hist.*;
import hep.aida.ref.*;
import hep.aida.ref.event.*;
import hep.aida.*;
import java.util.Observer;
import java.util.Observable;
/**
 * Creates a datasource from an IHistogram
 * @author  manj
 * @version $Id: AIDAHistogramAdapter.java,v 1.5 2005/02/09 02:35:32 serbo Exp $
 */
abstract class AIDAHistogramAdapter extends Observable implements DataSource, AIDAListener
{
    protected int axisType = DOUBLE;

    public void setAxisType( int type ) {
        this.axisType = type;
    }
    
    public int getAxisType() {
        return axisType;
    }
    
    /**
     * Create a DataSource from a Histogram
     */
    public static DataSource create(IHistogram h) {
        AIDAHistogramAdapter result;
        
        if      (h instanceof IHistogram1D) result = new AIDAHistogramAdapter1D((IHistogram1D)h);
        else if (h instanceof IHistogram2D) result = new AIDAHistogramAdapter2D((IHistogram2D)h);
        else throw new IllegalArgumentException("Argument is an unsupported subtype of IHistogram");
        return result;
    }
    protected AIDAHistogramAdapter(IHistogram h) {
        try {
            String xType = h.annotation().value("xAxisType");
            if (xType != null && xType.equalsIgnoreCase("date")) axisType = DataSource.DATE;
        } catch (IllegalArgumentException e) {}
        
        if (h instanceof IsObservable) {
            histo = (IsObservable) h;
            histo.addListener(this);
        }
    }
  protected void setValid()
   {
      if (histo != null) histo.setValid(this);
   }
   public void stateChanged(java.util.EventObject event)
   {
      setChanged();
      notifyObservers(hu);
   }
   private IsObservable histo;
   private final static jas.hist.HistogramUpdate hu = new jas.hist.HistogramUpdate(HistogramUpdate.TITLE_UPDATE+HistogramUpdate.DATA_UPDATE+HistogramUpdate.RANGE_UPDATE,false);
   static
   {
      hu.setAxis(hu.HORIZONTAL_AXIS);
      hu.setAxis(hu.VERTICAL_AXIS);
   }  
}
