Changeset 15609 in josm for trunk/src/org/openstreetmap


Ignore:
Timestamp:
2019-12-23T22:41:07+01:00 (5 years ago)
Author:
Don-vip
Message:

fix #18436 - add listeners to know when a DataSet's DataSources change (patch by taylor.smock)

Location:
trunk/src/org/openstreetmap/josm
Files:
5 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/AbstractUploadAction.java

    r15513 r15609  
    1212 * Abstract super-class of all upload actions.
    1313 * Listens to layer change events to update its enabled state.
    14  * @since xxx
     14 * @since 15513
    1515 */
    1616public abstract class AbstractUploadAction extends JosmAction {
  • trunk/src/org/openstreetmap/josm/data/osm/DataSet.java

    r15497 r15609  
    1111import java.util.HashSet;
    1212import java.util.Iterator;
     13import java.util.LinkedHashSet;
    1314import java.util.LinkedList;
    1415import java.util.List;
     
    4344import org.openstreetmap.josm.data.osm.event.DataChangedEvent;
    4445import org.openstreetmap.josm.data.osm.event.DataSetListener;
     46import org.openstreetmap.josm.data.osm.event.DataSourceAddedEvent;
     47import org.openstreetmap.josm.data.osm.event.DataSourceRemovedEvent;
    4548import org.openstreetmap.josm.data.osm.event.FilterChangedEvent;
    4649import org.openstreetmap.josm.data.osm.event.NodeMovedEvent;
     
    166169    private final Collection<DataSource> dataSources = new LinkedList<>();
    167170
     171    /**
     172     * A list of listeners that listen to DataSource changes on this layer
     173     */
     174    private final ListenerList<DataSourceListener> dataSourceListeners = ListenerList.create();
     175
    168176    private final ConflictCollection conflicts = new ConflictCollection();
    169177
     
    226234                        .collect(Collectors.toList()));
    227235            }
     236            DataSourceAddedEvent addedEvent = new DataSourceAddedEvent(this,
     237                    new LinkedHashSet<>(dataSources), copyFrom.dataSources.stream());
    228238            for (DataSource source : copyFrom.dataSources) {
    229239                dataSources.add(new DataSource(source));
    230240            }
     241            dataSourceListeners.fireEvent(d -> d.dataSourceChange(addedEvent));
    231242            version = copyFrom.version;
    232243            uploadPolicy = copyFrom.uploadPolicy;
     
    272283     */
    273284    public synchronized boolean addDataSources(Collection<DataSource> sources) {
     285        DataSourceAddedEvent addedEvent = new DataSourceAddedEvent(this,
     286                new LinkedHashSet<>(dataSources), sources.stream());
    274287        boolean changed = dataSources.addAll(sources);
    275288        if (changed) {
     
    277290            cachedDataSourceBounds = null;
    278291        }
     292        dataSourceListeners.fireEvent(d -> d.dataSourceChange(addedEvent));
    279293        return changed;
    280294    }
     
    573587    public void removeHighlightUpdateListener(HighlightUpdateListener listener) {
    574588        highlightUpdateListeners.removeListener(listener);
     589    }
     590
     591    /**
     592     * Adds a listener that gets notified whenever the data sources change
     593     *
     594     * @param listener The listener
     595     * @see #removeDataSourceListener
     596     * @see #getDataSources
     597     * @since 15609
     598     */
     599    public void addDataSourceListener(DataSourceListener listener) {
     600        dataSourceListeners.addListener(listener);
     601    }
     602
     603    /**
     604     * Removes a listener that gets notified whenever the data sources change
     605     *
     606     * @param listener The listener
     607     * @see #addDataSourceListener
     608     * @see #getDataSources
     609     * @since 15609
     610     */
     611    public void removeDataSourceListener(DataSourceListener listener) {
     612        dataSourceListeners.removeListener(listener);
    575613    }
    576614
     
    10851123            synchronized (from) {
    10861124                if (!from.dataSources.isEmpty()) {
    1087                     if (dataSources.addAll(from.dataSources)) {
     1125                    DataSourceAddedEvent addedEvent = new DataSourceAddedEvent(
     1126                            this, new LinkedHashSet<>(dataSources), from.dataSources.stream());
     1127                    DataSourceRemovedEvent clearEvent = new DataSourceRemovedEvent(
     1128                            this, new LinkedHashSet<>(from.dataSources), from.dataSources.stream());
     1129                    if (from.dataSources.stream().filter(dataSource -> !dataSources.contains(dataSource))
     1130                            .map(dataSources::add).filter(Boolean.TRUE::equals).count() > 0) {
    10881131                        cachedDataSourceArea = null;
    10891132                        cachedDataSourceBounds = null;
     
    10921135                    from.cachedDataSourceArea = null;
    10931136                    from.cachedDataSourceBounds = null;
     1137                    dataSourceListeners.fireEvent(d -> d.dataSourceChange(addedEvent));
     1138                    from.dataSourceListeners.fireEvent(d -> d.dataSourceChange(clearEvent));
    10941139                }
    10951140            }
Note: See TracChangeset for help on using the changeset viewer.