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

Last change on this file since 12920 was 12920, checked in by Don-vip, 7 years ago

fix #15387 - IAE occurs when pasting tags into relation editor using "Paste tags from buffer" (regression from #13036)

File size: 3.0 KB
RevLine 
[10604]1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.datatransfer.importers;
3
[10737]4import static org.openstreetmap.josm.tools.I18n.trn;
5
[10604]6import java.awt.datatransfer.DataFlavor;
7import java.awt.datatransfer.UnsupportedFlavorException;
8import java.io.IOException;
9import java.util.Collection;
[10737]10import java.util.Collections;
11import java.util.List;
[10604]12import java.util.Map;
13
14import javax.swing.TransferHandler.TransferSupport;
15
[12920]16import org.openstreetmap.josm.Main;
[10604]17import org.openstreetmap.josm.command.ChangePropertyCommand;
[10737]18import org.openstreetmap.josm.command.Command;
19import org.openstreetmap.josm.command.SequenceCommand;
[10604]20import org.openstreetmap.josm.data.coor.EastNorth;
21import org.openstreetmap.josm.data.osm.OsmPrimitive;
[12641]22import org.openstreetmap.josm.gui.MainApplication;
[10604]23import org.openstreetmap.josm.gui.layer.OsmDataLayer;
[10737]24import org.openstreetmap.josm.tools.I18n;
[10604]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 {
[12920]51 ChangePropertyCommand command = new ChangePropertyCommand(Main.main.getEditDataSet(), selection, getTags(support));
[10737]52 commitCommands(selection, Collections.singletonList(command));
[10604]53 return true;
54 }
55
56 /**
[10737]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 String title1 = trn("Pasting {0} tag", "Pasting {0} tags", commands.size(), commands.size());
65 String title2 = trn("to {0} object", "to {0} objects", selection.size(), selection.size());
66 @I18n.QuirkyPluralString
67 final String title = title1 + ' ' + title2;
[12641]68 MainApplication.undoRedo.add(new SequenceCommand(title, commands));
[10737]69 }
70 }
71
72 /**
[10604]73 * Gets the tags that should be pasted.
74 * @param support The TransferSupport to get the tags from.
75 * @return The tags
76 * @throws UnsupportedFlavorException if the requested data flavor is not supported
77 * @throws IOException if an I/O error occurs
78 */
79 protected abstract Map<String, String> getTags(TransferSupport support) throws UnsupportedFlavorException, IOException;
80}
Note: See TracBrowser for help on using the repository browser.