bb.gui
Class DuplicateEventActionListener

java.lang.Object
  extended by bb.gui.DuplicateEventActionListener
All Implemented Interfaces:
ActionListener, EventListener

public abstract class DuplicateEventActionListener
extends Object
implements ActionListener

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.

Author:
Brent Boyer
See Also:
Sun bug report, Java Ranch discussion

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

duplicateEventMaxTimeDifference

protected static final long duplicateEventMaxTimeDifference
Specifies the maximum time difference allowed between 2 events for them to be considered duplicate events. Value is in milliseconds.

See Also:
Constant Field Values

lastEventTime

protected long lastEventTime
Occurence of the last event registered by this instance. Value is in milliseconds. Is initialized to 0 (i.e. "the epoch": January 1, 1970, 00:00:00 GMT).


minimumNonDuplicateTimeDifference

protected long minimumNonDuplicateTimeDifference
Constructor Detail

DuplicateEventActionListener

public DuplicateEventActionListener()
                             throws IllegalStateException
Constructor.

Throws:
IllegalStateException - if calling thread is not EventQueue's dispatch thread
Method Detail

actionPerformed

public abstract void actionPerformed(ActionEvent ae)
Must be implemented by concrete subclass.

Specified by:
actionPerformed in interface ActionListener

isDuplicateEvent

protected boolean isDuplicateEvent(ActionEvent actionEvent)
                            throws IllegalArgumentException
Determines if this is a duplicate event or not by comparing the difference of when actionEvent occured and lastEventTime with duplicateEventMaxTimeDifference. A side effect is that lastEventTime is updated to when actionEvent occured before this method returns.

Throws:
IllegalArgumentException - if actionEvent is null

printInfo

protected void printInfo(long timeDifference,
                         boolean isDuplicate)