package Views; /* * SWView.java * This file contains the abstract view of a Stopwatch2 * Created on February 9, 2002 * * @author Stuart Hansen * @version */ import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.beans.*; // The SWView class is the base class for all views of the stopwatch public abstract class SWView extends JFrame implements PropertyChangeListener { public ViewState vstate; // the view state is frozen or updating public JButton lap; // the lap button // The constructor public SWView (String displayName) { super(displayName); Container c = getContentPane(); c.setLayout(null); lap = new JButton("Lap"); lap.addActionListener(new lapController()); lap.setBounds(25, 25, 60, 30); c.add(lap); vstate = new Updating(); } // Views vary by how they display the time // Hence, this method is abstract. abstract protected void setDisplayTime (long timeArg); // lapController sits between the button and the view public class lapController extends AbstractAction { public void actionPerformed (ActionEvent e) { vstate.lapPushed(); } } // View State is the state of the display // The display is either updating or frozen interface ViewState { public void lapPushed(); public void updateDisplayTime(long time); } // When updating, the display changes time with each // change to the stopwatch's time public class Updating implements ViewState { // The method that responds to the lap button public void lapPushed() { vstate = new Frozen(); } // The method that updates the display public void updateDisplayTime(long time) { setDisplayTime(time); } } // When frozen an earlier time is displayed public class Frozen implements ViewState { // the method that responds to the lap button public void lapPushed() { vstate = new Updating(); // When the stopwatch enters the updating mode, it // fires a requestTime event so it can set the current time firePropertyChange("requestTime", new Long(0), new Long(1)); } // When frozen the time is not updated public void updateDisplayTime(long time) { } } // This method receives the events from the stopwatch and dispatches them // to the state public void propertyChange (PropertyChangeEvent evt) { vstate.updateDisplayTime(((Long)evt.getNewValue()).longValue()); } }