| 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.geom.Point2D;
|
|---|
| 14 | import java.util.Vector;
|
|---|
| 15 |
|
|---|
| 16 | import javax.swing.JLabel;
|
|---|
| 17 | import javax.swing.JPanel;
|
|---|
| 18 |
|
|---|
| 19 | import org.openstreetmap.gui.jmapviewer.JMapViewer;
|
|---|
| 20 | import org.openstreetmap.gui.jmapviewer.MapMarkerDot;
|
|---|
| 21 | import org.openstreetmap.gui.jmapviewer.MemoryTileCache;
|
|---|
| 22 | import org.openstreetmap.gui.jmapviewer.OsmMercator;
|
|---|
| 23 | import org.openstreetmap.gui.jmapviewer.OsmTileSource;
|
|---|
| 24 | import org.openstreetmap.gui.jmapviewer.interfaces.MapMarker;
|
|---|
| 25 | import org.openstreetmap.gui.jmapviewer.interfaces.TileSource;
|
|---|
| 26 | import org.openstreetmap.josm.gui.download.DownloadDialog;
|
|---|
| 27 | import org.openstreetmap.josm.gui.download.DownloadSelection;
|
|---|
| 28 |
|
|---|
| 29 | /**
|
|---|
| 30 | * JComponent that displays the slippy map tiles
|
|---|
| 31 | *
|
|---|
| 32 | * @author Tim Haussmann
|
|---|
| 33 | *
|
|---|
| 34 | */
|
|---|
| 35 | public class SlippyMapChooser extends JMapViewer implements DownloadSelection {
|
|---|
| 36 |
|
|---|
| 37 | private DownloadDialog iGui;
|
|---|
| 38 |
|
|---|
| 39 | // upper left and lower right corners of the selection rectangle (x/y on
|
|---|
| 40 | // ZOOM_MAX)
|
|---|
| 41 | Point iSelectionRectStart;
|
|---|
| 42 | Point iSelectionRectEnd;
|
|---|
| 43 |
|
|---|
| 44 | private SizeButton iSizeButton = new SizeButton();
|
|---|
| 45 | private SourceButton iSourceButton = new SourceButton();
|
|---|
| 46 |
|
|---|
| 47 | // standard dimension
|
|---|
| 48 | private Dimension iDownloadDialogDimension;
|
|---|
| 49 | // screen size
|
|---|
| 50 | private Dimension iScreenSize;
|
|---|
| 51 |
|
|---|
| 52 | private TileSource[] sources = { new OsmTileSource.Mapnik(), new OsmTileSource.TilesAtHome() };
|
|---|
| 53 |
|
|---|
| 54 | /**
|
|---|
| 55 | * Create the chooser component.
|
|---|
| 56 | */
|
|---|
| 57 | public SlippyMapChooser() {
|
|---|
| 58 | super();
|
|---|
| 59 | setZoomContolsVisible(false);
|
|---|
| 60 | setMapMarkerVisible(false);
|
|---|
| 61 | setMinimumSize(new Dimension(350, 350 / 2));
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | public void addGui(final DownloadDialog gui) {
|
|---|
| 65 | iGui = gui;
|
|---|
| 66 | JPanel temp = new JPanel();
|
|---|
| 67 | temp.setLayout(new BorderLayout());
|
|---|
| 68 | temp.add(this, BorderLayout.CENTER);
|
|---|
| 69 | temp.add(new JLabel((tr("Zoom: Mousewheel or double click. "
|
|---|
| 70 | + "Move map: Hold right mousebutton and move mouse. Select: Click."))),
|
|---|
| 71 | BorderLayout.SOUTH);
|
|---|
| 72 | iGui.tabpane.add(temp, tr("Slippy map"));
|
|---|
| 73 |
|
|---|
| 74 | new OsmMapControl(this, temp, iSizeButton, iSourceButton);
|
|---|
| 75 | boundingBoxChanged(gui);
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | protected Point getTopLeftCoordinates() {
|
|---|
| 79 | return new Point(center.x - (getWidth() / 2), center.y - (getHeight() / 2));
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | /**
|
|---|
| 83 | * Draw the map.
|
|---|
| 84 | */
|
|---|
| 85 | @Override
|
|---|
| 86 | public void paint(Graphics g) {
|
|---|
| 87 | try {
|
|---|
| 88 | super.paint(g);
|
|---|
| 89 |
|
|---|
| 90 | // draw selection rectangle
|
|---|
| 91 | if (iSelectionRectStart != null && iSelectionRectEnd != null) {
|
|---|
| 92 |
|
|---|
| 93 | int zoomDiff = MAX_ZOOM - zoom;
|
|---|
| 94 | Point tlc = getTopLeftCoordinates();
|
|---|
| 95 | int x_min = (iSelectionRectStart.x >> zoomDiff) - tlc.x;
|
|---|
| 96 | int y_min = (iSelectionRectStart.y >> zoomDiff) - tlc.y;
|
|---|
| 97 | int x_max = (iSelectionRectEnd.x >> zoomDiff) - tlc.x;
|
|---|
| 98 | int y_max = (iSelectionRectEnd.y >> zoomDiff) - tlc.y;
|
|---|
| 99 |
|
|---|
| 100 | int w = x_max - x_min;
|
|---|
| 101 | int h = y_max - y_min;
|
|---|
| 102 | g.setColor(new Color(0.9f, 0.7f, 0.7f, 0.6f));
|
|---|
| 103 | g.fillRect(x_min, y_min, w, h);
|
|---|
| 104 |
|
|---|
| 105 | g.setColor(Color.BLACK);
|
|---|
| 106 | g.drawRect(x_min, y_min, w, h);
|
|---|
| 107 |
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | iSizeButton.paint(g);
|
|---|
| 111 | iSourceButton.paint(g);
|
|---|
| 112 | } catch (Exception e) {
|
|---|
| 113 | e.printStackTrace();
|
|---|
| 114 | }
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | public void boundingBoxChanged(DownloadDialog gui) {
|
|---|
| 118 |
|
|---|
| 119 | // test if a bounding box has been set set
|
|---|
| 120 | if (gui.minlat == 0.0 && gui.minlon == 0.0 && gui.maxlat == 0.0 && gui.maxlon == 0.0)
|
|---|
| 121 | return;
|
|---|
| 122 |
|
|---|
| 123 | int y1 = OsmMercator.LatToY(gui.minlat, MAX_ZOOM);
|
|---|
| 124 | int y2 = OsmMercator.LatToY(gui.maxlat, MAX_ZOOM);
|
|---|
| 125 | int x1 = OsmMercator.LonToX(gui.minlon, MAX_ZOOM);
|
|---|
| 126 | int x2 = OsmMercator.LonToX(gui.maxlon, MAX_ZOOM);
|
|---|
| 127 |
|
|---|
| 128 | iSelectionRectStart = new Point(Math.min(x1, x2), Math.min(y1, y2));
|
|---|
| 129 | iSelectionRectEnd = new Point(Math.max(x1, x2), Math.max(y1, y2));
|
|---|
| 130 |
|
|---|
| 131 | // calc the screen coordinates for the new selection rectangle
|
|---|
| 132 | MapMarkerDot xmin_ymin = new MapMarkerDot(gui.minlat, gui.minlon);
|
|---|
| 133 | MapMarkerDot xmax_ymax = new MapMarkerDot(gui.maxlat, gui.maxlon);
|
|---|
| 134 |
|
|---|
| 135 | Vector<MapMarker> marker = new Vector<MapMarker>(2);
|
|---|
| 136 | marker.add(xmin_ymin);
|
|---|
| 137 | marker.add(xmax_ymax);
|
|---|
| 138 | setMapMarkerList(marker);
|
|---|
| 139 | setDisplayToFitMapMarkers();
|
|---|
| 140 | zoomOut();
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | /**
|
|---|
| 144 | * Callback for the OsmMapControl. (Re-)Sets the start and end point of the
|
|---|
| 145 | * selection rectangle.
|
|---|
| 146 | *
|
|---|
| 147 | * @param aStart
|
|---|
| 148 | * @param aEnd
|
|---|
| 149 | */
|
|---|
| 150 | public void setSelection(Point aStart, Point aEnd) {
|
|---|
| 151 | if (aStart == null || aEnd == null)
|
|---|
| 152 | return;
|
|---|
| 153 | Point p_max = new Point(Math.max(aEnd.x, aStart.x), Math.max(aEnd.y, aStart.y));
|
|---|
| 154 | Point p_min = new Point(Math.min(aEnd.x, aStart.x), Math.min(aEnd.y, aStart.y));
|
|---|
| 155 |
|
|---|
| 156 | Point tlc = getTopLeftCoordinates();
|
|---|
| 157 | int zoomDiff = MAX_ZOOM - zoom;
|
|---|
| 158 | Point pEnd = new Point(p_max.x + tlc.x, p_max.y + tlc.y);
|
|---|
| 159 | Point pStart = new Point(p_min.x + tlc.x, p_min.y + tlc.y);
|
|---|
| 160 |
|
|---|
| 161 | pEnd.x <<= zoomDiff;
|
|---|
| 162 | pEnd.y <<= zoomDiff;
|
|---|
| 163 | pStart.x <<= zoomDiff;
|
|---|
| 164 | pStart.y <<= zoomDiff;
|
|---|
| 165 |
|
|---|
| 166 | iSelectionRectStart = pStart;
|
|---|
| 167 | iSelectionRectEnd = pEnd;
|
|---|
| 168 |
|
|---|
| 169 | Point2D.Double l1 = getPosition(p_max);
|
|---|
| 170 | Point2D.Double l2 = getPosition(p_min);
|
|---|
| 171 | iGui.minlat = Math.min(l2.x, l1.x);
|
|---|
| 172 | iGui.minlon = Math.min(l1.y, l2.y);
|
|---|
| 173 | iGui.maxlat = Math.max(l2.x, l1.x);
|
|---|
| 174 | iGui.maxlon = Math.max(l1.y, l2.y);
|
|---|
| 175 |
|
|---|
| 176 | iGui.boundingBoxChanged(this);
|
|---|
| 177 | repaint();
|
|---|
| 178 | }
|
|---|
| 179 |
|
|---|
| 180 | /**
|
|---|
| 181 | * Performs resizing of the DownloadDialog in order to enlarge or shrink the
|
|---|
| 182 | * map.
|
|---|
| 183 | */
|
|---|
| 184 | public void resizeSlippyMap() {
|
|---|
| 185 | if (iScreenSize == null) {
|
|---|
| 186 | Component c =
|
|---|
| 187 | iGui.getParent().getParent().getParent().getParent().getParent().getParent()
|
|---|
| 188 | .getParent().getParent().getParent();
|
|---|
| 189 | // remember the initial set screen dimensions
|
|---|
| 190 | iDownloadDialogDimension = c.getSize();
|
|---|
| 191 | // retrive the size of the display
|
|---|
| 192 | iScreenSize = Toolkit.getDefaultToolkit().getScreenSize();
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | // resize
|
|---|
| 196 | Component co =
|
|---|
| 197 | iGui.getParent().getParent().getParent().getParent().getParent().getParent()
|
|---|
| 198 | .getParent().getParent().getParent();
|
|---|
| 199 | Dimension currentDimension = co.getSize();
|
|---|
| 200 |
|
|---|
| 201 | // enlarge
|
|---|
| 202 | if (currentDimension.equals(iDownloadDialogDimension)) {
|
|---|
| 203 | // make the each dimension 90% of the absolute display size and
|
|---|
| 204 | // center the DownloadDialog
|
|---|
| 205 | int w = iScreenSize.width * 90 / 100;
|
|---|
| 206 | int h = iScreenSize.height * 90 / 100;
|
|---|
| 207 | co.setBounds((iScreenSize.width - w) / 2, (iScreenSize.height - h) / 2, w, h);
|
|---|
| 208 |
|
|---|
| 209 | }
|
|---|
| 210 | // shrink
|
|---|
| 211 | else {
|
|---|
| 212 | // set the size back to the initial dimensions and center the
|
|---|
| 213 | // DownloadDialog
|
|---|
| 214 | int w = iDownloadDialogDimension.width;
|
|---|
| 215 | int h = iDownloadDialogDimension.height;
|
|---|
| 216 | co.setBounds((iScreenSize.width - w) / 2, (iScreenSize.height - h) / 2, w, h);
|
|---|
| 217 |
|
|---|
| 218 | }
|
|---|
| 219 |
|
|---|
| 220 | repaint();
|
|---|
| 221 | }
|
|---|
| 222 |
|
|---|
| 223 | public void toggleMapSource(int mapSource) {
|
|---|
| 224 | this.tileCache = new MemoryTileCache();
|
|---|
| 225 | if (mapSource == SourceButton.MAPNIK) {
|
|---|
| 226 | this.setTileSource(sources[0]);
|
|---|
| 227 | } else {
|
|---|
| 228 | this.setTileSource(sources[1]);
|
|---|
| 229 | }
|
|---|
| 230 | }
|
|---|
| 231 |
|
|---|
| 232 | }
|
|---|