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

Last change on this file since 3083 was 3083, checked in by bastiK, 15 years ago

added svn:eol-style=native to source files

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