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.MainApplication; 023import org.openstreetmap.josm.gui.layer.OsmDataLayer; 024import org.openstreetmap.josm.tools.I18n; 025 026/** 027 * This transfer support allows us to transfer tags to the selected primitives 028 * @author Michael Zangl 029 * @since 10604 030 */ 031public abstract class AbstractTagPaster extends AbstractOsmDataPaster { 032 033 AbstractTagPaster(DataFlavor df) { 034 super(df); 035 } 036 037 @Override 038 public boolean importData(TransferSupport support, OsmDataLayer layer, EastNorth pasteAt) 039 throws UnsupportedFlavorException, IOException { 040 Collection<OsmPrimitive> selection = layer.data.getSelected(); 041 if (selection.isEmpty()) { 042 return false; 043 } 044 045 return importTagsOn(support, selection); 046 } 047 048 @Override 049 public boolean importTagsOn(TransferSupport support, Collection<? extends OsmPrimitive> selection) 050 throws UnsupportedFlavorException, IOException { 051 ChangePropertyCommand command = new ChangePropertyCommand(Main.main.getEditDataSet(), selection, getTags(support)); 052 commitCommands(selection, Collections.singletonList(command)); 053 return true; 054 } 055 056 /** 057 * Create and execute SequenceCommand with descriptive title 058 * @param selection selected primitives 059 * @param commands the commands to perform in a sequential command 060 * @since 10737 061 */ 062 protected static void commitCommands(Collection<? extends OsmPrimitive> selection, List<Command> commands) { 063 if (!commands.isEmpty()) { 064 String title1 = trn("Pasting {0} tag", "Pasting {0} tags", commands.size(), commands.size()); 065 String title2 = trn("to {0} object", "to {0} objects", selection.size(), selection.size()); 066 @I18n.QuirkyPluralString 067 final String title = title1 + ' ' + title2; 068 MainApplication.undoRedo.add(new SequenceCommand(title, commands)); 069 } 070 } 071 072 /** 073 * Gets the tags that should be pasted. 074 * @param support The TransferSupport to get the tags from. 075 * @return The tags 076 * @throws UnsupportedFlavorException if the requested data flavor is not supported 077 * @throws IOException if an I/O error occurs 078 */ 079 protected abstract Map<String, String> getTags(TransferSupport support) throws UnsupportedFlavorException, IOException; 080}