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

Last change on this file since 12641 was 12641, checked in by Don-vip, 4 months ago

see #15182 - deprecate Main.main.undoRedo. Replacement: gui.MainApplication.undoRedo

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.command.ChangePropertyCommand;
17import org.openstreetmap.josm.command.Command;
18import org.openstreetmap.josm.command.SequenceCommand;
19import org.openstreetmap.josm.data.coor.EastNorth;
20import org.openstreetmap.josm.data.osm.OsmPrimitive;
21import org.openstreetmap.josm.gui.MainApplication;
22import org.openstreetmap.josm.gui.layer.OsmDataLayer;
23import org.openstreetmap.josm.tools.I18n;
24
25/**
26 * This transfer support allows us to transfer tags to the selected primitives
27 * @author Michael Zangl
28 * @since 10604
29 */
30public abstract class AbstractTagPaster extends AbstractOsmDataPaster {
31
32    AbstractTagPaster(DataFlavor df) {
33        super(df);
34    }
35
36    @Override
37    public boolean importData(TransferSupport support, OsmDataLayer layer, EastNorth pasteAt)
38            throws UnsupportedFlavorException, IOException {
39        Collection<OsmPrimitive> selection = layer.data.getSelected();
40        if (selection.isEmpty()) {
41            return false;
42        }
43
44        return importTagsOn(support, selection);
45    }
46
47    @Override
48    public boolean importTagsOn(TransferSupport support, Collection<? extends OsmPrimitive> selection)
49            throws UnsupportedFlavorException, IOException {
50        ChangePropertyCommand command = new ChangePropertyCommand(selection, getTags(support));
51        commitCommands(selection, Collections.singletonList(command));
52        return true;
53    }
54
55    /**
56     * Create and execute SequenceCommand with descriptive title
57     * @param selection selected primitives
58     * @param commands the commands to perform in a sequential command
59     * @since 10737
60     */
61    protected static void commitCommands(Collection<? extends OsmPrimitive> selection, List<Command> commands) {
62        if (!commands.isEmpty()) {
63            String title1 = trn("Pasting {0} tag", "Pasting {0} tags", commands.size(), commands.size());
64            String title2 = trn("to {0} object", "to {0} objects", selection.size(), selection.size());
65            @I18n.QuirkyPluralString
66            final String title = title1 + ' ' + title2;
67            MainApplication.undoRedo.add(new SequenceCommand(title, commands));
68        }
69    }
70
71    /**
72     * Gets the tags that should be pasted.
73     * @param support The TransferSupport to get the tags from.
74     * @return The tags
75     * @throws UnsupportedFlavorException if the requested data flavor is not supported
76     * @throws IOException if an I/O error occurs
77     */
78    protected abstract Map<String, String> getTags(TransferSupport support) throws UnsupportedFlavorException, IOException;
79}
Note: See TracBrowser for help on using the repository browser.