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

Last change on this file since 2689 was 2689, checked in by Gubaer, 17 years ago

new: Changeset Cache Manager for querying, downloading, browsing, and managing changesets within JOSM. See also Changeset Manager and Changeset Query Dialog

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