[10604] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.gui.datatransfer.importers;
|
---|
| 3 |
|
---|
[10737] | 4 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
| 5 |
|
---|
[10604] | 6 | import java.awt.datatransfer.DataFlavor;
|
---|
| 7 | import java.awt.datatransfer.UnsupportedFlavorException;
|
---|
| 8 | import java.io.IOException;
|
---|
| 9 | import java.util.Collection;
|
---|
[10737] | 10 | import java.util.Collections;
|
---|
| 11 | import java.util.List;
|
---|
[10604] | 12 | import java.util.Map;
|
---|
| 13 |
|
---|
| 14 | import javax.swing.TransferHandler.TransferSupport;
|
---|
| 15 |
|
---|
| 16 | import org.openstreetmap.josm.command.ChangePropertyCommand;
|
---|
[10737] | 17 | import org.openstreetmap.josm.command.Command;
|
---|
| 18 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
[10604] | 19 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
| 20 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
[12641] | 21 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
[10604] | 22 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
[10737] | 23 | import org.openstreetmap.josm.tools.I18n;
|
---|
[10604] | 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));
|
---|
[10737] | 51 | commitCommands(selection, Collections.singletonList(command));
|
---|
[10604] | 52 | return true;
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | /**
|
---|
[10737] | 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;
|
---|
[12641] | 67 | MainApplication.undoRedo.add(new SequenceCommand(title, commands));
|
---|
[10737] | 68 | }
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | /**
|
---|
[10604] | 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 | }
|
---|