/************************************************************************** /* This class is part of the Java Plotlib Toolkit. /* /* Copyright (c) 1999-2004 by Bernhard Bablok (bablokb@gmx.de) /* /* This program is free software; you can redistribute it and/or modify /* it under the terms of the GNU General Public License as published /* by the Free Software Foundation; either version 2 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 Library General Public License for more details. /* /* You should have received a copy of the GNU General Public License /* along with this program; see the file COPYING. If not, write to /* the Free Software Foundation Inc., 59 Temple Place - Suite 330, /* Boston, MA 02111-1307 USA /**************************************************************************/ package de.bablokb.plotlib.data; import java.util.*; import javax.swing.event.*; import de.bablokb.plotlib.plot.*; /** This class keeps track of a number of Datasets. If a plot contains more than one Dataset, interested beans only have to register with an object of this class. This class also provides methods to query the minimum and maximum of all points combined. @see de.bablokb.plotlib.data.Dataset @see de.bablokb.plotlib.data.DatasetChangedEvent @version $Revision: 1.4 $ @author $Author: bablokb $ */ public class DatasetTracker implements DatasetListener { /** List of DatasetListeners to notify. */ private EventListenerList iListener = new EventListenerList(); /** List of Datasets to keep track of. */ private ArrayList iDatasets = new ArrayList(); ///////////////////////////////////////////////////////////////////////////// /** Constructor. */ public DatasetTracker() { } ///////////////////////////////////////////////////////////////////////////// /** Track a Dataset. */ public void add(Dataset s) { synchronized (iDatasets) { iDatasets.add(s); } s.addDatasetListener(this); } ///////////////////////////////////////////////////////////////////////////// /** Remove a dataset from tracking. */ public void remove(Dataset s) { synchronized (iDatasets) { iDatasets.remove(s); } s.removeDatasetListener(this); } ///////////////////////////////////////////////////////////////////////////// /** Callback to execute if one of the Datasets to track changes. This method just notifies all registered listeners. */ public void datasetChanged(DatasetChangedEvent e) { Object[] listeners = iListener.getListenerList(); for (int i = listeners.length-2; i>=0; i-=2) { ((DatasetListener)listeners[i+1]).datasetChanged(e); } } ///////////////////////////////////////////////////////////////////////////// /** Add a DatasetListener to the list of listeners. */ public void addDatasetListener(DatasetListener l) { iListener.add(DatasetListener.class, l); } ///////////////////////////////////////////////////////////////////////////// /** Remove a DatasetListener from the list of listeners. */ public void removeDatasetListener(DatasetListener l) { iListener.remove(DatasetListener.class, l); } ///////////////////////////////////////////////////////////////////////////// /** Minimum of x-coordinates. */ public double xmin() { if (iDatasets == null) throw new MissingDatasetException(); double min = Double.POSITIVE_INFINITY; Iterator it = iDatasets.iterator(); while (it.hasNext()) { Dataset s = (Dataset) it.next(); if (s != null) { double smin = s.xmin(); if (smin < min) min = smin; } } if (min == Double.POSITIVE_INFINITY) throw new NoDataException(); return min; } ///////////////////////////////////////////////////////////////////////////// /** Maximum of x-coordinates. */ public double xmax() { if (iDatasets == null) throw new MissingDatasetException(); double max = Double.NEGATIVE_INFINITY; Iterator it = iDatasets.iterator(); while (it.hasNext()) { Dataset s = (Dataset) it.next(); if (s != null) { double smax = s.xmax(); if (smax > max) max = smax; } } if (max == Double.NEGATIVE_INFINITY) throw new NoDataException(); return max; } ///////////////////////////////////////////////////////////////////////////// /** Minimum of y-coordinates. */ public double ymin() { if (iDatasets == null) throw new MissingDatasetException(); double min = Double.POSITIVE_INFINITY; Iterator it = iDatasets.iterator(); while (it.hasNext()) { Dataset s = (Dataset) it.next(); if (s != null) { double smin = s.ymin(); if (smin < min) min = smin; } } if (min == Double.POSITIVE_INFINITY) throw new NoDataException(); return min; } ///////////////////////////////////////////////////////////////////////////// /** Maximum of y-coordinates. */ public double ymax() { if (iDatasets == null) throw new MissingDatasetException(); double max = Double.NEGATIVE_INFINITY; Iterator it = iDatasets.iterator(); while (it.hasNext()) { Dataset s = (Dataset) it.next(); if (s != null) { double smax = s.ymax(); if (smax > max) max = smax; } } if (max == Double.NEGATIVE_INFINITY) throw new NoDataException(); return max; } }