1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.datatransfer;
|
---|
3 |
|
---|
4 | import java.awt.datatransfer.DataFlavor;
|
---|
5 | import java.awt.datatransfer.Transferable;
|
---|
6 | import java.awt.datatransfer.UnsupportedFlavorException;
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.util.Arrays;
|
---|
9 | import java.util.Collection;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.Main;
|
---|
12 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
13 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
14 | import org.openstreetmap.josm.gui.datatransfer.importers.AbstractOsmDataPaster;
|
---|
15 | import org.openstreetmap.josm.gui.datatransfer.importers.FilePaster;
|
---|
16 | import org.openstreetmap.josm.gui.datatransfer.importers.OsmLinkPaster;
|
---|
17 | import org.openstreetmap.josm.gui.datatransfer.importers.PrimitiveDataPaster;
|
---|
18 | import org.openstreetmap.josm.gui.datatransfer.importers.PrimitiveTagTransferPaster;
|
---|
19 | import org.openstreetmap.josm.gui.datatransfer.importers.TagTransferPaster;
|
---|
20 | import org.openstreetmap.josm.gui.datatransfer.importers.TextTagPaster;
|
---|
21 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * This transfer hanlder provides the ability to transfer OSM data. It allows you to receive files, primitives or tags.
|
---|
25 | * @author Michael Zangl
|
---|
26 | * @since 10604
|
---|
27 | */
|
---|
28 | public class OsmTransferHandler extends AbstractStackTransferHandler {
|
---|
29 |
|
---|
30 | private static final Collection<AbstractOsmDataPaster> SUPPORTED = Arrays.asList(
|
---|
31 | new FilePaster(), new PrimitiveDataPaster(),
|
---|
32 | new PrimitiveTagTransferPaster(),
|
---|
33 | new TagTransferPaster(), new OsmLinkPaster(), new TextTagPaster());
|
---|
34 |
|
---|
35 | @Override
|
---|
36 | protected Collection<AbstractOsmDataPaster> getSupportedPasters() {
|
---|
37 | return SUPPORTED;
|
---|
38 | }
|
---|
39 |
|
---|
40 | private boolean importTags(TransferSupport support, Collection<? extends OsmPrimitive> primitives) {
|
---|
41 | for (AbstractOsmDataPaster df : SUPPORTED) {
|
---|
42 | if (df.supports(support)) {
|
---|
43 | try {
|
---|
44 | if (df.importTagsOn(support, primitives)) {
|
---|
45 | return true;
|
---|
46 | }
|
---|
47 | } catch (UnsupportedFlavorException | IOException e) {
|
---|
48 | Main.warn(e);
|
---|
49 | }
|
---|
50 | }
|
---|
51 | }
|
---|
52 | return super.importData(support);
|
---|
53 | }
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * Paste the current clipboard current at the given position
|
---|
57 | * @param editLayer The layer to paste on.
|
---|
58 | * @param mPosition The position to paste at. If it is <code>null</code>, the original position will be used.
|
---|
59 | */
|
---|
60 | public void pasteOn(OsmDataLayer editLayer, EastNorth mPosition) {
|
---|
61 | Transferable transferable = ClipboardUtils.getClipboard().getContents(null);
|
---|
62 | pasteOn(editLayer, mPosition, transferable);
|
---|
63 | }
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * Paste the given clipboard current at the given position
|
---|
67 | * @param editLayer The layer to paste on.
|
---|
68 | * @param mPosition The position to paste at. If it is <code>null</code>, the original position will be used.
|
---|
69 | * @param transferable The transferable to use.
|
---|
70 | */
|
---|
71 | public void pasteOn(OsmDataLayer editLayer, EastNorth mPosition, Transferable transferable) {
|
---|
72 | importData(new TransferSupport(Main.panel, transferable), editLayer, mPosition);
|
---|
73 | }
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Paste the given tags on the primitives.
|
---|
77 | * @param primitives The primitives to paste on.
|
---|
78 | */
|
---|
79 | public void pasteTags(Collection<? extends OsmPrimitive> primitives) {
|
---|
80 | Transferable transferable = ClipboardUtils.getClipboard().getContents(null);
|
---|
81 | importTags(new TransferSupport(Main.panel, transferable), primitives);
|
---|
82 | }
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * Check if any primitive data or any other supported data is available in the clipboard.
|
---|
86 | * @return <code>true</code> if any flavor is supported.
|
---|
87 | */
|
---|
88 | public boolean isDataAvailable() {
|
---|
89 | try {
|
---|
90 | Collection<DataFlavor> available = Arrays.asList(ClipboardUtils.getClipboard().getAvailableDataFlavors());
|
---|
91 | for (AbstractOsmDataPaster s : SUPPORTED) {
|
---|
92 | if (s.supports(available)) {
|
---|
93 | return true;
|
---|
94 | }
|
---|
95 | }
|
---|
96 | } catch (IllegalStateException e) {
|
---|
97 | Main.debug(e);
|
---|
98 | } catch (NullPointerException e) {
|
---|
99 | // JDK-6322854: On Linux/X11, NPE can happen for unknown reasons, on all versions of Java
|
---|
100 | Main.error(e);
|
---|
101 | }
|
---|
102 | return false;
|
---|
103 | }
|
---|
104 | }
|
---|