source: josm/trunk/src/org/openstreetmap/josm/gui/download/SlippyMapChooser.java@ 1602

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

better keyboard support for slippy map chooser - patch by jpstotz

File size: 9.7 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 static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.awt.BorderLayout;
8import java.awt.Color;
9import java.awt.Component;
10import java.awt.Dimension;
11import java.awt.Graphics;
12import java.awt.Point;
13import java.awt.Rectangle;
14import java.awt.Toolkit;
15import java.awt.geom.Point2D;
16import java.util.Vector;
17
18import javax.swing.JLabel;
19import javax.swing.JPanel;
20
21import org.openstreetmap.gui.jmapviewer.JMapViewer;
22import org.openstreetmap.gui.jmapviewer.MapMarkerDot;
23import org.openstreetmap.gui.jmapviewer.MemoryTileCache;
24import org.openstreetmap.gui.jmapviewer.OsmFileCacheTileLoader;
25import org.openstreetmap.gui.jmapviewer.OsmMercator;
26import org.openstreetmap.gui.jmapviewer.OsmTileLoader;
27import org.openstreetmap.gui.jmapviewer.OsmTileSource;
28import org.openstreetmap.gui.jmapviewer.interfaces.MapMarker;
29import org.openstreetmap.gui.jmapviewer.interfaces.TileLoader;
30import org.openstreetmap.gui.jmapviewer.interfaces.TileSource;
31import org.openstreetmap.josm.Main;
32
33/**
34 * JComponent that displays the slippy map tiles
35 *
36 * @author Tim Haussmann
37 *
38 */
39public class SlippyMapChooser extends JMapViewer implements DownloadSelection {
40
41 private DownloadDialog iGui;
42
43 // upper left and lower right corners of the selection rectangle (x/y on
44 // ZOOM_MAX)
45 Point iSelectionRectStart;
46 Point iSelectionRectEnd;
47
48 private SizeButton iSizeButton = new SizeButton();
49 private SourceButton iSourceButton = new SourceButton();
50
51 // standard dimension
52 private Dimension iDownloadDialogDimension;
53 // screen size
54 private Dimension iScreenSize;
55
56 private TileSource[] sources = { new OsmTileSource.Mapnik(), new OsmTileSource.TilesAtHome(),
57 new OsmTileSource.CycleMap() };
58 TileLoader cachedLoader;
59 TileLoader uncachedLoader;
60 JPanel slipyyMapTabPanel;
61
62 /**
63 * Create the chooser component.
64 */
65 public SlippyMapChooser() {
66 super();
67 cachedLoader = new OsmFileCacheTileLoader(this);
68 uncachedLoader = new OsmTileLoader(this);
69 setZoomContolsVisible(false);
70 setMapMarkerVisible(false);
71 setMinimumSize(new Dimension(350, 350 / 2));
72 // We need to set an initial size - this prevents a wrong zoom selection for
73 // the area before the component has been displayed the first time
74 setBounds(new Rectangle(getMinimumSize()));
75 setFileCacheEnabled(Main.pref.getBoolean("slippy_map_chooser.file_cache", true));
76 setMaxTilesInMemory(Main.pref.getInteger("slippy_map_chooser.max_tiles", 1000));
77
78 String mapStyle = Main.pref.get("slippy_map_chooser.mapstyle", "mapnik");
79 if (mapStyle.equals("osmarender")) {
80 iSourceButton.setMapStyle(SourceButton.OSMARENDER);
81 this.setTileSource(sources[1]);
82 } else if (mapStyle.equals("cyclemap")) {
83 iSourceButton.setMapStyle(SourceButton.CYCLEMAP);
84 this.setTileSource(sources[2]);
85 } else {
86 if (!mapStyle.equals("mapnik"))
87 Main.pref.put("slippy_map_chooser.mapstyle", "mapnik");
88 }
89 }
90
91 public void setMaxTilesInMemory(int tiles) {
92 ((MemoryTileCache) getTileCache()).setCacheSize(tiles);
93 }
94
95 public void setFileCacheEnabled(boolean enabled) {
96 if (enabled)
97 setTileLoader(cachedLoader);
98 else
99 setTileLoader(uncachedLoader);
100 }
101
102 public void addGui(final DownloadDialog gui) {
103 iGui = gui;
104 slipyyMapTabPanel = new JPanel();
105 slipyyMapTabPanel.setLayout(new BorderLayout());
106 slipyyMapTabPanel.add(this, BorderLayout.CENTER);
107 String labelText = "<b>Zoom:</b> Mousewheel, double click or Ctrl + Up/Down "
108 + "<b>Move map:</b> Hold right mousebutton and move mouse or use cursor keys. <b>Select:</b> Click.";
109 slipyyMapTabPanel.add(new JLabel("<html>" + tr(labelText) + "</html>"), BorderLayout.SOUTH);
110 iGui.tabpane.add(slipyyMapTabPanel, tr("Slippy map"));
111 new OsmMapControl(this, slipyyMapTabPanel, iSizeButton, iSourceButton);
112 }
113
114 protected Point getTopLeftCoordinates() {
115 return new Point(center.x - (getWidth() / 2), center.y - (getHeight() / 2));
116 }
117
118 /**
119 * Draw the map.
120 */
121 @Override
122 public void paint(Graphics g) {
123 try {
124 super.paint(g);
125
126 // draw selection rectangle
127 if (iSelectionRectStart != null && iSelectionRectEnd != null) {
128
129 int zoomDiff = MAX_ZOOM - zoom;
130 Point tlc = getTopLeftCoordinates();
131 int x_min = (iSelectionRectStart.x >> zoomDiff) - tlc.x;
132 int y_min = (iSelectionRectStart.y >> zoomDiff) - tlc.y;
133 int x_max = (iSelectionRectEnd.x >> zoomDiff) - tlc.x;
134 int y_max = (iSelectionRectEnd.y >> zoomDiff) - tlc.y;
135
136 int w = x_max - x_min;
137 int h = y_max - y_min;
138 g.setColor(new Color(0.9f, 0.7f, 0.7f, 0.6f));
139 g.fillRect(x_min, y_min, w, h);
140
141 g.setColor(Color.BLACK);
142 g.drawRect(x_min, y_min, w, h);
143
144 }
145
146 iSizeButton.paint(g);
147 iSourceButton.paint(g);
148 } catch (Exception e) {
149 e.printStackTrace();
150 }
151 }
152
153 public void boundingBoxChanged(DownloadDialog gui) {
154
155 // test if a bounding box has been set set
156 if (gui.minlat == 0.0 && gui.minlon == 0.0 && gui.maxlat == 0.0 && gui.maxlon == 0.0)
157 return;
158
159 int y1 = OsmMercator.LatToY(gui.minlat, MAX_ZOOM);
160 int y2 = OsmMercator.LatToY(gui.maxlat, MAX_ZOOM);
161 int x1 = OsmMercator.LonToX(gui.minlon, MAX_ZOOM);
162 int x2 = OsmMercator.LonToX(gui.maxlon, MAX_ZOOM);
163
164 iSelectionRectStart = new Point(Math.min(x1, x2), Math.min(y1, y2));
165 iSelectionRectEnd = new Point(Math.max(x1, x2), Math.max(y1, y2));
166
167 // calc the screen coordinates for the new selection rectangle
168 MapMarkerDot xmin_ymin = new MapMarkerDot(gui.minlat, gui.minlon);
169 MapMarkerDot xmax_ymax = new MapMarkerDot(gui.maxlat, gui.maxlon);
170
171 Vector<MapMarker> marker = new Vector<MapMarker>(2);
172 marker.add(xmin_ymin);
173 marker.add(xmax_ymax);
174 setMapMarkerList(marker);
175 setDisplayToFitMapMarkers();
176 zoomOut();
177 }
178
179 /**
180 * Callback for the OsmMapControl. (Re-)Sets the start and end point of the
181 * selection rectangle.
182 *
183 * @param aStart
184 * @param aEnd
185 */
186 public void setSelection(Point aStart, Point aEnd) {
187 if (aStart == null || aEnd == null)
188 return;
189 Point p_max = new Point(Math.max(aEnd.x, aStart.x), Math.max(aEnd.y, aStart.y));
190 Point p_min = new Point(Math.min(aEnd.x, aStart.x), Math.min(aEnd.y, aStart.y));
191
192 Point tlc = getTopLeftCoordinates();
193 int zoomDiff = MAX_ZOOM - zoom;
194 Point pEnd = new Point(p_max.x + tlc.x, p_max.y + tlc.y);
195 Point pStart = new Point(p_min.x + tlc.x, p_min.y + tlc.y);
196
197 pEnd.x <<= zoomDiff;
198 pEnd.y <<= zoomDiff;
199 pStart.x <<= zoomDiff;
200 pStart.y <<= zoomDiff;
201
202 iSelectionRectStart = pStart;
203 iSelectionRectEnd = pEnd;
204
205 Point2D.Double l1 = getPosition(p_max);
206 Point2D.Double l2 = getPosition(p_min);
207 iGui.minlat = Math.min(l2.x, l1.x);
208 iGui.minlon = Math.min(l1.y, l2.y);
209 iGui.maxlat = Math.max(l2.x, l1.x);
210 iGui.maxlon = Math.max(l1.y, l2.y);
211
212 iGui.boundingBoxChanged(this);
213 repaint();
214 }
215
216 /**
217 * Performs resizing of the DownloadDialog in order to enlarge or shrink the
218 * map.
219 */
220 public void resizeSlippyMap() {
221 if (iScreenSize == null) {
222 Component c = iGui.getParent().getParent().getParent().getParent().getParent().getParent().getParent()
223 .getParent().getParent();
224 // remember the initial set screen dimensions
225 iDownloadDialogDimension = c.getSize();
226 // retrive the size of the display
227 iScreenSize = Toolkit.getDefaultToolkit().getScreenSize();
228 }
229
230 // resize
231 Component co = iGui.getParent().getParent().getParent().getParent().getParent().getParent().getParent()
232 .getParent().getParent();
233 Dimension currentDimension = co.getSize();
234
235 // enlarge
236 if (currentDimension.equals(iDownloadDialogDimension)) {
237 // make the each dimension 90% of the absolute display size and
238 // center the DownloadDialog
239 int w = iScreenSize.width * 90 / 100;
240 int h = iScreenSize.height * 90 / 100;
241 co.setBounds((iScreenSize.width - w) / 2, (iScreenSize.height - h) / 2, w, h);
242
243 }
244 // shrink
245 else {
246 // set the size back to the initial dimensions and center the
247 // DownloadDialog
248 int w = iDownloadDialogDimension.width;
249 int h = iDownloadDialogDimension.height;
250 co.setBounds((iScreenSize.width - w) / 2, (iScreenSize.height - h) / 2, w, h);
251
252 }
253
254 repaint();
255 }
256
257 public void toggleMapSource(int mapSource) {
258 this.tileCache = new MemoryTileCache();
259 if (mapSource == SourceButton.MAPNIK) {
260 this.setTileSource(sources[0]);
261 Main.pref.put("slippy_map_chooser.mapstyle", "mapnik");
262 } else if (mapSource == SourceButton.CYCLEMAP) {
263 this.setTileSource(sources[2]);
264 Main.pref.put("slippy_map_chooser.mapstyle", "cyclemap");
265 } else {
266 this.setTileSource(sources[1]);
267 Main.pref.put("slippy_map_chooser.mapstyle", "osmarender");
268 }
269 }
270
271}
Note: See TracBrowser for help on using the repository browser.