001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.gui.datatransfer.importers; 003 004import static org.openstreetmap.josm.tools.I18n.trn; 005 006import java.awt.datatransfer.DataFlavor; 007import java.awt.datatransfer.UnsupportedFlavorException; 008import java.io.IOException; 009import java.util.Collection; 010import java.util.Collections; 011import java.util.List; 012import java.util.Map; 013 014import javax.swing.TransferHandler.TransferSupport; 015 016import org.openstreetmap.josm.Main; 017import org.openstreetmap.josm.command.ChangePropertyCommand; 018import org.openstreetmap.josm.command.Command; 019import org.openstreetmap.josm.command.SequenceCommand; 020import org.openstreetmap.josm.data.coor.EastNorth; 021import org.openstreetmap.josm.data.osm.OsmPrimitive; 022import org.openstreetmap.josm.gui.layer.OsmDataLayer; 023import org.openstreetmap.josm.tools.I18n; 024 025/** 026 * This transfer support allows us to transfer tags to the selected primitives 027 * @author Michael Zangl 028 * @since 10604 029 */ 030public abstract class AbstractTagPaster extends AbstractOsmDataPaster { 031 032 AbstractTagPaster(DataFlavor df) { 033 super(df); 034 } 035 036 @Override 037 public boolean importData(TransferSupport support, OsmDataLayer layer, EastNorth pasteAt) 038 throws UnsupportedFlavorException, IOException { 039 Collection<OsmPrimitive> selection = layer.data.getSelected(); 040 if (selection.isEmpty()) { 041 return false; 042 } 043 044 return importTagsOn(support, selection); 045 } 046 047 @Override 048 public boolean importTagsOn(TransferSupport support, Collection<? extends OsmPrimitive> selection) 049 throws UnsupportedFlavorException, IOException { 050 ChangePropertyCommand command = new ChangePropertyCommand(selection, getTags(support)); 051 commitCommands(selection, Collections.singletonList(command)); 052 return true; 053 } 054 055 /** 056 * Create and execute SequenceCommand with descriptive title 057 * @param selection selected primitives 058 * @param commands the commands to perform in a sequential command 059 * @since 10737 060 */ 061 protected static void commitCommands(Collection<? extends OsmPrimitive> selection, List<Command> commands) { 062 if (!commands.isEmpty()) { 063 String title1 = trn("Pasting {0} tag", "Pasting {0} tags", commands.size(), commands.size()); 064 String title2 = trn("to {0} object", "to {0} objects", selection.size(), selection.size()); 065 @I18n.QuirkyPluralString 066 final String title = title1 + ' ' + title2; 067 Main.main.undoRedo.add(new SequenceCommand(title, commands)); 068 } 069 } 070 071 /** 072 * Gets the tags that should be pasted. 073 * @param support The TransferSupport to get the tags from. 074 * @return The tags 075 * @throws UnsupportedFlavorException if the requested data flavor is not supported 076 * @throws IOException if an I/O error occurs 077 */ 078 protected abstract Map<String, String> getTags(TransferSupport support) throws UnsupportedFlavorException, IOException; 079}