001package jmri.beans;
002
003import java.beans.PropertyChangeEvent;
004import java.beans.PropertyChangeListener;
005import javax.swing.event.SwingPropertyChangeSupport;
006
007/**
008 * Generic implementation of {@link jmri.beans.BeanInterface} with a complete
009 * implementation of {@link java.beans.PropertyChangeSupport}.
010 * <p>
011 * See the PropertyChangeSupport documentation for complete documentation of
012 * those methods.
013 * <p>
014 * This class is thread safe.
015 *
016 * @author Randall Wood (c) 2011, 2014, 2015, 2016, 2020
017 * @see java.beans.PropertyChangeSupport
018 */
019public abstract class Bean extends UnboundBean implements PropertyChangeFirer, PropertyChangeProvider {
020
021    /**
022     * Provide a {@link java.beans.PropertyChangeSupport} helper.
023     */
024    protected final SwingPropertyChangeSupport propertyChangeSupport;
025
026    /**
027     * Create a bean that notifies property change listeners on the thread the
028     * event was generated on.
029     */
030    protected Bean() {
031        this(false);
032    }
033
034    /**
035     * Create a bean.
036     *
037     * @param notifyOnEDT true to notify property change listeners on the EDT;
038     *                    false to notify listeners on the thread the event was
039     *                    generated on (which may or may not be the EDT)
040     */
041    protected Bean(boolean notifyOnEDT) {
042        propertyChangeSupport = new SwingPropertyChangeSupport(this, notifyOnEDT);
043    }
044
045    /**
046     * {@inheritDoc}
047     */
048    @Override
049    public void addPropertyChangeListener(PropertyChangeListener listener) {
050        propertyChangeSupport.addPropertyChangeListener(listener);
051    }
052
053    /**
054     * {@inheritDoc}
055     */
056    @Override
057    public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) {
058        propertyChangeSupport.addPropertyChangeListener(propertyName, listener);
059    }
060
061    /**
062     * {@inheritDoc}
063     */
064    @Override
065    public void fireIndexedPropertyChange(String propertyName, int index, boolean oldValue, boolean newValue) {
066        propertyChangeSupport.fireIndexedPropertyChange(propertyName, index, oldValue, newValue);
067    }
068
069    /**
070     * {@inheritDoc}
071     */
072    @Override
073    public void fireIndexedPropertyChange(String propertyName, int index, int oldValue, int newValue) {
074        propertyChangeSupport.fireIndexedPropertyChange(propertyName, index, oldValue, newValue);
075    }
076
077    /**
078     * {@inheritDoc}
079     */
080    @Override
081    public void fireIndexedPropertyChange(String propertyName, int index, Object oldValue, Object newValue) {
082        propertyChangeSupport.fireIndexedPropertyChange(propertyName, index, oldValue, newValue);
083    }
084
085    /**
086     * {@inheritDoc}
087     */
088    @Override
089    public void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) {
090        propertyChangeSupport.firePropertyChange(propertyName, oldValue, newValue);
091    }
092
093    /**
094     * {@inheritDoc}
095     */
096    @Override
097    public void firePropertyChange(PropertyChangeEvent event) {
098        propertyChangeSupport.firePropertyChange(event);
099    }
100
101    /**
102     * {@inheritDoc}
103     */
104    @Override
105    public void firePropertyChange(String propertyName, int oldValue, int newValue) {
106        propertyChangeSupport.firePropertyChange(propertyName, oldValue, newValue);
107    }
108
109    /**
110     * {@inheritDoc}
111     */
112    @Override
113    public void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
114        propertyChangeSupport.firePropertyChange(propertyName, oldValue, newValue);
115    }
116
117    /**
118     * {@inheritDoc}
119     */
120    @Override
121    public PropertyChangeListener[] getPropertyChangeListeners() {
122        return propertyChangeSupport.getPropertyChangeListeners();
123    }
124
125    /**
126     * {@inheritDoc}
127     */
128    @Override
129    public PropertyChangeListener[] getPropertyChangeListeners(String propertyName) {
130        return propertyChangeSupport.getPropertyChangeListeners(propertyName);
131    }
132
133    /**
134     * {@inheritDoc}
135     */
136    @Override
137    public void removePropertyChangeListener(PropertyChangeListener listener) {
138        propertyChangeSupport.removePropertyChangeListener(listener);
139    }
140
141    /**
142     * {@inheritDoc}
143     */
144    @Override
145    public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener) {
146        propertyChangeSupport.removePropertyChangeListener(propertyName, listener);
147    }
148
149    /**
150     * Is this Bean assuring that all property change listeners will be notified
151     * on the EDT?
152     *
153     * @return true if notifying listeners of events on the EDT; false if
154     *         notifying listeners on the thread that the event was generated on
155     *         (which may or may not be the EDT)
156     */
157    public boolean isNotifyOnEDT() {
158        return propertyChangeSupport.isNotifyOnEDT();
159    }
160}