Changeset 12181 in josm


Ignore:
Timestamp:
2017-05-15T19:28:35+02:00 (6 months ago)
Author:
michael2402
Message:

Add a new ListenableWeakReference. Use it to not lose references to weak preference listeners too early.

Location:
trunk
Files:
2 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/preferences/AbstractProperty.java

    r11553 r12181  
    66import org.openstreetmap.josm.data.Preferences.PreferenceChangeEvent;
    77import org.openstreetmap.josm.data.Preferences.PreferenceChangedListener;
     8import org.openstreetmap.josm.tools.ListenableWeakReference;
    89
    910/**
     
    250251     */
    251252    public void addWeakListener(ValueChangeListener<? super T> listener) {
    252         addWeakListenerImpl(new PreferenceChangedListenerAdapter(listener));
    253     }
    254 
    255     protected void addWeakListenerImpl(PreferenceChangedListener adapter) {
    256         getPreferences().addWeakKeyPreferenceChangeListener(getKey(), adapter);
     253        ValueChangeListener<T> weakListener = new WeakPreferenceAdapter(listener);
     254        PreferenceChangedListenerAdapter adapter = new PreferenceChangedListenerAdapter(weakListener);
     255        addListenerImpl(adapter);
     256    }
     257
     258    private class WeakPreferenceAdapter extends ListenableWeakReference<ValueChangeListener<? super T>>
     259            implements ValueChangeListener<T> {
     260        public WeakPreferenceAdapter(ValueChangeListener<? super T> referent) {
     261            super(referent);
     262        }
     263
     264        @Override
     265        public void valueChanged(ValueChangeEvent<? extends T> e) {
     266            ValueChangeListener<? super T> r = this.get();
     267            r.valueChanged(e);
     268        }
     269
     270        @Override
     271        protected void onDereference() {
     272            removeListenerImpl(new PreferenceChangedListenerAdapter(this));
     273        }
     274
    257275    }
    258276
  • trunk/src/org/openstreetmap/josm/data/preferences/AbstractToStringProperty.java

    r10939 r12181  
    5757            super.addListenerImpl(adapter);
    5858            parent.addListenerImpl(adapter);
    59         }
    60 
    61         @Override
    62         protected void addWeakListenerImpl(PreferenceChangedListener adapter) {
    63             super.addWeakListenerImpl(adapter);
    64             parent.addWeakListenerImpl(adapter);
    65         }
    66 
    67         @Override
    68         protected void removeListenerImpl(PreferenceChangedListener adapter) {
    69             super.removeListenerImpl(adapter);
    70             parent.removeListenerImpl(adapter);
    7159        }
    7260
Note: See TracChangeset for help on using the changeset viewer.