source: osm/applications/editors/josm/plugins/utilsplugin2/src/org/openstreetmap/josm/plugins/utilsplugin2/selection/SelectBoundaryAction.java

Last change on this file was 35673, checked in by GerdP, 3 years ago

see #17184: Memory Leaks

  • clear references to command instances or OSM objects to ease finding of real memory leaks
File size: 4.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.plugins.utilsplugin2.selection;
3
4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.awt.Point;
8import java.awt.event.ActionEvent;
9import java.awt.event.KeyEvent;
10import java.util.Collection;
11import java.util.HashSet;
12import java.util.Set;
13
14import javax.swing.JOptionPane;
15
16import org.openstreetmap.josm.actions.JosmAction;
17import org.openstreetmap.josm.actions.SelectByInternalPointAction;
18import org.openstreetmap.josm.data.osm.DataSet;
19import org.openstreetmap.josm.data.osm.Node;
20import org.openstreetmap.josm.data.osm.OsmPrimitive;
21import org.openstreetmap.josm.data.osm.Way;
22import org.openstreetmap.josm.gui.MainApplication;
23import org.openstreetmap.josm.gui.MapView;
24import org.openstreetmap.josm.gui.Notification;
25import org.openstreetmap.josm.tools.Shortcut;
26
27/**
28 * Extends current selection by selecting nodes on all touched ways
29 */
30public 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}
Note: See TracBrowser for help on using the repository browser.