Ticket #14862: 14862-v3.patch

File 14862-v3.patch, 6.9 KB (added by GerdP, 5 years ago)

requires JOSM r14518 or newer

  • build.xml

     
    44    <!-- enter the SVN commit message -->
    55    <property name="commit.message" value="[josm_utilsplugin2]: select boundary by double-click; multitagger table highlights"/>
    66    <!-- enter the *lowest* JOSM version this plugin is currently compatible with -->
    7     <property name="plugin.main.version" value="14153"/>
     7    <property name="plugin.main.version" value="14518"/>
    88
    99    <property name="plugin.author" value="Kalle Lampila, Upliner, Zverik, akks, joshdoe and others"/>
    1010    <property name="plugin.class" value="org.openstreetmap.josm.plugins.utilsplugin2.UtilsPlugin2"/>
  • src/org/openstreetmap/josm/plugins/utilsplugin2/actions/TagBufferAction.java

     
    77import java.awt.event.KeyEvent;
    88import java.util.ArrayList;
    99import java.util.Collection;
    10 import java.util.HashMap;
    1110import java.util.HashSet;
    1211import java.util.List;
    13 import java.util.Map;
    1412import java.util.Set;
    15 import java.util.function.Predicate;
    1613
    1714import org.openstreetmap.josm.actions.JosmAction;
    1815import org.openstreetmap.josm.command.ChangePropertyCommand;
     
    2017import org.openstreetmap.josm.command.SequenceCommand;
    2118import org.openstreetmap.josm.data.UndoRedoHandler;
    2219import org.openstreetmap.josm.data.osm.OsmPrimitive;
     20import org.openstreetmap.josm.data.osm.Tag;
     21import org.openstreetmap.josm.data.osm.TagCollection;
    2322import org.openstreetmap.josm.tools.Shortcut;
    2423import org.openstreetmap.josm.tools.SubclassFilteredCollection;
    2524
     
    3029 */
    3130public class TagBufferAction extends JosmAction {
    3231    private static final String TITLE = tr("Copy tags from previous selection");
    33     private static final Predicate<OsmPrimitive> IS_TAGGED_PREDICATE = object -> object.isTagged();
    34     private Map<String, String> tags = new HashMap<>();
    35     private Map<String, String> currentTags = new HashMap<>();
    36     private Set<OsmPrimitive> selectionBuf = new HashSet<>();
    37 
     32    private static final TagCollection EmptyTags = new TagCollection();
     33    private final Set<OsmPrimitive> selectionBuf = new HashSet<>();
     34    private TagCollection tagsToPaste = EmptyTags;
    3835    /**
    3936     * Constructs a new {@code TagBufferAction}.
    4037     */
     
    5047    @Override
    5148    public void actionPerformed(ActionEvent e) {
    5249        Collection<OsmPrimitive> selection = getLayerManager().getEditDataSet().getSelected();
    53         if (selection.isEmpty())
     50        if (selection.isEmpty() || tagsToPaste.isEmpty())
    5451            return;
    5552
    5653        List<Command> commands = new ArrayList<>();
    57         for (String key : tags.keySet()) {
    58             String value = tags.get(key);
     54        for (Tag tag : tagsToPaste) {
    5955            boolean foundNew = false;
    6056            for (OsmPrimitive p : selection) {
    61                 if (!p.hasKey(key) || !p.get(key).equals(value)) {
     57                if (!p.hasTag(tag.getKey(), tag.getValue())) {
    6258                    foundNew = true;
    6359                    break;
    6460                }
    6561            }
    6662            if (foundNew)
    67                 commands.add(new ChangePropertyCommand(selection, key, value));
     63                commands.add(new ChangePropertyCommand(selection, tag.getKey(), tag.getValue()));
    6864        }
    6965
    7066        if (!commands.isEmpty())
     
    7571    protected void updateEnabledState() {
    7672        if (getLayerManager().getEditDataSet() == null) {
    7773            setEnabled(false);
    78             if (selectionBuf != null)
    79                 selectionBuf.clear();
     74            selectionBuf.clear();
     75            tagsToPaste = EmptyTags;
    8076        } else
    8177            updateEnabledState(getLayerManager().getEditDataSet().getSelected());
    8278    }
     
    8379
    8480    @Override
    8581    protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
    86         // selection changed => check if selection is completely different from before
    87         boolean foundOld = false;
    88         if (selection != null) {
    89             for (OsmPrimitive p : selectionBuf) {
    90                 if (selection.contains(p)) {
    91                     foundOld = true;
    92                     break;
    93                 }
    94             }
    95             selectionBuf.clear();
    96             selectionBuf.addAll(selection);
    97         } else {
    98             foundOld = selectionBuf.isEmpty();
    99             selectionBuf.clear();
    100         }
    101         if (!foundOld) {
    102             // selection has completely changed, remember tags
    103             tags.clear();
    104             tags.putAll(currentTags);
    105         }
    106         if (getLayerManager().getEditDataSet() != null)
    107             rememberSelectionTags();
     82        TagCollection oldTags = getCommonTags(selectionBuf);
     83        if (!oldTags.isEmpty()) {
     84            tagsToPaste = new TagCollection(oldTags);
     85        }
     86        selectionBuf.clear();
     87        selectionBuf.addAll(selection);
    10888
    109         setEnabled(selection != null && !selection.isEmpty() && !tags.isEmpty());
     89        setEnabled(!selection.isEmpty() && !tagsToPaste.isEmpty());
    11090    }
    11191
    112     private void rememberSelectionTags() {
    113         // Fix #8350 - only care about tagged objects
    114         final Collection<OsmPrimitive> selectedTaggedObjects = SubclassFilteredCollection.filter(
    115                 getLayerManager().getEditDataSet().getSelected(), IS_TAGGED_PREDICATE);
    116         if (!selectedTaggedObjects.isEmpty()) {
    117             currentTags.clear();
    118             Set<String> bad = new HashSet<>();
    119             for (OsmPrimitive p : selectedTaggedObjects) {
    120                 if (currentTags.isEmpty()) {
    121                     for (String key : p.keySet()) {
    122                         currentTags.put(key, p.get(key));
    123                     }
    124                 } else {
    125                     for (String key : p.keySet()) {
    126                         if (!currentTags.containsKey(key) || !currentTags.get(key).equals(p.get(key)))
    127                             bad.add(key);
    128                     }
    129                     for (String key : currentTags.keySet()) {
    130                         if (!p.hasKey(key))
    131                             bad.add(key);
    132                     }
    133                 }
    134             }
    135             for (String key : bad) {
    136                 currentTags.remove(key);
    137             }
    138         }
     92    /**
     93     * Find those tags which appear in all primitives of the selection
     94     * @param selection the selection
     95     */
     96    private static TagCollection getCommonTags(Set<OsmPrimitive> selection) {
     97        if (selection.isEmpty())
     98                return EmptyTags;
     99//              // Fix #8350 - only care about tagged objects
     100                return TagCollection.commonToAllPrimitives(SubclassFilteredCollection.filter(selection, p -> p.isTagged()));
    139101    }
    140102}