[Date Prev][Date Next][Index][Thread] [SGT Mail Archive] [SGT Main Page]

Re: Refreshing Graph



Hi Don,

Thanks for the quick reply :) I must admit that i'm completely perplexed! I 
don't have much experience of using actionlisteners and change events :( - i 
just can't get it to work!

I hope you don't mind me attatching the files, i've been at this for hours and 
i still can't get anywhere!

I have two files, Grapher.java and Grapher_List.java. The former is the main 
applet that constructs the frames and graphs, and the List is basically a 
vector of the graphs that i want to plot. I've incorporated a button to 
on the Grapher to basically add a graph object into Grapher_List.

When i add a new graph into the Grapher_List, you've told me that i need to 
generate a PropertyChangeEvent of "dataChanged" and "rangeChanged" 
(incidentally, it's rangeModified and dataModified in the examples - but 
i've tried both), with the Grapher, i've added a addPropertyChangeListener on 
the graphList object.

I've excluded the propertyChange( PropertyChangeEvent e ) in Grapher as the 
examples state that the JPloyLayout should 'hear' these.


HELP!!!!!!

Thanks in advance! ;)

Yee.

On Mon, 15 Apr 2002, Donald Denbo wrote:

> *************************************************
> Message from the sgt mail list.
> *************************************************
> 
> Yee,
>          If you look closely at the code in 
> gov.noaa.pmel.sgt.demo.PseudoRealTimeData (used by the JRealTimeDemo), you 
> will see that when the data is changed in the object you need to have that 
> object fire a PropertyChangeEvent of type "dataChanged", when the data has 
> changed, and "rangeChanged", when the range has changed.  JPlotLayout (and 
> SGT) register as listeners for these events.
> 
> Don
> 
-- 
*************************************************************************
 Yee-Ting Li                                    email: ytl@hep.ucl.ac.uk
 High Energy Particle Physics              http://www.hep.ucl.ac.uk/~ytl
 University College London                     Tel: +44 (0) 20 7679 1376
 Gower Street                                  Fax: +44 (0) 20 7679 7145
 London, WC1E 6BT
 United Kingdom
*************************************************************************
/*
 * $Id: JTimeSeriesDemo.java,v 1.7 2001/02/06 00:14:35 dwd Exp $
 *
 * This software is provided by NOAA for full, free and open release.  It is
 * understood by the recipient/user that NOAA assumes no liability for any
 * errors contained in the code.  Although this software is released without
 * conditions or restrictions in its use, it is expected that appropriate
 * credit be given to its author and to the National Oceanic and Atmospheric
 * Administration should the software be included by the recipient as an
 * element in other product development.
 */

package uk.ac.ucl.hep.GridNM;

import gov.noaa.pmel.sgt.swing.JPlotLayout;
import gov.noaa.pmel.sgt.swing.JClassTree;
import gov.noaa.pmel.sgt.swing.prop.LineAttributeDialog;

import gov.noaa.pmel.sgt.dm.SGTData;

import gov.noaa.pmel.sgt.*;
import gov.noaa.pmel.util.*;

import java.awt.*;
import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import java.util.Date;
import gov.noaa.pmel.util.GeoDate;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

/**
 * Example demonstrating how to use <code>JPlotLayout</code>
 * to create a time series plot.
 *
 * @author Donald Denbo
 * @version $Revision: 1.7 $, $Date: 2001/02/06 00:14:35 $
 * @since 2.0
 */
public class Grapher extends JApplet implements PropertyChangeListener
{
	JButton tree_;
	JButton add_ = null;
	JButton delete_ = null;
	JButton refresh_ = null;
	
	JPane pane_;
	MyMouse myMouse_;
	LineAttributeDialog lad_;
	
	private static boolean DEBUG = true;
	
	Grapher_List graphList;
	
	Layer layer_;
	boolean isStandalone = false;
	
	Grapher () {
		super ();
		graphList = new Grapher_List();
	}
	
	
	/**Initialize the applet*/
	public void init() {
		/*
		 * Create the data source
		 */
		graphList = new Grapher_List();
		try {
			jbInit();
		}
		catch(Exception e) {
			e.printStackTrace();
		}
		
		// add listener for data source listening
		// for rangeModified events
		graphList.addPropertyChangeListener(this);
	}
	
	
	
