source: josm/trunk/src/org/openstreetmap/josm/gui/dialogs/relation/actions/EditAction.java @ 14030

Last change on this file since 14030 was 14030, checked in by michael2402, 8 months ago

See #16388: Fix Checkstyle / Test issues.

File size: 2.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.dialogs.relation.actions;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.event.ActionEvent;
7import java.util.Collection;
8import java.util.HashSet;
9
10import org.openstreetmap.josm.data.osm.OsmPrimitive;
11import org.openstreetmap.josm.data.osm.Relation;
12import org.openstreetmap.josm.data.osm.RelationMember;
13import org.openstreetmap.josm.gui.dialogs.relation.RelationEditor;
14import org.openstreetmap.josm.tools.ImageProvider;
15
16/**
17 * Action for editing the currently selected relation.
18 * @since 9496
19 */
20public class EditAction extends AbstractRelationEditorAction {
21    private static final long serialVersionUID = 1L;
22
23    /**
24     * Constructs a new {@code EditAction}.
25     * @param editorAccess An interface to access the relation editor contents.
26     */
27    public EditAction(IRelationEditorActionAccess editorAccess) {
28        super(editorAccess, IRelationEditorUpdateOn.MEMBER_TABLE_SELECTION);
29        putValue(SHORT_DESCRIPTION, tr("Edit the relation the currently selected relation member refers to"));
30        new ImageProvider("dialogs", "edit").getResource().attachImageIcon(this, true);
31        updateEnabledState();
32    }
33
34    @Override
35    protected void updateEnabledState() {
36        setEnabled(editorAccess.getMemberTable().getSelectedRowCount() == 1
37                && editorAccess.getMemberTableModel()
38                        .isEditableRelation(editorAccess.getMemberTable().getSelectedRow()));
39    }
40
41    protected Collection<RelationMember> getMembersForCurrentSelection(Relation r) {
42        Collection<RelationMember> members = new HashSet<>();
43        Collection<OsmPrimitive> selection = getLayer().data.getSelected();
44        for (RelationMember member: r.getMembers()) {
45            if (selection.contains(member.getMember())) {
46                members.add(member);
47            }
48        }
49        return members;
50    }
51
52    @Override
53    public void actionPerformed(ActionEvent e) {
54        if (!isEnabled())
55            return;
56        int idx = editorAccess.getMemberTable().getSelectedRow();
57        if (idx < 0)
58            return;
59        OsmPrimitive primitive = editorAccess.getMemberTableModel().getReferredPrimitive(idx);
60        if (!(primitive instanceof Relation))
61            return;
62        Relation r = (Relation) primitive;
63        if (r.isIncomplete())
64            return;
65
66        RelationEditor.getEditor(getLayer(), r, getMembersForCurrentSelection(r)).setVisible(true);
67    }
68}
Note: See TracBrowser for help on using the repository browser.