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.util.ArrayList;
|
---|
8 | import java.util.Collection;
|
---|
9 |
|
---|
10 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
11 | import org.openstreetmap.josm.data.osm.PrimitiveData;
|
---|
12 | import org.openstreetmap.josm.gui.DefaultNameFormatter;
|
---|
13 |
|
---|
14 | /**
|
---|
15 | * Transferable objects for {@link PrimitiveData}.
|
---|
16 | * @since 9369
|
---|
17 | */
|
---|
18 | public class PrimitiveTransferable implements Transferable {
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * A wrapper for a collection of {@link PrimitiveData}.
|
---|
22 | */
|
---|
23 | public static final class Data {
|
---|
24 | private final Collection<PrimitiveData> primitiveData;
|
---|
25 |
|
---|
26 | private Data(Collection<PrimitiveData> primitiveData) {
|
---|
27 | this.primitiveData = primitiveData;
|
---|
28 | }
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Returns the contained {@link PrimitiveData}
|
---|
32 | * @return the contained {@link PrimitiveData}
|
---|
33 | */
|
---|
34 | public Collection<PrimitiveData> getPrimitiveData() {
|
---|
35 | return primitiveData;
|
---|
36 | }
|
---|
37 | }
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Data flavor for {@link PrimitiveData} which is wrapped in {@link Data}.
|
---|
41 | */
|
---|
42 | public static final DataFlavor PRIMITIVE_DATA = new DataFlavor(Data.class, Data.class.getName());
|
---|
43 | private final Collection<? extends OsmPrimitive> primitives;
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Constructs a new {@code PrimitiveTransferable}.
|
---|
47 | * @param primitives collection of OSM primitives
|
---|
48 | */
|
---|
49 | public PrimitiveTransferable(Collection<? extends OsmPrimitive> primitives) {
|
---|
50 | this.primitives = primitives;
|
---|
51 | }
|
---|
52 |
|
---|
53 | @Override
|
---|
54 | public DataFlavor[] getTransferDataFlavors() {
|
---|
55 | return new DataFlavor[]{PRIMITIVE_DATA, DataFlavor.stringFlavor};
|
---|
56 | }
|
---|
57 |
|
---|
58 | @Override
|
---|
59 | public boolean isDataFlavorSupported(DataFlavor flavor) {
|
---|
60 | return flavor == PRIMITIVE_DATA;
|
---|
61 | }
|
---|
62 |
|
---|
63 | @Override
|
---|
64 | public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException {
|
---|
65 | if (DataFlavor.stringFlavor.equals(flavor)) {
|
---|
66 | return getStringData();
|
---|
67 | } else if (PRIMITIVE_DATA.equals(flavor)) {
|
---|
68 | return getPrimitiveData();
|
---|
69 | }
|
---|
70 | throw new UnsupportedFlavorException(flavor);
|
---|
71 | }
|
---|
72 |
|
---|
73 | protected String getStringData() {
|
---|
74 | final StringBuilder sb = new StringBuilder();
|
---|
75 | for (OsmPrimitive primitive : primitives) {
|
---|
76 | sb.append(primitive.getType());
|
---|
77 | sb.append(" ").append(primitive.getUniqueId());
|
---|
78 | sb.append(" # ").append(primitive.getDisplayName(DefaultNameFormatter.getInstance()));
|
---|
79 | sb.append("\n");
|
---|
80 | }
|
---|
81 | return sb.toString().replace("\u200E", "").replace("\u200F", "");
|
---|
82 | }
|
---|
83 |
|
---|
84 | protected Data getPrimitiveData() {
|
---|
85 | final Collection<PrimitiveData> r = new ArrayList<>(primitives.size());
|
---|
86 | for (OsmPrimitive primitive : primitives) {
|
---|
87 | r.add(primitive.save());
|
---|
88 | }
|
---|
89 | return new Data(r);
|
---|
90 | }
|
---|
91 | }
|
---|