	/**Component initialization*/
	private void jbInit() throws Exception {
		
		this.setSize(new Dimension(800, 440));
		this.getContentPane().setLayout(new BorderLayout());
		
		// Create a JFrame to run JTimeSeriesDemo in.
		pane_ = new JPane("Time Series Demo", new Dimension(800,400));
		// and buttons
		JPanel buttonPanel = makeButtonPanel(true);
		
		pane_.setBatch(true);
		pane_ = makeGraph();
		pane_.setLayout(new StackedLayout());
		pane_.setBackground(Color.white);
		
		this.getContentPane().add(pane_, BorderLayout.CENTER);
		this.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
		
		if(!isStandalone) pane_.setBatch(false);
	}
	
	
	JPanel makeButtonPanel( boolean editable ) {
		
		JPanel button = new JPanel();
		button.setLayout(new FlowLayout());
		tree_ = new JButton("Tree View");
		
		MyAction myAction = new MyAction();
		tree_.addActionListener(myAction);
		button.add(tree_);
		
		if( editable ) {
			refresh_ = new JButton("Refresh");
			refresh_.addActionListener(myAction);
			button.add(refresh_ );
			
			add_ = new JButton("Add Graph");
			add_.addActionListener(myAction);
			button.add(add_);
			
			delete_ = new JButton("Delete Graph");
			delete_.addActionListener(myAction);
			button.add(delete_);
		}
		return button;
	}
	
		
	public static void main(String[] args) {
		
		Grapher applet = new Grapher();
		applet.isStandalone = true;
		
		// Create a JFrame to run JTimeSeriesDemo in.
		JFrame frame = new JFrame("Time Series Demo");
		frame.setDefaultCloseOperation(3);
		frame.getContentPane().add(applet, BorderLayout.CENTER);
		
		applet.init();
		applet.start();
		
		frame.setSize(800,440);
		
		Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
		frame.setLocation((d.width - frame.getSize().width) / 2,
							  (d.height - frame.getSize().height) / 2);
		frame.setVisible(true);
		applet.pane_.setBatch(false);
		
		
		
		if ( args.length < 5 ) {
			System.err.println("Usage: grapher [src] [sink] [tool] [param] [days]");
			System.exit(1);
		}
		
		String src = args[0] ;	//"pc35.hep.ucl.ac.uk";
		String sink = args[1] ; // "bsesrv1.phy.bris.ac.uk"; //dev04.hepgrid.clrc.ac.uk
		String tool = args[2] ; 	//"ping";
		String param = args[3] ; 	//"min_rtt";
		int days = Integer.parseInt(args[4]);	//ndays
		
		String filterParam = null;
		int filterType = 0;
		String filterValue = null;
		
		// filter stuff
		if ( args.length > 5 ) {
			filterParam = args[5];
			filterType = Integer.parseInt(args[6]);
			filterValue = args[7];
		}
		
		// work out number of days
		Date end = new Date();
		long diff = (long)days * 24l * 3600000l;
		
		Date start = new Date ( end.getTime() - diff );
		if ( DEBUG ) System.out.println( "s: " + start + " \te: " + end +" \td: " + diff);
		
		// add the graph from the args
		applet.addGraph( src, sink, tool, param, start, end, tool, filterParam, filterType, filterValue  );
	}
	
	
	/*
	 * This example uses a pre-created "Layout" for time
	 * series to simplify the construction of a plot. The
	 * LineTimeSeriesLayout can plot multiple lines, with
	 * a legend and provides zooming and line hi-lighting
	 * capabilities.
	 */
	JPlotLayout makeGraph( ) {
		
		// time series
		JPlotLayout jpl = new JPlotLayout( 1, true, false, "Blah", null, false );
		
		if ( DEBUG )
			System.out.println("MakeGraph()...");
		
		for ( int i=0; i< graphList.getNoOfDataSeries() ; i++ ) {
			// Add the time series to the layout and give a label for the legend.
			Grapher_DataSeries gd = graphList.getElement(i);
			jpl.addData( gd.getSGTData() , gd.getKey() );
		}
		
		// Change the layout's three title lines and place the Pane on the Applet.
		jpl.setTitles("",
					  "",
					  "");
		jpl.setClipping(true);
		jpl.init();
		
		
		
		myMouse_ = new MyMouse();
		jpl.addMouseListener(myMouse_);
		
		return jpl;
	}
	
	
	/**
	 * Creates a graph object to put into the vector of graphs ready for plotting
	 */
	public void addGraph( String src, String sink, String tool , String attr, Date start, Date end, String filterTool, String filterParam, int filterType, String filterValue ) {
		
		graphList.add( new Grapher_DataSeries( src, sink, tool, attr, start, end, filterTool, filterParam, filterType, filterValue ) );
		
		if ( DEBUG ){
			System.out.println( "addGraph: " + src + " " + sink + " "  + attr + " " + start + " " + end + " " + filterParam + " " + filterType + " " + filterValue );
			System.out.println( " Num in graphlist: " + graphList.getNoOfDataSeries() );
		}
		
		pane_ = makeGraph();
	}
	
	public void addGraph( String src, String sink, String tool , String attr, Date start, Date end ) {
		addGraph( src, sink, tool, attr, start, end , null, null, 0, null);
	}
	
	void tree_actionPerformed(java.awt.event.ActionEvent e) {
		 // Create JClassTree showing object tree.
		JClassTree ct = new JClassTree();
		ct.setModal(false);
		ct.setJPane(pane_);
		ct.show();
	}
	
	public void propertyChange(PropertyChangeEvent evt) {
		/**
		 * dataModified property is handled by CartesianGraph
		 * only need to look for rangeModified here to make sure
		 * range is properly updated
		 */
		if("rangeModified".equals(evt.getPropertyName())) {
			System.out.println("rangeModified:");
		}
		else if("dataModified".equals(evt.getPropertyName())) {
				System.out.println("dataModified:");
		}
		
	}
	
       	
	class MyAction implements java.awt.event.ActionListener {
		
