Ticket #14862: 14862-v2.patch

File 14862-v2.patch, 2.7 KB (added by GerdP, 6 years ago)
  • src/org/openstreetmap/josm/plugins/utilsplugin2/actions/TagBufferAction.java

     
    2020import org.openstreetmap.josm.command.SequenceCommand;
    2121import org.openstreetmap.josm.data.UndoRedoHandler;
    2222import org.openstreetmap.josm.data.osm.OsmPrimitive;
     23import org.openstreetmap.josm.data.osm.event.AbstractDatasetChangedEvent;
     24import org.openstreetmap.josm.data.osm.event.DataSetListenerAdapter;
     25import org.openstreetmap.josm.data.osm.event.DatasetEventManager;
     26import org.openstreetmap.josm.data.osm.event.DatasetEventManager.FireMode;
     27import org.openstreetmap.josm.data.osm.event.TagsChangedEvent;
    2328import org.openstreetmap.josm.tools.Shortcut;
    2429import org.openstreetmap.josm.tools.SubclassFilteredCollection;
    2530
     
    2833 *
    2934 * @author Zverik
    3035 */
    31 public class TagBufferAction extends JosmAction {
     36public class TagBufferAction extends JosmAction implements DataSetListenerAdapter.Listener {
    3237    private static final String TITLE = tr("Copy tags from previous selection");
    3338    private static final Predicate<OsmPrimitive> IS_TAGGED_PREDICATE = object -> object.isTagged();
    3439    private Map<String, String> tags = new HashMap<>();
    3540    private Map<String, String> currentTags = new HashMap<>();
    3641    private Set<OsmPrimitive> selectionBuf = new HashSet<>();
     42    private final DataSetListenerAdapter dataSetListenerAdapter = new DataSetListenerAdapter(this);
    3743
    3844    /**
    3945     * Constructs a new {@code TagBufferAction}.
     
    4551                true, false);
    4652        // The fields are not initialized while the super constructor is running, so we have to call this afterwards:
    4753        installAdapters();
     54        DatasetEventManager.getInstance().addDatasetListener(dataSetListenerAdapter, FireMode.IMMEDIATELY);
    4855    }
    4956
    5057    @Override
     
    7784            setEnabled(false);
    7885            if (selectionBuf != null)
    7986                selectionBuf.clear();
    80         } else
     87        } else {
    8188            updateEnabledState(getLayerManager().getEditDataSet().getSelected());
     89        }
    8290    }
    8391
    8492    @Override
     
    137145            }
    138146        }
    139147    }
     148
     149        @Override
     150        public void processDatasetEvent(AbstractDatasetChangedEvent event) {
     151                if (event instanceof TagsChangedEvent) {
     152                        TagsChangedEvent e = (TagsChangedEvent) event;
     153                        if (!e.getPrimitive().isSelected() || e.getPrimitive().getKeys().isEmpty())
     154                                return;
     155                        currentTags.clear();
     156                        currentTags.putAll(e.getPrimitive().getKeys());
     157                }
     158        }
    140159}