source: josm/trunk/src/org/openstreetmap/josm/actions/DeleteAction.java@ 13660

Last change on this file since 13660 was 13434, checked in by Don-vip, 6 years ago

see #8039, see #10456 - support read-only data layers

  • Property svn:eol-style set to native
File size: 5.7 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;
6import static org.openstreetmap.josm.tools.I18n.trn;
7
8import java.awt.GridBagLayout;
9import java.awt.event.ActionEvent;
10import java.awt.event.KeyEvent;
11import java.util.Collection;
12
13import javax.swing.JOptionPane;
14import javax.swing.JPanel;
15
16import org.openstreetmap.josm.Main;
17import org.openstreetmap.josm.command.DeleteCommand.DeletionCallback;
18import org.openstreetmap.josm.data.osm.DefaultNameFormatter;
19import org.openstreetmap.josm.data.osm.OsmPrimitive;
20import org.openstreetmap.josm.data.osm.Relation;
21import org.openstreetmap.josm.data.osm.RelationToChildReference;
22import org.openstreetmap.josm.gui.ConditionalOptionPaneUtil;
23import org.openstreetmap.josm.gui.MainApplication;
24import org.openstreetmap.josm.gui.MapFrame;
25import org.openstreetmap.josm.gui.dialogs.DeleteFromRelationConfirmationDialog;
26import org.openstreetmap.josm.gui.widgets.JMultilineLabel;
27import org.openstreetmap.josm.tools.Shortcut;
28
29/**
30 * Action that deletes selected objects.
31 * @since 770
32 */
33public final class DeleteAction extends JosmAction {
34
35 /**
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) {
43 return DeleteAction.checkAndConfirmOutlyingDelete(primitives, ignore);
44 }
45
46 @Override
47 public boolean confirmRelationDeletion(Collection<Relation> relations) {
48 return DeleteAction.confirmRelationDeletion(relations);
49 }
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 }
58 };
59
60 /**
61 * Constructs a new {@code DeleteAction}.
62 */
63 public DeleteAction() {
64 super(tr("Delete"), "dialogs/delete", tr("Delete selected objects."),
65 Shortcut.registerShortcut("system:delete", tr("Edit: {0}", tr("Delete")), KeyEvent.VK_DELETE, Shortcut.DIRECT), true);
66 putValue("help", ht("/Action/Delete"));
67 }
68
69 @Override
70 public void actionPerformed(ActionEvent e) {
71 MapFrame map = MainApplication.getMap();
72 if (!isEnabled() || !map.mapView.isActiveLayerVisible())
73 return;
74 map.mapModeDelete.doActionPerformed(e);
75 }
76
77 @Override
78 protected void updateEnabledState() {
79 updateEnabledStateOnCurrentSelection();
80 }
81
82 @Override
83 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
84 updateEnabledStateOnModifiableSelection(selection);
85 }
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 }
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 }
141}
Note: See TracBrowser for help on using the repository browser.