source: josm/trunk/src/org/openstreetmap/josm/actions/PasteAction.java@ 2163

Last change on this file since 2163 was 2070, checked in by Gubaer, 15 years ago

new: rewrite of CombineWay action
new: conflict resolution dialog for CombineWay, including conflicts for different relation memberships
cleanup: cleanup in OsmReader, reduces memory footprint and reduces parsing time
cleanup: made most of the public fields in OsmPrimitive @deprecated, added accessors and changed the code
cleanup: replaced usages of @deprecated constructors for ExtendedDialog
fixed #3208: Combine ways brokes relation order

WARNING: this changeset touches a lot of code all over the code base. "latest" might become slightly unstable in the next days. Also experience incompatibility issues with plugins in the next few days.

  • Property svn:eol-style set to native
File size: 5.0 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.ArrayList;
10import java.util.Collection;
11import java.util.HashMap;
12import java.util.LinkedList;
13import java.util.List;
14
15import org.openstreetmap.josm.Main;
16import org.openstreetmap.josm.command.AddCommand;
17import org.openstreetmap.josm.command.Command;
18import org.openstreetmap.josm.command.SequenceCommand;
19import org.openstreetmap.josm.data.coor.EastNorth;
20import org.openstreetmap.josm.data.osm.DataSet;
21import org.openstreetmap.josm.data.osm.Node;
22import org.openstreetmap.josm.data.osm.OsmPrimitive;
23import org.openstreetmap.josm.data.osm.Relation;
24import org.openstreetmap.josm.data.osm.RelationMember;
25import org.openstreetmap.josm.data.osm.Way;
26import org.openstreetmap.josm.gui.layer.Layer;
27import org.openstreetmap.josm.tools.Shortcut;
28
29public final class PasteAction extends JosmAction {
30
31 public PasteAction() {
32 super(tr("Paste"), "paste", tr("Paste contents of paste buffer."),
33 Shortcut.registerShortcut("system:paste", tr("Edit: {0}", tr("Paste")), KeyEvent.VK_V, Shortcut.GROUP_MENU), true);
34 }
35
36 public void actionPerformed(ActionEvent e) {
37 if (!isEnabled())
38 return;
39 pasteData(Main.pasteBuffer, Main.pasteSource, e);
40 }
41
42 public void pasteData(DataSet pasteBuffer, Layer source, ActionEvent e) {
43 /* Find the middle of the pasteBuffer area */
44 double maxEast = -1E100, minEast = 1E100, maxNorth = -1E100, minNorth = 1E100;
45 for (Node n : pasteBuffer.nodes) {
46 double east = n.getEastNorth().east();
47 double north = n.getEastNorth().north();
48 if (east > maxEast) { maxEast = east; }
49 if (east < minEast) { minEast = east; }
50 if (north > maxNorth) { maxNorth = north; }
51 if (north < minNorth) { minNorth = north; }
52 }
53
54 EastNorth mPosition;
55 if((e.getModifiers() & ActionEvent.CTRL_MASK) ==0){
56 /* adjust the coordinates to the middle of the visible map area */
57 mPosition = Main.map.mapView.getCenter();
58 } else {
59 mPosition = Main.map.mapView.getEastNorth(Main.map.mapView.lastMEvent.getX(), Main.map.mapView.lastMEvent.getY());
60 }
61
62 double offsetEast = mPosition.east() - (maxEast + minEast)/2.0;
63 double offsetNorth = mPosition.north() - (maxNorth + minNorth)/2.0;
64
65 HashMap<OsmPrimitive,OsmPrimitive> map = new HashMap<OsmPrimitive,OsmPrimitive>();
66 /* temporarily maps old nodes to new so we can do a true deep copy */
67
68 /* do the deep copy of the paste buffer contents, leaving the pasteBuffer unchanged */
69 for (Node n : pasteBuffer.nodes) {
70 Node nnew = new Node(n);
71 nnew.clearOsmId();
72 if (Main.map.mapView.getEditLayer() == source) {
73 nnew.setEastNorth(nnew.getEastNorth().add(offsetEast, offsetNorth));
74 }
75 map.put(n, nnew);
76 }
77 for (Way w : pasteBuffer.ways) {
78 Way wnew = new Way();
79 wnew.cloneFrom(w);
80 wnew.clearOsmId();
81 /* make sure we reference the new nodes corresponding to the old ones */
82 List<Node> nodes = new ArrayList<Node>();
83 for (Node n : w.getNodes()) {
84 nodes.add((Node)map.get(n));
85 }
86 wnew.setNodes(nodes);
87 map.put(w, wnew);
88 }
89 for (Relation r : pasteBuffer.relations) {
90 Relation rnew = new Relation(r);
91 r.clearOsmId();
92 List<RelationMember> members = new ArrayList<RelationMember>();
93 for (RelationMember m : r.getMembers()) {
94 OsmPrimitive mo = map.get(m.getMember());
95 if(mo != null) /* FIXME - This only prevents illegal data, but kills the relation */
96 {
97 RelationMember mnew = new RelationMember(m.getRole(), map.get(m.getMember()));
98 members.add(mnew);
99 }
100 }
101 rnew.setMembers(members);
102 map.put(r, rnew);
103 }
104
105 /* Now execute the commands to add the dupicated contents of the paste buffer to the map */
106 Collection<OsmPrimitive> osms = map.values();
107 Collection<Command> clist = new LinkedList<Command>();
108 for (OsmPrimitive osm : osms) {
109 clist.add(new AddCommand(osm));
110 }
111
112 Main.main.undoRedo.add(new SequenceCommand(tr("Paste"), clist));
113 getCurrentDataSet().setSelected(osms);
114 Main.map.mapView.repaint();
115 }
116
117 @Override
118 protected void updateEnabledState() {
119 if (getCurrentDataSet() == null || Main.pasteBuffer == null) {
120 setEnabled(false);
121 return;
122 }
123 setEnabled(
124 !Main.pasteBuffer.nodes.isEmpty()
125 || !Main.pasteBuffer.ways.isEmpty()
126 || !Main.pasteBuffer.relations.isEmpty()
127 );
128 }
129}
Note: See TracBrowser for help on using the repository browser.