be.ugent.caagt.swirl
Class ActionRepeater
java.lang.Object
be.ugent.caagt.swirl.ActionRepeater
- ActionListener, ChangeListener
public abstract class ActionRepeater
extends java.lang.Object
implements ChangeListener, 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(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.
void | actionPerformed(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.
|
AbstractButton | getButton() - Button to which this repeater is currently registered, or
null
if none.
|
void | registerWith(AbstractButton button) - Register this repeater with a button.
|
void | setInterval(int interval) - Set the interval between succesive calls to
doAction() .
|
void | stateChanged(ChangeEvent e) - Listens to changes in the state of the button to which this object is
registered.
|
ActionRepeater
public ActionRepeater()
Create a new object of this type. Will repeatedly 'fire' every 150 ms.
actionPerformed
public void actionPerformed(ActionEvent e)
Called whenever the timer fires. Subclasses should override
doAction()
instead of this method.
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.
doAction
public abstract void doAction()
Action which is performed repeatedly while the button
is pressed.
getButton
public AbstractButton getButton()
Button to which this repeater is currently registered, or null
if none.
registerWith
public void registerWith(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.
button
- Button to which this repeater should be registered, or null
to unregister.
setInterval
public void setInterval(int interval)
Set the interval between succesive calls to
doAction()
.
interval
- new interval in milliseconds
stateChanged
public void stateChanged(ChangeEvent e)