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