+ * If the {@code java.desktop} module is not present in the JRE, + * calling this method will throw an exception. + *
+ * * @param listener the listener to be added */ public void addChangeListener(final PropertyChangeListener listener) { - changeSupport.addPropertyChangeListener(listener); + synchronized (changeSupportMonitor) { + if (changeSupport == null) { + changeSupport = new PropertyChangeSupport(this); + } + + changeSupport.addPropertyChangeListener(listener); + } } /** @@ -115,7 +133,11 @@ public void addChangeListener(final PropertyChangeListener listener) { */ protected void changeState(final State newState) { if (state.compareAndSet(newState.oppositeState(), newState)) { - changeSupport.firePropertyChange(PROPERTY_NAME, !isOpen(newState), isOpen(newState)); + synchronized (changeSupportMonitor) { + if (changeSupport != null) { + changeSupport.firePropertyChange(PROPERTY_NAME, !isOpen(newState), isOpen(newState)); + } + } } } @@ -169,7 +191,11 @@ public void open() { * @param listener the listener to be removed */ public void removeChangeListener(final PropertyChangeListener listener) { - changeSupport.removePropertyChangeListener(listener); + synchronized (changeSupportMonitor) { + if (changeSupport != null) { + changeSupport.removePropertyChangeListener(listener); + } + } } }