001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.gui.datatransfer.importers; 003 004import java.awt.datatransfer.DataFlavor; 005import java.awt.datatransfer.UnsupportedFlavorException; 006import java.io.IOException; 007import java.util.Collection; 008 009import javax.swing.TransferHandler; 010import javax.swing.TransferHandler.TransferSupport; 011 012import org.openstreetmap.josm.data.coor.EastNorth; 013import org.openstreetmap.josm.data.osm.OsmPrimitive; 014import org.openstreetmap.josm.gui.layer.OsmDataLayer; 015 016/** 017 * This is an abstract class that helps implementing the transfer support required by swing. 018 * <p> 019 * It implements a mechanism to import a given data flavor into the current OSM data layer. 020 * @author Michael Zangl 021 * @since 10604 022 */ 023public abstract class AbstractOsmDataPaster { 024 protected final DataFlavor df; 025 026 /** 027 * Create a new {@link AbstractOsmDataPaster} 028 * @param df The data flavor that this support supports. 029 */ 030 protected AbstractOsmDataPaster(DataFlavor df) { 031 this.df = df; 032 } 033 034 /** 035 * Checks if this supports importing the given transfer support. 036 * @param support The support that should be supported. 037 * @return True if we support that transfer. 038 */ 039 public boolean supports(TransferSupport support) { 040 return support.isDataFlavorSupported(df) && isCopy(support); 041 } 042 043 /** 044 * Checks if this supports any of the available flavors. 045 * @param available The flavors that should be supported 046 * @return True if any of them is supported. 047 */ 048 public boolean supports(Collection<DataFlavor> available) { 049 return available.contains(df); 050 } 051 052 private static boolean isCopy(TransferSupport support) { 053 return !support.isDrop() || (TransferHandler.COPY & support.getSourceDropActions()) == TransferHandler.COPY; 054 } 055 056 /** 057 * Attempts to import the given transfer data. 058 * @param support The transfer support to import from. 059 * @param layer The layer to paste at. May be null. 060 * @param pasteAt The position to paste at. 061 * @return <code>true</code> if the import was successful. 062 * @throws UnsupportedFlavorException if the requested data flavor is not supported 063 * @throws IOException if an I/O error occurs 064 */ 065 public abstract boolean importData(TransferSupport support, OsmDataLayer layer, EastNorth pasteAt) 066 throws UnsupportedFlavorException, IOException; 067 068 /** 069 * Imports only if this import changes the tags only. Does nothing if more than tags would be changed. 070 * @param support The support 071 * @param selection The primitives to apply on. 072 * @return <code>true</code> if an import was done. 073 * @throws UnsupportedFlavorException if the requested data flavor is not supported 074 * @throws IOException if an I/O error occurs 075 */ 076 public boolean importTagsOn(TransferSupport support, Collection<? extends OsmPrimitive> selection) 077 throws UnsupportedFlavorException, IOException { 078 return false; 079 } 080}