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

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

fix #14613 - Special HTML characters not escaped in GUI error messages

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