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