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

Last change on this file since 1180 was 1169, checked in by stoecker, 15 years ago

removed usage of tab stops

  • Property svn:eol-style set to native
File size: 4.8 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2// Author: David Earl
3package org.openstreetmap.josm.actions;
4
5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.awt.event.ActionEvent;
8import java.awt.event.KeyEvent;
9import java.util.Collection;
10import java.util.HashMap;
11import java.util.Iterator;
12import java.util.LinkedList;
13import java.util.Map;
14
15import org.openstreetmap.josm.Main;
16import org.openstreetmap.josm.command.ChangePropertyCommand;
17import org.openstreetmap.josm.command.Command;
18import org.openstreetmap.josm.command.SequenceCommand;
19import org.openstreetmap.josm.data.SelectionChangedListener;
20import org.openstreetmap.josm.data.osm.DataSet;
21import org.openstreetmap.josm.data.osm.OsmPrimitive;
22import org.openstreetmap.josm.tools.Shortcut;
23
24public final class PasteTagsAction extends JosmAction implements SelectionChangedListener {
25
26 public PasteTagsAction(JosmAction copyAction) {
27 super(tr("Paste Tags"), "pastetags",
28 tr("Apply tags of contents of paste buffer to all selected items."),
29 Shortcut.registerShortcut("system:pastestyle", tr("Edit: {0}", tr("Paste Tags")), KeyEvent.VK_V, Shortcut.GROUP_MENU, Shortcut.SHIFT_DEFAULT), true);
30 DataSet.selListeners.add(this);
31 copyAction.addListener(this);
32 setEnabled(false);
33 }
34
35 private void pasteKeys(Collection<Command> clist, Collection<? extends OsmPrimitive> pasteBufferSubset, Collection<OsmPrimitive> selectionSubset) {
36 /* scan the paste buffer, and add tags to each of the selected objects.
37 * If a tag already exists, it is overwritten */
38 if (selectionSubset != null && ! selectionSubset.isEmpty()) {
39 for (Iterator<? extends OsmPrimitive> it = pasteBufferSubset.iterator(); it.hasNext();) {
40 OsmPrimitive osm = it.next();
41 Map<String, String> m = osm.keys;
42 if(m != null)
43 {
44 for (String key : m.keySet()) {
45 if (! key.equals("created_by"))
46 clist.add(new ChangePropertyCommand(selectionSubset, key, osm.keys.get(key)));
47 }
48 }
49 }
50 }
51 }
52
53 public void actionPerformed(ActionEvent e) {
54 Collection<Command> clist = new LinkedList<Command>();
55 pasteKeys(clist, Main.pasteBuffer.nodes, Main.ds.getSelectedNodes());
56 pasteKeys(clist, Main.pasteBuffer.ways, Main.ds.getSelectedWays());
57 pasteKeys(clist, Main.pasteBuffer.relations, Main.ds.getSelectedRelations());
58 Main.main.undoRedo.add(new SequenceCommand(tr("Paste Tags"), clist));
59 Main.ds.setSelected(Main.ds.getSelected()); // to force selection listeners, in particular the tag panel, to update
60 Main.map.mapView.repaint();
61 }
62
63 private boolean containsSameKeysWithDifferentValues(Collection<? extends OsmPrimitive> osms) {
64 Map<String,String> kvSeen = new HashMap<String,String>();
65 for (Iterator<? extends OsmPrimitive> it = osms.iterator(); it.hasNext();) {
66 OsmPrimitive osm = it.next();
67 if (osm.keys == null || osm.keys.isEmpty())
68 continue;
69 for (String key : osm.keys.keySet()) {
70 if (key.equals("created_by")) // we ignore created_by
71 continue;
72 String value = osm.keys.get(key);
73 if (! kvSeen.containsKey(key))
74 kvSeen.put(key, value);
75 else if (! kvSeen.get(key).equals(value))
76 return true;
77 }
78 }
79 return false;
80 }
81
82 /**
83 * Determines whether to enable the widget depending on the contents of the paste
84 * buffer and current selection
85 * @param pasteBuffer
86 */
87 private void possiblyEnable(Collection<? extends OsmPrimitive> selection, DataSet pasteBuffer) {
88 /* only enable if there is something selected to paste into and
89 if we don't have conflicting keys in the pastebuffer */
90 setEnabled(selection != null &&
91 ! selection.isEmpty() &&
92 ! pasteBuffer.allPrimitives().isEmpty() &&
93 (Main.ds.getSelectedNodes().isEmpty() ||
94 ! containsSameKeysWithDifferentValues(pasteBuffer.nodes)) &&
95 (Main.ds.getSelectedWays().isEmpty() ||
96 ! containsSameKeysWithDifferentValues(pasteBuffer.ways)) &&
97 (Main.ds.getSelectedRelations().isEmpty() ||
98 ! containsSameKeysWithDifferentValues(pasteBuffer.relations)));
99 }
100
101 @Override public void pasteBufferChanged(DataSet newPasteBuffer) {
102 possiblyEnable(Main.ds.getSelected(), newPasteBuffer);
103 }
104
105 public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) {
106 possiblyEnable(newSelection, Main.pasteBuffer);
107 }
108}
Note: See TracBrowser for help on using the repository browser.