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