001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.actions; 003 004import static org.openstreetmap.josm.gui.help.HelpUtil.ht; 005import static org.openstreetmap.josm.tools.I18n.tr; 006import static org.openstreetmap.josm.tools.I18n.trn; 007 008import java.awt.GridBagLayout; 009import java.awt.event.ActionEvent; 010import java.awt.event.KeyEvent; 011import java.util.Collection; 012import java.util.Collections; 013 014import javax.swing.JOptionPane; 015import javax.swing.JPanel; 016 017import org.openstreetmap.josm.command.DeleteCommand.DeletionCallback; 018import org.openstreetmap.josm.data.osm.DefaultNameFormatter; 019import org.openstreetmap.josm.data.osm.INode; 020import org.openstreetmap.josm.data.osm.IRelation; 021import org.openstreetmap.josm.data.osm.IWay; 022import org.openstreetmap.josm.data.osm.OsmPrimitive; 023import org.openstreetmap.josm.data.osm.Relation; 024import org.openstreetmap.josm.data.osm.RelationToChildReference; 025import org.openstreetmap.josm.gui.ConditionalOptionPaneUtil; 026import org.openstreetmap.josm.gui.MainApplication; 027import org.openstreetmap.josm.gui.MapFrame; 028import org.openstreetmap.josm.gui.dialogs.DeleteFromRelationConfirmationDialog; 029import org.openstreetmap.josm.gui.util.GuiHelper; 030import org.openstreetmap.josm.gui.widgets.JMultilineLabel; 031import org.openstreetmap.josm.tools.Pair; 032import org.openstreetmap.josm.tools.Shortcut; 033 034/** 035 * Action that deletes selected objects. 036 * @since 770 037 */ 038public final class DeleteAction extends JosmAction { 039 040 /** 041 * The default {@link DeletionCallback} for {@code DeleteCommand}. 042 * @since 12760 043 */ 044 public static final DeletionCallback defaultDeletionCallback = new DeletionCallback() { 045 @Override 046 public boolean checkAndConfirmOutlyingDelete(Collection<? extends OsmPrimitive> primitives, 047 Collection<? extends OsmPrimitive> ignore) { 048 return DeleteAction.checkAndConfirmOutlyingDelete(primitives, ignore); 049 } 050 051 @Override 052 public boolean confirmRelationDeletion(Collection<Relation> relations) { 053 return DeleteAction.confirmRelationDeletion(relations); 054 } 055 056 @Override 057 public boolean confirmDeletionFromRelation(Collection<RelationToChildReference> references) { 058 return this.confirmDeletionFromRelation(references, Collections.emptyList()); 059 } 060 061 @Override 062 public boolean confirmDeletionFromRelation(Collection<RelationToChildReference> references, 063 Collection<Pair<Relation, Boolean>> parentsToDelete) { 064 DeleteFromRelationConfirmationDialog dialog = DeleteFromRelationConfirmationDialog.getInstance(); 065 dialog.getModel().populate(references); 066 dialog.getDeletedRelationsModel().populate(parentsToDelete); 067 dialog.setVisible(true); 068 return !dialog.isCanceled(); 069 } 070 }; 071 072 /** 073 * Constructs a new {@code DeleteAction}. 074 */ 075 public DeleteAction() { 076 super(tr("Delete"), "dialogs/delete", tr("Delete selected objects."), 077 Shortcut.registerShortcut("system:delete", tr("Edit: {0}", tr("Delete")), KeyEvent.VK_DELETE, Shortcut.DIRECT), true); 078 setHelpId(ht("/Action/EditDelete")); 079 } 080 081 @Override 082 public void actionPerformed(ActionEvent e) { 083 MapFrame map = MainApplication.getMap(); 084 if (!isEnabled() || !map.mapView.isActiveLayerVisible()) 085 return; 086 map.mapModeDelete.doActionPerformed(e); 087 } 088 089 @Override 090 protected void updateEnabledState() { 091 updateEnabledStateOnCurrentSelection(); 092 } 093 094 @Override 095 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) { 096 updateEnabledStateOnModifiableSelection(selection); 097 } 098 099 /** 100 * Check whether user is about to delete data outside of the download area. 101 * Request confirmation if he is. 102 * @param primitives the primitives to operate on 103 * @param ignore {@code null} or a primitive to be ignored 104 * @return true, if operating on outlying primitives is OK; false, otherwise 105 * @since 12749 (moved from DeleteCommand) 106 */ 107 public static boolean checkAndConfirmOutlyingDelete(Collection<? extends OsmPrimitive> primitives, 108 Collection<? extends OsmPrimitive> ignore) { 109 final boolean nodes = primitives.stream().anyMatch(INode.class::isInstance); 110 final boolean ways = primitives.stream().anyMatch(IWay.class::isInstance); 111 final boolean relations = primitives.stream().anyMatch(IRelation.class::isInstance); 112 final String type; 113 if (nodes && !ways && !relations) { 114 type = tr("You are about to delete nodes which can have other referrers not yet downloaded."); 115 } else if (!nodes && ways && !relations) { 116 type = tr("You are about to delete ways which can have other referrers not yet downloaded."); 117 } else if (!nodes && !ways && relations) { 118 type = tr("You are about to delete relations which can have other referrers not yet downloaded."); 119 } else { 120 // OK. We have multiple types being deleted. 121 type = tr("You are about to delete primitives which can have other referrers not yet downloaded."); 122 } 123 return Boolean.TRUE.equals(GuiHelper.runInEDTAndWaitAndReturn(() -> checkAndConfirmOutlyingOperation("delete", 124 tr("Delete confirmation"), 125 tr("{0}" 126 + "<br>" 127 + "This can cause problems because other objects (that you do not see) might use them." 128 + "<br>" 129 + "Do you really want to delete?", type), 130 tr("You are about to delete incomplete objects." 131 + "<br>" 132 + "This will cause problems because you don''t see the real object." 133 + "<br>" + "Do you really want to delete?"), 134 primitives, ignore))); 135 } 136 137 /** 138 * Confirm before deleting a relation, as it is a common newbie error. 139 * @param relations relation to check for deletion 140 * @return {@code true} if user confirms the deletion 141 * @since 12760 142 */ 143 public static boolean confirmRelationDeletion(Collection<Relation> relations) { 144 if (relations.stream().allMatch(Relation::isNew)) { 145 return true; 146 } 147 JPanel msg = new JPanel(new GridBagLayout()); 148 msg.add(new JMultilineLabel("<html>" + trn( 149 "You are about to delete {0} relation: {1}" 150 + "<br/>" 151 + "This step is rarely necessary and cannot be undone easily after being uploaded to the server." 152 + "<br/>" 153 + "Do you really want to delete?", 154 "You are about to delete {0} relations: {1}" 155 + "<br/>" 156 + "This step is rarely necessary and cannot be undone easily after being uploaded to the server." 157 + "<br/>" 158 + "Do you really want to delete?", 159 relations.size(), relations.size(), DefaultNameFormatter.getInstance().formatAsHtmlUnorderedList(relations, 20)) 160 + "</html>")); 161 return ConditionalOptionPaneUtil.showConfirmationDialog( 162 "delete_relations", 163 MainApplication.getMainFrame(), 164 msg, 165 tr("Delete relation?"), 166 JOptionPane.YES_NO_OPTION, 167 JOptionPane.QUESTION_MESSAGE, 168 JOptionPane.YES_OPTION); 169 } 170}