1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.plugins.utilsplugin2.selection;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 |
|
---|
7 | import java.awt.event.ActionEvent;
|
---|
8 | import java.awt.event.KeyEvent;
|
---|
9 | import java.util.Collection;
|
---|
10 | import java.util.HashSet;
|
---|
11 | import java.util.Set;
|
---|
12 |
|
---|
13 | import org.openstreetmap.josm.actions.JosmAction;
|
---|
14 | import org.openstreetmap.josm.command.Command;
|
---|
15 | import org.openstreetmap.josm.data.UndoRedoHandler;
|
---|
16 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
17 | import org.openstreetmap.josm.data.osm.Node;
|
---|
18 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
19 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * Select last modified nodes.
|
---|
23 | */
|
---|
24 | public class SelectModNodesAction extends JosmAction {
|
---|
25 | private Command lastCmd;
|
---|
26 |
|
---|
27 | public SelectModNodesAction() {
|
---|
28 | super(tr("Select last modified nodes"), "selmodnodes",
|
---|
29 | tr("Select last modified nodes"),
|
---|
30 | Shortcut.registerShortcut("tools:selmodnodes", tr("Selection: {0}", tr("Select last modified nodes")),
|
---|
31 | KeyEvent.VK_Z, Shortcut.SHIFT), true);
|
---|
32 | putValue("help", ht("/Action/SelectLastModifiedNodes"));
|
---|
33 | }
|
---|
34 |
|
---|
35 | @Override
|
---|
36 | public void actionPerformed(ActionEvent e) {
|
---|
37 | DataSet ds = getLayerManager().getEditDataSet();
|
---|
38 | if (ds != null) {
|
---|
39 | Collection<OsmPrimitive> selection = ds.getSelected();
|
---|
40 | ds.clearSelection(ds.getSelectedWays());
|
---|
41 | Command cmd = null;
|
---|
42 |
|
---|
43 | int num = UndoRedoHandler.getInstance().getUndoCommands().size();
|
---|
44 | if (num == 0) return;
|
---|
45 | int k = 0, idx;
|
---|
46 | // check if executed again, we cycle through all available commands
|
---|
47 | if (lastCmd != null && !selection.isEmpty()) {
|
---|
48 | idx = UndoRedoHandler.getInstance().getUndoCommands().lastIndexOf(lastCmd);
|
---|
49 | } else {
|
---|
50 | idx = num;
|
---|
51 | }
|
---|
52 |
|
---|
53 | Set<Node> nodes = new HashSet<>(10);
|
---|
54 | do { // select next history element
|
---|
55 | if (idx > 0) idx--; else idx = num-1;
|
---|
56 | cmd = UndoRedoHandler.getInstance().getUndoCommands().get(idx);
|
---|
57 | if (cmd.getAffectedDataSet() == ds) {
|
---|
58 | Collection<? extends OsmPrimitive> pp = cmd.getParticipatingPrimitives();
|
---|
59 | nodes.clear();
|
---|
60 | for (OsmPrimitive p : pp) { // find all affected ways
|
---|
61 | if (p instanceof Node && !p.isDeleted() && !p.isDisabled())
|
---|
62 | nodes.add((Node) p);
|
---|
63 | }
|
---|
64 | if (!nodes.isEmpty() && !ds.getSelectedNodes().containsAll(nodes)) {
|
---|
65 | ds.setSelected(nodes);
|
---|
66 | lastCmd = cmd; // remember last used command and last selection
|
---|
67 | return;
|
---|
68 | }
|
---|
69 | }
|
---|
70 | k++;
|
---|
71 | } while (k < num); // try to find previous command if this affects nothing
|
---|
72 | lastCmd = null;
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | @Override
|
---|
77 | protected boolean listenToSelectionChange() {
|
---|
78 | return false;
|
---|
79 | }
|
---|
80 |
|
---|
81 | @Override
|
---|
82 | protected void updateEnabledState() {
|
---|
83 | boolean hasEditLayer = getLayerManager().getEditDataSet() != null;
|
---|
84 | if (!hasEditLayer) {
|
---|
85 | lastCmd = null;
|
---|
86 | }
|
---|
87 | setEnabled(hasEditLayer);
|
---|
88 | }
|
---|
89 | }
|
---|