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.LinkedList;
|
---|
10 |
|
---|
11 | import javax.swing.JOptionPane;
|
---|
12 |
|
---|
13 | import org.openstreetmap.josm.command.Command;
|
---|
14 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
15 | import org.openstreetmap.josm.data.UndoRedoHandler;
|
---|
16 | import org.openstreetmap.josm.data.osm.DataSelectionListener;
|
---|
17 | import org.openstreetmap.josm.data.osm.IPrimitive;
|
---|
18 | import org.openstreetmap.josm.data.osm.OsmData;
|
---|
19 | import org.openstreetmap.josm.data.osm.OsmUtils;
|
---|
20 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
21 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
22 | import org.openstreetmap.josm.gui.Notification;
|
---|
23 | import org.openstreetmap.josm.gui.dialogs.relation.GenericRelationEditor;
|
---|
24 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
25 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
26 | import org.openstreetmap.josm.tools.Utils;
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Add all objects selected in the current dataset after the last member of relation(s).
|
---|
30 | * @since 5799
|
---|
31 | */
|
---|
32 | public class AddSelectionToRelations extends AbstractRelationAction implements DataSelectionListener {
|
---|
33 | /**
|
---|
34 | * Constructs a new <code>AddSelectionToRelation</code>.
|
---|
35 | */
|
---|
36 | public AddSelectionToRelations() {
|
---|
37 | new ImageProvider("dialogs/conflict", "copyendright").getResource().attachImageIcon(this, true);
|
---|
38 | putValue(SHORT_DESCRIPTION, tr("Add all objects selected in the current dataset after the last member"));
|
---|
39 | }
|
---|
40 |
|
---|
41 | @Override
|
---|
42 | public void actionPerformed(ActionEvent e) {
|
---|
43 | Collection<Command> cmds = new LinkedList<>();
|
---|
44 | for (Relation orig : Utils.filteredCollection(relations, Relation.class)) {
|
---|
45 | Command c = GenericRelationEditor.addPrimitivesToRelation(orig, MainApplication.getLayerManager().getActiveDataSet().getSelected());
|
---|
46 | if (c != null) {
|
---|
47 | cmds.add(c);
|
---|
48 | }
|
---|
49 | }
|
---|
50 | if (!cmds.isEmpty()) {
|
---|
51 | UndoRedoHandler.getInstance().add(new SequenceCommand(tr("Add selection to relation"), cmds));
|
---|
52 | new Notification(
|
---|
53 | "<html>"+
|
---|
54 | tr("{0}Add selection to relation{1}: Verify every single relation to avoid damage!", "<strong>", "</strong>")+
|
---|
55 | "</html>")
|
---|
56 | .setIcon(JOptionPane.WARNING_MESSAGE)
|
---|
57 | .show();
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | @Override
|
---|
62 | public void updateEnabledState() {
|
---|
63 | int size = relations.size();
|
---|
64 | putValue(NAME, trn("Add selection to {0} relation", "Add selection to {0} relations", size, size));
|
---|
65 | OsmData<?, ?, ?, ?> ds = MainApplication.getLayerManager().getActiveData();
|
---|
66 | if (ds != null) {
|
---|
67 | selectionChanged(ds.getSelected());
|
---|
68 | } else {
|
---|
69 | setEnabled(false);
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | private void selectionChanged(final Collection<? extends IPrimitive> newSelection) {
|
---|
74 | GuiHelper.runInEDT(() -> setEnabled(newSelection != null && !newSelection.isEmpty()
|
---|
75 | && OsmUtils.isOsmCollectionEditable(relations)));
|
---|
76 | }
|
---|
77 |
|
---|
78 | @Override
|
---|
79 | public void selectionChanged(SelectionChangeEvent event) {
|
---|
80 | selectionChanged(event.getSelection());
|
---|
81 | }
|
---|
82 | }
|
---|