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

Last change on this file since 1856 was 1856, checked in by Gubaer, 15 years ago

improved deleting relations

  • Property svn:eol-style set to native
File size: 6.5 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.actions.mapmode;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.event.ActionEvent;
7import java.awt.event.InputEvent;
8import java.awt.event.KeyEvent;
9import java.awt.event.MouseEvent;
10import java.util.Collections;
11
12import org.openstreetmap.josm.Main;
13import org.openstreetmap.josm.command.Command;
14import org.openstreetmap.josm.command.DeleteCommand;
15import org.openstreetmap.josm.data.osm.DataSet;
16import org.openstreetmap.josm.data.osm.OsmPrimitive;
17import org.openstreetmap.josm.data.osm.Relation;
18import org.openstreetmap.josm.data.osm.WaySegment;
19import org.openstreetmap.josm.gui.MapFrame;
20import org.openstreetmap.josm.gui.dialogs.relation.RelationDialogManager;
21import org.openstreetmap.josm.gui.layer.Layer;
22import org.openstreetmap.josm.gui.layer.OsmDataLayer;
23import org.openstreetmap.josm.tools.ImageProvider;
24import org.openstreetmap.josm.tools.Shortcut;
25
26/**
27 * An action that enables the user to delete nodes and other objects.
28 *
29 * The user can click on an object, which gets deleted if possible. When Ctrl is
30 * pressed when releasing the button, the objects and all its references are
31 * deleted.
32 *
33 * If the user did not press Ctrl and the object has any references, the user
34 * is informed and nothing is deleted.
35 *
36 * If the user enters the mapmode and any object is selected, all selected
37 * objects that can be deleted will.
38 *
39 * @author imi
40 */
41public class DeleteAction extends MapMode {
42
43 /**
44 * Construct a new DeleteAction. Mnemonic is the delete - key.
45 * @param mapFrame The frame this action belongs to.
46 */
47 public DeleteAction(MapFrame mapFrame) {
48 super(tr("Delete Mode"),
49 "delete",
50 tr("Delete nodes or ways."),
51 Shortcut.registerShortcut("mapmode:delete", tr("Mode: {0}",tr("Delete")), KeyEvent.VK_D, Shortcut.GROUP_EDIT),
52 mapFrame,
53 ImageProvider.getCursor("normal", "delete"));
54 }
55
56 @Override public void enterMode() {
57 super.enterMode();
58 if (!isEnabled())
59 return;
60 Main.map.mapView.addMouseListener(this);
61 }
62
63 @Override public void exitMode() {
64 super.exitMode();
65 Main.map.mapView.removeMouseListener(this);
66 }
67
68
69 @Override public void actionPerformed(ActionEvent e) {
70 super.actionPerformed(e);
71 if(!Main.map.mapView.isActiveLayerDrawable())
72 return;
73 doActionPerformed(e);
74 }
75
76 public void doActionPerformed(ActionEvent e) {
77 if(!Main.map.mapView.isActiveLayerDrawable())
78 return;
79 boolean ctrl = (e.getModifiers() & ActionEvent.CTRL_MASK) != 0;
80 boolean alt = (e.getModifiers() & (ActionEvent.ALT_MASK|InputEvent.ALT_GRAPH_MASK)) != 0;
81
82 Command c;
83 if (ctrl) {
84 c = DeleteCommand.deleteWithReferences(getEditLayer(),getCurrentDataSet().getSelected());
85 } else {
86 c = DeleteCommand.delete(getEditLayer(),getCurrentDataSet().getSelected(), !alt);
87 }
88 if (c != null) {
89 Main.main.undoRedo.add(c);
90 }
91
92 getCurrentDataSet().setSelected();
93 Main.map.repaint();
94 }
95
96 /**
97 * If user clicked with the left button, delete the nearest object.
98 * position.
99 */
100 @Override public void mouseClicked(MouseEvent e) {
101 if (e.getButton() != MouseEvent.BUTTON1)
102 return;
103 if(!Main.map.mapView.isActiveLayerVisible())
104 return;
105 boolean ctrl = (e.getModifiers() & ActionEvent.CTRL_MASK) != 0;
106 boolean shift = (e.getModifiers() & ActionEvent.SHIFT_MASK) != 0;
107 boolean alt = (e.getModifiers() & (ActionEvent.ALT_MASK|InputEvent.ALT_GRAPH_MASK)) != 0;
108
109 OsmPrimitive sel = Main.map.mapView.getNearestNode(e.getPoint());
110 Command c = null;
111 if (sel == null) {
112 WaySegment ws = Main.map.mapView.getNearestWaySegment(e.getPoint());
113 if (ws != null) {
114 if (shift) {
115 c = DeleteCommand.deleteWaySegment(getEditLayer(),ws);
116 } else if (ctrl) {
117 c = DeleteCommand.deleteWithReferences(getEditLayer(),Collections.singleton((OsmPrimitive)ws.way));
118 } else {
119 c = DeleteCommand.delete(getEditLayer(),Collections.singleton((OsmPrimitive)ws.way), !alt);
120 }
121 }
122 } else if (ctrl) {
123 c = DeleteCommand.deleteWithReferences(getEditLayer(),Collections.singleton(sel));
124 } else {
125 c = DeleteCommand.delete(getEditLayer(),Collections.singleton(sel), !alt);
126 }
127 if (c != null) {
128 Main.main.undoRedo.add(c);
129 }
130
131 getCurrentDataSet().setSelected();
132 Main.map.mapView.repaint();
133 }
134
135 @Override public String getModeHelpText() {
136 return tr("Click to delete. Shift: delete way segment. Alt: don't delete unused nodes when deleting a way. Ctrl: delete referring objects.");
137 }
138
139 @Override public boolean layerIsSupported(Layer l) {
140 return l instanceof OsmDataLayer;
141 }
142
143 @Override
144 protected void updateEnabledState() {
145 setEnabled(Main.map != null && Main.map.mapView != null && Main.map.mapView.isActiveLayerDrawable());
146 }
147
148 /**
149 * Deletes the relation in the context of the given layer. Also notifies
150 * {@see RelationDialogManager} and {@see OsmDataLayer#fireDataChange()} events.
151 *
152 * @param layer the layer in whose context the relation is deleted. Must not be null.
153 * @param toDelete the relation to be deleted. Must not be null.
154 * @exception IllegalArgumentException thrown if layer is null
155 * @exception IllegalArgumentException thrown if toDelete is nul
156 */
157 public static void deleteRelation(OsmDataLayer layer, Relation toDelete) {
158 if (layer == null)
159 throw new IllegalArgumentException(tr("parameter ''{0}'' must not be null", "layer"));
160 if (toDelete == null)
161 throw new IllegalArgumentException(tr("parameter ''{0}'' must not be null", "toDelete"));
162 if (toDelete == null)
163 return;
164 Command cmd = DeleteCommand.delete(layer, Collections.singleton(toDelete));
165 if (cmd != null) {
166 // cmd can be null if the user cancels dialogs DialogCommand displays
167 //
168 Main.main.undoRedo.add(cmd);
169 RelationDialogManager.getRelationDialogManager().close(layer, toDelete);
170 layer.fireDataChange();
171 }
172 }
173}
Note: See TracBrowser for help on using the repository browser.