source: subversion/applications/editors/josm/plugins/utilsplugin2/src/org/openstreetmap/josm/plugins/utilsplugin2/actions/TagBufferAction.java

Last change on this file was 34913, checked in by gerdp, 7 weeks ago

see #16711josm: Use different text for tooltip

File size: 3.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.plugins.utilsplugin2.actions;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.event.ActionEvent;
7import java.awt.event.KeyEvent;
8import java.util.ArrayList;
9import java.util.Collection;
10import java.util.List;
11
12import org.openstreetmap.josm.actions.JosmAction;
13import org.openstreetmap.josm.command.ChangePropertyCommand;
14import org.openstreetmap.josm.command.Command;
15import org.openstreetmap.josm.command.SequenceCommand;
16import org.openstreetmap.josm.data.UndoRedoHandler;
17import org.openstreetmap.josm.data.osm.OsmPrimitive;
18import org.openstreetmap.josm.data.osm.Tag;
19import org.openstreetmap.josm.data.osm.TagCollection;
20import org.openstreetmap.josm.tools.Shortcut;
21import org.openstreetmap.josm.tools.SubclassFilteredCollection;
22
23/**
24 * Remembers tags of selected object(s) and when clicked, pastes them onto selection.
25 *
26 * @author Zverik
27 */
28public class TagBufferAction extends JosmAction {
29    private static final String TITLE = tr("Paste tags from previous selection");
30    private static final TagCollection EmptyTags = new TagCollection();
31    private transient List<OsmPrimitive> selectionBuf = new ArrayList<>();
32    private TagCollection tagsToPaste = EmptyTags;
33    /**
34     * Constructs a new {@code TagBufferAction}.
35     */
36    public TagBufferAction() {
37        super(TITLE, "dumbutils/tagbuffer", tr("Paste tags from the previously selected object(s) (not from clipboard)."),
38                Shortcut.registerShortcut("tools:tagbuffer", tr("Tool: {0}", tr("Paste tags from previous selection")),
39                        KeyEvent.VK_R, Shortcut.SHIFT),
40                true, false);
41        // The fields are not initialized while the super constructor is running, so we have to call this afterwards:
42        installAdapters();
43    }
44
45    @Override
46    public void actionPerformed(ActionEvent e) {
47        Collection<OsmPrimitive> selection = getLayerManager().getEditDataSet().getSelected();
48        if (selection.isEmpty() || tagsToPaste.isEmpty())
49            return;
50
51        List<Command> commands = new ArrayList<>();
52        for (Tag tag : tagsToPaste) {
53            boolean foundNew = false;
54            for (OsmPrimitive p : selection) {
55                if (!p.hasTag(tag.getKey(), tag.getValue())) {
56                    foundNew = true;
57                    break;
58                }
59            }
60            if (foundNew)
61                commands.add(new ChangePropertyCommand(selection, tag.getKey(), tag.getValue()));
62        }
63
64        if (!commands.isEmpty())
65            UndoRedoHandler.getInstance().add(new SequenceCommand(TITLE, commands));
66    }
67
68    @Override
69    protected void updateEnabledState() {
70        if (getLayerManager().getEditDataSet() == null) {
71            setEnabled(false);
72            selectionBuf = new ArrayList<>();
73            tagsToPaste = EmptyTags;
74        } else
75            updateEnabledState(getLayerManager().getEditDataSet().getSelected());
76    }
77
78    @Override
79    protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
80        TagCollection oldTags = getCommonTags(selectionBuf);
81        if (!oldTags.isEmpty()) {
82                tagsToPaste = new TagCollection(oldTags);
83        }
84        selectionBuf = new ArrayList<>(selection);
85        setEnabled(!selection.isEmpty() && !tagsToPaste.isEmpty());
86    }
87
88    /**
89     * Find those tags which appear in all tagged primitives of the selection.
90     * @param selection the selection
91     * @return the common tags of all tagged primitives in the selection
92     */
93    private static TagCollection getCommonTags(List<OsmPrimitive> selection) {
94        if (selection.isEmpty())
95            return EmptyTags;
96        // Fix #8350 - only care about tagged objects
97        return TagCollection.commonToAllPrimitives(SubclassFilteredCollection.filter(selection, OsmPrimitive::isTagged));
98    }
99}
Note: See TracBrowser for help on using the repository browser.