source: osm/applications/editors/josm/plugins/slippy_map_chooser/src/OsmMapControl.java@ 9819

Last change on this file since 9819 was 9623, checked in by tim, 16 years ago

ADDED:
Quick and dirty implementation to switch between Mapnik and Osmarender map tiles

File size: 3.6 KB
Line 
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
4import java.awt.Point;
5import java.awt.event.KeyEvent;
6import java.awt.event.MouseAdapter;
7import java.awt.event.MouseEvent;
8import java.awt.event.MouseListener;
9import java.awt.event.MouseMotionListener;
10
11import javax.swing.JComponent;
12import javax.swing.JPanel;
13import 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 */
23public 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}
Note: See TracBrowser for help on using the repository browser.