[298] | 1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
[4] | 2 | package org.openstreetmap.josm.gui;
|
---|
[3] | 3 |
|
---|
| 4 | import java.awt.Color;
|
---|
| 5 | import java.awt.Component;
|
---|
| 6 | import java.awt.Graphics;
|
---|
| 7 | import java.awt.Point;
|
---|
| 8 | import java.awt.Rectangle;
|
---|
| 9 | import java.awt.event.InputEvent;
|
---|
| 10 | import java.awt.event.MouseEvent;
|
---|
| 11 | import java.awt.event.MouseListener;
|
---|
| 12 | import java.awt.event.MouseMotionListener;
|
---|
[7] | 13 | import java.beans.PropertyChangeEvent;
|
---|
| 14 | import java.beans.PropertyChangeListener;
|
---|
| 15 | import java.util.Collection;
|
---|
| 16 | import java.util.LinkedList;
|
---|
[3] | 17 |
|
---|
[7] | 18 | import org.openstreetmap.josm.data.osm.Node;
|
---|
| 19 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
[64] | 20 | import org.openstreetmap.josm.data.osm.Way;
|
---|
[7] | 21 |
|
---|
[3] | 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.
|
---|
[1169] | 25 | *
|
---|
[3] | 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.
|
---|
[1169] | 31 | *
|
---|
| 32 | * The point where the left mouse button was pressed and the current mouse
|
---|
[3] | 33 | * position are two opposite corners of the selection rectangle.
|
---|
[1169] | 34 | *
|
---|
| 35 | * It is possible to specify an aspect ratio (width per height) which the
|
---|
[3] | 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
|
---|
[1169] | 38 | * mouse button was pressed and the corner of the current mouse position are at
|
---|
[3] | 39 | * opposite sites (the mouse position corner is the corner nearest to the mouse
|
---|
[1169] | 40 | * cursor).
|
---|
| 41 | *
|
---|
| 42 | * When the left mouse button was released, an ActionEvent is send to the
|
---|
[3] | 43 | * ActionListener given at constructor. The source of this event is this manager.
|
---|
[1169] | 44 | *
|
---|
[3] | 45 | * @author imi
|
---|
| 46 | */
|
---|
[7] | 47 | public class SelectionManager implements MouseListener, MouseMotionListener, PropertyChangeListener {
|
---|
[3] | 48 |
|
---|
[1169] | 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;
|
---|
[3] | 98 |
|
---|
[1169] | 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);
|
---|
[100] | 122 | eventSource.addPropertyChangeListener("scale", new PropertyChangeListener(){
|
---|
[1169] | 123 | public void propertyChange(PropertyChangeEvent evt) {
|
---|
| 124 | if (mousePosStart != null) {
|
---|
| 125 | paintRect();
|
---|
| 126 | mousePos = mousePosStart = null;
|
---|
| 127 | }
|
---|
[100] | 128 | }
|
---|
| 129 | });
|
---|
[1169] | 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 | }
|
---|
[3] | 142 |
|
---|
[1169] | 143 | /**
|
---|
| 144 | * If the correct button, from the "drawing rectangle" mode
|
---|
| 145 | */
|
---|
| 146 | public void mousePressed(MouseEvent e) {
|
---|
[1911] | 147 | if (e.getButton() == MouseEvent.BUTTON1) {
|
---|
[1169] | 148 | mousePosStart = mousePos = e.getPoint();
|
---|
[1911] | 149 | }
|
---|
[1169] | 150 | }
|
---|
[3] | 151 |
|
---|
[1169] | 152 | /**
|
---|
| 153 | * If the correct button is hold, draw the rectangle.
|
---|
| 154 | */
|
---|
| 155 | public void mouseDragged(MouseEvent e) {
|
---|
| 156 | int buttonPressed = e.getModifiersEx() & (MouseEvent.BUTTON1_DOWN_MASK | MouseEvent.BUTTON3_DOWN_MASK);
|
---|
[3] | 157 |
|
---|
| 158 |
|
---|
[1169] | 159 | if (buttonPressed != 0) {
|
---|
[1911] | 160 | if (mousePosStart == null) {
|
---|
[1169] | 161 | mousePosStart = mousePos = e.getPoint();
|
---|
[1911] | 162 | }
|
---|
[1169] | 163 | paintRect();
|
---|
| 164 | }
|
---|
[7] | 165 |
|
---|
[1169] | 166 | if (buttonPressed == MouseEvent.BUTTON1_DOWN_MASK) {
|
---|
| 167 | mousePos = e.getPoint();
|
---|
| 168 | paintRect();
|
---|
| 169 | } else if (buttonPressed == (MouseEvent.BUTTON1_DOWN_MASK | MouseEvent.BUTTON3_DOWN_MASK)) {
|
---|
| 170 | mousePosStart.x += e.getX()-mousePos.x;
|
---|
| 171 | mousePosStart.y += e.getY()-mousePos.y;
|
---|
| 172 | mousePos = e.getPoint();
|
---|
| 173 | paintRect();
|
---|
| 174 | }
|
---|
| 175 | }
|
---|
[3] | 176 |
|
---|
[1169] | 177 | /**
|
---|
| 178 | * Check the state of the keys and buttons and set the selection accordingly.
|
---|
| 179 | */
|
---|
| 180 | public void mouseReleased(MouseEvent e) {
|
---|
| 181 | if (e.getButton() != MouseEvent.BUTTON1)
|
---|
| 182 | return;
|
---|
| 183 | if (mousePos == null || mousePosStart == null)
|
---|
| 184 | return; // injected release from outside
|
---|
[3] | 185 |
|
---|
[1169] | 186 | // disable the selection rect
|
---|
| 187 | paintRect();
|
---|
| 188 | Rectangle r = getSelectionRectangle();
|
---|
| 189 | mousePosStart = null;
|
---|
| 190 | mousePos = null;
|
---|
[3] | 191 |
|
---|
[1169] | 192 | boolean shift = (e.getModifiersEx() & MouseEvent.SHIFT_DOWN_MASK) != 0;
|
---|
| 193 | boolean alt = (e.getModifiersEx() & MouseEvent.ALT_DOWN_MASK) != 0;
|
---|
| 194 | boolean ctrl = (e.getModifiersEx() & MouseEvent.CTRL_DOWN_MASK) != 0;
|
---|
[1911] | 195 | if ((e.getModifiersEx() & MouseEvent.BUTTON3_DOWN_MASK) == 0) {
|
---|
[1169] | 196 | selectionEndedListener.selectionEnded(r, alt, shift, ctrl);
|
---|
[1911] | 197 | }
|
---|
[1169] | 198 | }
|
---|
[3] | 199 |
|
---|
[7] | 200 |
|
---|
[1169] | 201 | /**
|
---|
| 202 | * Draw a selection rectangle on screen. If already a rectangle is drawn,
|
---|
| 203 | * it is removed instead.
|
---|
| 204 | */
|
---|
| 205 | private void paintRect() {
|
---|
| 206 | if (mousePos == null || mousePosStart == null || mousePos == mousePosStart)
|
---|
| 207 | return;
|
---|
| 208 | Graphics g = nc.getGraphics();
|
---|
| 209 | g.setColor(Color.BLACK);
|
---|
| 210 | g.setXORMode(Color.WHITE);
|
---|
[7] | 211 |
|
---|
[1169] | 212 | Rectangle r = getSelectionRectangle();
|
---|
| 213 | g.drawRect(r.x,r.y,r.width,r.height);
|
---|
| 214 | }
|
---|
[7] | 215 |
|
---|
[1169] | 216 | /**
|
---|
| 217 | * Calculate and return the current selection rectangle
|
---|
| 218 | * @return A rectangle that spans from mousePos to mouseStartPos
|
---|
| 219 | */
|
---|
| 220 | private Rectangle getSelectionRectangle() {
|
---|
| 221 | int x = mousePosStart.x;
|
---|
| 222 | int y = mousePosStart.y;
|
---|
| 223 | int w = mousePos.x - mousePosStart.x;
|
---|
| 224 | int h = mousePos.y - mousePosStart.y;
|
---|
| 225 | if (w < 0) {
|
---|
| 226 | x += w;
|
---|
| 227 | w = -w;
|
---|
| 228 | }
|
---|
| 229 | if (h < 0) {
|
---|
| 230 | y += h;
|
---|
| 231 | h = -h;
|
---|
| 232 | }
|
---|
[7] | 233 |
|
---|
[1169] | 234 | if (aspectRatio) {
|
---|
| 235 | /* Keep the aspect ratio by growing the rectangle; the
|
---|
| 236 | * rectangle is always under the cursor. */
|
---|
| 237 | double aspectRatio = (double)nc.getWidth()/nc.getHeight();
|
---|
| 238 | if ((double)w/h < aspectRatio) {
|
---|
| 239 | int neww = (int)(h*aspectRatio);
|
---|
[1911] | 240 | if (mousePos.x < mousePosStart.x) {
|
---|
[1169] | 241 | x += w - neww;
|
---|
[1911] | 242 | }
|
---|
[1169] | 243 | w = neww;
|
---|
| 244 | } else {
|
---|
| 245 | int newh = (int)(w/aspectRatio);
|
---|
[1911] | 246 | if (mousePos.y < mousePosStart.y) {
|
---|
[1169] | 247 | y += h - newh;
|
---|
[1911] | 248 | }
|
---|
[1169] | 249 | h = newh;
|
---|
| 250 | }
|
---|
| 251 | }
|
---|
| 252 |
|
---|
| 253 | return new Rectangle(x,y,w,h);
|
---|
| 254 | }
|
---|
| 255 |
|
---|
| 256 | /**
|
---|
| 257 | * If the action goes inactive, remove the selection rectangle from screen
|
---|
| 258 | */
|
---|
| 259 | public void propertyChange(PropertyChangeEvent evt) {
|
---|
| 260 | if (evt.getPropertyName().equals("active") && !(Boolean)evt.getNewValue() && mousePosStart != null) {
|
---|
| 261 | paintRect();
|
---|
| 262 | mousePosStart = null;
|
---|
| 263 | mousePos = null;
|
---|
| 264 | }
|
---|
| 265 | }
|
---|
| 266 |
|
---|
| 267 | /**
|
---|
| 268 | * Return a list of all objects in the rectangle, respecting the different
|
---|
| 269 | * modifier.
|
---|
| 270 | * @param alt Whether the alt key was pressed, which means select all objects
|
---|
| 271 | * that are touched, instead those which are completly covered.
|
---|
| 272 | */
|
---|
| 273 | public Collection<OsmPrimitive> getObjectsInRectangle(Rectangle r, boolean alt) {
|
---|
| 274 | Collection<OsmPrimitive> selection = new LinkedList<OsmPrimitive>();
|
---|
| 275 |
|
---|
| 276 | // whether user only clicked, not dragged.
|
---|
| 277 | boolean clicked = r.width <= 2 && r.height <= 2;
|
---|
| 278 | Point center = new Point(r.x+r.width/2, r.y+r.height/2);
|
---|
| 279 |
|
---|
| 280 | if (clicked) {
|
---|
| 281 | OsmPrimitive osm = nc.getNearest(center);
|
---|
[1911] | 282 | if (osm != null) {
|
---|
[1169] | 283 | selection.add(osm);
|
---|
[1911] | 284 | }
|
---|
[1169] | 285 | } else {
|
---|
| 286 | // nodes
|
---|
[1814] | 287 | for (Node n : nc.getCurrentDataSet().nodes) {
|
---|
[1911] | 288 | if (!n.deleted && !n.incomplete && r.contains(nc.getPoint(n))) {
|
---|
[1169] | 289 | selection.add(n);
|
---|
[1911] | 290 | }
|
---|
[1169] | 291 | }
|
---|
| 292 |
|
---|
| 293 | // ways
|
---|
[1814] | 294 | for (Way w : nc.getCurrentDataSet().ways) {
|
---|
[1911] | 295 | if (w.deleted || w.getNodesCount() == 0 || w.incomplete) {
|
---|
| 296 | continue;
|
---|
| 297 | }
|
---|
[1169] | 298 | if (alt) {
|
---|
[1910] | 299 | for (Node n : w.getNodes()) {
|
---|
[1725] | 300 | if (!n.incomplete && r.contains(nc.getPoint(n))) {
|
---|
[1169] | 301 | selection.add(w);
|
---|
| 302 | break;
|
---|
| 303 | }
|
---|
| 304 | }
|
---|
| 305 | } else {
|
---|
| 306 | boolean allIn = true;
|
---|
[1910] | 307 | for (Node n : w.getNodes()) {
|
---|
[1725] | 308 | if (!n.incomplete && !r.contains(nc.getPoint(n))) {
|
---|
[1169] | 309 | allIn = false;
|
---|
| 310 | break;
|
---|
| 311 | }
|
---|
| 312 | }
|
---|
[1911] | 313 | if (allIn) {
|
---|
| 314 | selection.add(w);
|
---|
| 315 | }
|
---|
[1169] | 316 | }
|
---|
| 317 | }
|
---|
| 318 | }
|
---|
| 319 | return selection;
|
---|
| 320 | }
|
---|
| 321 |
|
---|
| 322 | public void mouseClicked(MouseEvent e) {}
|
---|
| 323 | public void mouseEntered(MouseEvent e) {}
|
---|
| 324 | public void mouseExited(MouseEvent e) {}
|
---|
| 325 | public void mouseMoved(MouseEvent e) {}
|
---|
[3] | 326 | }
|
---|