source: josm/trunk/src/org/openstreetmap/josm/gui/download/OsmMapControl.java@ 1415

Last change on this file since 1415 was 1390, checked in by stoecker, 15 years ago

added slippy_map_chooser

File size: 4.2 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
3package org.openstreetmap.josm.gui.download;
4
5import java.awt.Point;
6import java.awt.event.KeyEvent;
7import java.awt.event.MouseAdapter;
8import java.awt.event.MouseEvent;
9import java.awt.event.MouseListener;
10import java.awt.event.MouseMotionListener;
11
12import javax.swing.JComponent;
13import javax.swing.JPanel;
14import javax.swing.KeyStroke;
15
16/**
17 * This class controls the user input by listening to mouse and key events.
18 * Currently implemented is: - zooming in and out with scrollwheel - zooming in
19 * and centering by double clicking - selecting an area by clicking and dragging
20 * the mouse
21 *
22 * @author Tim Haussmann
23 */
24public class OsmMapControl extends MouseAdapter implements MouseMotionListener, MouseListener {
25
26 // start and end point of selection rectangle
27 private Point iStartSelectionPoint;
28 private Point iEndSelectionPoint;
29
30 // the SlippyMapChooserComponent
31 private final SlippyMapChooser iSlippyMapChooser;
32
33 private SizeButton iSizeButton = null;
34 private SourceButton iSourceButton = null;
35
36 /**
37 * Create a new OsmMapControl
38 */
39 public OsmMapControl(SlippyMapChooser navComp, JPanel contentPane, SizeButton sizeButton, SourceButton sourceButton) {
40 this.iSlippyMapChooser = navComp;
41 iSlippyMapChooser.addMouseListener(this);
42 iSlippyMapChooser.addMouseMotionListener(this);
43
44 String[] n = { ",", ".", "up", "right", "down", "left" };
45 int[] k =
46 { KeyEvent.VK_COMMA, KeyEvent.VK_PERIOD, KeyEvent.VK_UP, KeyEvent.VK_RIGHT,
47 KeyEvent.VK_DOWN, KeyEvent.VK_LEFT };
48
49 if (contentPane != null) {
50 for (int i = 0; i < n.length; ++i) {
51 contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
52 KeyStroke.getKeyStroke(k[i], KeyEvent.CTRL_DOWN_MASK),
53 "MapMover.Zoomer." + n[i]);
54 }
55 }
56 iSizeButton = sizeButton;
57 iSourceButton = sourceButton;
58 }
59
60 /**
61 * Start drawing the selection rectangle if it was the 1st button (left
62 * button)
63 */
64 @Override
65 public void mousePressed(MouseEvent e) {
66 if (e.getButton() == MouseEvent.BUTTON1) {
67 if (!iSizeButton.hit(e.getPoint())) {
68 iStartSelectionPoint = e.getPoint();
69 iEndSelectionPoint = e.getPoint();
70 }
71 }
72
73 }
74
75 public void mouseDragged(MouseEvent e) {
76 if((e.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK) == MouseEvent.BUTTON1_DOWN_MASK){
77 if (iStartSelectionPoint != null) {
78 iEndSelectionPoint = e.getPoint();
79 iSlippyMapChooser.setSelection(iStartSelectionPoint, iEndSelectionPoint);
80 }
81 }
82 }
83
84 /**
85 * When dragging the map change the cursor back to it's pre-move cursor. If
86 * a double-click occurs center and zoom the map on the clicked location.
87 */
88 @Override
89 public void mouseReleased(MouseEvent e) {
90 if (e.getButton() == MouseEvent.BUTTON1) {
91
92 int sourceButton = iSourceButton.hit(e.getPoint());
93
94 if (iSizeButton.hit(e.getPoint())) {
95 iSizeButton.toggle();
96 iSlippyMapChooser.resizeSlippyMap();
97 }
98 else if(sourceButton == SourceButton.HIDE_OR_SHOW) {
99 iSourceButton.toggle();
100 iSlippyMapChooser.repaint();
101
102 }else if(sourceButton == SourceButton.MAPNIK || sourceButton == SourceButton.OSMARENDER || sourceButton == SourceButton.CYCLEMAP) {
103 iSlippyMapChooser.toggleMapSource(sourceButton);
104 }
105 else {
106 if (e.getClickCount() == 1) {
107 iSlippyMapChooser.setSelection(iStartSelectionPoint, e.getPoint());
108
109 // reset the selections start and end
110 iEndSelectionPoint = null;
111 iStartSelectionPoint = null;
112 }
113 }
114
115 }
116 }
117
118 public void mouseMoved(MouseEvent e) {
119 }
120
121}
Note: See TracBrowser for help on using the repository browser.