source: josm/trunk/src/org/openstreetmap/josm/actions/MergeSelectionAction.java @ 10552

Last change on this file since 10552 was 10552, checked in by Don-vip, 3 years ago

checkstyle

  • Property svn:eol-style set to native
File size: 3.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions;
3
4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.awt.event.ActionEvent;
8import java.awt.event.KeyEvent;
9import java.util.Collection;
10import java.util.List;
11
12import org.openstreetmap.josm.data.osm.OsmPrimitive;
13import org.openstreetmap.josm.data.osm.visitor.MergeSourceBuildingVisitor;
14import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
15import org.openstreetmap.josm.gui.layer.Layer;
16import org.openstreetmap.josm.gui.layer.OsmDataLayer;
17import org.openstreetmap.josm.gui.util.GuiHelper;
18import org.openstreetmap.josm.tools.ImageProvider;
19import org.openstreetmap.josm.tools.Shortcut;
20
21/**
22 * Merge the currently selected objects into another layer.
23 * @since 1890
24 */
25public class MergeSelectionAction extends AbstractMergeAction {
26
27    /**
28     * Constructs a new {@code MergeSelectionAction}.
29     */
30    public MergeSelectionAction() {
31        super(tr("Merge selection"), "dialogs/mergedown", tr("Merge the currently selected objects into another layer"),
32            Shortcut.registerShortcut("system:mergeselection", tr("Edit: {0}", tr("Merge selection")),
33            KeyEvent.VK_M, Shortcut.CTRL_SHIFT),
34            true /* register */
35        );
36        putValue("help", ht("/Action/MergeSelection"));
37    }
38
39    /**
40     * Merge the currently selected objects into another layer.
41     */
42    public void mergeSelected() {
43        OsmDataLayer editLayer = getLayerManager().getEditLayer();
44        List<Layer> targetLayers = LayerListDialog.getInstance().getModel().getPossibleMergeTargets(editLayer);
45        if (targetLayers.isEmpty()) {
46            warnNoTargetLayersForSourceLayer(editLayer);
47            return;
48        }
49        Layer targetLayer = askTargetLayer(targetLayers);
50        if (targetLayer == null)
51            return;
52        if (editLayer.isUploadDiscouraged() && targetLayer instanceof OsmDataLayer
53                && !((OsmDataLayer) targetLayer).isUploadDiscouraged()
54                && editLayer.data.getAllSelected().size() > 1
55                && warnMergingUploadDiscouragedObjects(targetLayer)) {
56            return;
57        }
58        MergeSourceBuildingVisitor builder = new MergeSourceBuildingVisitor(editLayer.data);
59        ((OsmDataLayer) targetLayer).mergeFrom(builder.build());
60    }
61
62    @Override
63    public void actionPerformed(ActionEvent e) {
64        OsmDataLayer editLayer = getLayerManager().getEditLayer();
65        if (editLayer == null || editLayer.data.selectionEmpty())
66            return;
67        mergeSelected();
68    }
69
70    @Override
71    protected void updateEnabledState() {
72        updateEnabledStateOnCurrentSelection();
73    }
74
75    @Override
76    protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
77        setEnabled(selection != null && !selection.isEmpty());
78    }
79
80    /**
81     * Warns the user about merging too many objects with different upload policies.
82     * @param targetLayer Target layer
83     * @return true if the user wants to cancel, false if they want to continue
84     */
85    public final boolean warnMergingUploadDiscouragedObjects(Layer targetLayer) {
86        return GuiHelper.warnUser(tr("Merging too many objects with different upload policies"),
87                "<html>" +
88                tr("You are about to merge more than 1 object between layers ''{0}'' and ''{1}''.<br /><br />"+
89                        "<b>This is not the recommended way of merging such data</b>.<br />"+
90                        "You should instead check and merge each object, <b>one by one</b>.<br /><br />"+
91                        "Are you sure you want to continue?",
92                        getLayerManager().getEditLayer().getName(), targetLayer.getName(), targetLayer.getName())+
93                "</html>",
94                ImageProvider.get("dialogs", "mergedown"), tr("Ignore this hint and merge anyway"));
95    }
96}
Note: See TracBrowser for help on using the repository browser.