Changeset 21625 in osm for applications/editors/josm/plugins/reverter/src
- Timestamp:
- 2010-06-09T18:14:18+02:00 (15 years ago)
- Location:
- applications/editors/josm/plugins/reverter/src/reverter
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/reverter/src/reverter/ChangesetReverter.java
r21201 r21625 2 2 3 3 import java.util.HashMap; 4 import java.util.HashSet; 4 5 import java.util.Iterator; 5 6 import java.util.LinkedList; 6 7 import java.util.List; 7 8 import java.util.Map; 9 import java.util.Set; 8 10 9 11 import org.openstreetmap.josm.Main; … … 15 17 import org.openstreetmap.josm.data.osm.Node; 16 18 import org.openstreetmap.josm.data.osm.OsmPrimitive; 19 import org.openstreetmap.josm.data.osm.OsmPrimitiveType; 17 20 import org.openstreetmap.josm.data.osm.PrimitiveId; 18 21 import org.openstreetmap.josm.data.osm.Relation; … … 22 25 import org.openstreetmap.josm.data.osm.ChangesetDataSet.ChangesetModificationType; 23 26 import org.openstreetmap.josm.data.osm.history.HistoryOsmPrimitive; 24 import org.openstreetmap.josm.gui.layer.OsmDataLayer; 27 import org.openstreetmap.josm.data.osm.history.HistoryRelation; 28 import org.openstreetmap.josm.data.osm.history.HistoryWay; 29 import org.openstreetmap.josm.data.osm.history.RelationMember; 25 30 import org.openstreetmap.josm.gui.progress.NullProgressMonitor; 26 31 import org.openstreetmap.josm.gui.progress.ProgressMonitor; 32 import org.openstreetmap.josm.io.MultiFetchServerObjectReader; 27 33 import org.openstreetmap.josm.io.OsmServerChangesetReader; 28 34 import org.openstreetmap.josm.io.OsmTransferException; 29 35 30 36 public class ChangesetReverter { 31 private int changesetId; 32 private Changeset changeset; 33 private ChangesetDataSet osmchange; 37 public final int changesetId; 38 public final Changeset changeset; 39 40 private final DataSet ds; 41 private final ChangesetDataSet osmchange; 42 43 private final HashSet<PrimitiveId> missing = new HashSet<PrimitiveId>(); 44 private LinkedList<Command> cmds; 45 34 46 static class MyCsDataset { 35 47 public HashMap<PrimitiveId,Integer> created = new HashMap<PrimitiveId,Integer>(); … … 65 77 } 66 78 67 public ChangesetReverter(int changesetId) { 79 private void AddIfMissing(PrimitiveId id) { 80 if (ds.getPrimitiveById(id) == null) { 81 missing.add(id); 82 } 83 } 84 private void AddMissingIds(Set<HistoryOsmPrimitive> primitives) { 85 for (HistoryOsmPrimitive p : primitives) { 86 AddIfMissing(new SimplePrimitiveId(p.getId(),p.getType())); 87 if (p.getType() == OsmPrimitiveType.WAY) { 88 for (long nd : ((HistoryWay)p).getNodes()) { 89 AddIfMissing(new SimplePrimitiveId(nd,OsmPrimitiveType.NODE)); 90 } 91 } 92 if (p.getType() == OsmPrimitiveType.RELATION) { 93 for (RelationMember member : ((HistoryRelation)p).getMembers()) { 94 AddIfMissing(new SimplePrimitiveId(member.getPrimitiveId(), 95 member.getPrimitiveType())); 96 } 97 } 98 } 99 } 100 private void CheckMissing() { 101 /* Create list of objects that created or modified my the changeset but 102 * doesn't present in the current dataset. 103 */ 104 missing.clear(); 105 AddMissingIds(osmchange. 106 getPrimitivesByModificationType(ChangesetModificationType.CREATED)); 107 AddMissingIds(osmchange. 108 getPrimitivesByModificationType(ChangesetModificationType.UPDATED)); 109 } 110 111 public ChangesetReverter(int changesetId) throws OsmTransferException { 68 112 this.changesetId = changesetId; 69 try { 70 OsmServerChangesetReader csr = new OsmServerChangesetReader(); 71 changeset = csr.readChangeset(changesetId, NullProgressMonitor.INSTANCE); 72 osmchange = csr.downloadChangeset(changesetId, NullProgressMonitor.INSTANCE); 73 } catch (OsmTransferException e) { 74 e.printStackTrace(); 75 } 76 } 77 LinkedList<Command> cmds; 113 this.ds = Main.main.getCurrentDataSet(); 114 115 OsmServerChangesetReader csr = new OsmServerChangesetReader(); 116 changeset = csr.readChangeset(changesetId, NullProgressMonitor.INSTANCE); 117 osmchange = csr.downloadChangeset(changesetId, NullProgressMonitor.INSTANCE); 118 CheckMissing(); 119 } 78 120 79 121 public void RevertChangeset(ProgressMonitor progressMonitor) throws OsmTransferException { 80 final OsmDataLayer layer = Main.main.getEditLayer();81 final DataSet ds = layer.data;82 122 final MyCsDataset cds = new MyCsDataset(osmchange); 83 123 final OsmServerMultiObjectReader rdr = new OsmServerMultiObjectReader(); … … 90 130 rdr.ReadObject(entry.getKey().getUniqueId(), entry.getValue()-1, entry.getKey().getType(), 91 131 progressMonitor.createSubTaskMonitor(1, true)); 132 if (progressMonitor.isCancelled()) return; 92 133 } 93 134 for (Map.Entry<PrimitiveId,Integer> entry : cds.deleted.entrySet()) { 94 135 rdr.ReadObject(entry.getKey().getUniqueId(), entry.getValue()-1, entry.getKey().getType(), 95 136 progressMonitor.createSubTaskMonitor(1, true)); 137 if (progressMonitor.isCancelled()) return; 96 138 } 97 139 final DataSet nds = rdr.parseOsm(progressMonitor.createSubTaskMonitor(1, true)); … … 126 168 } 127 169 } 170 171 public void DownloadMissingPrimitives(ProgressMonitor monitor) throws OsmTransferException { 172 MultiFetchServerObjectReader rdr = new MultiFetchServerObjectReader(); 173 for (PrimitiveId id : missing) { 174 switch (id.getType()) { 175 case NODE: 176 rdr.append(new Node(id.getUniqueId())); 177 break; 178 case WAY: 179 rdr.append(new Way(id.getUniqueId())); 180 break; 181 case RELATION: 182 rdr.append(new Relation(id.getUniqueId())); 183 break; 184 default: throw new AssertionError(); 185 } 186 } 187 DataSet from = rdr.parseOsm(monitor); 188 for (Node n : from.getNodes()) { 189 if (ds.getPrimitiveById(n) == null) { 190 ds.addPrimitive(new Node(n)); 191 } 192 } 193 for (Way w : from.getWays()) { 194 if (ds.getPrimitiveById(w) == null) { 195 Way nw = new Way(w.getId()); 196 ds.addPrimitive(nw); 197 nw.load(w.save()); 198 } 199 } 200 final LinkedList<Relation> rels = new LinkedList<Relation>(); 201 for (Relation r : from.getRelations()) { 202 if (ds.getPrimitiveById(r) == null) { 203 ds.addPrimitive(new Relation(r.getId())); 204 rels.add(r); 205 } 206 } 207 for (Relation r : rels) { 208 ((Relation)ds.getPrimitiveById(r)).load(r.save()); 209 } 210 missing.clear(); 211 } 212 128 213 public List<Command> getCommands() { 129 214 return cmds; 130 215 } 216 public Set<PrimitiveId> getMissingObjects() { 217 return missing; 218 } 131 219 } -
applications/editors/josm/plugins/reverter/src/reverter/RevertChangesetAction.java
r21237 r21625 7 7 import java.util.List; 8 8 9 import javax.swing.JOptionPane; 10 9 11 import org.openstreetmap.josm.Main; 10 12 import org.openstreetmap.josm.actions.JosmAction; … … 12 14 import org.openstreetmap.josm.command.SequenceCommand; 13 15 import org.openstreetmap.josm.gui.PleaseWaitRunnable; 16 import org.openstreetmap.josm.gui.progress.PleaseWaitProgressMonitor; 14 17 import org.openstreetmap.josm.gui.progress.ProgressMonitor; 15 18 import org.openstreetmap.josm.io.OsmTransferException; … … 33 36 setEnabled(getCurrentDataSet() != null); 34 37 } 35 38 36 39 public void actionPerformed(ActionEvent arg0) { 37 40 if (getCurrentDataSet() == null) … … 44 47 Main.worker.submit(new PleaseWaitRunnable(tr("Reverting...")) { 45 48 @Override 46 protected void realRun() { 47 try { 48 ChangesetReverter rev = new ChangesetReverter(changesetId); 49 rev.RevertChangeset(progressMonitor.createSubTaskMonitor(ProgressMonitor.ALL_TICKS, true)); 50 List<Command> cmds = rev.getCommands(); 51 Command cmd = new SequenceCommand(tr("Revert changeset #{0}",changesetId),cmds); 52 Main.main.undoRedo.add(cmd); 53 } catch (OsmTransferException e) { 54 e.printStackTrace(); 55 } finally { 49 protected void realRun() throws OsmTransferException { 50 ChangesetReverter rev = new ChangesetReverter(changesetId); 51 if (!rev.getMissingObjects().isEmpty()) 52 { 53 if (JOptionPane.showConfirmDialog(Main.parent, 54 tr("This changeset have objects outside the downloaded area.\n" + 55 "It is needed to download them before reverting. Do you want to continue?"), 56 tr("Confirm"), JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) { 57 final PleaseWaitProgressMonitor monitor = 58 new PleaseWaitProgressMonitor(tr("Fetching missing primitives")); 59 try { 60 rev.DownloadMissingPrimitives(monitor); 61 } finally { 62 monitor.close(); 63 } 64 } else { 65 return; 66 } 56 67 } 68 rev.RevertChangeset(progressMonitor.createSubTaskMonitor(ProgressMonitor.ALL_TICKS, true)); 69 if (progressMonitor.isCancelled()) return; 70 List<Command> cmds = rev.getCommands(); 71 Command cmd = new SequenceCommand(tr("Revert changeset #{0}",changesetId),cmds); 72 Main.main.undoRedo.add(cmd); 57 73 } 58 74
Note:
See TracChangeset
for help on using the changeset viewer.