		public void actionPerformed(java.awt.event.ActionEvent event) {
			
			Object obj = event.getSource();
			
			if(obj == add_) {
				
				System.out.println("  <<Add>>");
				
				Date start = new Date( 102, 3, 1 );
				Date end = new Date( 102, 3, 5);
				
				// would be a panel here
				addGraph( "bsesrv1.phy.bris.ac.uk", "rtlin1.dl.ac.uk", "iperf", "link_utilisation", start, end , null, null, 0, null );
				pane_ = makeGraph();
				
			}
			else if ( obj == delete_) {
				System.out.println("  <<Delete>>");
			}
			else if (obj == tree_)
				tree_actionPerformed(event);
			else if ( obj == refresh_ ) {
				System.out.println("Refresh!");
				repaint();
				
			}
			
			
		}
	}
	
	
	class MyMouse extends MouseAdapter {
		/*
		 * process mouse events.
		 */
		public void mouseReleased(MouseEvent event) {
			Object object = event.getSource();
			if(object == pane_)
				maybeShowLineAttributeDialog(event);
		}
		
		void maybeShowLineAttributeDialog(MouseEvent e) {
			
			if(e.isPopupTrigger() || e.getClickCount() == 2) {
				
				Object obj = pane_.getObjectAt(e.getX(), e.getY());
				pane_.setSelectedObject(obj);
				
				if(obj instanceof LineCartesianRenderer) {
					
					LineAttribute attr = ((LineCartesianRenderer)obj).getLineAttribute();
					
					if(lad_ == null) {
						lad_ = new LineAttributeDialog();
					}
					
					lad_.setLineAttribute(attr);
					
					if(!lad_.isShowing())
						lad_.setVisible(true);
				}
			}
		}
	}
	
	
	
	
	
	
}
/**
 * Grapher_List.java
 *
 * Implements a simple JFC frame to display a list of DataSeries objects
 * that can be queried and spat out.
 *
 * @author Yee-Ting Li
 */

package uk.ac.ucl.hep.GridNM;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

import gov.noaa.pmel.util.SoTRange;
import gov.noaa.pmel.util.GeoDate;

import java.util.*;

public class Grapher_List implements ActionListener
{
	
	// Vector of DataSeries objects
	Vector dataSeries;
	Vector oldDataSeries ;
	
	private PropertyChangeSupport changes_ = new PropertyChangeSupport(this);
	
	private SoTRange.GeoDate dateRange_;
	private SoTRange.Double yRange_;
	
	
	// Initializes the frame.
	public Grapher_List(){
		dataSeries = new Vector();
		oldDataSeries = new Vector();
	}
	
	public void add( Grapher_DataSeries ds ) {
		oldDataSeries = dataSeries;
		dataSeries.add( ds );
	}
	
	public Grapher_DataSeries getElement( int i ) {
		// sanity check on int i
		if ( i > dataSeries.size() )
			return null;
		
		return (Grapher_DataSeries)dataSeries.elementAt(i);
	}
	
	public int getNoOfDataSeries() {
		return dataSeries.size();
	}
	
	public Grapher_DataSeries[] getArray() {
		return (Grapher_DataSeries[])dataSeries.toArray( new Grapher_DataSeries[ getNoOfDataSeries() ] );
	}
	
	public void addPropertyChangeListener(PropertyChangeListener l) {
		changes_.addPropertyChangeListener(l);
	}
	
	public void removePropertyChangeListener(PropertyChangeListener l) {
		changes_.removePropertyChangeListener(l);
	}
	
	/**
	 * Handle timer ActionEvents
	 * <BR><B>Property Change:</B> <code>rangeModified</code> and
	 * <code>DataModified</code>
	 */
	public void actionPerformed(ActionEvent e) {
		
		SoTRange.GeoDate oldRange = (SoTRange.GeoDate)dateRange_.copy();
		setDateRange();
		
		changes_.firePropertyChange("rangeModified", oldRange, dateRange_);
		changes_.firePropertyChange("dataModified", oldDataSeries , dataSeries );
		
	}
	
	private void setDateRange() {
		
		GeoDate start = (GeoDate)dateRange_.getStartObject();
		GeoDate end = (GeoDate)dateRange_.getEndObject();
		
		// loop through each data set to find the earliest occurance
		for ( int i=0; i<getNoOfDataSeries() ; i++ ) {
			Grapher_DataSeries range = getElement( i );
			GeoDate compareStartDate = range.getDateRangeStart();
			GeoDate compareEndDate = range.getDateRangeEnd();
			
			if ( compareStartDate.before( start ) )
				start = compareStartDate;
			if ( compareEndDate.after( end ) )
				end = compareEndDate;
		}
		
		dateRange_.start = start;
		dateRange_.end = end;
		
	}
	
	public SoTRange getDateXRange() {
		return dateRange_.copy();
	}
	
	public SoTRange getYRange() {
		return yRange_.copy();
	}
	
	
}