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