| 1 | // This code has been adapted and copied from code that has been written by Immanuel Scholz and others for JOSM.
|
|---|
| 2 | // License: GPL. Copyright 2007 by Tim Haussmann
|
|---|
| 3 |
|
|---|
| 4 | import java.awt.Point;
|
|---|
| 5 | import java.awt.event.KeyEvent;
|
|---|
| 6 | import java.awt.event.MouseAdapter;
|
|---|
| 7 | import java.awt.event.MouseEvent;
|
|---|
| 8 | import java.awt.event.MouseListener;
|
|---|
| 9 | import java.awt.event.MouseMotionListener;
|
|---|
| 10 |
|
|---|
| 11 | import javax.swing.JComponent;
|
|---|
| 12 | import javax.swing.JPanel;
|
|---|
| 13 | import javax.swing.KeyStroke;
|
|---|
| 14 |
|
|---|
| 15 | /**
|
|---|
| 16 | * This class controls the user input by listening to mouse and key events.
|
|---|
| 17 | * Currently implemented is: - zooming in and out with scrollwheel - zooming in
|
|---|
| 18 | * and centering by double clicking - selecting an area by clicking and dragging
|
|---|
| 19 | * the mouse
|
|---|
| 20 | *
|
|---|
| 21 | * @author Tim Haussmann
|
|---|
| 22 | */
|
|---|
| 23 | public class OsmMapControl extends MouseAdapter implements MouseMotionListener, MouseListener {
|
|---|
| 24 |
|
|---|
| 25 | // start and end point of selection rectangle
|
|---|
| 26 | private Point iStartSelectionPoint;
|
|---|
| 27 | private Point iEndSelectionPoint;
|
|---|
| 28 |
|
|---|
| 29 | // the SlippyMapChooserComponent
|
|---|
| 30 | private final SlippyMapChooser iSlippyMapChooser;
|
|---|
| 31 |
|
|---|
| 32 | private SizeButton iSizeButton = null;
|
|---|
| 33 |
|
|---|
| 34 | /**
|
|---|
| 35 | * Create a new OsmMapControl
|
|---|
| 36 | */
|
|---|
| 37 | public OsmMapControl(SlippyMapChooser navComp, JPanel contentPane, SizeButton sizeButton) {
|
|---|
| 38 | this.iSlippyMapChooser = navComp;
|
|---|
| 39 | iSlippyMapChooser.addMouseListener(this);
|
|---|
| 40 | iSlippyMapChooser.addMouseMotionListener(this);
|
|---|
| 41 |
|
|---|
| 42 | String[] n = { ",", ".", "up", "right", "down", "left" };
|
|---|
| 43 | int[] k =
|
|---|
| 44 | { KeyEvent.VK_COMMA, KeyEvent.VK_PERIOD, KeyEvent.VK_UP, KeyEvent.VK_RIGHT,
|
|---|
| 45 | KeyEvent.VK_DOWN, KeyEvent.VK_LEFT };
|
|---|
| 46 |
|
|---|
| 47 | if (contentPane != null) {
|
|---|
| 48 | for (int i = 0; i < n.length; ++i) {
|
|---|
| 49 | contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
|
|---|
| 50 | KeyStroke.getKeyStroke(k[i], KeyEvent.CTRL_DOWN_MASK),
|
|---|
| 51 | "MapMover.Zoomer." + n[i]);
|
|---|
| 52 | }
|
|---|
| 53 | }
|
|---|
| 54 | iSizeButton = sizeButton;
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | /**
|
|---|
| 58 | * Start drawing the selection rectangle if it was the 1st button (left
|
|---|
| 59 | * button)
|
|---|
| 60 | */
|
|---|
| 61 | @Override
|
|---|
| 62 | public void mousePressed(MouseEvent e) {
|
|---|
| 63 | if (e.getButton() == MouseEvent.BUTTON1) {
|
|---|
| 64 | if (!iSizeButton.hit(e.getPoint())) {
|
|---|
| 65 | iStartSelectionPoint = e.getPoint();
|
|---|
| 66 | iEndSelectionPoint = e.getPoint();
|
|---|
| 67 | }
|
|---|
| 68 | }
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | @Override
|
|---|
| 72 | public void mouseDragged(MouseEvent e) {
|
|---|
| 73 | if (iStartSelectionPoint != null) {
|
|---|
| 74 | iEndSelectionPoint = e.getPoint();
|
|---|
| 75 | iSlippyMapChooser.setSelection(iStartSelectionPoint, iEndSelectionPoint);
|
|---|
| 76 | }
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | /**
|
|---|
| 80 | * When dragging the map change the cursor back to it's pre-move cursor. If
|
|---|
| 81 | * a double-click occurs center and zoom the map on the clicked location.
|
|---|
| 82 | */
|
|---|
| 83 | @Override
|
|---|
| 84 | public void mouseReleased(MouseEvent e) {
|
|---|
| 85 | if (e.getButton() == MouseEvent.BUTTON1) {
|
|---|
| 86 | if (iSizeButton.hit(e.getPoint())) {
|
|---|
| 87 | iSizeButton.toggle();
|
|---|
| 88 | iSlippyMapChooser.resizeSlippyMap();
|
|---|
| 89 | } else {
|
|---|
| 90 | if (e.getClickCount() == 1) {
|
|---|
| 91 | iSlippyMapChooser.setSelection(iStartSelectionPoint, e.getPoint());
|
|---|
| 92 |
|
|---|
| 93 | // reset the selections start and end
|
|---|
| 94 | iEndSelectionPoint = null;
|
|---|
| 95 | iStartSelectionPoint = null;
|
|---|
| 96 | }
|
|---|
| 97 | }
|
|---|
| 98 | }
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | }
|
|---|