Back to index

wims  3.65+svn20090927
PlotLive.java
Go to the documentation of this file.
00001 /* A live signal plotter.
00002 
00003  @Copyright (c) 1997-2005 The Regents of the University of California.
00004  All rights reserved.
00005 
00006  Permission is hereby granted, without written agreement and without
00007  license or royalty fees, to use, copy, modify, and distribute this
00008  software and its documentation for any purpose, provided that the
00009  above copyright notice and the following two paragraphs appear in all
00010  copies of this software.
00011 
00012  IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
00013  FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
00014  ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
00015  THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
00016  SUCH DAMAGE.
00017 
00018  THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
00019  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00020  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
00021  PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
00022  CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
00023  ENHANCEMENTS, OR MODIFICATIONS.
00024 
00025  PT_COPYRIGHT_VERSION_2
00026  COPYRIGHTENDKEY
00027  */
00028 package ptolemy.plot;
00029 
00030 import java.awt.event.ActionEvent;
00031 import java.awt.event.ActionListener;
00032 
00033 import javax.swing.JButton;
00034 
00037 
00065 public abstract class PlotLive extends Plot implements Runnable {
00068 
00080     public abstract void addPoints();
00081 
00086     public void makeButtons() {
00087         if (_startButton == null) {
00088             _startButton = new JButton("start");
00089             _startButton.addActionListener(new StartButtonListener());
00090             add(_startButton);
00091         }
00092 
00093         _startButton.setVisible(true);
00094 
00095         if (_stopButton == null) {
00096             _stopButton = new JButton("stop");
00097             _stopButton.addActionListener(new StopButtonListener());
00098             add(_stopButton);
00099         }
00100 
00101         _stopButton.setVisible(true);
00102         _stopButton.setEnabled(false);
00103         _startButton.setEnabled(true);
00104     }
00105 
00108     public void pause() {
00109         _paused = true;
00110         _plotting = false;
00111         _stopButton.setEnabled(false);
00112         _startButton.setEnabled(true);
00113     }
00114 
00127     public void run() {
00128         while (_plotting || _paused) {
00129             if (_plotting) {
00130                 addPoints();
00131 
00132                 // Give the event thread a chance.
00133                 Thread.yield();
00134             } else if (_paused) {
00135                 // NOTE: Cannot synchronize this entire method because then
00136                 // the Thread.yield() call above does not yield to any
00137                 // synchronized methods (like _drawPlot()).
00138                 synchronized (this) {
00139                     try {
00140                         wait();
00141                     } catch (InterruptedException e) {
00142                     }
00143                 }
00144             }
00145         }
00146     }
00147 
00156     public void setButtons(boolean visible) {
00157         super.setButtons(visible);
00158 
00159         if (_startButton == null) {
00160             _startButton = new JButton("start");
00161             _startButton.addActionListener(new StartButtonListener());
00162             add(_startButton);
00163         }
00164 
00165         _startButton.setVisible(visible);
00166 
00167         if (_stopButton == null) {
00168             _stopButton = new JButton("stop");
00169             _stopButton.addActionListener(new StopButtonListener());
00170             add(_stopButton);
00171         }
00172 
00173         _stopButton.setVisible(visible);
00174 
00175         if (visible) {
00176             _stopButton.setEnabled(false);
00177             _startButton.setEnabled(true);
00178         }
00179     }
00180 
00183     public synchronized void start() {
00184         _plotting = true;
00185         _paused = false;
00186         _stopButton.setEnabled(true);
00187         _startButton.setEnabled(false);
00188 
00189         if (_plotLiveThread == null) {
00190             _plotLiveThread = new Thread(this, "PlotLive Thread");
00191             _plotLiveThread.start();
00192         } else {
00193             synchronized (this) {
00194                 notifyAll();
00195             }
00196         }
00197     }
00198 
00202     public void stop() {
00203         _plotting = false;
00204         _paused = false;
00205         _plotLiveThread = null;
00206     }
00207 
00210 
00212     private Thread _plotLiveThread = null;
00213 
00215     private boolean _plotting = false;
00216 
00218     private boolean _paused = false;
00219 
00221     private JButton _startButton;
00222 
00224     private JButton _stopButton;
00225 
00228     class StartButtonListener implements ActionListener {
00229         public void actionPerformed(ActionEvent event) {
00230             start();
00231         }
00232     }
00233 
00234     // Despite the name, the stop button calls pause.
00235     class StopButtonListener implements ActionListener {
00236         public void actionPerformed(ActionEvent event) {
00237             pause();
00238         }
00239     }
00240 }