[626] | 1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
| 2 | // Author: David Earl
|
---|
| 3 | package org.openstreetmap.josm.actions;
|
---|
| 4 |
|
---|
[2818] | 5 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
[626] | 6 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 7 |
|
---|
| 8 | import java.awt.event.ActionEvent;
|
---|
| 9 | import java.awt.event.KeyEvent;
|
---|
| 10 | import java.util.ArrayList;
|
---|
| 11 | import java.util.HashMap;
|
---|
| 12 | import java.util.List;
|
---|
[2305] | 13 | import java.util.Map;
|
---|
[626] | 14 |
|
---|
| 15 | import org.openstreetmap.josm.Main;
|
---|
[2305] | 16 | import org.openstreetmap.josm.command.AddPrimitivesCommand;
|
---|
[1636] | 17 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
[2305] | 18 | import org.openstreetmap.josm.data.osm.NodeData;
|
---|
[2818] | 19 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
[2305] | 20 | import org.openstreetmap.josm.data.osm.PrimitiveData;
|
---|
| 21 | import org.openstreetmap.josm.data.osm.PrimitiveDeepCopy;
|
---|
| 22 | import org.openstreetmap.josm.data.osm.RelationData;
|
---|
| 23 | import org.openstreetmap.josm.data.osm.RelationMemberData;
|
---|
| 24 | import org.openstreetmap.josm.data.osm.WayData;
|
---|
[3384] | 25 | import org.openstreetmap.josm.data.osm.PrimitiveDeepCopy.PasteBufferChangedListener;
|
---|
[2818] | 26 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
[1592] | 27 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
[1084] | 28 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
[626] | 29 |
|
---|
[3384] | 30 | public final class PasteAction extends JosmAction implements PasteBufferChangedListener {
|
---|
[626] | 31 |
|
---|
| 32 | public PasteAction() {
|
---|
[1169] | 33 | super(tr("Paste"), "paste", tr("Paste contents of paste buffer."),
|
---|
[1814] | 34 | Shortcut.registerShortcut("system:paste", tr("Edit: {0}", tr("Paste")), KeyEvent.VK_V, Shortcut.GROUP_MENU), true);
|
---|
[2323] | 35 | putValue("help", ht("/Action/Paste"));
|
---|
[3384] | 36 | Main.pasteBuffer.addPasteBufferChangedListener(this);
|
---|
[626] | 37 | }
|
---|
| 38 |
|
---|
[1169] | 39 | public void actionPerformed(ActionEvent e) {
|
---|
[1820] | 40 | if (!isEnabled())
|
---|
| 41 | return;
|
---|
[1592] | 42 | pasteData(Main.pasteBuffer, Main.pasteSource, e);
|
---|
[1351] | 43 | }
|
---|
[1592] | 44 |
|
---|
[2305] | 45 | public void pasteData(PrimitiveDeepCopy pasteBuffer, Layer source, ActionEvent e) {
|
---|
[1169] | 46 | /* Find the middle of the pasteBuffer area */
|
---|
| 47 | double maxEast = -1E100, minEast = 1E100, maxNorth = -1E100, minNorth = 1E100;
|
---|
[2818] | 48 | boolean incomplete = false;
|
---|
[2305] | 49 | for (PrimitiveData data : pasteBuffer.getAll()) {
|
---|
| 50 | if (data instanceof NodeData) {
|
---|
| 51 | NodeData n = (NodeData)data;
|
---|
| 52 | double east = n.getEastNorth().east();
|
---|
| 53 | double north = n.getEastNorth().north();
|
---|
| 54 | if (east > maxEast) { maxEast = east; }
|
---|
| 55 | if (east < minEast) { minEast = east; }
|
---|
| 56 | if (north > maxNorth) { maxNorth = north; }
|
---|
| 57 | if (north < minNorth) { minNorth = north; }
|
---|
| 58 | }
|
---|
[2818] | 59 | if (data.isIncomplete()) {
|
---|
| 60 | incomplete = true;
|
---|
| 61 | }
|
---|
[1169] | 62 | }
|
---|
[811] | 63 |
|
---|
[2818] | 64 | // Allow to cancel paste if there are incomplete primitives
|
---|
| 65 | if (incomplete) {
|
---|
| 66 | if (!confirmDeleteIncomplete()) return;
|
---|
| 67 | }
|
---|
| 68 |
|
---|
[1169] | 69 | EastNorth mPosition;
|
---|
| 70 | if((e.getModifiers() & ActionEvent.CTRL_MASK) ==0){
|
---|
[1592] | 71 | /* adjust the coordinates to the middle of the visible map area */
|
---|
[1169] | 72 | mPosition = Main.map.mapView.getCenter();
|
---|
| 73 | } else {
|
---|
[2312] | 74 | if (Main.map.mapView.lastMEvent != null) {
|
---|
| 75 | mPosition = Main.map.mapView.getEastNorth(Main.map.mapView.lastMEvent.getX(), Main.map.mapView.lastMEvent.getY());
|
---|
| 76 | } else {
|
---|
| 77 | mPosition = Main.map.mapView.getCenter();
|
---|
| 78 | }
|
---|
[1169] | 79 | }
|
---|
[811] | 80 |
|
---|
[1169] | 81 | double offsetEast = mPosition.east() - (maxEast + minEast)/2.0;
|
---|
| 82 | double offsetNorth = mPosition.north() - (maxNorth + minNorth)/2.0;
|
---|
[1023] | 83 |
|
---|
[2305] | 84 | // Make a copy of pasteBuffer and map from old id to copied data id
|
---|
| 85 | List<PrimitiveData> bufferCopy = new ArrayList<PrimitiveData>();
|
---|
[2818] | 86 | Map<Long, Long> newNodeIds = new HashMap<Long, Long>();
|
---|
| 87 | Map<Long, Long> newWayIds = new HashMap<Long, Long>();
|
---|
| 88 | Map<Long, Long> newRelationIds = new HashMap<Long, Long>();
|
---|
| 89 | for (PrimitiveData data: pasteBuffer.getAll()) {
|
---|
| 90 | if (data.isIncomplete()) {
|
---|
| 91 | continue;
|
---|
| 92 | }
|
---|
[2305] | 93 | PrimitiveData copy = data.makeCopy();
|
---|
| 94 | copy.clearOsmId();
|
---|
[2818] | 95 | if (data instanceof NodeData) {
|
---|
[2932] | 96 | newNodeIds.put(data.getUniqueId(), copy.getUniqueId());
|
---|
[2818] | 97 | } else if (data instanceof WayData) {
|
---|
[2932] | 98 | newWayIds.put(data.getUniqueId(), copy.getUniqueId());
|
---|
[2818] | 99 | } else if (data instanceof RelationData) {
|
---|
[2932] | 100 | newRelationIds.put(data.getUniqueId(), copy.getUniqueId());
|
---|
[2818] | 101 | }
|
---|
[2305] | 102 | bufferCopy.add(copy);
|
---|
[1169] | 103 | }
|
---|
[2305] | 104 |
|
---|
| 105 | // Update references in copied buffer
|
---|
| 106 | for (PrimitiveData data:bufferCopy) {
|
---|
| 107 | if (data instanceof NodeData) {
|
---|
| 108 | NodeData nodeData = (NodeData)data;
|
---|
| 109 | if (Main.map.mapView.getEditLayer() == source) {
|
---|
| 110 | nodeData.setEastNorth(nodeData.getEastNorth().add(offsetEast, offsetNorth));
|
---|
[1205] | 111 | }
|
---|
[2305] | 112 | } else if (data instanceof WayData) {
|
---|
[2818] | 113 | List<Long> newNodes = new ArrayList<Long>();
|
---|
| 114 | for (Long oldNodeId: ((WayData)data).getNodes()) {
|
---|
| 115 | Long newNodeId = newNodeIds.get(oldNodeId);
|
---|
| 116 | if (newNodeId != null) {
|
---|
| 117 | newNodes.add(newNodeId);
|
---|
| 118 | }
|
---|
[2305] | 119 | }
|
---|
[2818] | 120 | ((WayData)data).setNodes(newNodes);
|
---|
[2305] | 121 | } else if (data instanceof RelationData) {
|
---|
[2818] | 122 | List<RelationMemberData> newMembers = new ArrayList<RelationMemberData>();
|
---|
| 123 | for (RelationMemberData member: ((RelationData)data).getMembers()) {
|
---|
| 124 | OsmPrimitiveType memberType = member.getMemberType();
|
---|
| 125 | Long newId = null;
|
---|
| 126 | switch (memberType) {
|
---|
| 127 | case NODE:
|
---|
| 128 | newId = newNodeIds.get(member.getMemberId());
|
---|
| 129 | break;
|
---|
| 130 | case WAY:
|
---|
| 131 | newId = newWayIds.get(member.getMemberId());
|
---|
| 132 | break;
|
---|
| 133 | case RELATION:
|
---|
| 134 | newId = newRelationIds.get(member.getMemberId());
|
---|
| 135 | break;
|
---|
| 136 | }
|
---|
| 137 | if (newId != null) {
|
---|
| 138 | newMembers.add(new RelationMemberData(member.getRole(), memberType, newId));
|
---|
| 139 | }
|
---|
[2305] | 140 | }
|
---|
[2818] | 141 | ((RelationData)data).setMembers(newMembers);
|
---|
[1169] | 142 | }
|
---|
| 143 | }
|
---|
[1023] | 144 |
|
---|
[2305] | 145 | /* Now execute the commands to add the duplicated contents of the paste buffer to the map */
|
---|
[626] | 146 |
|
---|
[2305] | 147 | Main.main.undoRedo.add(new AddPrimitivesCommand(bufferCopy));
|
---|
[1169] | 148 | Main.map.mapView.repaint();
|
---|
[626] | 149 | }
|
---|
[1820] | 150 |
|
---|
[2818] | 151 | protected boolean confirmDeleteIncomplete() {
|
---|
| 152 | ExtendedDialog ed = new ExtendedDialog(Main.parent,
|
---|
| 153 | tr("Delete incomplete members?"),
|
---|
| 154 | new String[] {tr("Paste without incomplete members"), tr("Cancel")});
|
---|
| 155 | ed.setButtonIcons(new String[] {"dialogs/relation/deletemembers.png", "cancel.png"});
|
---|
| 156 | ed.setContent(tr("The copied data contains incomplete primitives. "
|
---|
| 157 | + "When pasting the incomplete primitives are removed. "
|
---|
| 158 | + "Do you want to paste the data without the incomplete primitives?"));
|
---|
| 159 | ed.showDialog();
|
---|
| 160 | return ed.getValue() == 1;
|
---|
| 161 | }
|
---|
| 162 |
|
---|
[1820] | 163 | @Override
|
---|
| 164 | protected void updateEnabledState() {
|
---|
[1844] | 165 | if (getCurrentDataSet() == null || Main.pasteBuffer == null) {
|
---|
[1820] | 166 | setEnabled(false);
|
---|
| 167 | return;
|
---|
| 168 | }
|
---|
[2305] | 169 | setEnabled(!Main.pasteBuffer.isEmpty());
|
---|
[1820] | 170 | }
|
---|
[3384] | 171 |
|
---|
| 172 | @Override
|
---|
| 173 | public void pasteBufferChanged(PrimitiveDeepCopy pasteBuffer) {
|
---|
| 174 | updateEnabledState();
|
---|
| 175 | }
|
---|
[626] | 176 | }
|
---|