source: josm/trunk/src/org/openstreetmap/josm/actions/PasteTagsAction.java@ 8931

Last change on this file since 8931 was 8931, checked in by Don-vip, 8 years ago

javadoc fixes

  • Property svn:eol-style set to native
File size: 12.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions;
3
4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
5import static org.openstreetmap.josm.tools.I18n.tr;
6import static org.openstreetmap.josm.tools.I18n.trn;
7
8import java.awt.event.ActionEvent;
9import java.awt.event.KeyEvent;
10import java.util.ArrayList;
11import java.util.Collection;
12import java.util.EnumMap;
13import java.util.List;
14import java.util.Map;
15import java.util.Map.Entry;
16
17import org.openstreetmap.josm.Main;
18import org.openstreetmap.josm.command.ChangePropertyCommand;
19import org.openstreetmap.josm.command.Command;
20import org.openstreetmap.josm.command.SequenceCommand;
21import org.openstreetmap.josm.data.osm.OsmPrimitive;
22import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
23import org.openstreetmap.josm.data.osm.PrimitiveData;
24import org.openstreetmap.josm.data.osm.Tag;
25import org.openstreetmap.josm.data.osm.TagCollection;
26import org.openstreetmap.josm.gui.conflict.tags.PasteTagsConflictResolverDialog;
27import org.openstreetmap.josm.tools.Shortcut;
28import org.openstreetmap.josm.tools.TextTagParser;
29import org.openstreetmap.josm.tools.Utils;
30
31/**
32 * Action, to paste all tags from one primitive to another.
33 *
34 * It will take the primitive from the copy-paste buffer an apply all its tags
35 * to the selected primitive(s).
36 *
37 * @author David Earl
38 */
39public final class PasteTagsAction extends JosmAction {
40
41 private static final String help = ht("/Action/PasteTags");
42
43 /**
44 * Constructs a new {@code PasteTagsAction}.
45 */
46 public PasteTagsAction() {
47 super(tr("Paste Tags"), "pastetags",
48 tr("Apply tags of contents of paste buffer to all selected items."),
49 Shortcut.registerShortcut("system:pastestyle", tr("Edit: {0}", tr("Paste Tags")),
50 KeyEvent.VK_V, Shortcut.CTRL_SHIFT), true);
51 putValue("help", help);
52 }
53
54 public static class TagPaster {
55
56 private final Collection<PrimitiveData> source;
57 private final Collection<OsmPrimitive> target;
58 private final List<Tag> tags = new ArrayList<>();
59
60 public TagPaster(Collection<PrimitiveData> source, Collection<OsmPrimitive> target) {
61 this.source = source;
62 this.target = target;
63 }
64
65 /**
66 * Determines if the source for tag pasting is heterogeneous, i.e. if it doesn't consist of
67 * {@link OsmPrimitive}s of exactly one type
68 * @return true if the source for tag pasting is heterogeneous
69 */
70 protected boolean isHeteogeneousSource() {
71 int count = 0;
72 count = !getSourcePrimitivesByType(OsmPrimitiveType.NODE).isEmpty() ? count + 1 : count;
73 count = !getSourcePrimitivesByType(OsmPrimitiveType.WAY).isEmpty() ? count + 1 : count;
74 count = !getSourcePrimitivesByType(OsmPrimitiveType.RELATION).isEmpty() ? count + 1 : count;
75 return count > 1;
76 }
77
78 /**
79 * Replies all primitives of type <code>type</code> in the current selection.
80 *
81 * @param type the type
82 * @return all primitives of type <code>type</code> in the current selection.
83 */
84 protected Collection<? extends PrimitiveData> getSourcePrimitivesByType(OsmPrimitiveType type) {
85 return PrimitiveData.getFilteredList(source, type);
86 }
87
88 /**
89 * Replies the collection of tags for all primitives of type <code>type</code> in the current
90 * selection
91 *
92 * @param type the type
93 * @return the collection of tags for all primitives of type <code>type</code> in the current
94 * selection
95 */
96 protected TagCollection getSourceTagsByType(OsmPrimitiveType type) {
97 return TagCollection.unionOfAllPrimitives(getSourcePrimitivesByType(type));
98 }
99
100 /**
101 * Replies true if there is at least one tag in the current selection for primitives of
102 * type <code>type</code>
103 *
104 * @param type the type
105 * @return true if there is at least one tag in the current selection for primitives of
106 * type <code>type</code>
107 */
108 protected boolean hasSourceTagsByType(OsmPrimitiveType type) {
109 return !getSourceTagsByType(type).isEmpty();
110 }
111
112 protected void buildTags(TagCollection tc) {
113 for (String key : tc.getKeys()) {
114 tags.add(new Tag(key, tc.getValues(key).iterator().next()));
115 }
116 }
117
118 protected Map<OsmPrimitiveType, Integer> getSourceStatistics() {
119 Map<OsmPrimitiveType, Integer> ret = new EnumMap<>(OsmPrimitiveType.class);
120 for (OsmPrimitiveType type: OsmPrimitiveType.dataValues()) {
121 if (!getSourceTagsByType(type).isEmpty()) {
122 ret.put(type, getSourcePrimitivesByType(type).size());
123 }
124 }
125 return ret;
126 }
127
128 protected Map<OsmPrimitiveType, Integer> getTargetStatistics() {
129 Map<OsmPrimitiveType, Integer> ret = new EnumMap<>(OsmPrimitiveType.class);
130 for (OsmPrimitiveType type: OsmPrimitiveType.dataValues()) {
131 int count = OsmPrimitive.getFilteredList(target, type.getOsmClass()).size();
132 if (count > 0) {
133 ret.put(type, count);
134 }
135 }
136 return ret;
137 }
138
139 /**
140 * Pastes the tags from a homogeneous source (the {@link Main#pasteBuffer}s selection consisting
141 * of one type of {@link OsmPrimitive}s only).
142 *
143 * Tags from a homogeneous source can be pasted to a heterogeneous target. All target primitives,
144 * regardless of their type, receive the same tags.
145 */
146 protected void pasteFromHomogeneousSource() {
147 TagCollection tc = null;
148 for (OsmPrimitiveType type : OsmPrimitiveType.dataValues()) {
149 TagCollection tc1 = getSourceTagsByType(type);
150 if (!tc1.isEmpty()) {
151 tc = tc1;
152 }
153 }
154 if (tc == null)
155 // no tags found to paste. Abort.
156 return;
157
158 if (!tc.isApplicableToPrimitive()) {
159 PasteTagsConflictResolverDialog dialog = new PasteTagsConflictResolverDialog(Main.parent);
160 dialog.populate(tc, getSourceStatistics(), getTargetStatistics());
161 dialog.setVisible(true);
162 if (dialog.isCanceled())
163 return;
164 buildTags(dialog.getResolution());
165 } else {
166 // no conflicts in the source tags to resolve. Just apply the tags
167 // to the target primitives
168 //
169 buildTags(tc);
170 }
171 }
172
173 /**
174 * Replies true if there is at least one primitive of type <code>type</code>
175 * is in the target collection
176 *
177 * @param type the type to look for
178 * @return true if there is at least one primitive of type <code>type</code> in the collection
179 * <code>selection</code>
180 */
181 protected boolean hasTargetPrimitives(Class<? extends OsmPrimitive> type) {
182 return !OsmPrimitive.getFilteredList(target, type).isEmpty();
183 }
184
185 /**
186 * Replies true if this a heterogeneous source can be pasted without conflict to targets
187 *
188 * @return true if this a heterogeneous source can be pasted without conflicts to targets
189 */
190 protected boolean canPasteFromHeterogeneousSourceWithoutConflict() {
191 for (OsmPrimitiveType type : OsmPrimitiveType.dataValues()) {
192 if (hasTargetPrimitives(type.getOsmClass())) {
193 TagCollection tc = TagCollection.unionOfAllPrimitives(getSourcePrimitivesByType(type));
194 if (!tc.isEmpty() && !tc.isApplicableToPrimitive())
195 return false;
196 }
197 }
198 return true;
199 }
200
201 /**
202 * Pastes the tags in the current selection of the paste buffer to a set of target primitives.
203 */
204 protected void pasteFromHeterogeneousSource() {
205 if (canPasteFromHeterogeneousSourceWithoutConflict()) {
206 for (OsmPrimitiveType type : OsmPrimitiveType.dataValues()) {
207 if (hasSourceTagsByType(type) && hasTargetPrimitives(type.getOsmClass())) {
208 buildTags(getSourceTagsByType(type));
209 }
210 }
211 } else {
212 PasteTagsConflictResolverDialog dialog = new PasteTagsConflictResolverDialog(Main.parent);
213 dialog.populate(
214 getSourceTagsByType(OsmPrimitiveType.NODE),
215 getSourceTagsByType(OsmPrimitiveType.WAY),
216 getSourceTagsByType(OsmPrimitiveType.RELATION),
217 getSourceStatistics(),
218 getTargetStatistics()
219 );
220 dialog.setVisible(true);
221 if (dialog.isCanceled())
222 return;
223 for (OsmPrimitiveType type : OsmPrimitiveType.dataValues()) {
224 if (hasSourceTagsByType(type) && hasTargetPrimitives(type.getOsmClass())) {
225 buildTags(dialog.getResolution(type));
226 }
227 }
228 }
229 }
230
231 public List<Tag> execute() {
232 tags.clear();
233 if (isHeteogeneousSource()) {
234 pasteFromHeterogeneousSource();
235 } else {
236 pasteFromHomogeneousSource();
237 }
238 return tags;
239 }
240
241 }
242
243 @Override
244 public void actionPerformed(ActionEvent e) {
245 Collection<OsmPrimitive> selection = getCurrentDataSet().getSelected();
246
247 if (selection.isEmpty())
248 return;
249
250 String buf = Utils.getClipboardContent();
251 if (buf == null || buf.isEmpty() || buf.matches(CopyAction.CLIPBOARD_REGEXP)) {
252 pasteTagsFromJOSMBuffer(selection);
253 } else {
254 // Paste tags from arbitrary text
255 pasteTagsFromText(selection, buf);
256 }
257 }
258
259 /** Paste tags from arbitrary text, not using JOSM buffer
260 * @return true if action was successful
261 */
262 public static boolean pasteTagsFromText(Collection<OsmPrimitive> selection, String text) {
263 Map<String, String> tags = TextTagParser.readTagsFromText(text);
264 if (tags == null || tags.isEmpty()) {
265 TextTagParser.showBadBufferMessage(help);
266 return false;
267 }
268 if (!TextTagParser.validateTags(tags)) return false;
269
270 List<Command> commands = new ArrayList<>(tags.size());
271 for (Entry<String, String> entry: tags.entrySet()) {
272 String v = entry.getValue();
273 commands.add(new ChangePropertyCommand(selection, entry.getKey(), "".equals(v) ? null : v));
274 }
275 commitCommands(selection, commands);
276 return !commands.isEmpty();
277 }
278
279 /** Paste tags from JOSM buffer
280 * @param selection objects that will have the tags
281 * @return false if JOSM buffer was empty
282 */
283 public static boolean pasteTagsFromJOSMBuffer(Collection<OsmPrimitive> selection) {
284 List<PrimitiveData> directlyAdded = Main.pasteBuffer.getDirectlyAdded();
285 if (directlyAdded == null || directlyAdded.isEmpty()) return false;
286
287 PasteTagsAction.TagPaster tagPaster = new PasteTagsAction.TagPaster(directlyAdded, selection);
288 List<Command> commands = new ArrayList<>();
289 for (Tag tag : tagPaster.execute()) {
290 commands.add(new ChangePropertyCommand(selection, tag.getKey(), "".equals(tag.getValue()) ? null : tag.getValue()));
291 }
292 commitCommands(selection, commands);
293 return true;
294 }
295
296 /**
297 * Create and execute SequenceCommand with descriptive title
298 * @param commands the commands to perform in a sequential command
299 */
300 private static void commitCommands(Collection<OsmPrimitive> selection, List<Command> commands) {
301 if (!commands.isEmpty()) {
302 String title1 = trn("Pasting {0} tag", "Pasting {0} tags", commands.size(), commands.size());
303 String title2 = trn("to {0} object", "to {0} objects", selection.size(), selection.size());
304 Main.main.undoRedo.add(
305 new SequenceCommand(
306 title1 + ' ' + title2,
307 commands
308 ));
309 }
310 }
311
312 @Override
313 protected void updateEnabledState() {
314 if (getCurrentDataSet() == null) {
315 setEnabled(false);
316 return;
317 }
318 // buffer listening slows down the program and is not very good for arbitrary text in buffer
319 setEnabled(!getCurrentDataSet().getSelected().isEmpty());
320 }
321
322 @Override
323 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
324 setEnabled(selection != null && !selection.isEmpty());
325 }
326}
Note: See TracBrowser for help on using the repository browser.