be.ugent.caagt.swirl
Class ActionRepeater

java.lang.Object
  extended by be.ugent.caagt.swirl.ActionRepeater
All Implemented Interfaces:
java.awt.event.ActionListener, java.util.EventListener, javax.swing.event.ChangeListener

public abstract class ActionRepeater
extends java.lang.Object
implements javax.swing.event.ChangeListener, java.awt.event.ActionListener

Provides a means of repeating an action as long as a certain button is pressed. This class must be extended to provide an implementation of doAction() and possibly of buttonFirstPressed() and buttonPressCanceled(). An object of that type can then be registered with a button using the registerWith(javax.swing.AbstractButton) method.

When the button is first pressed, the method buttonFirstPressed is called, followed by doAction. Then, while the button remains armed, doAction is called again and again. When the button press is canceled (i.e., when the mouse button is released while the button is not armed) a final call to buttonPressCanceled is invoked. A 'normal' button press is handled by the action listeners of the button, and not by this class.

In the following example an extension of this class is used to implement a 'zoom' button which repeatedly zooms in while the button is pressed. Releasing the button while not armed, resets the zoom factor to 1.

    class ZoomRepeater extends ActionRepeater {
          private double zoomFactor;

          public void buttonFirstPressed () {
              zoomFactor = 1.0;
          }
          
          public void doAction () {
              zoomFactor *= 1.2;
              setZoom (zoomFactor);
          }
            
          public void buttonPressCanceled () {
              setZoom (1.0);
          }
          ...
    }
 
An object of this class can be attached to the corresponding button as follows:
    new ZoomRepeater().registerWith (zoomButton);
 
This makes zoomButton behave as requested.


Constructor Summary
ActionRepeater()
          Create a new object of this type.
 
Method Summary
 void actionPerformed(java.awt.event.ActionEvent e)
          Called whenever the timer fires.
 void buttonFirstPressed()
          Method which is called when the button is first pressed.
 void buttonPressCanceled()
          Method which is called when the button press is canceled: i.e., when the mouse button is released when the button is not armed.
abstract  void doAction()
          Action which is performed repeatedly while the button is pressed.
 javax.swing.AbstractButton getButton()
          Button to which this repeater is currently registered, or null if none.
 void registerWith(javax.swing.AbstractButton button)
          Register this repeater with a button.
 void setInterval(int interval)
          Set the interval between succesive calls to doAction().
 void stateChanged(javax.swing.event.ChangeEvent e)
          Listens to changes in the state of the button to which this object is registered.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ActionRepeater

public ActionRepeater()
Create a new object of this type. Will repeatedly 'fire' every 150 ms.

Method Detail

setInterval

public void setInterval(int interval)
Set the interval between succesive calls to doAction().

Parameters:
interval - new interval in milliseconds

getButton

public javax.swing.AbstractButton getButton()
Button to which this repeater is currently registered, or null if none.


registerWith

public void registerWith(javax.swing.AbstractButton button)
Register this repeater with a button. A single repeater can be registered with at most a single button at the same time. Registering with a new button will automatically undo an earlier registration with another button.

Parameters:
button - Button to which this repeater should be registered, or null to unregister.

doAction

public abstract void doAction()
Action which is performed repeatedly while the button is pressed.


buttonFirstPressed

public void buttonFirstPressed()
Method which is called when the button is first pressed.

This implementation is empty.


buttonPressCanceled

public void buttonPressCanceled()
Method which is called when the button press is canceled: i.e., when the mouse button is released when the button is not armed.

This implementation is empty.


stateChanged

public void stateChanged(javax.swing.event.ChangeEvent e)
Listens to changes in the state of the button to which this object is registered. Subclasses should override doAction(), buttonFirstPressed() and buttonPressCanceled() instead of this method.

Specified by:
stateChanged in interface javax.swing.event.ChangeListener

actionPerformed

public void actionPerformed(java.awt.event.ActionEvent e)
Called whenever the timer fires. Subclasses should override doAction() instead of this method.

Specified by:
actionPerformed in interface java.awt.event.ActionListener