| 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 |
|
|---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 5 |
|
|---|
| 6 | import java.awt.BorderLayout;
|
|---|
| 7 | import java.awt.Color;
|
|---|
| 8 | import java.awt.Component;
|
|---|
| 9 | import java.awt.Dimension;
|
|---|
| 10 | import java.awt.Graphics;
|
|---|
| 11 | import java.awt.Point;
|
|---|
| 12 | import java.awt.Toolkit;
|
|---|
| 13 | import java.awt.event.ComponentEvent;
|
|---|
| 14 | import java.awt.event.ComponentListener;
|
|---|
| 15 | import java.awt.geom.Point2D;
|
|---|
| 16 | import java.util.Vector;
|
|---|
| 17 |
|
|---|
| 18 | import javax.swing.JLabel;
|
|---|
| 19 | import javax.swing.JPanel;
|
|---|
| 20 |
|
|---|
| 21 | import org.openstreetmap.gui.jmapviewer.JMapViewer;
|
|---|
| 22 | import org.openstreetmap.gui.jmapviewer.MapMarkerDot;
|
|---|
| 23 | import org.openstreetmap.gui.jmapviewer.MemoryTileCache;
|
|---|
| 24 | import org.openstreetmap.gui.jmapviewer.OsmFileCacheTileLoader;
|
|---|
| 25 | import org.openstreetmap.gui.jmapviewer.OsmMercator;
|
|---|
| 26 | import org.openstreetmap.gui.jmapviewer.OsmTileLoader;
|
|---|
| 27 | import org.openstreetmap.gui.jmapviewer.OsmTileSource;
|
|---|
| 28 | import org.openstreetmap.gui.jmapviewer.OsmTileSource.CycleMap;
|
|---|
| 29 | import org.openstreetmap.gui.jmapviewer.interfaces.MapMarker;
|
|---|
| 30 | import org.openstreetmap.gui.jmapviewer.interfaces.TileLoader;
|
|---|
| 31 | import org.openstreetmap.gui.jmapviewer.interfaces.TileSource;
|
|---|
| 32 | import org.openstreetmap.josm.Main;
|
|---|
| 33 | import org.openstreetmap.josm.gui.download.DownloadDialog;
|
|---|
| 34 | import org.openstreetmap.josm.gui.download.DownloadSelection;
|
|---|
| 35 |
|
|---|
| 36 | /**
|
|---|
| 37 | * JComponent that displays the slippy map tiles
|
|---|
| 38 | *
|
|---|
| 39 | * @author Tim Haussmann
|
|---|
| 40 | *
|
|---|
| 41 | */
|
|---|
| 42 | public class SlippyMapChooser extends JMapViewer implements DownloadSelection, ComponentListener {
|
|---|
| 43 |
|
|---|
| 44 | private DownloadDialog iGui;
|
|---|
| 45 |
|
|---|
| 46 | // upper left and lower right corners of the selection rectangle (x/y on
|
|---|
| 47 | // ZOOM_MAX)
|
|---|
| 48 | Point iSelectionRectStart;
|
|---|
| 49 | Point iSelectionRectEnd;
|
|---|
| 50 |
|
|---|
| 51 | private SizeButton iSizeButton = new SizeButton();
|
|---|
| 52 | private SourceButton iSourceButton = new SourceButton();
|
|---|
| 53 |
|
|---|
| 54 | // standard dimension
|
|---|
| 55 | private Dimension iDownloadDialogDimension;
|
|---|
| 56 | // screen size
|
|---|
| 57 | private Dimension iScreenSize;
|
|---|
| 58 |
|
|---|
| 59 | private TileSource[] sources = { new OsmTileSource.Mapnik(), new OsmTileSource.TilesAtHome(),new OsmTileSource.CycleMap()};
|
|---|
| 60 | TileLoader cachedLoader;
|
|---|
| 61 | TileLoader uncachedLoader;
|
|---|
| 62 | JPanel slipyyMapTabPanel;
|
|---|
| 63 | boolean firstShown = true;
|
|---|
| 64 |
|
|---|
| 65 | /**
|
|---|
| 66 | * Create the chooser component.
|
|---|
| 67 | */
|
|---|
| 68 | public SlippyMapChooser() {
|
|---|
| 69 | super();
|
|---|
| 70 | cachedLoader = new OsmFileCacheTileLoader(this);
|
|---|
| 71 | uncachedLoader = new OsmTileLoader(this);
|
|---|
| 72 | setZoomContolsVisible(false);
|
|---|
| 73 | setMapMarkerVisible(false);
|
|---|
| 74 | setMinimumSize(new Dimension(350, 350 / 2));
|
|---|
| 75 | setFileCacheEnabled(SlippyMapChooserPlugin.ENABLE_FILE_CACHE);
|
|---|
| 76 | setMaxTilesInmemory(SlippyMapChooserPlugin.MAX_TILES_IN_MEMORY);
|
|---|
| 77 | addComponentListener(this);
|
|---|
| 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 | }
|
|---|
| 87 | else {
|
|---|
| 88 | if(!mapStyle.equals("mapnik"))
|
|---|
| 89 | Main.pref.put("slippy_map_chooser.mapstyle", "mapnik");
|
|---|
| 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 | public void addGui(final DownloadDialog gui) {
|
|---|
| 105 | iGui = gui;
|
|---|
| 106 | slipyyMapTabPanel = new JPanel();
|
|---|
| 107 | slipyyMapTabPanel.setLayout(new BorderLayout());
|
|---|
| 108 | slipyyMapTabPanel.add(this, BorderLayout.CENTER);
|
|---|
| 109 | slipyyMapTabPanel.add(new JLabel((tr("Zoom: Mousewheel or double click. "
|
|---|
| 110 | + "Move map: Hold right mousebutton and move mouse. Select: Click."))),
|
|---|
| 111 | BorderLayout.SOUTH);
|
|---|
| 112 | iGui.tabpane.add(slipyyMapTabPanel, tr("Slippy map"));
|
|---|
| 113 | iGui.tabpane.addComponentListener(this);
|
|---|
| 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 | Point2D.Double l1 = getPosition(p_max);
|
|---|
| 209 | Point2D.Double l2 = getPosition(p_min);
|
|---|
| 210 | iGui.minlat = Math.min(l2.x, l1.x);
|
|---|
| 211 | iGui.minlon = Math.min(l1.y, l2.y);
|
|---|
| 212 | iGui.maxlat = Math.max(l2.x, l1.x);
|
|---|
| 213 | iGui.maxlon = Math.max(l1.y, l2.y);
|
|---|
| 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 =
|
|---|
| 226 | iGui.getParent().getParent().getParent().getParent().getParent().getParent()
|
|---|
| 227 | .getParent().getParent().getParent();
|
|---|
| 228 | // remember the initial set screen dimensions
|
|---|
| 229 | iDownloadDialogDimension = c.getSize();
|
|---|
| 230 | // retrive the size of the display
|
|---|
| 231 | iScreenSize = Toolkit.getDefaultToolkit().getScreenSize();
|
|---|
| 232 | }
|
|---|
| 233 |
|
|---|
| 234 | // resize
|
|---|
| 235 | Component co =
|
|---|
| 236 | iGui.getParent().getParent().getParent().getParent().getParent().getParent()
|
|---|
| 237 | .getParent().getParent().getParent();
|
|---|
| 238 | Dimension currentDimension = co.getSize();
|
|---|
| 239 |
|
|---|
| 240 | // enlarge
|
|---|
| 241 | if (currentDimension.equals(iDownloadDialogDimension)) {
|
|---|
| 242 | // make the each dimension 90% of the absolute display size and
|
|---|
| 243 | // center the DownloadDialog
|
|---|
| 244 | int w = iScreenSize.width * 90 / 100;
|
|---|
| 245 | int h = iScreenSize.height * 90 / 100;
|
|---|
| 246 | co.setBounds((iScreenSize.width - w) / 2, (iScreenSize.height - h) / 2, w, h);
|
|---|
| 247 |
|
|---|
| 248 | }
|
|---|
| 249 | // shrink
|
|---|
| 250 | else {
|
|---|
| 251 | // set the size back to the initial dimensions and center the
|
|---|
| 252 | // DownloadDialog
|
|---|
| 253 | int w = iDownloadDialogDimension.width;
|
|---|
| 254 | int h = iDownloadDialogDimension.height;
|
|---|
| 255 | co.setBounds((iScreenSize.width - w) / 2, (iScreenSize.height - h) / 2, w, h);
|
|---|
| 256 |
|
|---|
| 257 | }
|
|---|
| 258 |
|
|---|
| 259 | repaint();
|
|---|
| 260 | }
|
|---|
| 261 |
|
|---|
| 262 | public void toggleMapSource(int mapSource) {
|
|---|
| 263 | this.tileCache = 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 | public void componentHidden(ComponentEvent e) {
|
|---|
| 277 | }
|
|---|
| 278 |
|
|---|
| 279 | public void componentMoved(ComponentEvent e) {
|
|---|
| 280 | }
|
|---|
| 281 |
|
|---|
| 282 | public void componentShown(ComponentEvent e) {
|
|---|
| 283 | }
|
|---|
| 284 |
|
|---|
| 285 | public void componentResized(ComponentEvent e) {
|
|---|
| 286 | if (!this.equals(e.getSource()) || getHeight() == 0 || getWidth() == 0)
|
|---|
| 287 | return;
|
|---|
| 288 | firstShown = false;
|
|---|
| 289 | // The bounding box has to be set after SlippyMapChooser's size has been
|
|---|
| 290 | // finally set - otherwise the zoom level will be totally wrong (too wide)
|
|---|
| 291 | boundingBoxChanged(iGui);
|
|---|
| 292 | }
|
|---|
| 293 |
|
|---|
| 294 | }
|
|---|