Package jsynoptic.plugins.jfreechart

Source Code of jsynoptic.plugins.jfreechart.SourceMeterDataset

/* ========================
* JSynoptic : a free Synoptic editor
* ========================
*
* Project Info:  http://jsynoptic.sourceforge.net/index.html
*
* This program is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation;
* either version 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* (C) Copyright 2001-2003, by :
*     Corporate:
*         Astrium SAS
*         EADS CRC
*     Individual:
*         Nicolas Brodu
*
* $Id: SourceMeterDataset.java,v 1.6 2006/02/15 18:29:06 cazenave Exp $
*
* Changes
* -------
* 25-Sep-2003 : Initial public release (NB);
*
*/

package jsynoptic.plugins.jfreechart;

import org.jfree.data.AbstractDataset;
import org.jfree.data.Range;
import org.jfree.data.ValueDataset;

import simtools.data.DataException;
import simtools.data.DataInfo;
import simtools.data.DataSource;
import simtools.data.DataSourceListener;
import simtools.data.DataSourcePool;
import simtools.data.EndNotificationListener;
import simtools.data.UnsupportedOperation;

/**
* @author nbrodu
*
* To change the template for this generated type comment go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
public class SourceMeterDataset extends AbstractDataset implements ValueDataset, DataSourceListener, EndNotificationListener {

  static final long serialVersionUID = 42714295797937638L;

  protected transient DataSource source = null;
  protected transient boolean dirty = false;
  double min, max, value;

  public SourceMeterDataset(DataSource ds) {
    source = ds;
    min = -1; max =1; value = 0;
  }

  /**
   *
   */
  public SourceMeterDataset() {
    this(null);
  }

  public DataSource getSource() {
    return source;
  }

  public void setSource(DataSource ds) {
    if (source!=null) {
        source.removeListener(this);
        source.removeEndNotificationListener(this);
    }
    source = ds;
    if (source==null) return;
    try {
      synchronized(source) {
        try  {
          source.computeMin(); source.computeMax();
          min = source.getDoubleMin();
          max = source.getDoubleMax();
          value = source.getDoubleValue(source.computeLastIndex());
        } catch (DataException e) {
          try {
            min = source.getDoubleMin();
            max = source.getDoubleMax();
            value = source.getDoubleValue(source.getLastIndex());
          } catch (DataException e1) {
            min = -1; max = 1; value = 0;
          }
        }
      }
      if (min>max) {double tmp=max; max=min; min=tmp;}
      if (min==max) {min=value-1; max=value+1;}
    } catch (ClassCastException cce) {
      cce.printStackTrace();
    } catch (IllegalArgumentException iae) {
      iae.printStackTrace();
    }
    source.addListener(this);
    source.addEndNotificationListener(this);
    this.fireDatasetChanged();
  }

//  Bridge between listeners from different worlds

  /* (non-Javadoc)
   * @see simtools.data.DataSourceListener#DataSourceValueChanged(simtools.data.DataSource, long, long)
   */
  public void DataSourceValueChanged(DataSource ds, long minIndex, long maxIndex) {
    // Notify listeners only if the change occurs for the last value, since we only take it into account
    try {
      if (maxIndex==ds.getLastIndex()) dirty = true;
    } catch (UnsupportedOperation e) {
    }
  }

  /* (non-Javadoc)
   * @see simtools.data.DataSourceListener#DataSourceIndexRangeChanged(simtools.data.DataSource, long, long)
   */
  public void DataSourceIndexRangeChanged(DataSource ds, long startIndex, long lastIndex) {
    try {
      // set value for superclass
      value = source.getDoubleValue(lastIndex);
      dirty = true;
    } catch (UnsupportedOperation e) {
      e.printStackTrace();
    } catch (DataException e) {
      e.printStackTrace();
    }
  }

  /* (non-Javadoc)
   * @see simtools.data.DataSourceListener#DataSourceInfoChanged(simtools.data.DataSource, simtools.data.DataInfo)
   */
  public void DataSourceInfoChanged(DataSource ds, DataInfo newInfo) {
    dirty = true; // update text
  }

  /* (non-Javadoc)
   * @see simtools.data.DataSourceListener#DataSourceValueRangeChanged(simtools.data.DataSource)
   */
  public void DataSourceValueRangeChanged(DataSource ds) {
    try  {
      min = source.getDoubleMin();
      max = source.getDoubleMax();
      value = source.getDoubleValue(ds.getLastIndex());
    } catch (DataException e) {
      min = value;
      max = value;
    }
    if (min==max) {
      min = value - Math.abs(value*0.01);
      max = value + Math.abs(value*0.01);
      if (min==max) min=value-1; max = value + 1; // value = 0
    }
    dirty = true;
  }

  /* (non-Javadoc)
   * @see simtools.data.DataSourceListener#DataSourceReplaced(simtools.data.DataSource, simtools.data.DataSource)
   */
  public void DataSourceReplaced(DataSource oldData, DataSource newData) {
    if (source == oldData) {
      source = newData;
      if(newData!=null){
        source.addListener(this);
        source.addEndNotificationListener(this);
      }
      oldData.removeListener(this);
      oldData.removeEndNotificationListener(this);
      dirty = true;
    }
  }

  /*
   * (non-Javadoc)
   *
   * @see simtools.data.EndNotificationListener#notificationEnd(java.lang.Object)
   */
    public void notificationEnd(Object referer) {
    if (dirty) {
        fireDatasetChanged();
        dirty = false;
    }
    }

  /* (non-Javadoc)
   * @see simtools.data.DataSourceListener#DataSourceOrderChanged(simtools.data.DataSource, int)
   */
  public void DataSourceOrderChanged(DataSource ds, int newOrder) {
    // don't care
  }

  public String getName() {
    if (source==null) return "";
    return DataInfo.getLabel(source);
  }

  public String getUnit() {
    if (source==null) return "";
    return DataInfo.getUnit(source);
  }
 
//  Take care of serialisation. Special handling for datasources

  private void writeObject(java.io.ObjectOutputStream out) throws java.io.IOException {
    out.defaultWriteObject();
    DataSourcePool.global.writeDataSource(out, source);
  }

  private void readObject(java.io.ObjectInputStream in) throws java.lang.ClassNotFoundException, java.io.IOException {
    in.defaultReadObject();
    source = DataSourcePool.global.readDataSource(in);
    if (source!=null) {
        source.addListener(this);
        source.addEndNotificationListener(this);
    }
  }

  /* (non-Javadoc)
   * @see org.jfree.data.Value#getValue()
   */
  public Number getValue() {
    if (Double.isNaN(value)) return null;
    if (Double.isInfinite(value)) return null;
    return new Double(value);
  }

  /* (non-Javadoc)
   * @see org.jfree.data.Value#getValue()
   */
  public double getDoubleValue() {
    return value;
  }

  public Range getRange() {
    return new Range(min,max);
  }

  /* (non-Javadoc)
   * @see java.lang.Object#clone()
   */
  public Object clone() throws CloneNotSupportedException {
      SourceMeterDataset c = (SourceMeterDataset)super.clone();
        if (source != null) {
            source.addListener(c);
        source.addEndNotificationListener(c);
        }
      return c;
  }

  /**
   * @return
   */
  public SourceMeterDataset cloneSet() {
      try {
          return (SourceMeterDataset)clone();
      } catch (CloneNotSupportedException e) {
          return null;
      }
  }

 
}
TOP

Related Classes of jsynoptic.plugins.jfreechart.SourceMeterDataset

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.