Ignore:
Timestamp:
2016-11-08T23:29:31+01:00 (4 years ago)
Author:
michael2402
Message:

Simplify ExpertToggleAction

Make ExpertToggleAction use the listener list to track weak listeners and add a method to set expert mode explicitly. Use it in OsmDataLayerTest to make tracing expert mode problems easier.

Add test case for ExpertToggleAction.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/ListenerList.java

    r11018 r11224  
    8080     */
    8181    public synchronized void addWeakListener(T listener) {
    82         ensureNotInList(listener);
    83         // clean the weak listeners, just to be sure...
    84         while (weakListeners.remove(new WeakListener<T>(null))) {
    85             // continue
    86         }
    87         weakListeners.add(new WeakListener<>(listener));
     82        if (ensureNotInList(listener)) {
     83            // clean the weak listeners, just to be sure...
     84            while (weakListeners.remove(new WeakListener<T>(null))) {
     85                // continue
     86            }
     87            weakListeners.add(new WeakListener<>(listener));
     88        }
    8889    }
    8990
     
    9394     */
    9495    public synchronized void addListener(T listener) {
    95         ensureNotInList(listener);
    96         listeners.add(listener);
    97     }
    98 
    99     private void ensureNotInList(T listener) {
     96        if (ensureNotInList(listener)) {
     97            listeners.add(listener);
     98        }
     99    }
     100
     101    private boolean ensureNotInList(T listener) {
    100102        CheckParameterUtil.ensureParameterNotNull(listener, "listener");
    101103        if (containsListener(listener)) {
    102104            failAdd(listener);
     105            return false;
     106        } else {
     107            return true;
    103108        }
    104109    }
     
    217222    }
    218223
     224    private static class UncheckedListenerList<T> extends ListenerList<T> {
     225        @Override
     226        protected void failAdd(T listener) {
     227            Logging.warn("Listener was alreaady added: {0}", listener);
     228            // ignore
     229        }
     230
     231        @Override
     232        protected void failRemove(T listener) {
     233            Logging.warn("Listener was removed twice or not added: {0}", listener);
     234            // ignore
     235        }
     236    }
     237
    219238    /**
    220239     * Create a new listener list
     
    229248        }
    230249    }
     250
     251    /**
     252     * Creates a new listener list that does not fail if listeners are added ore removed twice.
     253     * <p>
     254     * Use of this list is discouraged. You should always use {@link #create()} in new implementations and check your listeners.
     255     * @param <T> The listener type
     256     * @return A new list.
     257     * @since 11224
     258     */
     259    public static <T> ListenerList<T> createUnchecked() {
     260        return new UncheckedListenerList<>();
     261    }
    231262}
Note: See TracChangeset for help on using the changeset viewer.