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

Last change on this file since 1102 was 1003, checked in by framm, 16 years ago
  • patch so change zoom rectangle behaviour by Matthew W.S. Bell <matthew at bells23.org.uk>
  • Property svn:eol-style set to native
File size: 9.8 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.gui;
3
4import java.awt.Color;
5import java.awt.Component;
6import java.awt.Graphics;
7import java.awt.Point;
8import java.awt.Rectangle;
9import java.awt.event.InputEvent;
10import java.awt.event.MouseEvent;
11import java.awt.event.MouseListener;
12import java.awt.event.MouseMotionListener;
13import java.beans.PropertyChangeEvent;
14import java.beans.PropertyChangeListener;
15import java.util.Collection;
16import java.util.LinkedList;
17
18import org.openstreetmap.josm.data.osm.Node;
19import org.openstreetmap.josm.data.osm.OsmPrimitive;
20import org.openstreetmap.josm.data.osm.Way;
21
22/**
23 * Manages the selection of a rectangle. Listening to left and right mouse button
24 * presses and to mouse motions and draw the rectangle accordingly.
25 *
26 * Left mouse button selects a rectangle from the press until release. Pressing
27 * right mouse button while left is still pressed enable the rectangle to move
28 * around. Releasing the left button fires an action event to the listener given
29 * at constructor, except if the right is still pressed, which just remove the
30 * selection rectangle and does nothing.
31 *
32 * The point where the left mouse button was pressed and the current mouse
33 * position are two opposite corners of the selection rectangle.
34 *
35 * It is possible to specify an aspect ratio (width per height) which the
36 * selection rectangle always must have. In this case, the selection rectangle
37 * will be the largest window with this aspect ratio, where the position the left
38 * mouse button was pressed and the corner of the current mouse position are at
39 * opposite sites (the mouse position corner is the corner nearest to the mouse
40 * cursor).
41 *
42 * When the left mouse button was released, an ActionEvent is send to the
43 * ActionListener given at constructor. The source of this event is this manager.
44 *
45 * @author imi
46 */
47public class SelectionManager implements MouseListener, MouseMotionListener, PropertyChangeListener {
48
49 /**
50 * This is the interface that an user of SelectionManager has to implement
51 * to get informed when a selection closes.
52 * @author imi
53 */
54 public interface SelectionEnded {
55 /**
56 * Called, when the left mouse button was released.
57 * @param r The rectangle that is currently the selection.
58 * @param alt Whether the alt key was pressed
59 * @param shift Whether the shift key was pressed
60 * @param ctrl Whether the ctrl key was pressed
61 * @see InputEvent#getModifiersEx()
62 */
63 public void selectionEnded(Rectangle r, boolean alt, boolean shift, boolean ctrl);
64 /**
65 * Called to register the selection manager for "active" property.
66 * @param listener The listener to register
67 */
68 public void addPropertyChangeListener(PropertyChangeListener listener);
69 /**
70 * Called to remove the selection manager from the listener list
71 * for "active" property.
72 * @param listener The listener to register
73 */
74 public void removePropertyChangeListener(PropertyChangeListener listener);
75 }
76 /**
77 * The listener that receives the events after left mouse button is released.
78 */
79 private final SelectionEnded selectionEndedListener;
80 /**
81 * Position of the map when the mouse button was pressed.
82 * If this is not <code>null</code>, a rectangle is drawn on screen.
83 */
84 private Point mousePosStart;
85 /**
86 * Position of the map when the selection rectangle was last drawn.
87 */
88 private Point mousePos;
89 /**
90 * The Component, the selection rectangle is drawn onto.
91 */
92 private final NavigatableComponent nc;
93 /**
94 * Whether the selection rectangle must obtain the aspect ratio of the
95 * drawComponent.
96 */
97 private boolean aspectRatio;
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 */
118 public void register(NavigatableComponent eventSource) {
119 eventSource.addMouseListener(this);
120 eventSource.addMouseMotionListener(this);
121 selectionEndedListener.addPropertyChangeListener(this);
122 eventSource.addPropertyChangeListener("scale", new PropertyChangeListener(){
123 public void propertyChange(PropertyChangeEvent evt) {
124 if (mousePosStart != null) {
125 paintRect();
126 mousePos = mousePosStart = null;
127 }
128 }
129 });
130 }
131 /**
132 * Unregister itself from the given event source. If a selection rectangle is
133 * shown, hide it first.
134 *
135 * @param eventSource The emitter of the mouse events.
136 */
137 public void unregister(Component eventSource) {
138 eventSource.removeMouseListener(this);
139 eventSource.removeMouseMotionListener(this);
140 selectionEndedListener.removePropertyChangeListener(this);
141 }
142
143 /**
144 * If the correct button, from the "drawing rectangle" mode
145 */
146 public void mousePressed(MouseEvent e) {
147 if (e.getButton() == MouseEvent.BUTTON1)
148 mousePosStart = mousePos = e.getPoint();
149 }
150
151 /**
152 * If the correct button is hold, draw the rectangle.
153 */
154 public void mouseDragged(MouseEvent e) {
155 int buttonPressed = e.getModifiersEx() & (MouseEvent.BUTTON1_DOWN_MASK | MouseEvent.BUTTON3_DOWN_MASK);
156
157
158 if (buttonPressed != 0) {
159 if (mousePosStart == null)
160 mousePosStart = mousePos = e.getPoint();
161 paintRect();
162 }
163
164 if (buttonPressed == MouseEvent.BUTTON1_DOWN_MASK) {
165 mousePos = e.getPoint();
166 paintRect();
167 } else if (buttonPressed == (MouseEvent.BUTTON1_DOWN_MASK | MouseEvent.BUTTON3_DOWN_MASK)) {
168 mousePosStart.x += e.getX()-mousePos.x;
169 mousePosStart.y += e.getY()-mousePos.y;
170 mousePos = e.getPoint();
171 paintRect();
172 }
173 }
174
175 /**
176 * Check the state of the keys and buttons and set the selection accordingly.
177 */
178 public void mouseReleased(MouseEvent e) {
179 if (e.getButton() != MouseEvent.BUTTON1)
180 return;
181 if (mousePos == null || mousePosStart == null)
182 return; // injected release from outside
183
184 // disable the selection rect
185 paintRect();
186 Rectangle r = getSelectionRectangle();
187 mousePosStart = null;
188 mousePos = null;
189
190 boolean shift = (e.getModifiersEx() & MouseEvent.SHIFT_DOWN_MASK) != 0;
191 boolean alt = (e.getModifiersEx() & MouseEvent.ALT_DOWN_MASK) != 0;
192 boolean ctrl = (e.getModifiersEx() & MouseEvent.CTRL_DOWN_MASK) != 0;
193 if ((e.getModifiersEx() & MouseEvent.BUTTON3_DOWN_MASK) == 0)
194 selectionEndedListener.selectionEnded(r, alt, shift, ctrl);
195 }
196
197
198 /**
199 * Draw a selection rectangle on screen. If already a rectangle is drawn,
200 * it is removed instead.
201 */
202 private void paintRect() {
203 if (mousePos == null || mousePosStart == null || mousePos == mousePosStart)
204 return;
205 Graphics g = nc.getGraphics();
206 g.setColor(Color.BLACK);
207 g.setXORMode(Color.WHITE);
208
209 Rectangle r = getSelectionRectangle();
210 g.drawRect(r.x,r.y,r.width,r.height);
211 }
212
213 /**
214 * Calculate and return the current selection rectangle
215 * @return A rectangle that spans from mousePos to mouseStartPos
216 */
217 private Rectangle getSelectionRectangle() {
218 int x = mousePosStart.x;
219 int y = mousePosStart.y;
220 int w = mousePos.x - mousePosStart.x;
221 int h = mousePos.y - mousePosStart.y;
222 if (w < 0) {
223 x += w;
224 w = -w;
225 }
226 if (h < 0) {
227 y += h;
228 h = -h;
229 }
230
231 if (aspectRatio) {
232 /* Keep the aspect ratio by growing the rectangle; the
233 * rectangle is always under the cursor. */
234 double aspectRatio = (double)nc.getWidth()/nc.getHeight();
235 if ((double)w/h < aspectRatio) {
236 int neww = (int)(h*aspectRatio);
237 if (mousePos.x < mousePosStart.x)
238 x += w - neww;
239 w = neww;
240 } else {
241 int newh = (int)(w/aspectRatio);
242 if (mousePos.y < mousePosStart.y)
243 y += h - newh;
244 h = newh;
245 }
246 }
247
248 return new Rectangle(x,y,w,h);
249 }
250
251 /**
252 * If the action goes inactive, remove the selection rectangle from screen
253 */
254 public void propertyChange(PropertyChangeEvent evt) {
255 if (evt.getPropertyName().equals("active") && !(Boolean)evt.getNewValue() && mousePosStart != null) {
256 paintRect();
257 mousePosStart = null;
258 mousePos = null;
259 }
260 }
261
262 /**
263 * Return a list of all objects in the rectangle, respecting the different
264 * modifier.
265 * @param alt Whether the alt key was pressed, which means select all objects
266 * that are touched, instead those which are completly covered.
267 */
268 public Collection<OsmPrimitive> getObjectsInRectangle(Rectangle r, boolean alt) {
269 Collection<OsmPrimitive> selection = new LinkedList<OsmPrimitive>();
270
271 // whether user only clicked, not dragged.
272 boolean clicked = r.width <= 2 && r.height <= 2;
273 Point center = new Point(r.x+r.width/2, r.y+r.height/2);
274
275 if (clicked) {
276 OsmPrimitive osm = nc.getNearest(center);
277 if (osm != null)
278 selection.add(osm);
279 } else {
280 // nodes
281 for (Node n : nc.getData().nodes) {
282 if (!n.deleted && !n.incomplete && r.contains(nc.getPoint(n.eastNorth)))
283 selection.add(n);
284 }
285
286 // ways
287 for (Way w : nc.getData().ways) {
288 if (w.deleted || w.nodes.isEmpty() || w.incomplete)
289 continue;
290 if (alt) {
291 for (Node n : w.nodes) {
292 if (!n.incomplete && r.contains(nc.getPoint(n.eastNorth))) {
293 selection.add(w);
294 break;
295 }
296 }
297 } else {
298 boolean allIn = true;
299 for (Node n : w.nodes) {
300 if (!n.incomplete && !r.contains(nc.getPoint(n.eastNorth))) {
301 allIn = false;
302 break;
303 }
304 }
305 if (allIn) selection.add(w);
306 }
307 }
308 }
309 return selection;
310 }
311
312 public void mouseClicked(MouseEvent e) {}
313 public void mouseEntered(MouseEvent e) {}
314 public void mouseExited(MouseEvent e) {}
315 public void mouseMoved(MouseEvent e) {}
316}
Note: See TracBrowser for help on using the repository browser.