source: josm/trunk/src/org/openstreetmap/josm/actions/SelectSharedChildObjectsAction.java

Last change on this file was 18814, checked in by taylor.smock, 11 months ago

Fix #23105: Add action to select shared/common child objects (patch by Woazboat, modified)

Modifications are as follows:

  • Basic test added
  • Icon added (copied and modified from selectall.svg)
  • default methods for IPrimitive.getChildren were moved to the appropriate locations
  • Lint cleanups in modified files

Additional notes:

  • The behavior when only one way is selected is very similar to SelectWayNodesAction from utilsplugin2
File size: 2.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions;
3
4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.awt.event.ActionEvent;
8import java.awt.event.KeyEvent;
9import java.util.Collection;
10import java.util.Collections;
11import java.util.HashSet;
12import java.util.Set;
13
14import org.openstreetmap.josm.data.osm.IPrimitive;
15import org.openstreetmap.josm.data.osm.OsmPrimitive;
16import org.openstreetmap.josm.tools.Shortcut;
17import org.openstreetmap.josm.tools.Utils;
18
19/**
20 * Select child objects (way nodes and relation members) that are shared by all objects in the current selection.
21 * @since 18814
22 */
23public class SelectSharedChildObjectsAction extends JosmAction {
24
25 /**
26 * Create a new SelectSharedChildObjectsAction
27 */
28 public SelectSharedChildObjectsAction() {
29 super(tr("Select shared child objects"),
30 "select_shared_children",
31 tr("Select child objects (way nodes and relation members) that are shared by all objects in the current selection"),
32 Shortcut.registerShortcut("selection:sharedchildobjects",
33 tr("Selection: {0}", tr("Shared Child Objects")),
34 KeyEvent.CHAR_UNDEFINED, Shortcut.NONE),
35 true);
36 setHelpId(ht("/Action/SelectSharedChildObjectsAction"));
37 }
38
39 @Override
40 public void actionPerformed(ActionEvent e) {
41 Collection<? extends IPrimitive> selection = getLayerManager().getActiveData().getSelected();
42 Set<? extends IPrimitive> shared = getSharedChildren(selection);
43 getLayerManager().getActiveData().setSelected(shared);
44 }
45
46 @Override
47 protected void updateEnabledState() {
48 updateEnabledStateOnCurrentSelection(true);
49 }
50
51 @Override
52 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
53 setEnabled(!Utils.isEmpty(selection));
54 }
55
56 /**
57 * Get the shared children for a selection
58 * @param selection The selection to get shared children for
59 * @return The shared children
60 */
61 private static Set<? extends IPrimitive> getSharedChildren(Collection<? extends IPrimitive> selection) {
62 Set<IPrimitive> sharedChildObjects = new HashSet<>(selection.stream()
63 .findAny().map(IPrimitive::getChildren).orElse(Collections.emptyList()));
64
65 for (IPrimitive p : selection) {
66 if (sharedChildObjects.isEmpty())
67 break;
68
69 sharedChildObjects.retainAll(p.getChildren());
70 }
71
72 return sharedChildObjects;
73 }
74}
Note: See TracBrowser for help on using the repository browser.