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

Last change on this file since 2001 was 1924, checked in by jttt, 15 years ago

Made OsmPrimitive.keys deprecated, removed remaining references in JOSM

  • Property svn:eol-style set to native
File size: 5.4 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.osm.DataSet;
20import org.openstreetmap.josm.data.osm.DataSource;
21import org.openstreetmap.josm.data.osm.OsmPrimitive;
22import org.openstreetmap.josm.tools.Shortcut;
23
24public final class PasteTagsAction extends JosmAction {
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 copyAction.addListener(this);
31 }
32
33 private void pasteKeys(Collection<Command> clist, Collection<? extends OsmPrimitive> pasteBufferSubset, Collection<OsmPrimitive> selectionSubset) {
34 /* scan the paste buffer, and add tags to each of the selected objects.
35 * If a tag already exists, it is overwritten */
36 if (selectionSubset == null || selectionSubset.isEmpty())
37 return;
38
39 for (Iterator<? extends OsmPrimitive> it = pasteBufferSubset.iterator(); it.hasNext();) {
40 OsmPrimitive osm = it.next();
41
42 for (String key : osm.keySet()) {
43 clist.add(new ChangePropertyCommand(selectionSubset, key, osm.get(key)));
44 }
45 }
46 }
47
48 public void actionPerformed(ActionEvent e) {
49 Collection<Command> clist = new LinkedList<Command>();
50 String pbSource = "Multiple Sources";
51 if(Main.pasteBuffer.dataSources.size() == 1) {
52 pbSource = ((DataSource) Main.pasteBuffer.dataSources.toArray()[0]).origin;
53 }
54
55 boolean pbNodes = Main.pasteBuffer.nodes.size() > 0;
56 boolean pbWays = Main.pasteBuffer.ways.size() > 0;
57
58 boolean seNodes = getCurrentDataSet().getSelectedNodes().size() > 0;
59 boolean seWays = getCurrentDataSet().getSelectedWays().size() > 0;
60 boolean seRels = getCurrentDataSet().getSelectedRelations().size() > 0;
61
62 if(!seNodes && seWays && !seRels && pbNodes && pbSource.equals("Copied Nodes")) {
63 // Copy from nodes to ways
64 pasteKeys(clist, Main.pasteBuffer.nodes, getCurrentDataSet().getSelectedWays());
65 } else if(seNodes && !seWays && !seRels && pbWays && pbSource.equals("Copied Ways")) {
66 // Copy from ways to nodes
67 pasteKeys(clist, Main.pasteBuffer.ways, getCurrentDataSet().getSelectedNodes());
68 } else {
69 // Copy from equal to equal
70 pasteKeys(clist, Main.pasteBuffer.nodes, getCurrentDataSet().getSelectedNodes());
71 pasteKeys(clist, Main.pasteBuffer.ways, getCurrentDataSet().getSelectedWays());
72 pasteKeys(clist, Main.pasteBuffer.relations, getCurrentDataSet().getSelectedRelations());
73 }
74 Main.main.undoRedo.add(new SequenceCommand(tr("Paste Tags"), clist));
75 getCurrentDataSet().setSelected(getCurrentDataSet().getSelected()); // to force selection listeners, in particular the tag panel, to update
76 Main.map.mapView.repaint();
77 }
78
79 private boolean containsSameKeysWithDifferentValues(Collection<? extends OsmPrimitive> osms) {
80 Map<String,String> kvSeen = new HashMap<String,String>();
81 for (OsmPrimitive osm:osms) {
82 for (String key : osm.keySet()) {
83 String value = osm.get(key);
84 if (! kvSeen.containsKey(key)) {
85 kvSeen.put(key, value);
86 } else if (! kvSeen.get(key).equals(value))
87 return true;
88 }
89 }
90 return false;
91 }
92
93 /**
94 * Determines whether to enable the widget depending on the contents of the paste
95 * buffer and current selection
96 * @param pasteBuffer
97 */
98 private void possiblyEnable(Collection<? extends OsmPrimitive> selection, DataSet pasteBuffer) {
99 /* only enable if there is something selected to paste into and
100 if we don't have conflicting keys in the pastebuffer */
101 DataSet ds = getCurrentDataSet();
102 if (ds == null || ds.getSelected().isEmpty() || pasteBuffer == null || pasteBuffer.allPrimitives().isEmpty()) {
103 setEnabled(false);
104 return;
105 }
106 setEnabled((!ds.getSelectedNodes().isEmpty() && ! containsSameKeysWithDifferentValues(pasteBuffer.nodes)) ||
107 (!ds.getSelectedWays().isEmpty() && ! containsSameKeysWithDifferentValues(pasteBuffer.ways)) ||
108 (! ds.getSelectedRelations().isEmpty() && ! containsSameKeysWithDifferentValues(pasteBuffer.relations)));
109 }
110
111 @Override public void pasteBufferChanged(DataSet newPasteBuffer) {
112 updateEnabledState();
113 }
114
115 @Override
116 protected void updateEnabledState() {
117 if (getCurrentDataSet() == null || Main.pasteBuffer == null) {
118 setEnabled(false);
119 return;
120 }
121 possiblyEnable(getCurrentDataSet().getSelected(), Main.pasteBuffer);
122 }
123}
Note: See TracBrowser for help on using the repository browser.