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, 3 weeks ago

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

File size: 3.0 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.Main;
17import org.openstreetmap.josm.command.ChangePropertyCommand;
18import org.openstreetmap.josm.command.Command;
19import org.openstreetmap.josm.command.SequenceCommand;
20import org.openstreetmap.josm.data.coor.EastNorth;
21import org.openstreetmap.josm.data.osm.OsmPrimitive;
22import org.openstreetmap.josm.gui.MainApplication;
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(Main.main.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            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;
68            MainApplication.undoRedo.add(new SequenceCommand(title, commands));
69        }
70    }
71
72    /**
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.