[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;
|
---|
[14134] | 19 | import org.openstreetmap.josm.data.UndoRedoHandler;
|
---|
[10604] | 20 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
[14143] | 21 | import org.openstreetmap.josm.data.osm.OsmDataManager;
|
---|
[10604] | 22 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
| 23 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
[10737] | 24 | import 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 | */
|
---|
| 31 | public 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 {
|
---|
[14143] | 51 | ChangePropertyCommand command = new ChangePropertyCommand(OsmDataManager.getInstance().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()) {
|
---|
[18610] | 64 | final int changedTags = commands.stream()
|
---|
| 65 | .filter(ChangePropertyCommand.class::isInstance)
|
---|
| 66 | .map(ChangePropertyCommand.class::cast)
|
---|
| 67 | .mapToInt(p -> p.getTags().size())
|
---|
| 68 | .sum();
|
---|
| 69 | String title1 = trn("Pasting {0} tag", "Pasting {0} tags", changedTags, changedTags);
|
---|
[10737] | 70 | String title2 = trn("to {0} object", "to {0} objects", selection.size(), selection.size());
|
---|
| 71 | @I18n.QuirkyPluralString
|
---|
| 72 | final String title = title1 + ' ' + title2;
|
---|
[14134] | 73 | UndoRedoHandler.getInstance().add(new SequenceCommand(title, commands));
|
---|
[10737] | 74 | }
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | /**
|
---|
[10604] | 78 | * Gets the tags that should be pasted.
|
---|
| 79 | * @param support The TransferSupport to get the tags from.
|
---|
| 80 | * @return The tags
|
---|
| 81 | * @throws UnsupportedFlavorException if the requested data flavor is not supported
|
---|
| 82 | * @throws IOException if an I/O error occurs
|
---|
| 83 | */
|
---|
| 84 | protected abstract Map<String, String> getTags(TransferSupport support) throws UnsupportedFlavorException, IOException;
|
---|
| 85 | }
|
---|