1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.datatransfer.importers;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
5 |
|
---|
6 | import java.awt.datatransfer.DataFlavor;
|
---|
7 | import java.awt.datatransfer.UnsupportedFlavorException;
|
---|
8 | import java.io.IOException;
|
---|
9 | import java.util.Collection;
|
---|
10 | import java.util.Collections;
|
---|
11 | import java.util.List;
|
---|
12 | import java.util.Map;
|
---|
13 |
|
---|
14 | import javax.swing.TransferHandler.TransferSupport;
|
---|
15 |
|
---|
16 | import org.openstreetmap.josm.command.ChangePropertyCommand;
|
---|
17 | import org.openstreetmap.josm.command.Command;
|
---|
18 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
19 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
20 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
21 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
22 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
23 | import 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 | */
|
---|
30 | public 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 | }
|
---|