1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.actions.mapmode;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Cursor;
|
---|
7 | import java.awt.Point;
|
---|
8 | import java.awt.Rectangle;
|
---|
9 | import java.awt.event.KeyEvent;
|
---|
10 | import java.awt.event.MouseEvent;
|
---|
11 | import java.awt.event.ActionEvent;
|
---|
12 | import java.util.Collection;
|
---|
13 | import java.util.LinkedList;
|
---|
14 |
|
---|
15 | import javax.swing.JOptionPane;
|
---|
16 |
|
---|
17 | import org.openstreetmap.josm.Main;
|
---|
18 | import org.openstreetmap.josm.actions.GroupAction;
|
---|
19 | import org.openstreetmap.josm.command.Command;
|
---|
20 | import org.openstreetmap.josm.command.MoveCommand;
|
---|
21 | import org.openstreetmap.josm.command.RotateCommand;
|
---|
22 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
23 | import org.openstreetmap.josm.data.osm.Node;
|
---|
24 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
25 | import org.openstreetmap.josm.data.osm.visitor.AllNodesVisitor;
|
---|
26 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
27 | import org.openstreetmap.josm.gui.SelectionManager;
|
---|
28 | import org.openstreetmap.josm.gui.SelectionManager.SelectionEnded;
|
---|
29 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
30 | /**
|
---|
31 | * Move is an action that can move all kind of OsmPrimitives (except Keys for now).
|
---|
32 | *
|
---|
33 | * If an selected object is under the mouse when dragging, move all selected objects.
|
---|
34 | * If an unselected object is under the mouse when dragging, it becomes selected
|
---|
35 | * and will be moved.
|
---|
36 | * If no object is under the mouse, move all selected objects (if any)
|
---|
37 | *
|
---|
38 | * @author imi
|
---|
39 | */
|
---|
40 | public class SelectAction extends MapMode implements SelectionEnded {
|
---|
41 |
|
---|
42 | enum Mode { move, rotate, select }
|
---|
43 | private Mode mode = null;
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * The old cursor before the user pressed the mouse button.
|
---|
47 | */
|
---|
48 | private Cursor oldCursor;
|
---|
49 | /**
|
---|
50 | * The position of the mouse before the user moves a node.
|
---|
51 | */
|
---|
52 | private Point mousePos;
|
---|
53 | private SelectionManager selectionManager;
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * Create a new MoveAction
|
---|
57 | * @param mapFrame The MapFrame, this action belongs to.
|
---|
58 | */
|
---|
59 | public SelectAction(MapFrame mapFrame) {
|
---|
60 | super(tr("Select"), "move/move", tr("Select, move and rotate objects"),
|
---|
61 | KeyEvent.VK_S, mapFrame,
|
---|
62 | getCursor("normal", "selection", Cursor.DEFAULT_CURSOR));
|
---|
63 | putValue("help", "Action/Move/Move");
|
---|
64 | selectionManager = new SelectionManager(this, false, mapFrame.mapView);
|
---|
65 | }
|
---|
66 |
|
---|
67 | private static Cursor getCursor(String name, String mod, int def) {
|
---|
68 | try {
|
---|
69 | return ImageProvider.getCursor(name, mod);
|
---|
70 | } catch (Exception e) {
|
---|
71 | }
|
---|
72 | return Cursor.getPredefinedCursor(def);
|
---|
73 | }
|
---|
74 |
|
---|
75 | private static Cursor getCursor(String name, int def) {
|
---|
76 | return getCursor(name, null, def);
|
---|
77 | }
|
---|
78 |
|
---|
79 | private void setCursor(Cursor c) {
|
---|
80 | if (oldCursor == null) {
|
---|
81 | oldCursor = Main.map.mapView.getCursor();
|
---|
82 | Main.map.mapView.setCursor(c);
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | private void restoreCursor() {
|
---|
87 | if (oldCursor != null) {
|
---|
88 | Main.map.mapView.setCursor(oldCursor);
|
---|
89 | oldCursor = null;
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | @Override public void enterMode() {
|
---|
94 | super.enterMode();
|
---|
95 | Main.map.mapView.addMouseListener(this);
|
---|
96 | Main.map.mapView.addMouseMotionListener(this);
|
---|
97 | }
|
---|
98 |
|
---|
99 | @Override public void exitMode() {
|
---|
100 | super.exitMode();
|
---|
101 | Main.map.mapView.removeMouseListener(this);
|
---|
102 | Main.map.mapView.removeMouseMotionListener(this);
|
---|
103 | }
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * If the left mouse button is pressed, move all currently selected
|
---|
107 | * objects (if one of them is under the mouse) or the current one under the
|
---|
108 | * mouse (which will become selected).
|
---|
109 | */
|
---|
110 | @Override public void mouseDragged(MouseEvent e) {
|
---|
111 | if (mode == Mode.select) return;
|
---|
112 |
|
---|
113 | if ((e.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK) == 0)
|
---|
114 | return;
|
---|
115 |
|
---|
116 | if (mode == Mode.move) {
|
---|
117 | setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
|
---|
118 | }
|
---|
119 |
|
---|
120 | if (mousePos == null) {
|
---|
121 | mousePos = e.getPoint();
|
---|
122 | }
|
---|
123 |
|
---|
124 | EastNorth mouseEN = Main.map.mapView.getEastNorth(e.getX(), e.getY());
|
---|
125 | EastNorth mouseStartEN = Main.map.mapView.getEastNorth(mousePos.x, mousePos.y);
|
---|
126 | double dx = mouseEN.east() - mouseStartEN.east();
|
---|
127 | double dy = mouseEN.north() - mouseStartEN.north();
|
---|
128 | if (dx == 0 && dy == 0)
|
---|
129 | return;
|
---|
130 |
|
---|
131 | Collection<OsmPrimitive> selection = Main.ds.getSelected();
|
---|
132 | Collection<Node> affectedNodes = AllNodesVisitor.getAllNodes(selection);
|
---|
133 |
|
---|
134 | // when rotating, having only one node makes no sense - quit silently
|
---|
135 | if (mode == Mode.rotate && affectedNodes.size() < 2)
|
---|
136 | return;
|
---|
137 |
|
---|
138 |
|
---|
139 | // check if any coordinate would be outside the world
|
---|
140 | for (OsmPrimitive osm : affectedNodes) {
|
---|
141 | if (osm instanceof Node && ((Node)osm).coor.isOutSideWorld()) {
|
---|
142 | JOptionPane.showMessageDialog(Main.parent,
|
---|
143 | tr("Cannot move objects outside of the world."));
|
---|
144 | return;
|
---|
145 | }
|
---|
146 | }
|
---|
147 | Command c = !Main.main.undoRedo.commands.isEmpty()
|
---|
148 | ? Main.main.undoRedo.commands.getLast() : null;
|
---|
149 |
|
---|
150 | if (mode == Mode.move) {
|
---|
151 | if (c instanceof MoveCommand && affectedNodes.equals(((MoveCommand)c).objects))
|
---|
152 | ((MoveCommand)c).moveAgain(dx,dy);
|
---|
153 | else
|
---|
154 | Main.main.undoRedo.add(new MoveCommand(selection, dx, dy));
|
---|
155 | } else if (mode == Mode.rotate) {
|
---|
156 | if (c instanceof RotateCommand && affectedNodes.equals(((RotateCommand)c).objects))
|
---|
157 | ((RotateCommand)c).rotateAgain(mouseStartEN, mouseEN);
|
---|
158 | else
|
---|
159 | Main.main.undoRedo.add(new RotateCommand(selection, mouseStartEN, mouseEN));
|
---|
160 | }
|
---|
161 |
|
---|
162 | Main.map.mapView.repaint();
|
---|
163 | mousePos = e.getPoint();
|
---|
164 | }
|
---|
165 |
|
---|
166 | /**
|
---|
167 | * Look, whether any object is selected. If not, select the nearest node.
|
---|
168 | * If there are no nodes in the dataset, do nothing.
|
---|
169 | *
|
---|
170 | * If the user did not press the left mouse button, do nothing.
|
---|
171 | *
|
---|
172 | * Also remember the starting position of the movement and change the mouse
|
---|
173 | * cursor to movement.
|
---|
174 | */
|
---|
175 | @Override public void mousePressed(MouseEvent e) {
|
---|
176 | if (e.getButton() != MouseEvent.BUTTON1)
|
---|
177 | return;
|
---|
178 | boolean ctrl = (e.getModifiers() & ActionEvent.CTRL_MASK) != 0;
|
---|
179 |
|
---|
180 | Collection<OsmPrimitive> sel = Main.ds.getSelected();
|
---|
181 | OsmPrimitive osm = Main.map.mapView.getNearest(e.getPoint());
|
---|
182 | if (ctrl) {
|
---|
183 | if (osm != null && !sel.contains(osm)) Main.ds.setSelected(osm);
|
---|
184 |
|
---|
185 | mode = Mode.rotate;
|
---|
186 | setCursor(ImageProvider.getCursor("rotate", null));
|
---|
187 | } else if (osm != null) {
|
---|
188 | if (!sel.contains(osm)) Main.ds.setSelected(osm);
|
---|
189 |
|
---|
190 | mode = Mode.move;
|
---|
191 | } else {
|
---|
192 | mode = Mode.select;
|
---|
193 | oldCursor = Main.map.mapView.getCursor();
|
---|
194 | selectionManager.register(Main.map.mapView);
|
---|
195 | selectionManager.mousePressed(e);
|
---|
196 | }
|
---|
197 |
|
---|
198 | Main.map.mapView.repaint();
|
---|
199 |
|
---|
200 | mousePos = e.getPoint();
|
---|
201 | }
|
---|
202 |
|
---|
203 | /**
|
---|
204 | * Restore the old mouse cursor.
|
---|
205 | */
|
---|
206 | @Override public void mouseReleased(MouseEvent e) {
|
---|
207 | if (mode == Mode.select) {
|
---|
208 | selectionManager.unregister(Main.map.mapView);
|
---|
209 | }
|
---|
210 | restoreCursor();
|
---|
211 | mode = null;
|
---|
212 | }
|
---|
213 |
|
---|
214 | public void selectionEnded(Rectangle r, boolean alt, boolean shift, boolean ctrl) {
|
---|
215 | if (shift && ctrl)
|
---|
216 | return; // not allowed together
|
---|
217 |
|
---|
218 | Collection<OsmPrimitive> curSel;
|
---|
219 | if (!ctrl && !shift)
|
---|
220 | curSel = new LinkedList<OsmPrimitive>(); // new selection will replace the old.
|
---|
221 | else
|
---|
222 | curSel = Main.ds.getSelected();
|
---|
223 |
|
---|
224 | Collection<OsmPrimitive> selectionList = selectionManager.getObjectsInRectangle(r,alt);
|
---|
225 | for (OsmPrimitive osm : selectionList)
|
---|
226 | if (ctrl)
|
---|
227 | curSel.remove(osm);
|
---|
228 | else
|
---|
229 | curSel.add(osm);
|
---|
230 | Main.ds.setSelected(curSel);
|
---|
231 | Main.map.mapView.repaint();
|
---|
232 | }
|
---|
233 | }
|
---|