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

Last change on this file since 1686 was 1686, checked in by Gubaer, 15 years ago

fixed update in JMapViewer code

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