source: josm/trunk/src/org/openstreetmap/josm/actions/relation/AddSelectionToRelations.java @ 14134

Last change on this file since 14134 was 14134, checked in by Don-vip, 4 months ago

see #15229 - deprecate Main*.undoRedo - make UndoRedoHandler a singleton

  • Property svn:eol-style set to native
File size: 3.2 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.LinkedList;
10
11import javax.swing.JOptionPane;
12
13import org.openstreetmap.josm.command.Command;
14import org.openstreetmap.josm.command.SequenceCommand;
15import org.openstreetmap.josm.data.UndoRedoHandler;
16import org.openstreetmap.josm.data.osm.DataSelectionListener;
17import org.openstreetmap.josm.data.osm.IPrimitive;
18import org.openstreetmap.josm.data.osm.OsmData;
19import org.openstreetmap.josm.data.osm.OsmUtils;
20import org.openstreetmap.josm.data.osm.Relation;
21import org.openstreetmap.josm.gui.MainApplication;
22import org.openstreetmap.josm.gui.Notification;
23import org.openstreetmap.josm.gui.dialogs.relation.GenericRelationEditor;
24import org.openstreetmap.josm.gui.util.GuiHelper;
25import org.openstreetmap.josm.tools.ImageProvider;
26import 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 */
32public 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}
Note: See TracBrowser for help on using the repository browser.