1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.datatransfer.importers;
|
---|
3 |
|
---|
4 | import java.awt.datatransfer.UnsupportedFlavorException;
|
---|
5 | import java.io.IOException;
|
---|
6 | import java.util.ArrayList;
|
---|
7 | import java.util.Arrays;
|
---|
8 | import java.util.Collection;
|
---|
9 | import java.util.EnumMap;
|
---|
10 | import java.util.List;
|
---|
11 | import java.util.Map;
|
---|
12 |
|
---|
13 | import javax.swing.TransferHandler.TransferSupport;
|
---|
14 |
|
---|
15 | import org.openstreetmap.josm.Main;
|
---|
16 | import org.openstreetmap.josm.command.ChangePropertyCommand;
|
---|
17 | import org.openstreetmap.josm.command.Command;
|
---|
18 | import org.openstreetmap.josm.data.osm.IPrimitive;
|
---|
19 | import org.openstreetmap.josm.data.osm.Node;
|
---|
20 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
21 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
22 | import org.openstreetmap.josm.data.osm.Tag;
|
---|
23 | import org.openstreetmap.josm.data.osm.TagCollection;
|
---|
24 | import org.openstreetmap.josm.data.osm.TagMap;
|
---|
25 | import org.openstreetmap.josm.gui.conflict.tags.PasteTagsConflictResolverDialog;
|
---|
26 | import org.openstreetmap.josm.gui.datatransfer.data.PrimitiveTagTransferData;
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * This class helps pasting tags form other primitives. It handles resolving conflicts.
|
---|
30 | * @author Michael Zangl
|
---|
31 | * @since 10737
|
---|
32 | */
|
---|
33 | public class PrimitiveTagTransferPaster extends AbstractTagPaster {
|
---|
34 | /**
|
---|
35 | * Create a new {@link PrimitiveTagTransferPaster}
|
---|
36 | */
|
---|
37 | public PrimitiveTagTransferPaster() {
|
---|
38 | super(PrimitiveTagTransferData.FLAVOR);
|
---|
39 | }
|
---|
40 |
|
---|
41 | @Override
|
---|
42 | public boolean importTagsOn(TransferSupport support, Collection<? extends OsmPrimitive> selection)
|
---|
43 | throws UnsupportedFlavorException, IOException {
|
---|
44 | PrimitiveTagTransferData data = (PrimitiveTagTransferData) support.getTransferable().getTransferData(df);
|
---|
45 |
|
---|
46 | TagPasteSupport tagPaster = new TagPasteSupport(data, selection);
|
---|
47 | List<Command> commands = new ArrayList<>();
|
---|
48 | for (Tag tag : tagPaster.execute()) {
|
---|
49 | commands.add(new ChangePropertyCommand(selection, tag.getKey(), "".equals(tag.getValue()) ? null : tag.getValue()));
|
---|
50 | }
|
---|
51 | commitCommands(selection, commands);
|
---|
52 | return true;
|
---|
53 | }
|
---|
54 |
|
---|
55 | @Override
|
---|
56 | protected Map<String, String> getTags(TransferSupport support) throws UnsupportedFlavorException, IOException {
|
---|
57 | PrimitiveTagTransferData data = (PrimitiveTagTransferData) support.getTransferable().getTransferData(df);
|
---|
58 |
|
---|
59 | TagPasteSupport tagPaster = new TagPasteSupport(data, Arrays.asList(new Node()));
|
---|
60 | return new TagMap(tagPaster.execute());
|
---|
61 | }
|
---|
62 |
|
---|
63 | private static class TagPasteSupport {
|
---|
64 | private final PrimitiveTagTransferData data;
|
---|
65 | private final Collection<? extends IPrimitive> selection;
|
---|
66 | private final List<Tag> tags = new ArrayList<>();
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Constructs a new {@code TagPasteSupport}.
|
---|
70 | * @param data source tags to paste
|
---|
71 | * @param selection target primitives
|
---|
72 | */
|
---|
73 | TagPasteSupport(PrimitiveTagTransferData data, Collection<? extends IPrimitive> selection) {
|
---|
74 | super();
|
---|
75 | this.data = data;
|
---|
76 | this.selection = selection;
|
---|
77 | }
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * Pastes the tags from a homogeneous source (the selection consisting
|
---|
81 | * of one type of {@link OsmPrimitive}s only).
|
---|
82 | *
|
---|
83 | * Tags from a homogeneous source can be pasted to a heterogeneous target. All target primitives,
|
---|
84 | * regardless of their type, receive the same tags.
|
---|
85 | */
|
---|
86 | protected void pasteFromHomogeneousSource() {
|
---|
87 | TagCollection tc = null;
|
---|
88 | for (OsmPrimitiveType type : OsmPrimitiveType.dataValues()) {
|
---|
89 | TagCollection tc1 = data.getForPrimitives(type);
|
---|
90 | if (!tc1.isEmpty()) {
|
---|
91 | tc = tc1;
|
---|
92 | }
|
---|
93 | }
|
---|
94 | if (tc == null)
|
---|
95 | // no tags found to paste. Abort.
|
---|
96 | return;
|
---|
97 |
|
---|
98 | if (!tc.isApplicableToPrimitive()) {
|
---|
99 | PasteTagsConflictResolverDialog dialog = new PasteTagsConflictResolverDialog(Main.parent);
|
---|
100 | dialog.populate(tc, data.getStatistics(), getTargetStatistics());
|
---|
101 | dialog.setVisible(true);
|
---|
102 | if (dialog.isCanceled())
|
---|
103 | return;
|
---|
104 | buildTags(dialog.getResolution());
|
---|
105 | } else {
|
---|
106 | // no conflicts in the source tags to resolve. Just apply the tags to the target primitives
|
---|
107 | buildTags(tc);
|
---|
108 | }
|
---|
109 | }
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * Replies true if this a heterogeneous source can be pasted without conflict to targets
|
---|
113 | *
|
---|
114 | * @return true if this a heterogeneous source can be pasted without conflicts to targets
|
---|
115 | */
|
---|
116 | protected boolean canPasteFromHeterogeneousSourceWithoutConflict() {
|
---|
117 | for (OsmPrimitiveType type : OsmPrimitiveType.dataValues()) {
|
---|
118 | if (hasTargetPrimitives(type)) {
|
---|
119 | TagCollection tc = data.getForPrimitives(type);
|
---|
120 | if (!tc.isEmpty() && !tc.isApplicableToPrimitive())
|
---|
121 | return false;
|
---|
122 | }
|
---|
123 | }
|
---|
124 | return true;
|
---|
125 | }
|
---|
126 |
|
---|
127 | /**
|
---|
128 | * Pastes the tags in the current selection of the paste buffer to a set of target primitives.
|
---|
129 | */
|
---|
130 | protected void pasteFromHeterogeneousSource() {
|
---|
131 | if (canPasteFromHeterogeneousSourceWithoutConflict()) {
|
---|
132 | for (OsmPrimitiveType type : OsmPrimitiveType.dataValues()) {
|
---|
133 | if (!data.getForPrimitives(type).isEmpty() && hasTargetPrimitives(type)) {
|
---|
134 | buildTags(data.getForPrimitives(type));
|
---|
135 | }
|
---|
136 | }
|
---|
137 | } else {
|
---|
138 | PasteTagsConflictResolverDialog dialog = new PasteTagsConflictResolverDialog(Main.parent);
|
---|
139 | dialog.populate(
|
---|
140 | data.getForPrimitives(OsmPrimitiveType.NODE),
|
---|
141 | data.getForPrimitives(OsmPrimitiveType.WAY),
|
---|
142 | data.getForPrimitives(OsmPrimitiveType.RELATION),
|
---|
143 | data.getStatistics(),
|
---|
144 | getTargetStatistics()
|
---|
145 | );
|
---|
146 | dialog.setVisible(true);
|
---|
147 | if (dialog.isCanceled())
|
---|
148 | return;
|
---|
149 | for (OsmPrimitiveType type : OsmPrimitiveType.dataValues()) {
|
---|
150 | if (!data.getForPrimitives(type).isEmpty() && hasTargetPrimitives(type)) {
|
---|
151 | buildTags(dialog.getResolution(type));
|
---|
152 | }
|
---|
153 | }
|
---|
154 | }
|
---|
155 | }
|
---|
156 |
|
---|
157 | protected Map<OsmPrimitiveType, Integer> getTargetStatistics() {
|
---|
158 | Map<OsmPrimitiveType, Integer> ret = new EnumMap<>(OsmPrimitiveType.class);
|
---|
159 | for (OsmPrimitiveType type: OsmPrimitiveType.dataValues()) {
|
---|
160 | int count = (int) selection.stream().filter(p -> type == p.getType()).count();
|
---|
161 | if (count > 0) {
|
---|
162 | ret.put(type, count);
|
---|
163 | }
|
---|
164 | }
|
---|
165 | return ret;
|
---|
166 | }
|
---|
167 |
|
---|
168 | /**
|
---|
169 | * Replies true if there is at least one primitive of type <code>type</code>
|
---|
170 | * is in the target collection
|
---|
171 | *
|
---|
172 | * @param type the type to look for
|
---|
173 | * @return true if there is at least one primitive of type <code>type</code> in the collection
|
---|
174 | * <code>selection</code>
|
---|
175 | */
|
---|
176 | protected boolean hasTargetPrimitives(OsmPrimitiveType type) {
|
---|
177 | return selection.stream().anyMatch(p -> type == p.getType());
|
---|
178 | }
|
---|
179 |
|
---|
180 | protected void buildTags(TagCollection tc) {
|
---|
181 | for (String key : tc.getKeys()) {
|
---|
182 | tags.add(new Tag(key, tc.getValues(key).iterator().next()));
|
---|
183 | }
|
---|
184 | }
|
---|
185 |
|
---|
186 | /**
|
---|
187 | * Performs the paste operation.
|
---|
188 | * @return list of tags
|
---|
189 | */
|
---|
190 | public List<Tag> execute() {
|
---|
191 | tags.clear();
|
---|
192 | if (data.isHeterogeneousSource()) {
|
---|
193 | pasteFromHeterogeneousSource();
|
---|
194 | } else {
|
---|
195 | pasteFromHomogeneousSource();
|
---|
196 | }
|
---|
197 | return tags;
|
---|
198 | }
|
---|
199 |
|
---|
200 | @Override
|
---|
201 | public String toString() {
|
---|
202 | return "PasteSupport [data=" + data + ", selection=" + selection + ']';
|
---|
203 | }
|
---|
204 | }
|
---|
205 | }
|
---|