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

Last change on this file since 8688 was 8510, checked in by Don-vip, 9 years ago

checkstyle: enable relevant whitespace checks and fix them

  • Property svn:eol-style set to native
File size: 13.1 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> commands = new ArrayList<>();
59
60 public TagPaster(Collection<PrimitiveData> source, Collection<OsmPrimitive> target) {
61 this.source = source;
62 this.target = target;
63 }
64
65 /**
66 * Replies true 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 */
69 protected boolean isHeteogeneousSource() {
70 int count = 0;
71 count = !getSourcePrimitivesByType(OsmPrimitiveType.NODE).isEmpty() ? count + 1 : count;
72 count = !getSourcePrimitivesByType(OsmPrimitiveType.WAY).isEmpty() ? count + 1 : count;
73 count = !getSourcePrimitivesByType(OsmPrimitiveType.RELATION).isEmpty() ? count + 1 : count;
74 return count > 1;
75 }
76
77 /**
78 * Replies all primitives of type <code>type</code> in the current selection.
79 *
80 * @param type the type
81 * @return all primitives of type <code>type</code> in the current selection.
82 */
83 protected Collection<? extends PrimitiveData> getSourcePrimitivesByType(OsmPrimitiveType type) {
84 return PrimitiveData.getFilteredList(source, type);
85 }
86
87 /**
88 * Replies the collection of tags for all primitives of type <code>type</code> in the current
89 * selection
90 *
91 * @param type the type
92 * @return the collection of tags for all primitives of type <code>type</code> in the current
93 * selection
94 */
95 protected TagCollection getSourceTagsByType(OsmPrimitiveType type) {
96 return TagCollection.unionOfAllPrimitives(getSourcePrimitivesByType(type));
97 }
98
99 /**
100 * Replies true if there is at least one tag in the current selection for primitives of
101 * type <code>type</code>
102 *
103 * @param type the type
104 * @return true if there is at least one tag in the current selection for primitives of
105 * type <code>type</code>
106 */
107 protected boolean hasSourceTagsByType(OsmPrimitiveType type) {
108 return !getSourceTagsByType(type).isEmpty();
109 }
110
111 protected void buildChangeCommand(Collection<? extends OsmPrimitive> selection, TagCollection tc) {
112 for (String key : tc.getKeys()) {
113 commands.add(new Tag(key, tc.getValues(key).iterator().next()));
114 }
115 }
116
117 protected Map<OsmPrimitiveType, Integer> getSourceStatistics() {
118 Map<OsmPrimitiveType, Integer> ret = new EnumMap<>(OsmPrimitiveType.class);
119 for (OsmPrimitiveType type: OsmPrimitiveType.dataValues()) {
120 if (!getSourceTagsByType(type).isEmpty()) {
121 ret.put(type, getSourcePrimitivesByType(type).size());
122 }
123 }
124 return ret;
125 }
126
127 protected Map<OsmPrimitiveType, Integer> getTargetStatistics() {
128 Map<OsmPrimitiveType, Integer> ret = new EnumMap<>(OsmPrimitiveType.class);
129 for (OsmPrimitiveType type: OsmPrimitiveType.dataValues()) {
130 int count = OsmPrimitive.getFilteredList(target, type.getOsmClass()).size();
131 if (count > 0) {
132 ret.put(type, count);
133 }
134 }
135 return ret;
136 }
137
138 /**
139 * Pastes the tags from a homogeneous source (the {@link Main#pasteBuffer}s selection consisting
140 * of one type of {@link OsmPrimitive}s only).
141 *
142 * Tags from a homogeneous source can be pasted to a heterogeneous target. All target primitives,
143 * regardless of their type, receive the same tags.
144 */
145 protected void pasteFromHomogeneousSource() {
146 TagCollection tc = null;
147 for (OsmPrimitiveType type : OsmPrimitiveType.dataValues()) {
148 TagCollection tc1 = getSourceTagsByType(type);
149 if (!tc1.isEmpty()) {
150 tc = tc1;
151 }
152 }
153 if (tc == null)
154 // no tags found to paste. Abort.
155 return;
156
157 if (!tc.isApplicableToPrimitive()) {
158 PasteTagsConflictResolverDialog dialog = new PasteTagsConflictResolverDialog(Main.parent);
159 dialog.populate(tc, getSourceStatistics(), getTargetStatistics());
160 dialog.setVisible(true);
161 if (dialog.isCanceled())
162 return;
163 buildChangeCommand(target, dialog.getResolution());
164 } else {
165 // no conflicts in the source tags to resolve. Just apply the tags
166 // to the target primitives
167 //
168 buildChangeCommand(target, tc);
169 }
170 }
171
172 /**
173 * Replies true if there is at least one primitive of type <code>type</code>
174 * is in the target collection
175 *
176 * @param type the type to look for
177 * @return true if there is at least one primitive of type <code>type</code> in the collection
178 * <code>selection</code>
179 */
180 protected boolean hasTargetPrimitives(Class<? extends OsmPrimitive> type) {
181 return !OsmPrimitive.getFilteredList(target, type).isEmpty();
182 }
183
184 /**
185 * Replies true if this a heterogeneous source can be pasted without conflict to targets
186 *
187 * @param targets the collection of target primitives
188 * @return true if this a heterogeneous source can be pasted without conflicts to targets
189 */
190 protected boolean canPasteFromHeterogeneousSourceWithoutConflict(Collection<OsmPrimitive> targets) {
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
203 * primitives.
204 */
205 protected void pasteFromHeterogeneousSource() {
206 if (canPasteFromHeterogeneousSourceWithoutConflict(target)) {
207 for (OsmPrimitiveType type : OsmPrimitiveType.dataValues()) {
208 if (hasSourceTagsByType(type) && hasTargetPrimitives(type.getOsmClass())) {
209 buildChangeCommand(target, getSourceTagsByType(type));
210 }
211 }
212 } else {
213 PasteTagsConflictResolverDialog dialog = new PasteTagsConflictResolverDialog(Main.parent);
214 dialog.populate(
215 getSourceTagsByType(OsmPrimitiveType.NODE),
216 getSourceTagsByType(OsmPrimitiveType.WAY),
217 getSourceTagsByType(OsmPrimitiveType.RELATION),
218 getSourceStatistics(),
219 getTargetStatistics()
220 );
221 dialog.setVisible(true);
222 if (dialog.isCanceled())
223 return;
224 for (OsmPrimitiveType type : OsmPrimitiveType.dataValues()) {
225 if (hasSourceTagsByType(type) && hasTargetPrimitives(type.getOsmClass())) {
226 buildChangeCommand(OsmPrimitive.getFilteredList(target, type.getOsmClass()), dialog.getResolution(type));
227 }
228 }
229 }
230 }
231
232 public List<Tag> execute() {
233 commands.clear();
234 if (isHeteogeneousSource()) {
235 pasteFromHeterogeneousSource();
236 } else {
237 pasteFromHomogeneousSource();
238 }
239 return commands;
240 }
241
242 }
243
244 @Override
245 public void actionPerformed(ActionEvent e) {
246 Collection<OsmPrimitive> selection = getCurrentDataSet().getSelected();
247
248 if (selection.isEmpty())
249 return;
250
251 String buf = Utils.getClipboardContent();
252 if (buf == null || buf.isEmpty() || buf.matches(CopyAction.CLIPBOARD_REGEXP)) {
253 pasteTagsFromJOSMBuffer(selection);
254 } else {
255 // Paste tags from arbitrary text
256 pasteTagsFromText(selection, buf);
257 }
258 }
259
260 /** Paste tags from arbitrary text, not using JOSM buffer
261 * @return true if action was successful
262 */
263 public static boolean pasteTagsFromText(Collection<OsmPrimitive> selection, String text) {
264 Map<String, String> tags = TextTagParser.readTagsFromText(text);
265 if (tags == null || tags.isEmpty()) {
266 TextTagParser.showBadBufferMessage(help);
267 return false;
268 }
269 if (!TextTagParser.validateTags(tags)) return false;
270
271 List<Command> commands = new ArrayList<>(tags.size());
272 for (Entry<String, String> entry: tags.entrySet()) {
273 String v = entry.getValue();
274 commands.add(new ChangePropertyCommand(selection, entry.getKey(), "".equals(v) ? null : v));
275 }
276 commitCommands(selection, commands);
277 return !commands.isEmpty();
278 }
279
280 /** Paste tags from JOSM buffer
281 * @param selection objects that will have the tags
282 * @return false if JOSM buffer was empty
283 */
284 public static boolean pasteTagsFromJOSMBuffer(Collection<OsmPrimitive> selection) {
285 List<PrimitiveData> directlyAdded = Main.pasteBuffer.getDirectlyAdded();
286 if (directlyAdded == null || directlyAdded.isEmpty()) return false;
287
288 PasteTagsAction.TagPaster tagPaster = new PasteTagsAction.TagPaster(directlyAdded, selection);
289 List<Command> commands = new ArrayList<>();
290 for (Tag tag : tagPaster.execute()) {
291 commands.add(new ChangePropertyCommand(selection, tag.getKey(), "".equals(tag.getValue()) ? null : tag.getValue()));
292 }
293 commitCommands(selection, commands);
294 return true;
295 }
296
297 /**
298 * Create and execute SequenceCommand with descriptive title
299 * @param commands the commands to perform in a sequential command
300 */
301 private static void commitCommands(Collection<OsmPrimitive> selection, List<Command> commands) {
302 if (!commands.isEmpty()) {
303 String title1 = trn("Pasting {0} tag", "Pasting {0} tags", commands.size(), commands.size());
304 String title2 = trn("to {0} object", "to {0} objects", selection.size(), selection.size());
305 Main.main.undoRedo.add(
306 new SequenceCommand(
307 title1 + " " + title2,
308 commands
309 ));
310 }
311 }
312
313 @Override
314 protected void updateEnabledState() {
315 if (getCurrentDataSet() == null) {
316 setEnabled(false);
317 return;
318 }
319 // buffer listening slows down the program and is not very good for arbitrary text in buffer
320 setEnabled(!getCurrentDataSet().getSelected().isEmpty());
321 }
322
323 @Override
324 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
325 setEnabled(selection != null && !selection.isEmpty());
326 }
327}
Note: See TracBrowser for help on using the repository browser.