Changeset 16391 in osm for applications/viewer/jmapviewer/src/org/openstreetmap/gui
- Timestamp:
- 2009-07-09T00:12:12+02:00 (15 years ago)
- Location:
- applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer
- Files:
-
- 1 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/JMapViewer.java
r16032 r16391 20 20 import javax.swing.event.ChangeListener; 21 21 22 import org.openstreetmap.gui.jmapviewer.JobDispatcher.JobThread;23 22 import org.openstreetmap.gui.jmapviewer.interfaces.MapMarker; 24 23 import org.openstreetmap.gui.jmapviewer.interfaces.MapSquare; … … 48 47 public static final int MIN_ZOOM = 0; 49 48 50 protected TileLoader tileLoader;51 protected TileCache tileCache;52 protected TileSource tileSource;53 54 49 protected List<MapMarker> mapMarkerList; 55 50 protected List<MapSquare> mapSquareList; … … 59 54 60 55 protected boolean tileGridVisible; 56 57 protected TileController tileController; 61 58 62 59 /** … … 75 72 protected JButton zoomOutButton; 76 73 77 JobDispatcher jobDispatcher;74 78 75 79 76 /** … … 91 88 public JMapViewer(TileCache tileCache, int downloadThreadCount) { 92 89 super(); 93 tileSource = new OsmTileSource.Mapnik(); 94 tileLoader = new OsmTileLoader(this); 95 this.tileCache = tileCache; 96 jobDispatcher = JobDispatcher.getInstance(); 90 tileController = new TileController(new OsmTileSource.Mapnik(), tileCache, this); 97 91 mapMarkerList = new LinkedList<MapMarker>(); 98 92 mapSquareList = new LinkedList<MapSquare>(); … … 108 102 109 103 protected void initializeZoomSlider() { 110 zoomSlider = new JSlider(MIN_ZOOM, tile Source.getMaxZoom());104 zoomSlider = new JSlider(MIN_ZOOM, tileController.getTileSource().getMaxZoom()); 111 105 zoomSlider.setOrientation(JSlider.VERTICAL); 112 106 zoomSlider.setBounds(10, 10, 30, 150); … … 195 189 196 190 public void setDisplayPosition(Point mapPoint, int x, int y, int zoom) { 197 if (zoom > tile Source.getMaxZoom() || zoom < MIN_ZOOM)191 if (zoom > tileController.getTileSource().getMaxZoom() || zoom < MIN_ZOOM) 198 192 return; 199 193 … … 228 222 int x_max = Integer.MIN_VALUE; 229 223 int y_max = Integer.MIN_VALUE; 230 int mapZoomMax = tile Source.getMaxZoom();224 int mapZoomMax = tileController.getTileSource().getMaxZoom(); 231 225 for (MapMarker marker : mapMarkerList) { 232 226 int x = OsmMercator.LonToX(marker.getLon(), mapZoomMax); … … 258 252 setDisplayPosition(x, y, newZoom); 259 253 } 254 255 /** 256 * Sets the displayed map pane and zoom level so that all map markers are 257 * visible. 258 */ 259 public void setDisplayToFitMapSquares() { 260 if (mapSquareList == null || mapSquareList.size() == 0) { 261 return; 262 } 263 int x_min = Integer.MAX_VALUE; 264 int y_min = Integer.MAX_VALUE; 265 int x_max = Integer.MIN_VALUE; 266 int y_max = Integer.MIN_VALUE; 267 int mapZoomMax = tileController.getTileSource().getMaxZoom(); 268 for (MapSquare square : mapSquareList) { 269 x_max = Math.max(x_max, OsmMercator.LonToX(square.getBottomRight().getLon(), mapZoomMax)); 270 y_max = Math.max(y_max, OsmMercator.LatToY(square.getTopLeft().getLat(), mapZoomMax)); 271 x_min = Math.min(x_min, OsmMercator.LonToX(square.getTopLeft().getLon(), mapZoomMax)); 272 y_min = Math.min(y_min, OsmMercator.LatToY(square.getBottomRight().getLat(), mapZoomMax)); 273 } 274 int height = Math.max(0, getHeight()); 275 int width = Math.max(0, getWidth()); 276 // System.out.println(x_min + " < x < " + x_max); 277 // System.out.println(y_min + " < y < " + y_max); 278 // System.out.println("tiles: " + width + " " + height); 279 int newZoom = mapZoomMax; 280 int x = x_max - x_min; 281 int y = y_max - y_min; 282 while (x > width || y > height) { 283 // System.out.println("zoom: " + zoom + " -> " + x + " " + y); 284 newZoom--; 285 x >>= 1; 286 y >>= 1; 287 } 288 x = x_min + (x_max - x_min) / 2; 289 y = y_min + (y_max - y_min) / 2; 290 int z = 1 << (mapZoomMax - newZoom); 291 x /= z; 292 y /= z; 293 setDisplayPosition(x, y, newZoom); 294 } 260 295 261 296 public Coordinate getPosition() { … … 286 321 * @param lat 287 322 * @param lon 288 * @return point on the map or <code>null</code> if the point is not visible 289 */ 290 public Point getMapPosition(double lat, double lon) { 323 * @param checkOutside 324 * @return point on the map or <code>null</code> if the point is not visible and checkOutside set to <code>true</code> 325 */ 326 public Point getMapPosition(double lat, double lon, boolean checkOutside) { 291 327 int x = OsmMercator.LonToX(lon, zoom); 292 328 int y = OsmMercator.LatToY(lat, zoom); 293 329 x -= center.x - getWidth() / 2; 294 330 y -= center.y - getHeight() / 2; 295 if (x < 0 || y < 0 || x > getWidth() || y > getHeight()) 331 if (checkOutside) { 332 if (x < 0 || y < 0 || x > getWidth() || y > getHeight()) { 333 return null; 334 } 335 } 336 return new Point(x, y); 337 } 338 339 /** 340 * Calculates the position on the map of a given coordinate 341 * 342 * @param lat 343 * @param lon 344 * @return point on the map or <code>null</code> if the point is not visible 345 */ 346 public Point getMapPosition(double lat, double lon) { 347 return getMapPosition(lat, lon, true); 348 } 349 350 /** 351 * Calculates the position on the map of a given coordinate 352 * 353 * @param coord 354 * @return point on the map or <code>null</code> if the point is not visible 355 */ 356 public Point getMapPosition(Coordinate coord) { 357 if (coord != null) { 358 return getMapPosition(coord.getLat(), coord.getLon()); 359 } else { 296 360 return null; 297 return new Point(x, y); 361 } 362 } 363 364 /** 365 * Calculates the position on the map of a given coordinate 366 * 367 * @param coord 368 * @return point on the map or <code>null</code> if the point is not visible and checkOutside set to <code>true</code> 369 */ 370 public Point getMapPosition(Coordinate coord, boolean checkOutside) { 371 if (coord != null) { 372 return getMapPosition(coord.getLat(), coord.getLon(), checkOutside); 373 } else { 374 return null; 375 } 298 376 } 299 377 … … 349 427 if (x_min <= posx && posx <= x_max && y_min <= posy && posy <= y_max) { 350 428 // tile is visible 351 Tile tile = getTile(tilex, tiley, zoom);429 Tile tile = tileController.getTile(tilex, tiley, zoom); 352 430 if (tile != null) { 353 431 painted = true; … … 377 455 Coordinate bottomRight = square.getBottomRight(); 378 456 if (topLeft != null && bottomRight != null) { 379 Point pTopLeft = getMapPosition(topLeft.getLat(), topLeft.getLon() );380 Point pBottomRight = getMapPosition(bottomRight.getLat(), bottomRight.getLon() );457 Point pTopLeft = getMapPosition(topLeft.getLat(), topLeft.getLon(), false); 458 Point pBottomRight = getMapPosition(bottomRight.getLat(), bottomRight.getLon(), false); 381 459 if (pTopLeft != null && pBottomRight != null) { 382 460 square.paint(g, pTopLeft, pBottomRight); … … 446 524 447 525 public void setZoom(int zoom, Point mapPoint) { 448 if (zoom > tile Source.getMaxZoom() || zoom < tileSource.getMinZoom() || zoom == this.zoom)526 if (zoom > tileController.getTileSource().getMaxZoom() || zoom < tileController.getTileSource().getMinZoom() || zoom == this.zoom) 449 527 return; 450 528 Coordinate zoomPos = getPosition(mapPoint); 451 jobDispatcher.cancelOutstandingJobs(); // Clearing outstanding load529 tileController.cancelOutstandingJobs(); // Clearing outstanding load 452 530 // requests 453 531 setDisplayPositionByLatLon(mapPoint, zoomPos.getLat(), zoomPos.getLon(), zoom); … … 456 534 public void setZoom(int zoom) { 457 535 setZoom(zoom, new Point(getWidth() / 2, getHeight() / 2)); 458 }459 460 /**461 * retrieves a tile from the cache. If the tile is not present in the cache462 * a load job is added to the working queue of {@link JobThread}.463 *464 * @param tilex465 * @param tiley466 * @param zoom467 * @return specified tile from the cache or <code>null</code> if the tile468 * was not found in the cache.469 */470 protected Tile getTile(int tilex, int tiley, int zoom) {471 int max = (1 << zoom);472 if (tilex < 0 || tilex >= max || tiley < 0 || tiley >= max)473 return null;474 Tile tile = tileCache.getTile(tileSource, tilex, tiley, zoom);475 if (tile == null) {476 tile = new Tile(tileSource, tilex, tiley, zoom);477 tileCache.addTile(tile);478 tile.loadPlaceholderFromCache(tileCache);479 }480 if (!tile.isLoaded()) {481 jobDispatcher.addJob(tileLoader.createTileLoaderJob(tileSource, tilex, tiley, zoom));482 }483 return tile;484 536 } 485 537 … … 496 548 zoomInButton.setToolTipText("Zoom to level " + (zoom + 1)); 497 549 zoomOutButton.setToolTipText("Zoom to level " + (zoom - 1)); 498 zoomOutButton.setEnabled(zoom > tile Source.getMinZoom());499 zoomInButton.setEnabled(zoom < tile Source.getMaxZoom());550 zoomOutButton.setEnabled(zoom > tileController.getTileSource().getMinZoom()); 551 zoomInButton.setEnabled(zoom < tileController.getTileSource().getMaxZoom()); 500 552 } 501 553 … … 566 618 public boolean getZoomContolsVisible() { 567 619 return zoomSlider.isVisible(); 568 }569 570 public TileCache getTileCache() {571 return tileCache;572 }573 574 public TileLoader getTileLoader() {575 return tileLoader;576 }577 578 public void setTileLoader(TileLoader tileLoader) {579 this.tileLoader = tileLoader;580 }581 582 public TileSource getTileLayerSource() {583 return tileSource;584 }585 586 public TileSource getTileSource() {587 return tileSource;588 620 } 589 621 … … 593 625 if (tileSource.getMinZoom() < MIN_ZOOM) 594 626 throw new RuntimeException("Minumim zoom level too low"); 595 t his.tileSource = tileSource;627 tileController.setTileSource(tileSource); 596 628 zoomSlider.setMinimum(tileSource.getMinZoom()); 597 629 zoomSlider.setMaximum(tileSource.getMaxZoom()); 598 jobDispatcher.cancelOutstandingJobs();630 tileController.cancelOutstandingJobs(); 599 631 if (zoom > tileSource.getMaxZoom()) 600 632 setZoom(tileSource.getMaxZoom()); … … 621 653 repaint(); 622 654 } 655 656 /* (non-Javadoc) 657 * @see org.openstreetmap.gui.jmapviewer.interfaces.TileLoaderListener#getTileCache() 658 */ 659 public TileCache getTileCache() { 660 return tileController.getTileCache(); 661 } 662 663 public void setTileLoader(TileLoader loader) { 664 tileController.setTileLoader(loader); 665 } 623 666 } -
applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/JobDispatcher.java
r14053 r16391 94 94 } 95 95 96 p rotectedclass JobThread extends Thread {96 public class JobThread extends Thread { 97 97 98 98 Runnable job;
Note:
See TracChangeset
for help on using the changeset viewer.