[2512] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.actions;
|
---|
| 3 |
|
---|
[3754] | 4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
[2512] | 5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 6 |
|
---|
| 7 | import java.awt.event.ActionEvent;
|
---|
| 8 | import java.awt.event.KeyEvent;
|
---|
| 9 | import java.util.Collection;
|
---|
| 10 | import java.util.List;
|
---|
| 11 |
|
---|
| 12 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
| 13 | import org.openstreetmap.josm.data.osm.visitor.MergeSourceBuildingVisitor;
|
---|
| 14 | import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
|
---|
| 15 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
| 16 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
[5233] | 17 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
| 18 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
[2512] | 19 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
[11848] | 20 | import org.openstreetmap.josm.tools.Utils;
|
---|
[2512] | 21 |
|
---|
[7859] | 22 | /**
|
---|
| 23 | * Merge the currently selected objects into another layer.
|
---|
| 24 | * @since 1890
|
---|
| 25 | */
|
---|
[2512] | 26 | public class MergeSelectionAction extends AbstractMergeAction {
|
---|
[7859] | 27 |
|
---|
| 28 | /**
|
---|
| 29 | * Constructs a new {@code MergeSelectionAction}.
|
---|
| 30 | */
|
---|
[2512] | 31 | public MergeSelectionAction() {
|
---|
[4836] | 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")),
|
---|
[4982] | 34 | KeyEvent.VK_M, Shortcut.CTRL_SHIFT),
|
---|
[4836] | 35 | true /* register */
|
---|
[2512] | 36 | );
|
---|
| 37 | putValue("help", ht("/Action/MergeSelection"));
|
---|
| 38 | }
|
---|
| 39 |
|
---|
[7859] | 40 | /**
|
---|
| 41 | * Merge the currently selected objects into another layer.
|
---|
| 42 | */
|
---|
| 43 | public void mergeSelected() {
|
---|
[10382] | 44 | OsmDataLayer editLayer = getLayerManager().getEditLayer();
|
---|
| 45 | List<Layer> targetLayers = LayerListDialog.getInstance().getModel().getPossibleMergeTargets(editLayer);
|
---|
[2512] | 46 | if (targetLayers.isEmpty()) {
|
---|
[10382] | 47 | warnNoTargetLayersForSourceLayer(editLayer);
|
---|
[2512] | 48 | return;
|
---|
| 49 | }
|
---|
| 50 | Layer targetLayer = askTargetLayer(targetLayers);
|
---|
| 51 | if (targetLayer == null)
|
---|
| 52 | return;
|
---|
[10382] | 53 | if (editLayer.isUploadDiscouraged() && targetLayer instanceof OsmDataLayer
|
---|
[8510] | 54 | && !((OsmDataLayer) targetLayer).isUploadDiscouraged()
|
---|
[10382] | 55 | && editLayer.data.getAllSelected().size() > 1
|
---|
[7859] | 56 | && warnMergingUploadDiscouragedObjects(targetLayer)) {
|
---|
| 57 | return;
|
---|
[5233] | 58 | }
|
---|
[10382] | 59 | MergeSourceBuildingVisitor builder = new MergeSourceBuildingVisitor(editLayer.data);
|
---|
[8510] | 60 | ((OsmDataLayer) targetLayer).mergeFrom(builder.build());
|
---|
[2512] | 61 | }
|
---|
| 62 |
|
---|
[6084] | 63 | @Override
|
---|
[2512] | 64 | public void actionPerformed(ActionEvent e) {
|
---|
[10382] | 65 | OsmDataLayer editLayer = getLayerManager().getEditLayer();
|
---|
[10383] | 66 | if (editLayer == null || editLayer.data.selectionEmpty())
|
---|
[2512] | 67 | return;
|
---|
[7859] | 68 | mergeSelected();
|
---|
[2512] | 69 | }
|
---|
| 70 |
|
---|
| 71 | @Override
|
---|
| 72 | protected void updateEnabledState() {
|
---|
[10548] | 73 | updateEnabledStateOnCurrentSelection();
|
---|
[2512] | 74 | }
|
---|
| 75 |
|
---|
| 76 | @Override
|
---|
| 77 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
|
---|
| 78 | setEnabled(selection != null && !selection.isEmpty());
|
---|
| 79 | }
|
---|
[6069] | 80 |
|
---|
[5233] | 81 | /**
|
---|
[7859] | 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
|
---|
[5233] | 85 | */
|
---|
[10448] | 86 | public final boolean warnMergingUploadDiscouragedObjects(Layer targetLayer) {
|
---|
[5233] | 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 />"+
|
---|
[7859] | 92 | "Are you sure you want to continue?",
|
---|
[11848] | 93 | Utils.escapeReservedCharactersHTML(getLayerManager().getEditLayer().getName()),
|
---|
| 94 | Utils.escapeReservedCharactersHTML(targetLayer.getName()),
|
---|
| 95 | Utils.escapeReservedCharactersHTML(targetLayer.getName()))+
|
---|
[5233] | 96 | "</html>",
|
---|
| 97 | ImageProvider.get("dialogs", "mergedown"), tr("Ignore this hint and merge anyway"));
|
---|
| 98 | }
|
---|
[2512] | 99 | }
|
---|