Changeset 16391 in osm for applications/viewer


Ignore:
Timestamp:
2009-07-09T00:12:12+02:00 (15 years ago)
Author:
szeller
Message:

refactored the TileController out of JMapViewer, that it can be used seperately

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  
    2020import javax.swing.event.ChangeListener;
    2121
    22 import org.openstreetmap.gui.jmapviewer.JobDispatcher.JobThread;
    2322import org.openstreetmap.gui.jmapviewer.interfaces.MapMarker;
    2423import org.openstreetmap.gui.jmapviewer.interfaces.MapSquare;
     
    4847    public static final int MIN_ZOOM = 0;
    4948
    50     protected TileLoader tileLoader;
    51     protected TileCache tileCache;
    52     protected TileSource tileSource;
    53 
    5449    protected List<MapMarker> mapMarkerList;
    5550    protected List<MapSquare> mapSquareList;
     
    5954
    6055    protected boolean tileGridVisible;
     56   
     57    protected TileController tileController;
    6158
    6259    /**
     
    7572    protected JButton zoomOutButton;
    7673
    77     JobDispatcher jobDispatcher;
     74   
    7875
    7976    /**
     
    9188    public JMapViewer(TileCache tileCache, int downloadThreadCount) {
    9289        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);
    9791        mapMarkerList = new LinkedList<MapMarker>();
    9892        mapSquareList = new LinkedList<MapSquare>();
     
    108102
    109103    protected void initializeZoomSlider() {
    110         zoomSlider = new JSlider(MIN_ZOOM, tileSource.getMaxZoom());
     104        zoomSlider = new JSlider(MIN_ZOOM, tileController.getTileSource().getMaxZoom());
    111105        zoomSlider.setOrientation(JSlider.VERTICAL);
    112106        zoomSlider.setBounds(10, 10, 30, 150);
     
    195189
    196190    public void setDisplayPosition(Point mapPoint, int x, int y, int zoom) {
    197         if (zoom > tileSource.getMaxZoom() || zoom < MIN_ZOOM)
     191        if (zoom > tileController.getTileSource().getMaxZoom() || zoom < MIN_ZOOM)
    198192            return;
    199193
     
    228222        int x_max = Integer.MIN_VALUE;
    229223        int y_max = Integer.MIN_VALUE;
    230         int mapZoomMax = tileSource.getMaxZoom();
     224        int mapZoomMax = tileController.getTileSource().getMaxZoom();
    231225        for (MapMarker marker : mapMarkerList) {
    232226            int x = OsmMercator.LonToX(marker.getLon(), mapZoomMax);
     
    258252        setDisplayPosition(x, y, newZoom);
    259253    }
     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    }
    260295
    261296    public Coordinate getPosition() {
     
    286321     * @param lat
    287322     * @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) {
    291327        int x = OsmMercator.LonToX(lon, zoom);
    292328        int y = OsmMercator.LatToY(lat, zoom);
    293329        x -= center.x - getWidth() / 2;
    294330        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 {
    296360            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        }
    298376    }
    299377
     
    349427                    if (x_min <= posx && posx <= x_max && y_min <= posy && posy <= y_max) {
    350428                        // tile is visible
    351                         Tile tile = getTile(tilex, tiley, zoom);
     429                        Tile tile = tileController.getTile(tilex, tiley, zoom);
    352430                        if (tile != null) {
    353431                            painted = true;
     
    377455                Coordinate bottomRight = square.getBottomRight();
    378456                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);
    381459                    if (pTopLeft != null && pBottomRight != null) {
    382460                        square.paint(g, pTopLeft, pBottomRight);
     
    446524
    447525    public void setZoom(int zoom, Point mapPoint) {
    448         if (zoom > tileSource.getMaxZoom() || zoom < tileSource.getMinZoom() || zoom == this.zoom)
     526        if (zoom > tileController.getTileSource().getMaxZoom() || zoom < tileController.getTileSource().getMinZoom() || zoom == this.zoom)
    449527            return;
    450528        Coordinate zoomPos = getPosition(mapPoint);
    451         jobDispatcher.cancelOutstandingJobs(); // Clearing outstanding load
     529        tileController.cancelOutstandingJobs(); // Clearing outstanding load
    452530        // requests
    453531        setDisplayPositionByLatLon(mapPoint, zoomPos.getLat(), zoomPos.getLon(), zoom);
     
    456534    public void setZoom(int zoom) {
    457535        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 cache
    462      * a load job is added to the working queue of {@link JobThread}.
    463      *
    464      * @param tilex
    465      * @param tiley
    466      * @param zoom
    467      * @return specified tile from the cache or <code>null</code> if the tile
    468      *         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;
    484536    }
    485537
     
    496548        zoomInButton.setToolTipText("Zoom to level " + (zoom + 1));
    497549        zoomOutButton.setToolTipText("Zoom to level " + (zoom - 1));
    498         zoomOutButton.setEnabled(zoom > tileSource.getMinZoom());
    499         zoomInButton.setEnabled(zoom < tileSource.getMaxZoom());
     550        zoomOutButton.setEnabled(zoom > tileController.getTileSource().getMinZoom());
     551        zoomInButton.setEnabled(zoom < tileController.getTileSource().getMaxZoom());
    500552    }
    501553
     
    566618    public boolean getZoomContolsVisible() {
    567619        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;
    588620    }
    589621
     
    593625        if (tileSource.getMinZoom() < MIN_ZOOM)
    594626            throw new RuntimeException("Minumim zoom level too low");
    595         this.tileSource = tileSource;
     627        tileController.setTileSource(tileSource);
    596628        zoomSlider.setMinimum(tileSource.getMinZoom());
    597629        zoomSlider.setMaximum(tileSource.getMaxZoom());
    598         jobDispatcher.cancelOutstandingJobs();
     630        tileController.cancelOutstandingJobs();
    599631        if (zoom > tileSource.getMaxZoom())
    600632            setZoom(tileSource.getMaxZoom());
     
    621653        repaint();
    622654    }
     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    }
    623666}
  • applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/JobDispatcher.java

    r14053 r16391  
    9494    }
    9595
    96     protected class JobThread extends Thread {
     96    public class JobThread extends Thread {
    9797
    9898        Runnable job;
Note: See TracChangeset for help on using the changeset viewer.