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

Last change on this file since 14027 was 14027, checked in by michael2402, 5 months ago

See #16388: New mechanism for plugins to register relation editor actions.

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 memberTable member table
26     * @param memberTableModel member table model
27     * @param layer layer
28     */
29    public EditAction(IRelationEditorActionAccess editorAccess) {
30        super(editorAccess, IRelationEditorUpdateOn.MEMBER_TABLE_SELECTION);
31        putValue(SHORT_DESCRIPTION, tr("Edit the relation the currently selected relation member refers to"));
32        new ImageProvider("dialogs", "edit").getResource().attachImageIcon(this, true);
33        updateEnabledState();
34    }
35
36    @Override
37    protected void updateEnabledState() {
38        setEnabled(editorAccess.getMemberTable().getSelectedRowCount() == 1
39                                && editorAccess.getMemberTableModel()
40                                                .isEditableRelation(editorAccess.getMemberTable().getSelectedRow()));
41    }
42
43    protected Collection<RelationMember> getMembersForCurrentSelection(Relation r) {
44        Collection<RelationMember> members = new HashSet<>();
45        Collection<OsmPrimitive> selection = getLayer().data.getSelected();
46        for (RelationMember member: r.getMembers()) {
47            if (selection.contains(member.getMember())) {
48                members.add(member);
49            }
50        }
51        return members;
52    }
53
54    @Override
55    public void actionPerformed(ActionEvent e) {
56        if (!isEnabled())
57            return;
58        int idx = editorAccess.getMemberTable().getSelectedRow();
59        if (idx < 0)
60            return;
61        OsmPrimitive primitive = editorAccess.getMemberTableModel().getReferredPrimitive(idx);
62        if (!(primitive instanceof Relation))
63            return;
64        Relation r = (Relation) primitive;
65        if (r.isIncomplete())
66            return;
67
68        RelationEditor.getEditor(getLayer(), r, getMembersForCurrentSelection(r)).setVisible(true);
69    }
70}
Note: See TracBrowser for help on using the repository browser.