source: josm/trunk/src/org/openstreetmap/josm/gui/datatransfer/importers/AbstractTagPaster.java

Last change on this file was 18610, checked in by taylor.smock, 17 months ago

Fix #21324: Command stack says "pasting 1 tag to [number] objects" regardless of how many tags are pasted

File size: 3.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.datatransfer.importers;
3
4import static org.openstreetmap.josm.tools.I18n.trn;
5
6import java.awt.datatransfer.DataFlavor;
7import java.awt.datatransfer.UnsupportedFlavorException;
8import java.io.IOException;
9import java.util.Collection;
10import java.util.Collections;
11import java.util.List;
12import java.util.Map;
13
14import javax.swing.TransferHandler.TransferSupport;
15
16import org.openstreetmap.josm.command.ChangePropertyCommand;
17import org.openstreetmap.josm.command.Command;
18import org.openstreetmap.josm.command.SequenceCommand;
19import org.openstreetmap.josm.data.UndoRedoHandler;
20import org.openstreetmap.josm.data.coor.EastNorth;
21import org.openstreetmap.josm.data.osm.OsmDataManager;
22import org.openstreetmap.josm.data.osm.OsmPrimitive;
23import org.openstreetmap.josm.gui.layer.OsmDataLayer;
24import org.openstreetmap.josm.tools.I18n;
25
26/**
27 * This transfer support allows us to transfer tags to the selected primitives
28 * @author Michael Zangl
29 * @since 10604
30 */
31public abstract class AbstractTagPaster extends AbstractOsmDataPaster {
32
33 AbstractTagPaster(DataFlavor df) {
34 super(df);
35 }
36
37 @Override
38 public boolean importData(TransferSupport support, OsmDataLayer layer, EastNorth pasteAt)
39 throws UnsupportedFlavorException, IOException {
40 Collection<OsmPrimitive> selection = layer.data.getSelected();
41 if (selection.isEmpty()) {
42 return false;
43 }
44
45 return importTagsOn(support, selection);
46 }
47
48 @Override
49 public boolean importTagsOn(TransferSupport support, Collection<? extends OsmPrimitive> selection)
50 throws UnsupportedFlavorException, IOException {
51 ChangePropertyCommand command = new ChangePropertyCommand(OsmDataManager.getInstance().getEditDataSet(), selection, getTags(support));
52 commitCommands(selection, Collections.singletonList(command));
53 return true;
54 }
55
56 /**
57 * Create and execute SequenceCommand with descriptive title
58 * @param selection selected primitives
59 * @param commands the commands to perform in a sequential command
60 * @since 10737
61 */
62 protected static void commitCommands(Collection<? extends OsmPrimitive> selection, List<Command> commands) {
63 if (!commands.isEmpty()) {
64 final int changedTags = commands.stream()
65 .filter(ChangePropertyCommand.class::isInstance)
66 .map(ChangePropertyCommand.class::cast)
67 .mapToInt(p -> p.getTags().size())
68 .sum();
69 String title1 = trn("Pasting {0} tag", "Pasting {0} tags", changedTags, changedTags);
70 String title2 = trn("to {0} object", "to {0} objects", selection.size(), selection.size());
71 @I18n.QuirkyPluralString
72 final String title = title1 + ' ' + title2;
73 UndoRedoHandler.getInstance().add(new SequenceCommand(title, commands));
74 }
75 }
76
77 /**
78 * Gets the tags that should be pasted.
79 * @param support The TransferSupport to get the tags from.
80 * @return The tags
81 * @throws UnsupportedFlavorException if the requested data flavor is not supported
82 * @throws IOException if an I/O error occurs
83 */
84 protected abstract Map<String, String> getTags(TransferSupport support) throws UnsupportedFlavorException, IOException;
85}
Note: See TracBrowser for help on using the repository browser.