Changeset 29474 in osm for applications/editors/josm/plugins/undelete/src
- Timestamp:
- 2013-04-04T13:59:51+02:00 (12 years ago)
- Location:
- applications/editors/josm/plugins/undelete/src/org/openstreetmap/josm/plugins/undelete
- Files:
-
- 2 added
- 1 deleted
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/undelete/src/org/openstreetmap/josm/plugins/undelete/Undelete.java
r28501 r29474 1 1 // License: GPL. See LICENSE file for details. 2 // Derived from DownloadPrimitiveAction by Matthias Julius3 4 2 package org.openstreetmap.josm.plugins.undelete; 5 3 6 import static org.openstreetmap.josm.tools.I18n.tr;7 8 import java.awt.GridBagConstraints;9 import java.awt.GridBagLayout;10 import java.awt.event.ActionEvent;11 import java.awt.event.KeyEvent;12 import java.util.ArrayList;13 import java.util.List;14 15 import javax.swing.JCheckBox;16 import javax.swing.JLabel;17 import javax.swing.JMenuItem;18 import javax.swing.JPanel;19 import javax.swing.KeyStroke;20 21 4 import org.openstreetmap.josm.Main; 22 import org.openstreetmap.josm.actions.JosmAction;23 import org.openstreetmap.josm.data.osm.DataSet;24 import org.openstreetmap.josm.data.osm.Node;25 import org.openstreetmap.josm.data.osm.OsmPrimitive;26 import org.openstreetmap.josm.data.osm.OsmPrimitiveType;27 import org.openstreetmap.josm.data.osm.PrimitiveId;28 import org.openstreetmap.josm.data.osm.Relation;29 import org.openstreetmap.josm.data.osm.RelationMember;30 import org.openstreetmap.josm.data.osm.RelationMemberData;31 import org.openstreetmap.josm.data.osm.SimplePrimitiveId;32 import org.openstreetmap.josm.data.osm.User;33 import org.openstreetmap.josm.data.osm.Way;34 import org.openstreetmap.josm.data.osm.history.History;35 import org.openstreetmap.josm.data.osm.history.HistoryDataSet;36 import org.openstreetmap.josm.data.osm.history.HistoryNode;37 import org.openstreetmap.josm.data.osm.history.HistoryOsmPrimitive;38 import org.openstreetmap.josm.data.osm.history.HistoryRelation;39 import org.openstreetmap.josm.data.osm.history.HistoryWay;40 import org.openstreetmap.josm.gui.ExtendedDialog;41 5 import org.openstreetmap.josm.gui.MainMenu; 42 import org.openstreetmap.josm.gui.history.HistoryLoadTask;43 import org.openstreetmap.josm.gui.layer.OsmDataLayer;44 import org.openstreetmap.josm.gui.widgets.OsmIdTextField;45 import org.openstreetmap.josm.gui.widgets.OsmPrimitiveTypesComboBox;46 6 import org.openstreetmap.josm.plugins.Plugin; 47 7 import org.openstreetmap.josm.plugins.PluginInformation; 48 import org.openstreetmap.josm.tools.Shortcut;49 50 /**51 * // TODO: undelete relation members if necessary52 */53 8 54 9 public class Undelete extends Plugin { 55 JMenuItem Undelete;56 10 57 11 public Undelete(PluginInformation info) { 58 12 super(info); 59 Undelete = MainMenu.addAfter(Main.main.menu.fileMenu, new UndeleteAction(), false, Main.main.menu.updateModified); 60 61 } 62 63 64 private class UndeleteAction extends JosmAction { 65 /** 66 * 67 */ 68 private static final long serialVersionUID = 1L; 69 public UndeleteAction() { 70 super(tr("Undelete object..."), "undelete", tr("Undelete object by id"), Shortcut.registerShortcut("tools:undelete", tr("File: {0}", tr("Undelete object...")), 71 KeyEvent.VK_U, Shortcut.ALT_SHIFT), true); 72 } 73 74 @Override 75 public void actionPerformed(ActionEvent e) { 76 JCheckBox layer = new JCheckBox(tr("Separate Layer")); 77 layer.setToolTipText(tr("Select if the data should be added into a new layer")); 78 layer.setSelected(Main.pref.getBoolean("undelete.newlayer")); 79 JPanel all = new JPanel(new GridBagLayout()); 80 GridBagConstraints gc = new GridBagConstraints(); 81 gc.fill = GridBagConstraints.HORIZONTAL; 82 gc.anchor = GridBagConstraints.FIRST_LINE_START; 83 gc.gridy = 0; 84 gc.weightx = 0; 85 all.add(new JLabel(tr("Object ID:")), gc); 86 OsmIdTextField tfId = new OsmIdTextField(); 87 tfId.setText(Main.pref.get("undelete.osmid")); 88 tfId.setToolTipText(tr("Enter the type and ID of the objects that should be undeleted, e.g., ''n1 w2''")); 89 // forward the enter key stroke to the undelete button 90 tfId.getKeymap().removeKeyStrokeBinding(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false)); 91 gc.weightx = 1; 92 all.add(tfId, gc); 93 gc.gridy++; 94 gc.fill = GridBagConstraints.BOTH; 95 gc.weighty = 1.0; 96 gc.weightx = 0; 97 gc.gridy++; 98 all.add(layer, gc); 99 ExtendedDialog dialog = new ExtendedDialog(Main.parent, 100 tr("Undelete Object"), 101 new String[] {tr("Undelete object"), tr("Cancel")} 102 ); 103 dialog.setContent(all, false); 104 dialog.setButtonIcons(new String[] {"undelete.png", "cancel.png"}); 105 dialog.setToolTipTexts(new String[] { 106 tr("Start undeleting"), 107 tr("Close dialog and cancel") 108 }); 109 dialog.setDefaultButton(1); 110 //dialog.configureContextsensitiveHelp("/Action/DownloadObject", true /* show help button */); 111 dialog.showDialog(); 112 if (dialog.getValue() != 1) return; 113 Main.pref.put("undelete.newlayer", layer.isSelected()); 114 Main.pref.put("undelete.osmid", tfId.getText()); 115 undelete(layer.isSelected(), tfId.getIds(), 0); 116 } 117 } 118 119 /** 120 * Download the given primitive. 121 */ 122 public void undelete(boolean newLayer, final List<PrimitiveId> ids, final long parent) { 123 OsmDataLayer tmpLayer = Main.main.getEditLayer(); 124 if ((tmpLayer == null) || newLayer) { 125 tmpLayer = new OsmDataLayer(new DataSet(), OsmDataLayer.createNewName(), null); 126 Main.main.addLayer(tmpLayer); 127 } 128 129 final DataSet datas = tmpLayer.data; 130 final OsmDataLayer layer=tmpLayer; 131 132 HistoryLoadTask task = new HistoryLoadTask(); 133 for (PrimitiveId id: ids) { 134 task.add(id); 135 } 136 137 138 139 Main.worker.execute(task); 140 141 Runnable r = new Runnable() { 142 @Override 143 public void run() { 144 List<Node> nodes=new ArrayList<Node>(); 145 for (PrimitiveId pid: ids) 146 { 147 148 final Long id = pid.getUniqueId(); 149 final OsmPrimitiveType type = pid.getType(); 150 151 History h = HistoryDataSet.getInstance().getHistory(id, type); 152 153 OsmPrimitive primitive; 154 HistoryOsmPrimitive hPrimitive1=h.getLatest(); 155 HistoryOsmPrimitive hPrimitive2; 156 157 boolean visible=hPrimitive1.isVisible(); 158 159 if (visible) 160 { 161 // If the object is not deleted we get the real object 162 DownloadPrimitiveTask download=new DownloadPrimitiveTask(pid, layer); 163 System.out.println(tr("Will get {0}", pid)); 164 download.run(); 165 166 167 System.out.println(tr("Looking for {0}", pid)); 168 primitive=datas.getPrimitiveById(id, type); 169 System.out.println(tr("Found {0}", String.valueOf(primitive.getId()))); 170 if (parent>0 && type.equals(OsmPrimitiveType.NODE)) 171 { 172 nodes.add((Node)primitive); 173 } 174 } 175 else 176 { 177 if (type.equals(OsmPrimitiveType.NODE)) 178 { 179 // We get version and user from the latest version, coordinates and tags from n-1 version 180 hPrimitive2=h.getByVersion(h.getNumVersions()-1); 181 182 Node node = new Node(id, (int) hPrimitive1.getVersion()); 183 184 HistoryNode hNode = (HistoryNode) hPrimitive2; 185 node.setCoor(hNode.getCoords()); 186 187 primitive=node; 188 if (parent>0) 189 { 190 nodes.add(node); 191 } 192 } 193 else if (type.equals(OsmPrimitiveType.WAY)) 194 { 195 // We get version and user from the latest version, nodes and tags from n-1 version 196 hPrimitive1 = h.getLatest(); 197 hPrimitive2 = h.getByVersion(h.getNumVersions()-1); 198 199 200 201 Way way = new Way(id, (int) hPrimitive1.getVersion()); 202 203 HistoryWay hWay = (HistoryWay) hPrimitive2; 204 //System.out.println(tr("Primitive {0} version {1}: {2} nodes", hPrimitive2.getId(), hPrimitive2.getVersion(), hWay.getNumNodes())); 205 List<PrimitiveId> nodeIds = new ArrayList<PrimitiveId>(); 206 for (Long i: hWay.getNodes()) { 207 nodeIds.add(new SimplePrimitiveId(i, OsmPrimitiveType.NODE)); 208 } 209 undelete(false, nodeIds, id); 210 211 primitive=way; 212 213 } 214 else 215 { 216 primitive=new Relation(); 217 hPrimitive1=h.getLatest(); 218 hPrimitive2=h.getByVersion(h.getNumVersions()-1); 219 220 Relation rel = new Relation(id, (int) hPrimitive1.getVersion()); 221 222 HistoryRelation hRel = (HistoryRelation) hPrimitive2; 223 224 List<RelationMember> members = new ArrayList<RelationMember>(hRel.getNumMembers()); 225 for (RelationMemberData m : hRel.getMembers()) { 226 OsmPrimitive p = datas.getPrimitiveById(m.getMemberId(), m.getMemberType()); 227 if (p == null) { 228 switch (m.getMemberType()) { 229 case NODE: p = new Node(m.getMemberId()); break; 230 case WAY: p = new Way(m.getMemberId()); break; 231 case RELATION: p = new Relation(m.getMemberId()); break; 232 } 233 datas.addPrimitive(p); 234 } 235 members.add(new RelationMember(m.getRole(), p)); 236 } 237 238 rel.setMembers(members); 239 240 primitive=rel; 241 } 242 243 User user = hPrimitive1.getUser(); 244 245 primitive.setUser(user); 246 247 primitive.setKeys(hPrimitive2.getTags()); 248 249 primitive.put("history", "retrieved using undelete JOSM plugin"); 250 251 primitive.setModified(true); 252 253 datas.addPrimitive(primitive); 254 } 255 256 257 //HistoryBrowserDialogManager.getInstance().show(h); 258 } 259 if (parent>0 && !ids.isEmpty() && ids.iterator().next().getType().equals(OsmPrimitiveType.NODE)) 260 { 261 Way parentWay=(Way)datas.getPrimitiveById(parent, OsmPrimitiveType.WAY); 262 263 parentWay.setNodes(nodes); 264 } 265 } 266 }; 267 Main.worker.submit(r); 268 269 //if (downloadReferrers) { 270 // Main.worker.submit(new DownloadReferrersTask(layer, id, type)); 271 //} 13 MainMenu.addAfter(Main.main.menu.fileMenu, new UndeleteAction(), false, Main.main.menu.updateModified); 272 14 } 273 15 }
Note:
See TracChangeset
for help on using the changeset viewer.