source: josm/trunk/src/org/openstreetmap/josm/gui/SelectionManager.java@ 5764

Last change on this file since 5764 was 5764, checked in by Don-vip, 11 years ago

use of getPoint2D() instead of getPoint() when possible

  • Property svn:eol-style set to native
File size: 12.5 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.gui;
3
4import java.awt.Component;
5import java.awt.Point;
6import java.awt.Polygon;
7import java.awt.Rectangle;
8import java.awt.event.InputEvent;
9import java.awt.event.MouseEvent;
10import java.awt.event.MouseListener;
11import java.awt.event.MouseMotionListener;
12import java.beans.PropertyChangeEvent;
13import java.beans.PropertyChangeListener;
14import java.util.Collection;
15import java.util.LinkedList;
16
17import org.openstreetmap.josm.data.osm.Node;
18import org.openstreetmap.josm.data.osm.OsmPrimitive;
19import org.openstreetmap.josm.data.osm.Way;
20
21/**
22 * Manages the selection of a rectangle. Listening to left and right mouse button
23 * presses and to mouse motions and draw the rectangle accordingly.
24 *
25 * Left mouse button selects a rectangle from the press until release. Pressing
26 * right mouse button while left is still pressed enable the rectangle to move
27 * around. Releasing the left button fires an action event to the listener given
28 * at constructor, except if the right is still pressed, which just remove the
29 * selection rectangle and does nothing.
30 *
31 * The point where the left mouse button was pressed and the current mouse
32 * position are two opposite corners of the selection rectangle.
33 *
34 * It is possible to specify an aspect ratio (width per height) which the
35 * selection rectangle always must have. In this case, the selection rectangle
36 * will be the largest window with this aspect ratio, where the position the left
37 * mouse button was pressed and the corner of the current mouse position are at
38 * opposite sites (the mouse position corner is the corner nearest to the mouse
39 * cursor).
40 *
41 * When the left mouse button was released, an ActionEvent is send to the
42 * ActionListener given at constructor. The source of this event is this manager.
43 *
44 * @author imi
45 */
46public class SelectionManager implements MouseListener, MouseMotionListener, PropertyChangeListener {
47
48 /**
49 * This is the interface that an user of SelectionManager has to implement
50 * to get informed when a selection closes.
51 * @author imi
52 */
53 public interface SelectionEnded {
54 /**
55 * Called, when the left mouse button was released.
56 * @param r The rectangle that is currently the selection.
57 * @param e The mouse event.
58 * @see InputEvent#getModifiersEx()
59 */
60 public void selectionEnded(Rectangle r, MouseEvent e);
61 /**
62 * Called to register the selection manager for "active" property.
63 * @param listener The listener to register
64 */
65 public void addPropertyChangeListener(PropertyChangeListener listener);
66 /**
67 * Called to remove the selection manager from the listener list
68 * for "active" property.
69 * @param listener The listener to register
70 */
71 public void removePropertyChangeListener(PropertyChangeListener listener);
72 }
73 /**
74 * The listener that receives the events after left mouse button is released.
75 */
76 private final SelectionEnded selectionEndedListener;
77 /**
78 * Position of the map when the mouse button was pressed.
79 * If this is not <code>null</code>, a rectangle is drawn on screen.
80 */
81 private Point mousePosStart;
82 /**
83 * Position of the map when the selection rectangle was last drawn.
84 */
85 private Point mousePos;
86 /**
87 * The Component, the selection rectangle is drawn onto.
88 */
89 private final NavigatableComponent nc;
90 /**
91 * Whether the selection rectangle must obtain the aspect ratio of the
92 * drawComponent.
93 */
94 private boolean aspectRatio;
95
96 private boolean lassoMode;
97 private Polygon lasso = new Polygon();
98
99 /**
100 * Create a new SelectionManager.
101 *
102 * @param selectionEndedListener The action listener that receives the event when
103 * the left button is released.
104 * @param aspectRatio If true, the selection window must obtain the aspect
105 * ratio of the drawComponent.
106 * @param navComp The component, the rectangle is drawn onto.
107 */
108 public SelectionManager(SelectionEnded selectionEndedListener, boolean aspectRatio, NavigatableComponent navComp) {
109 this.selectionEndedListener = selectionEndedListener;
110 this.aspectRatio = aspectRatio;
111 this.nc = navComp;
112 }
113
114 /**
115 * Register itself at the given event source.
116 * @param eventSource The emitter of the mouse events.
117 * @param lassoMode {@code true} to enable lasso mode, {@code false} to disable it.
118 */
119 public void register(NavigatableComponent eventSource, boolean lassoMode) {
120 this.lassoMode = lassoMode;
121 eventSource.addMouseListener(this);
122 eventSource.addMouseMotionListener(this);
123 selectionEndedListener.addPropertyChangeListener(this);
124 eventSource.addPropertyChangeListener("scale", new PropertyChangeListener(){
125 public void propertyChange(PropertyChangeEvent evt) {
126 if (mousePosStart != null) {
127 paintRect();
128 mousePos = mousePosStart = null;
129 }
130 }
131 });
132 }
133 /**
134 * Unregister itself from the given event source. If a selection rectangle is
135 * shown, hide it first.
136 *
137 * @param eventSource The emitter of the mouse events.
138 */
139 public void unregister(Component eventSource) {
140 eventSource.removeMouseListener(this);
141 eventSource.removeMouseMotionListener(this);
142 selectionEndedListener.removePropertyChangeListener(this);
143 }
144
145 /**
146 * If the correct button, from the "drawing rectangle" mode
147 */
148 public void mousePressed(MouseEvent e) {
149 if (e.getButton() == MouseEvent.BUTTON1) {
150 mousePosStart = mousePos = e.getPoint();
151
152 lasso.reset();
153 lasso.addPoint(mousePosStart.x, mousePosStart.y);
154 }
155 }
156
157 /**
158 * If the correct button is hold, draw the rectangle.
159 */
160 public void mouseDragged(MouseEvent e) {
161 int buttonPressed = e.getModifiersEx() & (MouseEvent.BUTTON1_DOWN_MASK | MouseEvent.BUTTON3_DOWN_MASK);
162
163 if (buttonPressed != 0) {
164 if (mousePosStart == null) {
165 mousePosStart = mousePos = e.getPoint();
166 }
167 if (!lassoMode) {
168 paintRect();
169 }
170 }
171
172 if (buttonPressed == MouseEvent.BUTTON1_DOWN_MASK) {
173 mousePos = e.getPoint();
174 if (lassoMode) {
175 paintLasso();
176 } else {
177 paintRect();
178 }
179 } else if (buttonPressed == (MouseEvent.BUTTON1_DOWN_MASK | MouseEvent.BUTTON3_DOWN_MASK)) {
180 mousePosStart.x += e.getX()-mousePos.x;
181 mousePosStart.y += e.getY()-mousePos.y;
182 mousePos = e.getPoint();
183 paintRect();
184 }
185 }
186
187 /**
188 * Check the state of the keys and buttons and set the selection accordingly.
189 */
190 public void mouseReleased(MouseEvent e) {
191 if (e.getButton() != MouseEvent.BUTTON1)
192 return;
193 if (mousePos == null || mousePosStart == null)
194 return; // injected release from outside
195 // disable the selection rect
196 Rectangle r;
197 if (!lassoMode) {
198 nc.requestClearRect();
199 r = getSelectionRectangle();
200
201 lasso = rectToPolygon(r);
202 } else {
203 nc.requestClearPoly();
204 lasso.addPoint(mousePos.x, mousePos.y);
205 r = lasso.getBounds();
206 }
207 mousePosStart = null;
208 mousePos = null;
209
210 if ((e.getModifiersEx() & MouseEvent.BUTTON3_DOWN_MASK) == 0) {
211 selectionEndedListener.selectionEnded(r, e);
212 }
213 }
214
215 /**
216 * Draws a selection rectangle on screen.
217 */
218 private void paintRect() {
219 if (mousePos == null || mousePosStart == null || mousePos == mousePosStart)
220 return;
221 nc.requestPaintRect(getSelectionRectangle());
222 }
223
224 private void paintLasso() {
225 if (mousePos == null || mousePosStart == null || mousePos == mousePosStart) {
226 return;
227 }
228 lasso.addPoint(mousePos.x, mousePos.y);
229 nc.requestPaintPoly(lasso);
230 }
231
232 /**
233 * Calculate and return the current selection rectangle
234 * @return A rectangle that spans from mousePos to mouseStartPos
235 */
236 private Rectangle getSelectionRectangle() {
237 int x = mousePosStart.x;
238 int y = mousePosStart.y;
239 int w = mousePos.x - mousePosStart.x;
240 int h = mousePos.y - mousePosStart.y;
241 if (w < 0) {
242 x += w;
243 w = -w;
244 }
245 if (h < 0) {
246 y += h;
247 h = -h;
248 }
249
250 if (aspectRatio) {
251 /* Keep the aspect ratio by growing the rectangle; the
252 * rectangle is always under the cursor. */
253 double aspectRatio = (double)nc.getWidth()/nc.getHeight();
254 if ((double)w/h < aspectRatio) {
255 int neww = (int)(h*aspectRatio);
256 if (mousePos.x < mousePosStart.x) {
257 x += w - neww;
258 }
259 w = neww;
260 } else {
261 int newh = (int)(w/aspectRatio);
262 if (mousePos.y < mousePosStart.y) {
263 y += h - newh;
264 }
265 h = newh;
266 }
267 }
268
269 return new Rectangle(x,y,w,h);
270 }
271
272 /**
273 * If the action goes inactive, remove the selection rectangle from screen
274 */
275 public void propertyChange(PropertyChangeEvent evt) {
276 if (evt.getPropertyName().equals("active") && !(Boolean)evt.getNewValue() && mousePosStart != null) {
277 paintRect();
278 mousePosStart = null;
279 mousePos = null;
280 }
281 }
282
283 /**
284 * Return a list of all objects in the selection, respecting the different
285 * modifier.
286 *
287 * @param alt Whether the alt key was pressed, which means select all
288 * objects that are touched, instead those which are completely covered.
289 * @return The collection of selected objects.
290 */
291 public Collection<OsmPrimitive> getSelectedObjects(boolean alt) {
292
293 Collection<OsmPrimitive> selection = new LinkedList<OsmPrimitive>();
294
295 // whether user only clicked, not dragged.
296 boolean clicked = false;
297 Rectangle bounding = lasso.getBounds();
298 if (bounding.height <= 2 && bounding.width <= 2) {
299 clicked = true;
300 }
301
302 if (clicked) {
303 Point center = new Point(lasso.xpoints[0], lasso.ypoints[0]);
304 OsmPrimitive osm = nc.getNearestNodeOrWay(center, OsmPrimitive.isSelectablePredicate, false);
305 if (osm != null) {
306 selection.add(osm);
307 }
308 } else {
309 // nodes
310 for (Node n : nc.getCurrentDataSet().getNodes()) {
311 if (n.isSelectable() && lasso.contains(nc.getPoint2D(n))) {
312 selection.add(n);
313 }
314 }
315
316 // ways
317 for (Way w : nc.getCurrentDataSet().getWays()) {
318 if (!w.isSelectable() || w.getNodesCount() == 0) {
319 continue;
320 }
321 if (alt) {
322 for (Node n : w.getNodes()) {
323 if (!n.isIncomplete() && lasso.contains(nc.getPoint2D(n))) {
324 selection.add(w);
325 break;
326 }
327 }
328 } else {
329 boolean allIn = true;
330 for (Node n : w.getNodes()) {
331 if (!n.isIncomplete() && !lasso.contains(nc.getPoint(n))) {
332 allIn = false;
333 break;
334 }
335 }
336 if (allIn) {
337 selection.add(w);
338 }
339 }
340 }
341 }
342 return selection;
343 }
344
345 private Polygon rectToPolygon(Rectangle r) {
346 Polygon poly = new Polygon();
347
348 poly.addPoint(r.x, r.y);
349 poly.addPoint(r.x, r.y + r.height);
350 poly.addPoint(r.x + r.width, r.y + r.height);
351 poly.addPoint(r.x + r.width, r.y);
352
353 return poly;
354 }
355
356 /**
357 * Enables or disables the lasso mode.
358 * @param lassoMode {@code true} to enable lasso mode, {@code false} to disable it.
359 */
360 public void setLassoMode(boolean lassoMode) {
361 this.lassoMode = lassoMode;
362 }
363
364 public void mouseClicked(MouseEvent e) {}
365 public void mouseEntered(MouseEvent e) {}
366 public void mouseExited(MouseEvent e) {}
367 public void mouseMoved(MouseEvent e) {}
368}
Note: See TracBrowser for help on using the repository browser.