One possibility entails:
- Create a
PropertyChangeEvent
instance using the PropertyChangeSupport
object.
- Create a new
fire
method to emulate firePropertyChange
.
- Iterate over the property change listeners.
- Invoke
propertyChange
for each listener using the new event instance.
Et la voilà, the if
conditionals that prevent firing when the old value equals the new value have been skirted.
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
/**
* Responsible for notifying its list of managed listeners when property
* change events have occurred. The change events will only be fired if the
* new value differs from the old value.
*
* @param <P> Type of property that, when changed, will try to issue a change
* event to any listeners.
*/
public abstract class PropertyDispatcher<P> {
private final PropertyChangeSupport mDispatcher =
new PropertyChangeSupport( this );
/**
* Adds a new listener to the internal dispatcher. The dispatcher uses a map,
* so calling this multiple times for the same listener will not result in
* the same listener receiving multiple notifications for one event.
*
* @param listener The class to notify when property values change, a value
* of {@code null} will have no effect.
*/
public void addPropertyChangeListener(
final PropertyChangeListener listener ) {
mDispatcher.addPropertyChangeListener( listener );
}
@SuppressWarnings("unused")
public void removePropertyChangeListener(
final PropertyChangeListener listener ) {
mDispatcher.removePropertyChangeListener( listener );
}
/**
* Called to fire the property change with the two given values differ.
* Normally events for the same old and new value are swallowed silently,
* which prevents double-key presses from bubbling up. Tracking double-key
* presses is used to increment a counter that is displayed on the key when
* the user continually types the same regular key.
*
* @param p Property name that has changed.
* @param o Old property value.
* @param n New property value.
*/
protected void fire( final P p, final String o, final String n ) {
final var pName = p.toString();
final var event = new PropertyChangeEvent( mDispatcher, pName, o, n );
for( final var listener : mDispatcher.getPropertyChangeListeners() ) {
listener.propertyChange( event );
}
}
}
This can be useful when firing multiple events for key presses using PropertyChangeSupport
. Typing "Hello" would bubble up as "Helo" because the old key event ("l") matches the second key press event ("l"). Direct notification in this manner allows the double-"l" to bubble up two distinct key press/release events.