Ignore:
Timestamp:
2017-06-08T01:09:44+02:00 (7 years ago)
Author:
michael2402
Message:

Change MapPaintStyles listener to use a ListenerList

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/mappaint/MapPaintStyles.java

    r12341 r12342  
    1313import java.util.List;
    1414import java.util.Set;
    15 import java.util.concurrent.CopyOnWriteArrayList;
    1615
    1716import javax.swing.ImageIcon;
     
    3534import org.openstreetmap.josm.io.CachedFile;
    3635import org.openstreetmap.josm.tools.ImageProvider;
     36import org.openstreetmap.josm.tools.ListenerList;
    3737import org.openstreetmap.josm.tools.Utils;
    3838
     
    460460     *  (get informed when the list of MapPaint StyleSources changes)
    461461     */
    462 
    463462    public interface MapPaintSylesUpdateListener {
     463        /**
     464         * Called on any style source changes that are not handled by {@link #mapPaintStyleEntryUpdated(int)}
     465         */
    464466        void mapPaintStylesUpdated();
    465467
    466         void mapPaintStyleEntryUpdated(int idx);
    467     }
    468 
    469     private static final CopyOnWriteArrayList<MapPaintSylesUpdateListener> listeners
    470             = new CopyOnWriteArrayList<>();
    471 
     468        /**
     469         * Called whenever a single style source entry was changed.
     470         * @param index The index of the entry.
     471         */
     472        void mapPaintStyleEntryUpdated(int index);
     473    }
     474
     475    private static final ListenerList<MapPaintSylesUpdateListener> listeners = ListenerList.createUnchecked();
     476
     477    /**
     478     * Add a listener that listens to global style changes.
     479     * @param listener The listener
     480     */
    472481    public static void addMapPaintSylesUpdateListener(MapPaintSylesUpdateListener listener) {
    473         if (listener != null) {
    474             listeners.addIfAbsent(listener);
    475         }
    476     }
    477 
     482        listeners.addListener(listener);
     483    }
     484
     485    /**
     486     * Removes a listener that listens to global style changes.
     487     * @param listener The listener
     488     */
    478489    public static void removeMapPaintSylesUpdateListener(MapPaintSylesUpdateListener listener) {
    479         listeners.remove(listener);
     490        listeners.removeListener(listener);
    480491    }
    481492
    482493    public static void fireMapPaintSylesUpdated() {
    483         for (MapPaintSylesUpdateListener l : listeners) {
    484             l.mapPaintStylesUpdated();
    485         }
    486     }
    487 
    488     public static void fireMapPaintStyleEntryUpdated(int idx) {
    489         for (MapPaintSylesUpdateListener l : listeners) {
    490             l.mapPaintStyleEntryUpdated(idx);
    491         }
     494        listeners.fireEvent(MapPaintSylesUpdateListener::mapPaintStylesUpdated);
     495    }
     496
     497    public static void fireMapPaintStyleEntryUpdated(int index) {
     498        listeners.fireEvent(l -> l.mapPaintStyleEntryUpdated(index));
    492499    }
    493500}
Note: See TracChangeset for help on using the changeset viewer.