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