Ticket #18728: 18728.3.patch
File 18728.3.patch, 6.5 KB (added by , 4 years ago) |
---|
-
src/org/openstreetmap/josm/actions/JoinAreasAction.java
48 48 import org.openstreetmap.josm.tools.Logging; 49 49 import org.openstreetmap.josm.tools.Pair; 50 50 import org.openstreetmap.josm.tools.Shortcut; 51 import org.openstreetmap.josm.tools.Stopwatch; 51 52 import org.openstreetmap.josm.tools.UserCancelException; 52 53 import org.openstreetmap.josm.tools.Utils; 53 54 … … 57 58 */ 58 59 public class JoinAreasAction extends JosmAction { 59 60 // This will be used to commit commands and unite them into one large command sequence at the end 61 private final transient LinkedList<Command> executedCmds = new LinkedList<>(); 60 62 private final transient LinkedList<Command> cmds = new LinkedList<>(); 61 private int cmdsCount;62 63 private DataSet ds; 63 64 private final transient List<Relation> addedRelations = new LinkedList<>(); 64 65 private final boolean addUndoRedo; … … 478 479 */ 479 480 @Override 480 481 public void actionPerformed(ActionEvent e) { 482 Stopwatch stopwatch = Stopwatch.createStarted(); 481 483 join(getLayerManager().getEditDataSet().getSelectedWays()); 484 Logging.debug("join areas took {0}", stopwatch.toString()); 482 485 clearFields(); 483 486 } 484 487 485 488 private void clearFields() { 486 489 ds = null; 487 cmdsCount = 0;488 490 cmds.clear(); 489 491 addedRelations.clear(); 490 492 } … … 561 563 } 562 564 commitCommands(tr("Move tags from ways to relations")); 563 565 564 makeCommitsOneAction(marktr("Joined overlapping areas"));566 commitExecuted(); 565 567 566 568 if (result.polygons != null && ds != null) { 567 569 List<Way> allWays = new ArrayList<>(); … … 579 581 } 580 582 } catch (UserCancelException exception) { 581 583 Logging.trace(exception); 582 //revert changes 583 //FIXME: this is dirty hack 584 makeCommitsOneAction(tr("Reverting changes")); 585 if (addUndoRedo) { 586 UndoRedoHandler.getInstance().undo(); 587 // add no-change commands to the stack to remove the half-done commands 588 Way w = ways.iterator().next(); 589 cmds.add(new ChangeCommand(w, w)); 590 cmds.add(new ChangeCommand(w, w)); 591 commitCommands(tr("Reverting changes")); 592 UndoRedoHandler.getInstance().undo(); 584 cmds.clear(); 585 if (!executedCmds.isEmpty()) { 586 // revert all executed commands 587 ds = executedCmds.getFirst().getAffectedDataSet(); 588 ds.beginUpdate(); 589 while (!executedCmds.isEmpty()) { 590 executedCmds.removeLast().undoCommand(); 591 } 592 ds.endUpdate(); 593 593 } 594 594 } 595 595 } … … 895 895 } 896 896 897 897 cmds.clear(); 898 cmdsCount++;899 898 } 900 899 901 900 private void commitCommand(Command c) { 902 if (addUndoRedo) { 903 UndoRedoHandler.getInstance().add(c); 904 } else { 905 c.executeCommand(); 901 c.executeCommand(); 902 executedCmds.add(c); 903 } 904 905 /** 906 * Add all executed commands as one command to the undo stack without executing them again. 907 */ 908 private void commitExecuted() { 909 cmds.clear(); 910 if (addUndoRedo && !executedCmds.isEmpty()) { 911 UndoRedoHandler ur = UndoRedoHandler.getInstance(); 912 if (executedCmds.size() == 1) { 913 ur.add(executedCmds.getFirst(), false); 914 } else { 915 ur.add(new JoinAreaCommand(executedCmds), false); 916 } 906 917 } 918 executedCmds.clear(); 907 919 } 908 920 909 921 /** … … 1435 1447 // This will turn ways so all of them point in the same direction and CombineAction won't bug 1436 1448 // the user about this. 1437 1449 1438 //TODO: ReverseWay and Combine way are really slow and we use them a lot here. This slows down large joins.1439 1450 List<Way> actionWays = new ArrayList<>(ways.size()); 1440 1451 int oldestPos = 0; 1441 1452 Way oldest = ways.get(0).way; … … 1449 1460 if (!way.insideToTheRight) { 1450 1461 ReverseWayResult res = ReverseWayAction.reverseWay(way.way); 1451 1462 commitCommand(res.getReverseCommand()); 1452 cmdsCount++;1453 1463 } 1454 1464 } 1455 1465 … … 1461 1471 throw new JosmRuntimeException("Join areas internal error."); 1462 1472 } 1463 1473 commitCommand(result.b); 1464 cmdsCount++;1465 1474 1466 1475 return result.a; 1467 1476 } … … 1707 1716 commitCommands(marktr("Remove tags from inner ways")); 1708 1717 } 1709 1718 1710 /**1711 * Takes the last cmdsCount actions back and combines them into a single action1712 * (for when the user wants to undo the join action)1713 * @param message The commit message to display1714 */1715 private void makeCommitsOneAction(String message) {1716 cmds.clear();1717 if (addUndoRedo) {1718 UndoRedoHandler ur = UndoRedoHandler.getInstance();1719 List<Command> commands = ur.getUndoCommands();1720 int i = Math.max(commands.size() - cmdsCount, 0);1721 for (; i < commands.size(); i++) {1722 cmds.add(commands.get(i));1723 }1724 ur.undo(cmds.size());1725 }1726 1727 commitCommands(message == null ? marktr("Join Areas Function") : message);1728 cmdsCount = 0;1729 }1730 1731 1719 @Override 1732 1720 protected void updateEnabledState() { 1733 1721 updateEnabledStateOnCurrentSelection(); … … 1737 1725 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) { 1738 1726 updateEnabledStateOnModifiableSelection(selection); 1739 1727 } 1728 1729 private static class JoinAreaCommand extends SequenceCommand { 1730 JoinAreaCommand(Collection<Command> sequenz) { 1731 super(tr("Joined overlapping areas"), sequenz, true); 1732 setSequenceComplete(true); 1733 } 1734 1735 @Override 1736 public void undoCommand() { 1737 getAffectedDataSet().beginUpdate(); 1738 super.undoCommand(); 1739 getAffectedDataSet().endUpdate(); 1740 } 1741 1742 @Override 1743 public boolean executeCommand() { 1744 getAffectedDataSet().beginUpdate(); 1745 boolean rc = super.executeCommand(); 1746 getAffectedDataSet().endUpdate(); 1747 return rc; 1748 } 1749 } 1750 1740 1751 }