| 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 | private SourceButton iSourceButton = null;
|
|---|
| 34 |
|
|---|
| 35 | /**
|
|---|
| 36 | * Create a new OsmMapControl
|
|---|
| 37 | */
|
|---|
| 38 | public OsmMapControl(SlippyMapChooser navComp, JPanel contentPane, SizeButton sizeButton, SourceButton sourceButton) {
|
|---|
| 39 | this.iSlippyMapChooser = navComp;
|
|---|
| 40 | iSlippyMapChooser.addMouseListener(this);
|
|---|
| 41 | iSlippyMapChooser.addMouseMotionListener(this);
|
|---|
| 42 |
|
|---|
| 43 | String[] n = { ",", ".", "up", "right", "down", "left" };
|
|---|
| 44 | int[] k =
|
|---|
| 45 | { KeyEvent.VK_COMMA, KeyEvent.VK_PERIOD, KeyEvent.VK_UP, KeyEvent.VK_RIGHT,
|
|---|
| 46 | KeyEvent.VK_DOWN, KeyEvent.VK_LEFT };
|
|---|
| 47 |
|
|---|
| 48 | if (contentPane != null) {
|
|---|
| 49 | for (int i = 0; i < n.length; ++i) {
|
|---|
| 50 | contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
|
|---|
| 51 | KeyStroke.getKeyStroke(k[i], KeyEvent.CTRL_DOWN_MASK),
|
|---|
| 52 | "MapMover.Zoomer." + n[i]);
|
|---|
| 53 | }
|
|---|
| 54 | }
|
|---|
| 55 | iSizeButton = sizeButton;
|
|---|
| 56 | iSourceButton = sourceButton;
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | /**
|
|---|
| 60 | * Start drawing the selection rectangle if it was the 1st button (left
|
|---|
| 61 | * button)
|
|---|
| 62 | */
|
|---|
| 63 | @Override
|
|---|
| 64 | public void mousePressed(MouseEvent e) {
|
|---|
| 65 | if (e.getButton() == MouseEvent.BUTTON1) {
|
|---|
| 66 | if (!iSizeButton.hit(e.getPoint())) {
|
|---|
| 67 | iStartSelectionPoint = e.getPoint();
|
|---|
| 68 | iEndSelectionPoint = e.getPoint();
|
|---|
| 69 | }
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | public void mouseDragged(MouseEvent e) {
|
|---|
| 75 | if((e.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK) == MouseEvent.BUTTON1_DOWN_MASK){
|
|---|
| 76 | if (iStartSelectionPoint != null) {
|
|---|
| 77 | iEndSelectionPoint = e.getPoint();
|
|---|
| 78 | iSlippyMapChooser.setSelection(iStartSelectionPoint, iEndSelectionPoint);
|
|---|
| 79 | }
|
|---|
| 80 | }
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | /**
|
|---|
| 84 | * When dragging the map change the cursor back to it's pre-move cursor. If
|
|---|
| 85 | * a double-click occurs center and zoom the map on the clicked location.
|
|---|
| 86 | */
|
|---|
| 87 | @Override
|
|---|
| 88 | public void mouseReleased(MouseEvent e) {
|
|---|
| 89 | if (e.getButton() == MouseEvent.BUTTON1) {
|
|---|
| 90 |
|
|---|
| 91 | int sourceButton = iSourceButton.hit(e.getPoint());
|
|---|
| 92 |
|
|---|
| 93 | if (iSizeButton.hit(e.getPoint())) {
|
|---|
| 94 | iSizeButton.toggle();
|
|---|
| 95 | iSlippyMapChooser.resizeSlippyMap();
|
|---|
| 96 | }
|
|---|
| 97 | else if(sourceButton == SourceButton.HIDE_OR_SHOW) {
|
|---|
| 98 | iSourceButton.toggle();
|
|---|
| 99 | iSlippyMapChooser.repaint();
|
|---|
| 100 |
|
|---|
| 101 | }else if(sourceButton == SourceButton.MAPNIK || sourceButton == SourceButton.OSMARENDER) {
|
|---|
| 102 | iSlippyMapChooser.toggleMapSource(sourceButton);
|
|---|
| 103 | }
|
|---|
| 104 | else {
|
|---|
| 105 | if (e.getClickCount() == 1) {
|
|---|
| 106 | iSlippyMapChooser.setSelection(iStartSelectionPoint, e.getPoint());
|
|---|
| 107 |
|
|---|
| 108 | // reset the selections start and end
|
|---|
| 109 | iEndSelectionPoint = null;
|
|---|
| 110 | iStartSelectionPoint = null;
|
|---|
| 111 | }
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | }
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | public void mouseMoved(MouseEvent e) {
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | }
|
|---|