source: josm/trunk/src/org/openstreetmap/josm/actions/relation/EditRelationAction.java @ 12841

Last change on this file since 12841 was 12841, checked in by bastiK, 6 weeks ago

see #15229 - fix deprecations caused by [12840]

  • Property svn:eol-style set to native
File size: 3.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions.relation;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5import static org.openstreetmap.josm.tools.I18n.trn;
6
7import java.awt.event.ActionEvent;
8import java.util.Collection;
9import java.util.HashSet;
10import java.util.Set;
11
12import javax.swing.JOptionPane;
13
14import org.openstreetmap.josm.Main;
15import org.openstreetmap.josm.data.osm.OsmPrimitive;
16import org.openstreetmap.josm.data.osm.Relation;
17import org.openstreetmap.josm.data.osm.RelationMember;
18import org.openstreetmap.josm.gui.MainApplication;
19import org.openstreetmap.josm.gui.dialogs.relation.RelationEditor;
20import org.openstreetmap.josm.gui.layer.OsmDataLayer;
21import org.openstreetmap.josm.tools.ImageProvider;
22
23/**
24 * The action for editing a relation.
25 * @since 5793
26 */
27public class EditRelationAction extends AbstractRelationAction {
28
29    /**
30     * Constructs a new <code>EditRelationAction</code>.
31     */
32    public EditRelationAction() {
33        putValue(NAME, tr("Edit"));
34        putValue(SHORT_DESCRIPTION, tr("Call relation editor for selected relation"));
35        new ImageProvider("dialogs", "edit").getResource().attachImageIcon(this, true);
36    }
37
38    /**
39     * Returns the set of currently selected relation members for the given relation.
40     * @param r The relation to inspect
41     * @return The set of currently selected relation members for the given relation.
42     */
43    public static Set<RelationMember> getMembersForCurrentSelection(Relation r) {
44        Set<RelationMember> members = new HashSet<>();
45        if (MainApplication.isDisplayingMapView()) {
46            OsmDataLayer editLayer = MainApplication.getLayerManager().getEditLayer();
47            if (editLayer != null && editLayer.data != null) {
48                Collection<OsmPrimitive> selection = editLayer.data.getSelected();
49                for (RelationMember member: r.getMembers()) {
50                    if (selection.contains(member.getMember())) {
51                        members.add(member);
52                    }
53                }
54            }
55        }
56        return members;
57    }
58
59    /**
60     * Launches relation editor for the given relation.
61     * @param toEdit The relation to edit
62     */
63    public static void launchEditor(Relation toEdit) {
64        if (toEdit == null || toEdit.isDeleted() || !MainApplication.isDisplayingMapView()) return;
65        RelationEditor.getEditor(MainApplication.getLayerManager().getEditLayer(), toEdit,
66                getMembersForCurrentSelection(toEdit)).setVisible(true);
67    }
68
69    @Override
70    public void actionPerformed(ActionEvent e) {
71        if (!isEnabled() || relations.isEmpty()) return;
72        if (relations.size() > Main.pref.getInt("warn.open.maxrelations", 5) &&
73            /* I18N english text for value 1 makes no real sense, never called for values <= maxrel (usually 5) */
74            JOptionPane.OK_OPTION != JOptionPane.showConfirmDialog(Main.parent,
75                    "<html>"+trn("You are about to open <b>{0}</b> different relation editor simultaneously.<br/>Do you want to continue?",
76                            "You are about to open <b>{0}</b> different relation editors simultaneously.<br/>Do you want to continue?",
77                            relations.size(), relations.size())+"</html>",
78                    tr("Confirmation"), JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE)) {
79            return;
80        }
81        for (Relation r : relations) {
82            launchEditor(r);
83        }
84    }
85
86    @Override
87    protected void updateEnabledState() {
88        boolean enabled = false;
89        for (Relation r : relations) {
90            if (!r.isDeleted()) {
91                enabled = true;
92                break;
93            }
94        }
95        setEnabled(enabled);
96    }
97}
Note: See TracBrowser for help on using the repository browser.