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