source: osm/applications/editors/josm/plugins/slippy_map_chooser/src/SlippyMapChooser.java@ 10310

Last change on this file since 10310 was 10310, checked in by stotz, 17 years ago

Bugfix: Zoom level was wrong when displaying an external specified bounding box for the first time

File size: 8.5 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
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.BorderLayout;
7import java.awt.Color;
8import java.awt.Component;
9import java.awt.Dimension;
10import java.awt.Graphics;
11import java.awt.Point;
12import java.awt.Toolkit;
13import java.awt.event.ComponentEvent;
14import java.awt.event.ComponentListener;
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.gui.download.DownloadDialog;
32import org.openstreetmap.josm.gui.download.DownloadSelection;
33
34/**
35 * JComponent that displays the slippy map tiles
36 *
37 * @author Tim Haussmann
38 *
39 */
40public class SlippyMapChooser extends JMapViewer implements DownloadSelection, ComponentListener {
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 TileLoader cachedLoader;
59 TileLoader uncachedLoader;
60 JPanel slipyyMapTabPanel;
61 boolean firstShown = true;
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 setFileCacheEnabled(SlippyMapChooserPlugin.ENABLE_FILE_CACHE);
74 setMaxTilesInmemory(SlippyMapChooserPlugin.MAX_TILES_IN_MEMORY);
75 addComponentListener(this);
76 }
77
78 public void setMaxTilesInmemory(int tiles) {
79 ((MemoryTileCache) getTileCache()).setCacheSize(tiles);
80 }
81
82 public void setFileCacheEnabled(boolean enabled) {
83 if (enabled)
84 setTileLoader(cachedLoader);
85 else
86 setTileLoader(uncachedLoader);
87 }
88
89 public void addGui(final DownloadDialog gui) {
90 iGui = gui;
91 slipyyMapTabPanel = new JPanel();
92 slipyyMapTabPanel.setLayout(new BorderLayout());
93 slipyyMapTabPanel.add(this, BorderLayout.CENTER);
94 slipyyMapTabPanel.add(new JLabel((tr("Zoom: Mousewheel or double click. "
95 + "Move map: Hold right mousebutton and move mouse. Select: Click."))),
96 BorderLayout.SOUTH);
97 iGui.tabpane.add(slipyyMapTabPanel, tr("Slippy map"));
98 iGui.tabpane.addComponentListener(this);
99 new OsmMapControl(this, slipyyMapTabPanel, iSizeButton, iSourceButton);
100 }
101
102 protected Point getTopLeftCoordinates() {
103 return new Point(center.x - (getWidth() / 2), center.y - (getHeight() / 2));
104 }
105
106 /**
107 * Draw the map.
108 */
109 @Override
110 public void paint(Graphics g) {
111 try {
112 super.paint(g);
113
114 // draw selection rectangle
115 if (iSelectionRectStart != null && iSelectionRectEnd != null) {
116
117 int zoomDiff = MAX_ZOOM - zoom;
118 Point tlc = getTopLeftCoordinates();
119 int x_min = (iSelectionRectStart.x >> zoomDiff) - tlc.x;
120 int y_min = (iSelectionRectStart.y >> zoomDiff) - tlc.y;
121 int x_max = (iSelectionRectEnd.x >> zoomDiff) - tlc.x;
122 int y_max = (iSelectionRectEnd.y >> zoomDiff) - tlc.y;
123
124 int w = x_max - x_min;
125 int h = y_max - y_min;
126 g.setColor(new Color(0.9f, 0.7f, 0.7f, 0.6f));
127 g.fillRect(x_min, y_min, w, h);
128
129 g.setColor(Color.BLACK);
130 g.drawRect(x_min, y_min, w, h);
131
132 }
133
134 iSizeButton.paint(g);
135 iSourceButton.paint(g);
136 } catch (Exception e) {
137 e.printStackTrace();
138 }
139 }
140
141 public void boundingBoxChanged(DownloadDialog gui) {
142
143 // test if a bounding box has been set set
144 if (gui.minlat == 0.0 && gui.minlon == 0.0 && gui.maxlat == 0.0 && gui.maxlon == 0.0)
145 return;
146
147 int y1 = OsmMercator.LatToY(gui.minlat, MAX_ZOOM);
148 int y2 = OsmMercator.LatToY(gui.maxlat, MAX_ZOOM);
149 int x1 = OsmMercator.LonToX(gui.minlon, MAX_ZOOM);
150 int x2 = OsmMercator.LonToX(gui.maxlon, MAX_ZOOM);
151
152 iSelectionRectStart = new Point(Math.min(x1, x2), Math.min(y1, y2));
153 iSelectionRectEnd = new Point(Math.max(x1, x2), Math.max(y1, y2));
154
155 // calc the screen coordinates for the new selection rectangle
156 MapMarkerDot xmin_ymin = new MapMarkerDot(gui.minlat, gui.minlon);
157 MapMarkerDot xmax_ymax = new MapMarkerDot(gui.maxlat, gui.maxlon);
158
159 Vector<MapMarker> marker = new Vector<MapMarker>(2);
160 marker.add(xmin_ymin);
161 marker.add(xmax_ymax);
162 setMapMarkerList(marker);
163 setDisplayToFitMapMarkers();
164 zoomOut();
165 }
166
167 /**
168 * Callback for the OsmMapControl. (Re-)Sets the start and end point of the
169 * selection rectangle.
170 *
171 * @param aStart
172 * @param aEnd
173 */
174 public void setSelection(Point aStart, Point aEnd) {
175 if (aStart == null || aEnd == null)
176 return;
177 Point p_max = new Point(Math.max(aEnd.x, aStart.x), Math.max(aEnd.y, aStart.y));
178 Point p_min = new Point(Math.min(aEnd.x, aStart.x), Math.min(aEnd.y, aStart.y));
179
180 Point tlc = getTopLeftCoordinates();
181 int zoomDiff = MAX_ZOOM - zoom;
182 Point pEnd = new Point(p_max.x + tlc.x, p_max.y + tlc.y);
183 Point pStart = new Point(p_min.x + tlc.x, p_min.y + tlc.y);
184
185 pEnd.x <<= zoomDiff;
186 pEnd.y <<= zoomDiff;
187 pStart.x <<= zoomDiff;
188 pStart.y <<= zoomDiff;
189
190 iSelectionRectStart = pStart;
191 iSelectionRectEnd = pEnd;
192
193 Point2D.Double l1 = getPosition(p_max);
194 Point2D.Double l2 = getPosition(p_min);
195 iGui.minlat = Math.min(l2.x, l1.x);
196 iGui.minlon = Math.min(l1.y, l2.y);
197 iGui.maxlat = Math.max(l2.x, l1.x);
198 iGui.maxlon = Math.max(l1.y, l2.y);
199
200 iGui.boundingBoxChanged(this);
201 repaint();
202 }
203
204 /**
205 * Performs resizing of the DownloadDialog in order to enlarge or shrink the
206 * map.
207 */
208 public void resizeSlippyMap() {
209 if (iScreenSize == null) {
210 Component c =
211 iGui.getParent().getParent().getParent().getParent().getParent().getParent()
212 .getParent().getParent().getParent();
213 // remember the initial set screen dimensions
214 iDownloadDialogDimension = c.getSize();
215 // retrive the size of the display
216 iScreenSize = Toolkit.getDefaultToolkit().getScreenSize();
217 }
218
219 // resize
220 Component co =
221 iGui.getParent().getParent().getParent().getParent().getParent().getParent()
222 .getParent().getParent().getParent();
223 Dimension currentDimension = co.getSize();
224
225 // enlarge
226 if (currentDimension.equals(iDownloadDialogDimension)) {
227 // make the each dimension 90% of the absolute display size and
228 // center the DownloadDialog
229 int w = iScreenSize.width * 90 / 100;
230 int h = iScreenSize.height * 90 / 100;
231 co.setBounds((iScreenSize.width - w) / 2, (iScreenSize.height - h) / 2, w, h);
232
233 }
234 // shrink
235 else {
236 // set the size back to the initial dimensions and center the
237 // DownloadDialog
238 int w = iDownloadDialogDimension.width;
239 int h = iDownloadDialogDimension.height;
240 co.setBounds((iScreenSize.width - w) / 2, (iScreenSize.height - h) / 2, w, h);
241
242 }
243
244 repaint();
245 }
246
247 public void toggleMapSource(int mapSource) {
248 this.tileCache = new MemoryTileCache();
249 if (mapSource == SourceButton.MAPNIK) {
250 this.setTileSource(sources[0]);
251 } else {
252 this.setTileSource(sources[1]);
253 }
254 }
255
256 public void componentHidden(ComponentEvent e) {
257 }
258
259 public void componentMoved(ComponentEvent e) {
260 }
261
262 public void componentShown(ComponentEvent e) {
263 }
264
265 public void componentResized(ComponentEvent e) {
266 if (!this.equals(e.getSource()) || getHeight() == 0 || getWidth() == 0)
267 return;
268 firstShown = false;
269 // The bounding box has to be set after SlippyMapChooser's size has been
270 // finally set - otherwise the zoom level will be totally wrong (too wide)
271 boundingBoxChanged(iGui);
272 }
273
274}
Note: See TracBrowser for help on using the repository browser.