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.Point;
|
---|
8 | import java.awt.event.ActionEvent;
|
---|
9 | import java.awt.event.KeyEvent;
|
---|
10 | import java.util.Collection;
|
---|
11 | import java.util.HashSet;
|
---|
12 | import java.util.Set;
|
---|
13 |
|
---|
14 | import javax.swing.JOptionPane;
|
---|
15 |
|
---|
16 | import org.openstreetmap.josm.actions.JosmAction;
|
---|
17 | import org.openstreetmap.josm.actions.SelectByInternalPointAction;
|
---|
18 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
19 | import org.openstreetmap.josm.data.osm.Node;
|
---|
20 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
21 | import org.openstreetmap.josm.data.osm.Way;
|
---|
22 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
23 | import org.openstreetmap.josm.gui.MapView;
|
---|
24 | import org.openstreetmap.josm.gui.Notification;
|
---|
25 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Extends current selection by selecting nodes on all touched ways
|
---|
29 | */
|
---|
30 | public class SelectBoundaryAction extends JosmAction {
|
---|
31 | private Way lastUsedStartingWay; //used for repeated calls
|
---|
32 | private boolean lastUsedLeft;
|
---|
33 |
|
---|
34 | public SelectBoundaryAction() {
|
---|
35 | super(tr("Area boundary [testing]"), "selboundary", tr("Select relation or all ways that forms area boundary"),
|
---|
36 | Shortcut.registerShortcut("tools:selboundary", tr("Selection: {0}", tr("Area boundary [testing]")),
|
---|
37 | KeyEvent.VK_SLASH, Shortcut.SHIFT), true);
|
---|
38 | putValue("help", ht("/Action/SelectAreaBoundary"));
|
---|
39 | }
|
---|
40 |
|
---|
41 | @Override
|
---|
42 | public void actionPerformed(ActionEvent e) {
|
---|
43 | DataSet ds = getLayerManager().getActiveDataSet();
|
---|
44 | if (ds != null) {
|
---|
45 | Collection<Way> selectedWays = ds.getSelectedWays();
|
---|
46 | Collection<Node> selectedNodes = ds.getSelectedNodes();
|
---|
47 |
|
---|
48 | Set<Way> newWays = new HashSet<>();
|
---|
49 |
|
---|
50 | Way w = null;
|
---|
51 |
|
---|
52 | if (selectedWays.isEmpty()) {
|
---|
53 | if (selectedNodes.size() == 1) {
|
---|
54 | for (OsmPrimitive p : selectedNodes.iterator().next().getReferrers()) {
|
---|
55 | if (p instanceof Way && p.isSelectable()) {
|
---|
56 | w = (Way) p;
|
---|
57 | break;
|
---|
58 | }
|
---|
59 | }
|
---|
60 | } else if (MainApplication.isDisplayingMapView()) {
|
---|
61 | MapView mapView = MainApplication.getMap().mapView;
|
---|
62 | Point p = mapView.getMousePosition();
|
---|
63 | if (p != null) {
|
---|
64 | SelectByInternalPointAction.performSelection(mapView.getEastNorth(p.x, p.y), false, false);
|
---|
65 | }
|
---|
66 | return;
|
---|
67 | }
|
---|
68 | } else if (selectedWays.size() == 1) {
|
---|
69 | w = selectedWays.iterator().next();
|
---|
70 | } else if (selectedWays.contains(lastUsedStartingWay)) {
|
---|
71 | w = lastUsedStartingWay; //repeated call for selected way
|
---|
72 | lastUsedLeft = !lastUsedLeft;
|
---|
73 | }
|
---|
74 |
|
---|
75 | if (w == null) return; //no starting way found
|
---|
76 | if (!w.isSelectable()) return;
|
---|
77 | if (w.isClosed()) return;
|
---|
78 | if (w.getNodesCount() < 2) return;
|
---|
79 |
|
---|
80 | newWays.add(w);
|
---|
81 | lastUsedStartingWay = w;
|
---|
82 |
|
---|
83 | // try going left at each turn
|
---|
84 | if (!NodeWayUtils.addAreaBoundary(w, newWays, lastUsedLeft)) {
|
---|
85 | NodeWayUtils.addAreaBoundary(w, newWays, !lastUsedLeft); // try going right at each turn
|
---|
86 | }
|
---|
87 |
|
---|
88 | if (!newWays.isEmpty()) {
|
---|
89 | ds.setSelected(newWays);
|
---|
90 | } else {
|
---|
91 | new Notification(tr("Nothing found. Please select way that is a part of some polygon formed by connected ways"))
|
---|
92 | .setIcon(JOptionPane.WARNING_MESSAGE).show();
|
---|
93 | }
|
---|
94 | }
|
---|
95 | }
|
---|
96 |
|
---|
97 | @Override
|
---|
98 | protected void updateEnabledState() {
|
---|
99 | setEnabled(getLayerManager().getActiveDataSet() != null);
|
---|
100 | }
|
---|
101 |
|
---|
102 | @Override
|
---|
103 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
|
---|
104 | boolean hasSel = selection != null && !selection.isEmpty();
|
---|
105 | if (!hasSel) {
|
---|
106 | lastUsedStartingWay = null;
|
---|
107 | }
|
---|
108 | setEnabled(hasSel);
|
---|
109 | }
|
---|
110 | }
|
---|