Search in sources :

Example 1 with WeakPropertyChangeListener

use of com.revolsys.beans.WeakPropertyChangeListener in project com.revolsys.open by revolsys.

the class Property method removeListener.

static void removeListener(final Object source, final String propertyName, final Object listener) {
    if (listener instanceof PropertyChangeListener) {
        final PropertyChangeListener propertyChangeListener = (PropertyChangeListener) listener;
        final PropertyChangeSupport propertyChangeSupport = propertyChangeSupport(source);
        if (propertyChangeSupport != null) {
            for (final PropertyChangeListener otherListener : propertyChangeSupport.getPropertyChangeListeners()) {
                if (otherListener instanceof PropertyChangeListenerProxy) {
                    final PropertyChangeListenerProxy proxy = (PropertyChangeListenerProxy) otherListener;
                    final PropertyChangeListener proxyListener = proxy.getListener();
                    final String proxyPropertyName = proxy.getPropertyName();
                    if (proxyListener instanceof WeakPropertyChangeListener) {
                        final WeakPropertyChangeListener weakListener = (WeakPropertyChangeListener) proxyListener;
                        final PropertyChangeListener listenerReference = weakListener.getPropertyChangeListener();
                        if (listenerReference == null) {
                            propertyChangeSupport.removePropertyChangeListener(proxyPropertyName, weakListener);
                        } else if (proxyPropertyName.equals(propertyName)) {
                            if (listenerReference == propertyChangeListener) {
                                propertyChangeSupport.removePropertyChangeListener(propertyName, weakListener);
                            }
                        }
                    } else if (propertyChangeListener.equals(proxyListener)) {
                        if (proxyPropertyName.equals(propertyName)) {
                            propertyChangeSupport.removePropertyChangeListener(propertyName, propertyChangeListener);
                        }
                    }
                } else if (otherListener instanceof WeakPropertyChangeListener) {
                    final WeakPropertyChangeListener weakListener = (WeakPropertyChangeListener) otherListener;
                    final PropertyChangeListener listenerReference = weakListener.getPropertyChangeListener();
                    if (listenerReference == null) {
                        propertyChangeSupport.removePropertyChangeListener(weakListener);
                    }
                }
            }
        }
        if (source instanceof Component) {
            final Component component = (Component) source;
            for (final PropertyChangeListener otherListener : component.getPropertyChangeListeners()) {
                if (otherListener.equals(propertyChangeListener)) {
                    component.removePropertyChangeListener(propertyName, propertyChangeListener);
                } else if (otherListener instanceof WeakPropertyChangeListener) {
                    final WeakPropertyChangeListener weakListener = (WeakPropertyChangeListener) otherListener;
                    final PropertyChangeListener listenerReference = weakListener.getPropertyChangeListener();
                    if (listenerReference == null || listenerReference.equals(propertyChangeListener)) {
                        component.removePropertyChangeListener(propertyName, propertyChangeListener);
                    }
                }
            }
        }
    }
}
Also used : WeakPropertyChangeListener(com.revolsys.beans.WeakPropertyChangeListener) ProxyPropertyChangeListener(com.revolsys.beans.ProxyPropertyChangeListener) PropertyChangeListener(java.beans.PropertyChangeListener) PropertyChangeSupport(com.revolsys.beans.PropertyChangeSupport) WeakPropertyChangeListener(com.revolsys.beans.WeakPropertyChangeListener) JComponent(javax.swing.JComponent) Component(java.awt.Component) PropertyChangeListenerProxy(java.beans.PropertyChangeListenerProxy)

Example 2 with WeakPropertyChangeListener

use of com.revolsys.beans.WeakPropertyChangeListener in project com.revolsys.open by revolsys.

the class Property method getPropertyChangeListener.

@SuppressWarnings("unchecked")
static PropertyChangeListener getPropertyChangeListener(final Object listener) {
    if (listener instanceof PropertyChangeListener) {
        final PropertyChangeListener propertyChangeListener = (PropertyChangeListener) listener;
        if (propertyChangeListener instanceof NonWeakListener) {
            return propertyChangeListener;
        } else {
            final WeakPropertyChangeListener weakListener = new WeakPropertyChangeListener(propertyChangeListener);
            return weakListener;
        }
    } else if (listener instanceof Consumer) {
        final Consumer<Object> consumer = (Consumer<Object>) listener;
        return (e) -> {
            final Object object = e.getNewValue();
            consumer.accept(object);
        };
    } else {
        return null;
    }
}
Also used : WeakPropertyChangeListener(com.revolsys.beans.WeakPropertyChangeListener) ProxyPropertyChangeListener(com.revolsys.beans.ProxyPropertyChangeListener) PropertyChangeListener(java.beans.PropertyChangeListener) BiConsumer(java.util.function.BiConsumer) Consumer(java.util.function.Consumer) WeakPropertyChangeListener(com.revolsys.beans.WeakPropertyChangeListener) NonWeakListener(com.revolsys.beans.NonWeakListener)

Aggregations

ProxyPropertyChangeListener (com.revolsys.beans.ProxyPropertyChangeListener)2 WeakPropertyChangeListener (com.revolsys.beans.WeakPropertyChangeListener)2 PropertyChangeListener (java.beans.PropertyChangeListener)2 NonWeakListener (com.revolsys.beans.NonWeakListener)1 PropertyChangeSupport (com.revolsys.beans.PropertyChangeSupport)1 Component (java.awt.Component)1 PropertyChangeListenerProxy (java.beans.PropertyChangeListenerProxy)1 BiConsumer (java.util.function.BiConsumer)1 Consumer (java.util.function.Consumer)1 JComponent (javax.swing.JComponent)1