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

Last change on this file since 2524 was 2524, checked in by stoecker, 14 years ago

close #2797 - no longer allow empty area in slippy map downloader

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