| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.actions;
|
|---|
| 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.Collections;
|
|---|
| 11 | import java.util.HashSet;
|
|---|
| 12 | import java.util.Set;
|
|---|
| 13 |
|
|---|
| 14 | import org.openstreetmap.josm.data.osm.IPrimitive;
|
|---|
| 15 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
|---|
| 16 | import org.openstreetmap.josm.tools.Shortcut;
|
|---|
| 17 | import 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 | */
|
|---|
| 23 | public 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 | }
|
|---|