[6380] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[770] | 2 | package org.openstreetmap.josm.actions;
|
---|
| 3 |
|
---|
[5275] | 4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
[770] | 5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
[12760] | 6 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
[770] | 7 |
|
---|
[12760] | 8 | import java.awt.GridBagLayout;
|
---|
[770] | 9 | import java.awt.event.ActionEvent;
|
---|
| 10 | import java.awt.event.KeyEvent;
|
---|
[2256] | 11 | import java.util.Collection;
|
---|
[770] | 12 |
|
---|
[12760] | 13 | import javax.swing.JOptionPane;
|
---|
| 14 | import javax.swing.JPanel;
|
---|
| 15 |
|
---|
| 16 | import org.openstreetmap.josm.Main;
|
---|
| 17 | import org.openstreetmap.josm.command.DeleteCommand.DeletionCallback;
|
---|
| 18 | import org.openstreetmap.josm.data.osm.DefaultNameFormatter;
|
---|
[2256] | 19 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
[12760] | 20 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
[12763] | 21 | import org.openstreetmap.josm.data.osm.RelationToChildReference;
|
---|
[12760] | 22 | import org.openstreetmap.josm.gui.ConditionalOptionPaneUtil;
|
---|
[12630] | 23 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
| 24 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
[12763] | 25 | import org.openstreetmap.josm.gui.dialogs.DeleteFromRelationConfirmationDialog;
|
---|
[12760] | 26 | import org.openstreetmap.josm.gui.widgets.JMultilineLabel;
|
---|
[1084] | 27 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
[770] | 28 |
|
---|
[7434] | 29 | /**
|
---|
| 30 | * Action that deletes selected objects.
|
---|
| 31 | * @since 770
|
---|
| 32 | */
|
---|
[1820] | 33 | public final class DeleteAction extends JosmAction {
|
---|
[770] | 34 |
|
---|
[7434] | 35 | /**
|
---|
[12760] | 36 | * The default {@link DeletionCallback} for {@code DeleteCommand}.
|
---|
| 37 | * @since 12760
|
---|
| 38 | */
|
---|
| 39 | public static final DeletionCallback defaultDeletionCallback = new DeletionCallback() {
|
---|
| 40 | @Override
|
---|
| 41 | public boolean checkAndConfirmOutlyingDelete(Collection<? extends OsmPrimitive> primitives,
|
---|
| 42 | Collection<? extends OsmPrimitive> ignore) {
|
---|
[12761] | 43 | return DeleteAction.checkAndConfirmOutlyingDelete(primitives, ignore);
|
---|
[12760] | 44 | }
|
---|
| 45 |
|
---|
| 46 | @Override
|
---|
| 47 | public boolean confirmRelationDeletion(Collection<Relation> relations) {
|
---|
[12761] | 48 | return DeleteAction.confirmRelationDeletion(relations);
|
---|
[12760] | 49 | }
|
---|
[12763] | 50 |
|
---|
| 51 | @Override
|
---|
| 52 | public boolean confirmDeletionFromRelation(Collection<RelationToChildReference> references) {
|
---|
| 53 | DeleteFromRelationConfirmationDialog dialog = DeleteFromRelationConfirmationDialog.getInstance();
|
---|
| 54 | dialog.getModel().populate(references);
|
---|
| 55 | dialog.setVisible(true);
|
---|
| 56 | return !dialog.isCanceled();
|
---|
| 57 | }
|
---|
[12760] | 58 | };
|
---|
| 59 |
|
---|
| 60 | /**
|
---|
[7434] | 61 | * Constructs a new {@code DeleteAction}.
|
---|
| 62 | */
|
---|
[1169] | 63 | public DeleteAction() {
|
---|
| 64 | super(tr("Delete"), "dialogs/delete", tr("Delete selected objects."),
|
---|
[4982] | 65 | Shortcut.registerShortcut("system:delete", tr("Edit: {0}", tr("Delete")), KeyEvent.VK_DELETE, Shortcut.DIRECT), true);
|
---|
[2323] | 66 | putValue("help", ht("/Action/Delete"));
|
---|
[1169] | 67 | }
|
---|
[770] | 68 |
|
---|
[6084] | 69 | @Override
|
---|
[1169] | 70 | public void actionPerformed(ActionEvent e) {
|
---|
[12630] | 71 | MapFrame map = MainApplication.getMap();
|
---|
| 72 | if (!isEnabled() || !map.mapView.isActiveLayerVisible())
|
---|
[1820] | 73 | return;
|
---|
[12630] | 74 | map.mapModeDelete.doActionPerformed(e);
|
---|
[1169] | 75 | }
|
---|
[1820] | 76 |
|
---|
| 77 | @Override
|
---|
| 78 | protected void updateEnabledState() {
|
---|
[10409] | 79 | updateEnabledStateOnCurrentSelection();
|
---|
[1087] | 80 | }
|
---|
[2256] | 81 |
|
---|
| 82 | @Override
|
---|
| 83 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
|
---|
| 84 | setEnabled(selection != null && !selection.isEmpty());
|
---|
| 85 | }
|
---|
[12749] | 86 |
|
---|
| 87 | /**
|
---|
| 88 | * Check whether user is about to delete data outside of the download area.
|
---|
| 89 | * Request confirmation if he is.
|
---|
| 90 | * @param primitives the primitives to operate on
|
---|
| 91 | * @param ignore {@code null} or a primitive to be ignored
|
---|
| 92 | * @return true, if operating on outlying primitives is OK; false, otherwise
|
---|
| 93 | * @since 12749 (moved from DeleteCommand)
|
---|
| 94 | */
|
---|
| 95 | public static boolean checkAndConfirmOutlyingDelete(Collection<? extends OsmPrimitive> primitives,
|
---|
| 96 | Collection<? extends OsmPrimitive> ignore) {
|
---|
| 97 | return checkAndConfirmOutlyingOperation("delete",
|
---|
| 98 | tr("Delete confirmation"),
|
---|
| 99 | tr("You are about to delete nodes outside of the area you have downloaded."
|
---|
| 100 | + "<br>"
|
---|
| 101 | + "This can cause problems because other objects (that you do not see) might use them."
|
---|
| 102 | + "<br>"
|
---|
| 103 | + "Do you really want to delete?"),
|
---|
| 104 | tr("You are about to delete incomplete objects."
|
---|
| 105 | + "<br>"
|
---|
| 106 | + "This will cause problems because you don''t see the real object."
|
---|
| 107 | + "<br>" + "Do you really want to delete?"),
|
---|
| 108 | primitives, ignore);
|
---|
| 109 | }
|
---|
[12760] | 110 |
|
---|
| 111 | /**
|
---|
| 112 | * Confirm before deleting a relation, as it is a common newbie error.
|
---|
| 113 | * @param relations relation to check for deletion
|
---|
| 114 | * @return {@code true} if user confirms the deletion
|
---|
| 115 | * @since 12760
|
---|
| 116 | */
|
---|
| 117 | public static boolean confirmRelationDeletion(Collection<Relation> relations) {
|
---|
| 118 | JPanel msg = new JPanel(new GridBagLayout());
|
---|
| 119 | msg.add(new JMultilineLabel("<html>" + trn(
|
---|
| 120 | "You are about to delete {0} relation: {1}"
|
---|
| 121 | + "<br/>"
|
---|
| 122 | + "This step is rarely necessary and cannot be undone easily after being uploaded to the server."
|
---|
| 123 | + "<br/>"
|
---|
| 124 | + "Do you really want to delete?",
|
---|
| 125 | "You are about to delete {0} relations: {1}"
|
---|
| 126 | + "<br/>"
|
---|
| 127 | + "This step is rarely necessary and cannot be undone easily after being uploaded to the server."
|
---|
| 128 | + "<br/>"
|
---|
| 129 | + "Do you really want to delete?",
|
---|
| 130 | relations.size(), relations.size(), DefaultNameFormatter.getInstance().formatAsHtmlUnorderedList(relations, 20))
|
---|
| 131 | + "</html>"));
|
---|
| 132 | return ConditionalOptionPaneUtil.showConfirmationDialog(
|
---|
| 133 | "delete_relations",
|
---|
| 134 | Main.parent,
|
---|
| 135 | msg,
|
---|
| 136 | tr("Delete relation?"),
|
---|
| 137 | JOptionPane.YES_NO_OPTION,
|
---|
| 138 | JOptionPane.QUESTION_MESSAGE,
|
---|
| 139 | JOptionPane.YES_OPTION);
|
---|
| 140 | }
|
---|
[770] | 141 | }
|
---|