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.LinkedHashSet;
|
---|
11 | import java.util.List;
|
---|
12 | import java.util.Set;
|
---|
13 |
|
---|
14 | import org.openstreetmap.josm.actions.JosmAction;
|
---|
15 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
16 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
17 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * Use selection history to restore previous selection
|
---|
21 | */
|
---|
22 | public class UndoSelectionAction extends JosmAction {
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * Create a new {@link UndoSelectionAction}
|
---|
26 | */
|
---|
27 | public UndoSelectionAction() {
|
---|
28 | super(tr("Undo selection"), "undoselection",
|
---|
29 | tr("Reselect last added object or selection form history"),
|
---|
30 | Shortcut.registerShortcut("tools:undoselection", tr("Selection: {0}", tr("Undo selection")),
|
---|
31 | KeyEvent.VK_Z, Shortcut.CTRL_SHIFT), true);
|
---|
32 | putValue("help", ht("/Action/UndoSelection"));
|
---|
33 | }
|
---|
34 |
|
---|
35 | private transient Collection<OsmPrimitive> lastSel;
|
---|
36 | private int index;
|
---|
37 |
|
---|
38 | @Override
|
---|
39 | public void actionPerformed(ActionEvent e) {
|
---|
40 | DataSet ds = getLayerManager().getActiveDataSet();
|
---|
41 | if (ds != null) {
|
---|
42 | List<Collection<? extends OsmPrimitive>> history = ds.getSelectionHistory();
|
---|
43 | if (history == null || history.isEmpty()) return; // empty history
|
---|
44 | if (lastSel != null) {
|
---|
45 | Collection<OsmPrimitive> selection = ds.getSelected();
|
---|
46 | if (lastSel.size() == selection.size() && selection.containsAll(lastSel)) {
|
---|
47 | // repeated action
|
---|
48 | } else {
|
---|
49 | index = -1;
|
---|
50 | }
|
---|
51 | }
|
---|
52 |
|
---|
53 | int num = history.size();
|
---|
54 | int k = 0;
|
---|
55 |
|
---|
56 | Set<OsmPrimitive> newSel = new LinkedHashSet<>();
|
---|
57 | while (k < num) {
|
---|
58 | if (index+1 < history.size()) index++; else index = 0;
|
---|
59 | Collection<? extends OsmPrimitive> histsel = history.get(index);
|
---|
60 | // remove deleted entities from selection
|
---|
61 | newSel.clear();
|
---|
62 | newSel.addAll(histsel);
|
---|
63 | newSel.removeIf(p -> p == null || p.isDeleted() || p.isDisabled());
|
---|
64 | k++;
|
---|
65 | if (!newSel.isEmpty()) {
|
---|
66 | Collection<OsmPrimitive> oldSel = ds.getSelected();
|
---|
67 | if (oldSel.size() == newSel.size() && newSel.containsAll(oldSel)) {
|
---|
68 | // ignore no-change selection
|
---|
69 | continue;
|
---|
70 | }
|
---|
71 | break;
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | // set new selection (is added to history)
|
---|
76 | ds.setSelected(newSel);
|
---|
77 | lastSel = ds.getSelected();
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | @Override
|
---|
82 | protected void updateEnabledState() {
|
---|
83 | lastSel = null;
|
---|
84 | index = -1;
|
---|
85 | setEnabled(getLayerManager().getActiveDataSet() != null);
|
---|
86 | }
|
---|
87 |
|
---|
88 | @Override
|
---|
89 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
|
---|
90 | setEnabled(getLayerManager().getActiveDataSet() != null);
|
---|
91 | }
|
---|
92 | }
|
---|