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 org.openstreetmap.josm.Main;
|
---|
12 | import org.openstreetmap.josm.command.Command;
|
---|
13 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
14 | import org.openstreetmap.josm.data.SelectionChangedListener;
|
---|
15 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
16 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
17 | import org.openstreetmap.josm.gui.dialogs.relation.GenericRelationEditor;
|
---|
18 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
19 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * Add all objects selected in the current dataset after the last member of relation(s).
|
---|
23 | * @since 5799
|
---|
24 | */
|
---|
25 | public class AddSelectionToRelations extends AbstractRelationAction implements SelectionChangedListener {
|
---|
26 | /**
|
---|
27 | * Constructs a new <code>AddSelectionToRelation</code>.
|
---|
28 | */
|
---|
29 | public AddSelectionToRelations() {
|
---|
30 | putValue(SMALL_ICON, ImageProvider.get("dialogs/conflict", "copyendright"));
|
---|
31 | putValue(SHORT_DESCRIPTION, tr("Add all objects selected in the current dataset after the last member"));
|
---|
32 | }
|
---|
33 |
|
---|
34 | @Override
|
---|
35 | public void actionPerformed(ActionEvent e) {
|
---|
36 | Collection<Command> cmds = new LinkedList<>();
|
---|
37 | for (Relation orig : relations) {
|
---|
38 | Command c = GenericRelationEditor.addPrimitivesToRelation(orig, Main.main.getCurrentDataSet().getSelected());
|
---|
39 | if (c != null) {
|
---|
40 | cmds.add(c);
|
---|
41 | }
|
---|
42 | }
|
---|
43 | if (!cmds.isEmpty()) {
|
---|
44 | Main.main.undoRedo.add(new SequenceCommand(tr("Add selection to relation"), cmds));
|
---|
45 | }
|
---|
46 | }
|
---|
47 |
|
---|
48 | @Override
|
---|
49 | public void updateEnabledState() {
|
---|
50 | putValue(NAME, trn("Add selection to {0} relation", "Add selection to {0} relations",
|
---|
51 | relations.size(), relations.size()));
|
---|
52 | }
|
---|
53 |
|
---|
54 | @Override
|
---|
55 | public void selectionChanged(final Collection<? extends OsmPrimitive> newSelection) {
|
---|
56 | GuiHelper.runInEDT(new Runnable() {
|
---|
57 | @Override
|
---|
58 | public void run() {
|
---|
59 | setEnabled(newSelection != null && !newSelection.isEmpty());
|
---|
60 | }
|
---|
61 | });
|
---|
62 | }
|
---|
63 | }
|
---|