|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectbb.gui.DuplicateEventActionListener
public abstract class DuplicateEventActionListener
Abstract implementation of ActionListener.
This class does not implement actionPerformed (the sole method of ActionListener);
that is for concrete subclasses to implement (and is why this class is abstract).
Instead, this class's isDuplicateEvent
method handles erroneous duplicate ActionEvents.
One known type of dupilcate event is with java.awt.Buttons: if the user clicks once on a Button only one event should be fired, but known bugs in AWT can cause multiple events to be fired. This class attempts to identify such duplicate events.
Application of this class is trivial. Old code that looks like
someButton.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
//action handling here...
}
}
);
can be replaced with
someButton.addActionListener(
new DuplicateEventActionListener() { // see DuplicateEventActionListener javadocs for why have to use it...
public void actionPerformed(ActionEvent actionEvent) {
if (isDuplicateEvent(actionEvent)) return;
//action handling here...
}
}
);
if you wish to ignore duplicate events.
Warning: the current implementation of this class expects each instance to be associated with just one source of events (e.g. a single Button, as in the above code sample).
Like typical Java GUI code, this class is not multithread safe:
it expects to only be called by EventQueue
's dispatch thread
.
This threading limitation is checked in every public method.
Field Summary | |
---|---|
protected static long |
duplicateEventMaxTimeDifference
Specifies the maximum time difference allowed between 2 events for them to be considered duplicate events. |
protected long |
lastEventTime
Occurence of the last event registered by this instance. |
protected long |
minimumNonDuplicateTimeDifference
|
Constructor Summary | |
---|---|
DuplicateEventActionListener()
Constructor. |
Method Summary | |
---|---|
abstract void |
actionPerformed(ActionEvent ae)
Must be implemented by concrete subclass. |
protected boolean |
isDuplicateEvent(ActionEvent actionEvent)
Determines if this is a duplicate event or not by comparing the difference of when actionEvent occured and lastEventTime
with duplicateEventMaxTimeDifference . |
protected void |
printInfo(long timeDifference,
boolean isDuplicate)
|
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
protected static final long duplicateEventMaxTimeDifference
protected long lastEventTime
protected long minimumNonDuplicateTimeDifference
Constructor Detail |
---|
public DuplicateEventActionListener() throws IllegalStateException
IllegalStateException
- if calling thread is not EventQueue
's dispatch thread
Method Detail |
---|
public abstract void actionPerformed(ActionEvent ae)
actionPerformed
in interface ActionListener
protected boolean isDuplicateEvent(ActionEvent actionEvent) throws IllegalArgumentException
lastEventTime
with duplicateEventMaxTimeDifference
.
A side effect is that lastEventTime
is updated
to when actionEvent occured before this method returns.
IllegalArgumentException
- if actionEvent is nullprotected void printInfo(long timeDifference, boolean isDuplicate)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |