001package jmri.beans;
002
003import java.beans.PropertyChangeEvent;
004import java.beans.PropertyChangeListener;
005
006/**
007 * If constructed with {@code SwingPropertyChangeListener(listener, true)} this
008 * subclass of {@link PropertyChangeListener} ensures listener is only ever
009 * notified on the <i>Event Dispatch Thread</i>.
010 *
011 * @author Randall Wood Copyright 2020
012 */
013public class SwingPropertyChangeListener implements PropertyChangeListener {
014
015    private final PropertyChangeListener listener;
016    private final boolean notifyOnEDT;
017
018    /**
019     * Create a SwingPropertyChangeListener with an associated listener that
020     * notifies the associated listener on the EDT.
021     *
022     * @param listener the listener that {@link PropertyChangeEvent}s will be
023     *                 passed to
024     */
025    public SwingPropertyChangeListener(PropertyChangeListener listener) {
026        this(listener, true);
027    }
028
029    /**
030     * Create a SwingPropertyChangeListener with an associated listener.
031     *
032     * @param listener    the listener that {@link PropertyChangeEvent}s will be
033     *                    passed to
034     * @param notifyOnEDT true to notify listener on the EDT; false to notify
035     *                    listener on current thread
036     */
037    public SwingPropertyChangeListener(PropertyChangeListener listener, boolean notifyOnEDT) {
038        this.listener = listener;
039        this.notifyOnEDT = notifyOnEDT;
040    }
041
042    /**
043     * {@inheritDoc}
044     *
045     * This implementation calls the listener's implementation on the EDT if
046     * {@link #isNotifyOnEDT()} is true.
047     */
048    @Override
049    public void propertyChange(PropertyChangeEvent evt) {
050        if (!notifyOnEDT || jmri.util.ThreadingUtil.isGUIThread()) {
051            listener.propertyChange(evt);
052        } else {
053            jmri.util.ThreadingUtil.runOnGUI(() -> listener.propertyChange(evt));
054        }
055    }
056
057    public boolean isNotifyOnEDT() {
058        return notifyOnEDT;
059    }
060
061    // private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(SwingPropertyChangeListener.class);
062}