001    // License: GPL. For details, see LICENSE file.
002    package org.openstreetmap.josm.gui.layer;
003    
004    import static org.openstreetmap.josm.tools.I18n.tr;
005    
006    import java.awt.Color;
007    import java.awt.Font;
008    import java.awt.Graphics;
009    import java.awt.Graphics2D;
010    import java.awt.Image;
011    import java.awt.Point;
012    import java.awt.Rectangle;
013    import java.awt.Toolkit;
014    import java.awt.event.ActionEvent;
015    import java.awt.event.MouseAdapter;
016    import java.awt.event.MouseEvent;
017    import java.awt.image.ImageObserver;
018    import java.io.File;
019    import java.io.IOException;
020    import java.io.StringReader;
021    import java.net.URL;
022    import java.util.ArrayList;
023    import java.util.Collections;
024    import java.util.HashSet;
025    import java.util.LinkedList;
026    import java.util.List;
027    import java.util.Map;
028    import java.util.Map.Entry;
029    import java.util.Scanner;
030    import java.util.concurrent.Callable;
031    import java.util.regex.Matcher;
032    import java.util.regex.Pattern;
033    
034    import javax.swing.AbstractAction;
035    import javax.swing.Action;
036    import javax.swing.JCheckBoxMenuItem;
037    import javax.swing.JMenuItem;
038    import javax.swing.JOptionPane;
039    import javax.swing.JPopupMenu;
040    
041    import org.openstreetmap.gui.jmapviewer.AttributionSupport;
042    import org.openstreetmap.gui.jmapviewer.Coordinate;
043    import org.openstreetmap.gui.jmapviewer.JobDispatcher;
044    import org.openstreetmap.gui.jmapviewer.MemoryTileCache;
045    import org.openstreetmap.gui.jmapviewer.OsmFileCacheTileLoader;
046    import org.openstreetmap.gui.jmapviewer.OsmFileCacheTileLoader.TileClearController;
047    import org.openstreetmap.gui.jmapviewer.OsmTileLoader;
048    import org.openstreetmap.gui.jmapviewer.Tile;
049    import org.openstreetmap.gui.jmapviewer.interfaces.TileCache;
050    import org.openstreetmap.gui.jmapviewer.interfaces.TileLoaderListener;
051    import org.openstreetmap.gui.jmapviewer.interfaces.TileSource;
052    import org.openstreetmap.gui.jmapviewer.tilesources.BingAerialTileSource;
053    import org.openstreetmap.gui.jmapviewer.tilesources.ScanexTileSource;
054    import org.openstreetmap.gui.jmapviewer.tilesources.TMSTileSource;
055    import org.openstreetmap.gui.jmapviewer.tilesources.TemplatedTMSTileSource;
056    import org.openstreetmap.josm.Main;
057    import org.openstreetmap.josm.actions.RenameLayerAction;
058    import org.openstreetmap.josm.data.Bounds;
059    import org.openstreetmap.josm.data.coor.EastNorth;
060    import org.openstreetmap.josm.data.coor.LatLon;
061    import org.openstreetmap.josm.data.imagery.ImageryInfo;
062    import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType;
063    import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
064    import org.openstreetmap.josm.data.preferences.BooleanProperty;
065    import org.openstreetmap.josm.data.preferences.IntegerProperty;
066    import org.openstreetmap.josm.data.preferences.StringProperty;
067    import org.openstreetmap.josm.data.projection.Projection;
068    import org.openstreetmap.josm.gui.MapFrame;
069    import org.openstreetmap.josm.gui.MapView;
070    import org.openstreetmap.josm.gui.MapView.LayerChangeListener;
071    import org.openstreetmap.josm.gui.PleaseWaitRunnable;
072    import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
073    import org.openstreetmap.josm.gui.dialogs.LayerListPopup;
074    import org.openstreetmap.josm.gui.progress.ProgressMonitor;
075    import org.openstreetmap.josm.gui.progress.ProgressMonitor.CancelListener;
076    import org.openstreetmap.josm.io.CacheCustomContent;
077    import org.openstreetmap.josm.io.OsmTransferException;
078    import org.openstreetmap.josm.io.UTFInputStreamReader;
079    import org.xml.sax.InputSource;
080    import org.xml.sax.SAXException;
081    
082    /**
083     * Class that displays a slippy map layer.
084     *
085     * @author Frederik Ramm <frederik@remote.org>
086     * @author LuVar <lubomir.varga@freemap.sk>
087     * @author Dave Hansen <dave@sr71.net>
088     * @author Upliner <upliner@gmail.com>
089     *
090     */
091    public class TMSLayer extends ImageryLayer implements ImageObserver, TileLoaderListener {
092        public static final String PREFERENCE_PREFIX   = "imagery.tms";
093    
094        public static final int MAX_ZOOM = 30;
095        public static final int MIN_ZOOM = 2;
096        public static final int DEFAULT_MAX_ZOOM = 20;
097        public static final int DEFAULT_MIN_ZOOM = 2;
098    
099        public static final BooleanProperty PROP_DEFAULT_AUTOZOOM = new BooleanProperty(PREFERENCE_PREFIX + ".default_autozoom", true);
100        public static final BooleanProperty PROP_DEFAULT_AUTOLOAD = new BooleanProperty(PREFERENCE_PREFIX + ".default_autoload", true);
101        public static final BooleanProperty PROP_DEFAULT_SHOWERRORS = new BooleanProperty(PREFERENCE_PREFIX + ".default_showerrors", true);
102        public static final IntegerProperty PROP_MIN_ZOOM_LVL = new IntegerProperty(PREFERENCE_PREFIX + ".min_zoom_lvl", DEFAULT_MIN_ZOOM);
103        public static final IntegerProperty PROP_MAX_ZOOM_LVL = new IntegerProperty(PREFERENCE_PREFIX + ".max_zoom_lvl", DEFAULT_MAX_ZOOM);
104        //public static final BooleanProperty PROP_DRAW_DEBUG = new BooleanProperty(PREFERENCE_PREFIX + ".draw_debug", false);
105        public static final BooleanProperty PROP_ADD_TO_SLIPPYMAP_CHOOSER = new BooleanProperty(PREFERENCE_PREFIX + ".add_to_slippymap_chooser", true);
106        public static final IntegerProperty PROP_TMS_JOBS = new IntegerProperty("tmsloader.maxjobs", 25);
107        public static final StringProperty PROP_TILECACHE_DIR;
108    
109        static {
110            String defPath = null;
111            try {
112                defPath = OsmFileCacheTileLoader.getDefaultCacheDir().getAbsolutePath();
113            } catch (SecurityException e) {
114            }
115            PROP_TILECACHE_DIR = new StringProperty(PREFERENCE_PREFIX + ".tilecache_path", defPath);
116        }
117    
118        /*boolean debug = true;*/
119    
120        protected MemoryTileCache tileCache;
121        protected TileSource tileSource;
122        protected OsmTileLoader tileLoader;
123    
124        HashSet<Tile> tileRequestsOutstanding = new HashSet<Tile>();
125        @Override
126        public synchronized void tileLoadingFinished(Tile tile, boolean success) {
127            if (tile.hasError()) {
128                success = false;
129                tile.setImage(null);
130            }
131            if (sharpenLevel != 0 && success) {
132                tile.setImage(sharpenImage(tile.getImage()));
133            }
134            tile.setLoaded(true);
135            needRedraw = true;
136            Main.map.repaint(100);
137            tileRequestsOutstanding.remove(tile);
138            /*if (debug) {
139                Main.debug("tileLoadingFinished() tile: " + tile + " success: " + success);
140            }*/
141        }
142        
143        @Override
144        public TileCache getTileCache() {
145            return tileCache;
146        }
147        
148        private class TmsTileClearController implements TileClearController, CancelListener {
149    
150            private final ProgressMonitor monitor;
151            private boolean cancel = false;
152            
153            public TmsTileClearController(ProgressMonitor monitor) {
154                this.monitor = monitor;
155                this.monitor.addCancelListener(this);
156            }
157    
158            @Override
159            public void initClearDir(File dir) {
160            }
161    
162            @Override
163            public void initClearFiles(File[] files) {
164                monitor.setTicksCount(files.length);
165                monitor.setTicks(0);
166            }
167    
168            @Override
169            public boolean cancel() {
170                return cancel;
171            }
172    
173            @Override
174            public void fileDeleted(File file) {
175                monitor.setTicks(monitor.getTicks()+1);
176            }
177    
178            @Override
179            public void clearFinished() {
180                monitor.finishTask();
181            }
182    
183            @Override
184            public void operationCanceled() {
185                cancel = true;
186            }
187        }
188    
189        /**
190         * Clears the tile cache.
191         * 
192         * If the current tileLoader is an instance of OsmTileLoader, a new 
193         * TmsTileClearController is created and passed to the according clearCache 
194         * method.
195         * 
196         * @param monitor 
197         * @see MemoryTileCache#clear()
198         * @see OsmFileCacheTileLoader#clearCache(org.openstreetmap.gui.jmapviewer.interfaces.TileSource, org.openstreetmap.gui.jmapviewer.OsmFileCacheTileLoader.TileClearController) 
199         */
200        void clearTileCache(ProgressMonitor monitor) {
201            tileCache.clear();
202            if (tileLoader instanceof OsmFileCacheTileLoader) {
203                ((OsmFileCacheTileLoader)tileLoader).clearCache(tileSource, new TmsTileClearController(monitor));
204            }
205        }
206    
207        /**
208         * Zoomlevel at which tiles is currently downloaded.
209         * Initial zoom lvl is set to bestZoom
210         */
211        public int currentZoomLevel;
212    
213        private Tile clickedTile;
214        private boolean needRedraw;
215        private JPopupMenu tileOptionMenu;
216        JCheckBoxMenuItem autoZoomPopup;
217        JCheckBoxMenuItem autoLoadPopup;
218        JCheckBoxMenuItem showErrorsPopup;
219        Tile showMetadataTile;
220        private AttributionSupport attribution = new AttributionSupport();
221        private static final Font InfoFont = new Font("sansserif", Font.BOLD, 13);
222    
223        protected boolean autoZoom;
224        protected boolean autoLoad;
225        protected boolean showErrors;
226    
227        /**
228         * Initiates a repaint of Main.map
229         * 
230         * @see Main#map
231         * @see MapFrame#repaint() 
232         */
233        void redraw() {
234            needRedraw = true;
235            Main.map.repaint();
236        }
237    
238        static int checkMaxZoomLvl(int maxZoomLvl, TileSource ts) {
239            if(maxZoomLvl > MAX_ZOOM) {
240                /*Main.debug("Max. zoom level should not be more than 30! Setting to 30.");*/
241                maxZoomLvl = MAX_ZOOM;
242            }
243            if(maxZoomLvl < PROP_MIN_ZOOM_LVL.get()) {
244                /*Main.debug("Max. zoom level should not be more than min. zoom level! Setting to min.");*/
245                maxZoomLvl = PROP_MIN_ZOOM_LVL.get();
246            }
247            if (ts != null && ts.getMaxZoom() != 0 && ts.getMaxZoom() < maxZoomLvl) {
248                maxZoomLvl = ts.getMaxZoom();
249            }
250            return maxZoomLvl;
251        }
252    
253        public static int getMaxZoomLvl(TileSource ts) {
254            return checkMaxZoomLvl(PROP_MAX_ZOOM_LVL.get(), ts);
255        }
256    
257        public static void setMaxZoomLvl(int maxZoomLvl) {
258            maxZoomLvl = checkMaxZoomLvl(maxZoomLvl, null);
259            PROP_MAX_ZOOM_LVL.put(maxZoomLvl);
260        }
261    
262        static int checkMinZoomLvl(int minZoomLvl, TileSource ts) {
263            if(minZoomLvl < MIN_ZOOM) {
264                /*Main.debug("Min. zoom level should not be less than "+MIN_ZOOM+"! Setting to that.");*/
265                minZoomLvl = MIN_ZOOM;
266            }
267            if(minZoomLvl > PROP_MAX_ZOOM_LVL.get()) {
268                /*Main.debug("Min. zoom level should not be more than Max. zoom level! Setting to Max.");*/
269                minZoomLvl = getMaxZoomLvl(ts);
270            }
271            if (ts != null && ts.getMinZoom() > minZoomLvl) {
272                /*Main.debug("Increasing min. zoom level to match tile source");*/
273                minZoomLvl = ts.getMinZoom();
274            }
275            return minZoomLvl;
276        }
277    
278        public static int getMinZoomLvl(TileSource ts) {
279            return checkMinZoomLvl(PROP_MIN_ZOOM_LVL.get(), ts);
280        }
281    
282        public static void setMinZoomLvl(int minZoomLvl) {
283            minZoomLvl = checkMinZoomLvl(minZoomLvl, null);
284            PROP_MIN_ZOOM_LVL.put(minZoomLvl);
285        }
286    
287        private static class CachedAttributionBingAerialTileSource extends BingAerialTileSource {
288    
289            class BingAttributionData extends CacheCustomContent<IOException> {
290    
291                public BingAttributionData() {
292                    super("bing.attribution.xml", CacheCustomContent.INTERVAL_HOURLY);
293                }
294    
295                @Override
296                protected byte[] updateData() throws IOException {
297                    URL u = getAttributionUrl();
298                    UTFInputStreamReader in = UTFInputStreamReader.create(u.openStream(), "utf-8");
299                    String r = new Scanner(in).useDelimiter("\\A").next();
300                    System.out.println("Successfully loaded Bing attribution data.");
301                    return r.getBytes("utf-8");
302                }
303            }
304    
305            @Override
306            protected Callable<List<Attribution>> getAttributionLoaderCallable() {
307                return new Callable<List<Attribution>>() {
308    
309                    @Override
310                    public List<Attribution> call() throws Exception {
311                        BingAttributionData attributionLoader = new BingAttributionData();
312                        int waitTimeSec = 1;
313                        while (true) {
314                            try {
315                                String xml = attributionLoader.updateIfRequiredString();
316                                return parseAttributionText(new InputSource(new StringReader((xml))));
317                            } catch (IOException ex) {
318                                System.err.println("Could not connect to Bing API. Will retry in " + waitTimeSec + " seconds.");
319                                Thread.sleep(waitTimeSec * 1000L);
320                                waitTimeSec *= 2;
321                            }
322                        }
323                    }
324                };
325            }
326        }
327    
328        /**
329         * Creates and returns a new TileSource instance depending on the {@link ImageryType}
330         * of the passed ImageryInfo object.
331         * 
332         * If no appropriate TileSource is found, null is returned.
333         * Currently supported ImageryType are {@link ImageryType#TMS}, 
334         * {@link ImageryType#BING}, {@link ImageryType#SCANEX}.
335         * 
336         * @param info
337         * @return a new TileSource instance or null if no TileSource for the ImageryInfo/ImageryType could be found.
338         * @throws IllegalArgumentException 
339         */
340        public static TileSource getTileSource(ImageryInfo info) throws IllegalArgumentException {
341            if (info.getImageryType() == ImageryType.TMS) {
342                checkUrl(info.getUrl());
343                TMSTileSource t = new TemplatedTMSTileSource(info.getName(), info.getUrl(), info.getMinZoom(), info.getMaxZoom());
344                info.setAttribution(t);
345                return t;
346            } else if (info.getImageryType() == ImageryType.BING)
347                return new CachedAttributionBingAerialTileSource();
348            else if (info.getImageryType() == ImageryType.SCANEX) {
349                return new ScanexTileSource(info.getUrl());
350            }
351            return null;
352        }
353    
354        public static void checkUrl(String url) throws IllegalArgumentException {
355            if (url == null) {
356                throw new IllegalArgumentException();
357            } else {
358                Matcher m = Pattern.compile("\\{[^}]*\\}").matcher(url);
359                while (m.find()) {
360                    boolean isSupportedPattern = false;
361                    for (String pattern : TemplatedTMSTileSource.ALL_PATTERNS) {
362                        if (m.group().matches(pattern)) {
363                            isSupportedPattern = true;
364                            break;
365                        }
366                    }
367                    if (!isSupportedPattern) {
368                        throw new IllegalArgumentException(tr("{0} is not a valid TMS argument. Please check this server URL:\n{1}", m.group(), url));
369                    }
370                }
371            }
372        }
373    
374        private void initTileSource(TileSource tileSource) {
375            this.tileSource = tileSource;
376            attribution.initialize(tileSource);
377    
378            currentZoomLevel = getBestZoom();
379    
380            tileCache = new MemoryTileCache();
381    
382            String cachePath = TMSLayer.PROP_TILECACHE_DIR.get();
383            tileLoader = null;
384            if (cachePath != null && !cachePath.isEmpty()) {
385                try {
386                    tileLoader = new OsmFileCacheTileLoader(this, new File(cachePath));
387                } catch (IOException e) {
388                }
389            }
390            if (tileLoader == null) {
391                tileLoader = new OsmTileLoader(this);
392            }
393            tileLoader.timeoutConnect = Main.pref.getInteger("socket.timeout.connect",15) * 1000;
394            tileLoader.timeoutRead = Main.pref.getInteger("socket.timeout.read", 30) * 1000;
395            if (tileSource instanceof TemplatedTMSTileSource) {
396                for(Entry<String, String> e : ((TemplatedTMSTileSource)tileSource).getHeaders().entrySet()) {
397                    tileLoader.headers.put(e.getKey(), e.getValue());
398                }
399            }
400        }
401    
402        @Override
403        public void setOffset(double dx, double dy) {
404            super.setOffset(dx, dy);
405            needRedraw = true;
406        }
407    
408        /**
409         * Returns average number of screen pixels per tile pixel for current mapview
410         */
411        private double getScaleFactor(int zoom) {
412            if (Main.map == null || Main.map.mapView == null) return 1;
413            MapView mv = Main.map.mapView;
414            LatLon topLeft = mv.getLatLon(0, 0);
415            LatLon botRight = mv.getLatLon(mv.getWidth(), mv.getHeight());
416            double x1 = tileSource.lonToTileX(topLeft.lon(), zoom);
417            double y1 = tileSource.latToTileY(topLeft.lat(), zoom);
418            double x2 = tileSource.lonToTileX(botRight.lon(), zoom);
419            double y2 = tileSource.latToTileY(botRight.lat(), zoom);
420    
421            int screenPixels = mv.getWidth()*mv.getHeight();
422            double tilePixels = Math.abs((y2-y1)*(x2-x1)*tileSource.getTileSize()*tileSource.getTileSize());
423            if (screenPixels == 0 || tilePixels == 0) return 1;
424            return screenPixels/tilePixels;
425        }
426    
427        private int getBestZoom() {
428            double factor = getScaleFactor(1);
429            double result = Math.log(factor)/Math.log(2)/2+1;
430            // In general, smaller zoom levels are more readable.  We prefer big,
431            // block, pixelated (but readable) map text to small, smeared,
432            // unreadable underzoomed text.  So, use .floor() instead of rounding
433            // to skew things a bit toward the lower zooms.
434            int intResult = (int)Math.floor(result);
435            if (intResult > getMaxZoomLvl())
436                return getMaxZoomLvl();
437            if (intResult < getMinZoomLvl())
438                return getMinZoomLvl();
439            return intResult;
440        }
441    
442        /**
443         * Function to set the maximum number of workers for tile loading to the value defined
444         * in preferences.
445         */
446        static public void setMaxWorkers() {
447            JobDispatcher.getInstance().setMaxWorkers(PROP_TMS_JOBS.get());
448            JobDispatcher.getInstance().setLIFO(true);
449        }
450    
451        @SuppressWarnings("serial")
452        public TMSLayer(ImageryInfo info) {
453            super(info);
454    
455            setMaxWorkers();
456            if(!isProjectionSupported(Main.getProjection())) {
457                JOptionPane.showMessageDialog(Main.parent,
458                    tr("TMS layers do not support the projection {0}.\n{1}\n"
459                    + "Change the projection or remove the layer.",
460                    Main.getProjection().toCode(), nameSupportedProjections()),
461                    tr("Warning"),
462                    JOptionPane.WARNING_MESSAGE);
463            }
464    
465            setBackgroundLayer(true);
466            this.setVisible(true);
467    
468            TileSource source = getTileSource(info);
469            if (source == null)
470                throw new IllegalStateException("Cannot create TMSLayer with non-TMS ImageryInfo");
471            initTileSource(source);
472        }
473    
474        /**
475         * Adds a context menu to the mapView.
476         */
477        @Override
478        public void hookUpMapView() {
479            tileOptionMenu = new JPopupMenu();
480    
481            autoZoom = PROP_DEFAULT_AUTOZOOM.get();
482            autoZoomPopup = new JCheckBoxMenuItem();
483            autoZoomPopup.setAction(new AbstractAction(tr("Auto Zoom")) {
484                @Override
485                public void actionPerformed(ActionEvent ae) {
486                    autoZoom = !autoZoom;
487                }
488            });
489            autoZoomPopup.setSelected(autoZoom);
490            tileOptionMenu.add(autoZoomPopup);
491    
492            autoLoad = PROP_DEFAULT_AUTOLOAD.get();
493            autoLoadPopup = new JCheckBoxMenuItem();
494            autoLoadPopup.setAction(new AbstractAction(tr("Auto load tiles")) {
495                @Override
496                public void actionPerformed(ActionEvent ae) {
497                    autoLoad= !autoLoad;
498                }
499            });
500            autoLoadPopup.setSelected(autoLoad);
501            tileOptionMenu.add(autoLoadPopup);
502    
503            showErrors = PROP_DEFAULT_SHOWERRORS.get();
504            showErrorsPopup = new JCheckBoxMenuItem();
505            showErrorsPopup.setAction(new AbstractAction(tr("Show Errors")) {
506                @Override
507                public void actionPerformed(ActionEvent ae) {
508                    showErrors = !showErrors;
509                }
510            });
511            showErrorsPopup.setSelected(showErrors);
512            tileOptionMenu.add(showErrorsPopup);
513    
514            tileOptionMenu.add(new JMenuItem(new AbstractAction(tr("Load Tile")) {
515                @Override
516                public void actionPerformed(ActionEvent ae) {
517                    if (clickedTile != null) {
518                        loadTile(clickedTile, true);
519                        redraw();
520                    }
521                }
522            }));
523    
524            tileOptionMenu.add(new JMenuItem(new AbstractAction(
525                    tr("Show Tile Info")) {
526                @Override
527                public void actionPerformed(ActionEvent ae) {
528                    if (clickedTile != null) {
529                        showMetadataTile = clickedTile;
530                        redraw();
531                    }
532                }
533            }));
534    
535            /* FIXME
536            tileOptionMenu.add(new JMenuItem(new AbstractAction(
537                    tr("Request Update")) {
538                public void actionPerformed(ActionEvent ae) {
539                    if (clickedTile != null) {
540                        clickedTile.requestUpdate();
541                        redraw();
542                    }
543                }
544            }));*/
545    
546            tileOptionMenu.add(new JMenuItem(new AbstractAction(
547                    tr("Load All Tiles")) {
548                @Override
549                public void actionPerformed(ActionEvent ae) {
550                    loadAllTiles(true);
551                    redraw();
552                }
553            }));
554    
555            tileOptionMenu.add(new JMenuItem(new AbstractAction(
556                    tr("Load All Error Tiles")) {
557                @Override
558                public void actionPerformed(ActionEvent ae) {
559                    loadAllErrorTiles(true);
560                    redraw();
561                }
562            }));
563    
564            // increase and decrease commands
565            tileOptionMenu.add(new JMenuItem(new AbstractAction(
566                    tr("Increase zoom")) {
567                @Override
568                public void actionPerformed(ActionEvent ae) {
569                    increaseZoomLevel();
570                    redraw();
571                }
572            }));
573    
574            tileOptionMenu.add(new JMenuItem(new AbstractAction(
575                    tr("Decrease zoom")) {
576                @Override
577                public void actionPerformed(ActionEvent ae) {
578                    decreaseZoomLevel();
579                    redraw();
580                }
581            }));
582    
583            tileOptionMenu.add(new JMenuItem(new AbstractAction(
584                    tr("Snap to tile size")) {
585                @Override
586                public void actionPerformed(ActionEvent ae) {
587                    double new_factor = Math.sqrt(getScaleFactor(currentZoomLevel));
588                    Main.map.mapView.zoomToFactor(new_factor);
589                    redraw();
590                }
591            }));
592    
593            tileOptionMenu.add(new JMenuItem(new AbstractAction(
594                    tr("Flush Tile Cache")) {
595                @Override
596                public void actionPerformed(ActionEvent ae) {
597                    new PleaseWaitRunnable(tr("Flush Tile Cache")) {
598                        @Override
599                        protected void realRun() throws SAXException, IOException,
600                                OsmTransferException {
601                            clearTileCache(getProgressMonitor());
602                        }
603    
604                        @Override
605                        protected void finish() {
606                        }
607    
608                        @Override
609                        protected void cancel() {
610                        }
611                    }.run();
612                }
613            }));
614            // end of adding menu commands
615    
616            final MouseAdapter adapter = new MouseAdapter() {
617                @Override
618                public void mouseClicked(MouseEvent e) {
619                    if (!isVisible()) return;
620                    if (e.getButton() == MouseEvent.BUTTON3) {
621                        clickedTile = getTileForPixelpos(e.getX(), e.getY());
622                        tileOptionMenu.show(e.getComponent(), e.getX(), e.getY());
623                    } else if (e.getButton() == MouseEvent.BUTTON1) {
624                        attribution.handleAttribution(e.getPoint(), true);
625                    }
626                }
627            };
628            Main.map.mapView.addMouseListener(adapter);
629    
630            MapView.addLayerChangeListener(new LayerChangeListener() {
631                @Override
632                public void activeLayerChange(Layer oldLayer, Layer newLayer) {
633                    //
634                }
635    
636                @Override
637                public void layerAdded(Layer newLayer) {
638                    //
639                }
640    
641                @Override
642                public void layerRemoved(Layer oldLayer) {
643                    if (oldLayer == TMSLayer.this) {
644                        Main.map.mapView.removeMouseListener(adapter);
645                        MapView.removeLayerChangeListener(this);
646                    }
647                }
648            });
649        }
650    
651        void zoomChanged() {
652            /*if (debug) {
653                Main.debug("zoomChanged(): " + currentZoomLevel);
654            }*/
655            needRedraw = true;
656            JobDispatcher.getInstance().cancelOutstandingJobs();
657            tileRequestsOutstanding.clear();
658        }
659    
660        int getMaxZoomLvl() {
661            if (info.getMaxZoom() != 0)
662                return checkMaxZoomLvl(info.getMaxZoom(), tileSource);
663            else
664                return getMaxZoomLvl(tileSource);
665        }
666    
667        int getMinZoomLvl() {
668            return getMinZoomLvl(tileSource);
669        }
670    
671        /**
672         * Zoom in, go closer to map.
673         *
674         * @return    true, if zoom increasing was successfull, false othervise
675         */
676        public boolean zoomIncreaseAllowed() {
677            boolean zia = currentZoomLevel < this.getMaxZoomLvl();
678            /*if (debug) {
679                Main.debug("zoomIncreaseAllowed(): " + zia + " " + currentZoomLevel + " vs. " + this.getMaxZoomLvl() );
680            }*/
681            return zia;
682        }
683        
684        public boolean increaseZoomLevel() {
685            if (zoomIncreaseAllowed()) {
686                currentZoomLevel++;
687                /*if (debug) {
688                    Main.debug("increasing zoom level to: " + currentZoomLevel);
689                }*/
690                zoomChanged();
691            } else {
692                Main.warn("Current zoom level ("+currentZoomLevel+") could not be increased. "+
693                        "Max.zZoom Level "+this.getMaxZoomLvl()+" reached.");
694                return false;
695            }
696            return true;
697        }
698    
699        public boolean setZoomLevel(int zoom) {
700            if (zoom == currentZoomLevel) return true;
701            if (zoom > this.getMaxZoomLvl()) return false;
702            if (zoom < this.getMinZoomLvl()) return false;
703            currentZoomLevel = zoom;
704            zoomChanged();
705            return true;
706        }
707    
708        /**
709         * Check if zooming out is allowed
710         *
711         * @return    true, if zooming out is allowed (currentZoomLevel > minZoomLevel)
712         */
713        public boolean zoomDecreaseAllowed() {
714            return currentZoomLevel > this.getMinZoomLvl();
715        }
716        
717        /**
718         * Zoom out from map.
719         * 
720         * @return    true, if zoom increasing was successfull, false othervise
721         */
722        public boolean decreaseZoomLevel() {
723            //int minZoom = this.getMinZoomLvl();
724            if (zoomDecreaseAllowed()) {
725                /*if (debug) {
726                    Main.debug("decreasing zoom level to: " + currentZoomLevel);
727                }*/
728                currentZoomLevel--;
729                zoomChanged();
730            } else {
731                /*Main.debug("Current zoom level could not be decreased. Min. zoom level "+minZoom+" reached.");*/
732                return false;
733            }
734            return true;
735        }
736    
737        /*
738         * We use these for quick, hackish calculations.  They
739         * are temporary only and intentionally not inserted
740         * into the tileCache.
741         */
742        synchronized Tile tempCornerTile(Tile t) {
743            int x = t.getXtile() + 1;
744            int y = t.getYtile() + 1;
745            int zoom = t.getZoom();
746            Tile tile = getTile(x, y, zoom);
747            if (tile != null)
748                return tile;
749            return new Tile(tileSource, x, y, zoom);
750        }
751        
752        synchronized Tile getOrCreateTile(int x, int y, int zoom) {
753            Tile tile = getTile(x, y, zoom);
754            if (tile == null) {
755                tile = new Tile(tileSource, x, y, zoom);
756                tileCache.addTile(tile);
757                tile.loadPlaceholderFromCache(tileCache);
758            }
759            return tile;
760        }
761    
762        /*
763         * This can and will return null for tiles that are not
764         * already in the cache.
765         */
766        synchronized Tile getTile(int x, int y, int zoom) {
767            int max = (1 << zoom);
768            if (x < 0 || x >= max || y < 0 || y >= max)
769                return null;
770            Tile tile = tileCache.getTile(tileSource, x, y, zoom);
771            return tile;
772        }
773    
774        synchronized boolean loadTile(Tile tile, boolean force) {
775            if (tile == null)
776                return false;
777            if (!force && (tile.hasError() || tile.isLoaded()))
778                return false;
779            if (tile.isLoading())
780                return false;
781            if (tileRequestsOutstanding.contains(tile))
782                return false;
783            tileRequestsOutstanding.add(tile);
784            JobDispatcher.getInstance().addJob(tileLoader.createTileLoaderJob(tile));
785            return true;
786        }
787    
788        void loadAllTiles(boolean force) {
789            MapView mv = Main.map.mapView;
790            EastNorth topLeft = mv.getEastNorth(0, 0);
791            EastNorth botRight = mv.getEastNorth(mv.getWidth(), mv.getHeight());
792    
793            TileSet ts = new TileSet(topLeft, botRight, currentZoomLevel);
794    
795            // if there is more than 18 tiles on screen in any direction, do not
796            // load all tiles!
797            if (ts.tooLarge()) {
798                Main.warn("Not downloading all tiles because there is more than 18 tiles on an axis!");
799                return;
800            }
801            ts.loadAllTiles(force);
802        }
803    
804        void loadAllErrorTiles(boolean force) {
805            MapView mv = Main.map.mapView;
806            EastNorth topLeft = mv.getEastNorth(0, 0);
807            EastNorth botRight = mv.getEastNorth(mv.getWidth(), mv.getHeight());
808    
809            TileSet ts = new TileSet(topLeft, botRight, currentZoomLevel);
810    
811            ts.loadAllErrorTiles(force);
812        }
813    
814        /*
815         * Attempt to approximate how much the image is being scaled. For instance,
816         * a 100x100 image being scaled to 50x50 would return 0.25.
817         */
818        Image lastScaledImage = null;
819        @Override
820        public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {
821            boolean done = ((infoflags & (ERROR | FRAMEBITS | ALLBITS)) != 0);
822            needRedraw = true;
823            /*if (debug) {
824                Main.debug("imageUpdate() done: " + done + " calling repaint");
825            }*/
826            Main.map.repaint(done ? 0 : 100);
827            return !done;
828        }
829        
830        boolean imageLoaded(Image i) {
831            if (i == null)
832                return false;
833            int status = Toolkit.getDefaultToolkit().checkImage(i, -1, -1, this);
834            if ((status & ALLBITS) != 0)
835                return true;
836            return false;
837        }
838        
839        /**
840         * Returns the image for the given tile if both tile and image are loaded. 
841         * Otherwise returns  null.
842         * 
843         * @param tile the Tile for which the image should be returned
844         * @return  the image of the tile or null.
845         */
846        Image getLoadedTileImage(Tile tile) {
847            if (!tile.isLoaded())
848                return null;
849            Image img = tile.getImage();
850            if (!imageLoaded(img))
851                return null;
852            return img;
853        }
854    
855        LatLon tileLatLon(Tile t) {
856            int zoom = t.getZoom();
857            return new LatLon(tileSource.tileYToLat(t.getYtile(), zoom),
858                    tileSource.tileXToLon(t.getXtile(), zoom));
859        }
860    
861        Rectangle tileToRect(Tile t1) {
862            /*
863             * We need to get a box in which to draw, so advance by one tile in
864             * each direction to find the other corner of the box.
865             * Note: this somewhat pollutes the tile cache
866             */
867            Tile t2 = tempCornerTile(t1);
868            Rectangle rect = new Rectangle(pixelPos(t1));
869            rect.add(pixelPos(t2));
870            return rect;
871        }
872    
873        // 'source' is the pixel coordinates for the area that
874        // the img is capable of filling in.  However, we probably
875        // only want a portion of it.
876        //
877        // 'border' is the screen cordinates that need to be drawn.
878        //  We must not draw outside of it.
879        void drawImageInside(Graphics g, Image sourceImg, Rectangle source, Rectangle border) {
880            Rectangle target = source;
881    
882            // If a border is specified, only draw the intersection
883            // if what we have combined with what we are supposed
884            // to draw.
885            if (border != null) {
886                target = source.intersection(border);
887                /*if (debug) {
888                    Main.debug("source: " + source + "\nborder: " + border + "\nintersection: " + target);
889                }*/
890            }
891    
892            // All of the rectangles are in screen coordinates.  We need
893            // to how these correlate to the sourceImg pixels.  We could
894            // avoid doing this by scaling the image up to the 'source' size,
895            // but this should be cheaper.
896            //
897            // In some projections, x any y are scaled differently enough to
898            // cause a pixel or two of fudge.  Calculate them separately.
899            double imageYScaling = sourceImg.getHeight(this) / source.getHeight();
900            double imageXScaling = sourceImg.getWidth(this) / source.getWidth();
901    
902            // How many pixels into the 'source' rectangle are we drawing?
903            int screen_x_offset = target.x - source.x;
904            int screen_y_offset = target.y - source.y;
905            // And how many pixels into the image itself does that
906            // correlate to?
907            int img_x_offset = (int)(screen_x_offset * imageXScaling);
908            int img_y_offset = (int)(screen_y_offset * imageYScaling);
909            // Now calculate the other corner of the image that we need
910            // by scaling the 'target' rectangle's dimensions.
911            int img_x_end   = img_x_offset + (int)(target.getWidth() * imageXScaling);
912            int img_y_end   = img_y_offset + (int)(target.getHeight() * imageYScaling);
913    
914            /*if (debug) {
915                Main.debug("drawing image into target rect: " + target);
916            }*/
917            g.drawImage(sourceImg,
918                    target.x, target.y,
919                    target.x + target.width, target.y + target.height,
920                    img_x_offset, img_y_offset,
921                    img_x_end, img_y_end,
922                    this);
923            if (PROP_FADE_AMOUNT.get() != 0) {
924                // dimm by painting opaque rect...
925                g.setColor(getFadeColorWithAlpha());
926                g.fillRect(target.x, target.y,
927                        target.width, target.height);
928            }
929        }
930        
931        // This function is called for several zoom levels, not just
932        // the current one.  It should not trigger any tiles to be
933        // downloaded.  It should also avoid polluting the tile cache
934        // with any tiles since these tiles are not mandatory.
935        //
936        // The "border" tile tells us the boundaries of where we may
937        // draw.  It will not be from the zoom level that is being
938        // drawn currently.  If drawing the displayZoomLevel,
939        // border is null and we draw the entire tile set.
940        List<Tile> paintTileImages(Graphics g, TileSet ts, int zoom, Tile border) {
941            if (zoom <= 0) return Collections.emptyList();
942            Rectangle borderRect = null;
943            if (border != null) {
944                borderRect = tileToRect(border);
945            }
946            List<Tile> missedTiles = new LinkedList<Tile>();
947            // The callers of this code *require* that we return any tiles
948            // that we do not draw in missedTiles.  ts.allExistingTiles() by
949            // default will only return already-existing tiles.  However, we
950            // need to return *all* tiles to the callers, so force creation
951            // here.
952            //boolean forceTileCreation = true;
953            for (Tile tile : ts.allTilesCreate()) {
954                Image img = getLoadedTileImage(tile);
955                if (img == null || tile.hasError()) {
956                    /*if (debug) {
957                        Main.debug("missed tile: " + tile);
958                    }*/
959                    missedTiles.add(tile);
960                    continue;
961                }
962                Rectangle sourceRect = tileToRect(tile);
963                if (borderRect != null && !sourceRect.intersects(borderRect)) {
964                    continue;
965                }
966                drawImageInside(g, img, sourceRect, borderRect);
967            }// end of for
968            return missedTiles;
969        }
970    
971        void myDrawString(Graphics g, String text, int x, int y) {
972            Color oldColor = g.getColor();
973            g.setColor(Color.black);
974            g.drawString(text,x+1,y+1);
975            g.setColor(oldColor);
976            g.drawString(text,x,y);
977        }
978    
979        void paintTileText(TileSet ts, Tile tile, Graphics g, MapView mv, int zoom, Tile t) {
980            int fontHeight = g.getFontMetrics().getHeight();
981            if (tile == null)
982                return;
983            Point p = pixelPos(t);
984            int texty = p.y + 2 + fontHeight;
985    
986            /*if (PROP_DRAW_DEBUG.get()) {
987                myDrawString(g, "x=" + t.getXtile() + " y=" + t.getYtile() + " z=" + zoom + "", p.x + 2, texty);
988                texty += 1 + fontHeight;
989                if ((t.getXtile() % 32 == 0) && (t.getYtile() % 32 == 0)) {
990                    myDrawString(g, "x=" + t.getXtile() / 32 + " y=" + t.getYtile() / 32 + " z=7", p.x + 2, texty);
991                    texty += 1 + fontHeight;
992                }
993            }*/// end of if draw debug
994    
995            if (tile == showMetadataTile) {
996                String md = tile.toString();
997                if (md != null) {
998                    myDrawString(g, md, p.x + 2, texty);
999                    texty += 1 + fontHeight;
1000                }
1001                Map<String, String> meta = tile.getMetadata();
1002                if (meta != null) {
1003                    for (Map.Entry<String, String> entry : meta.entrySet()) {
1004                        myDrawString(g, entry.getKey() + ": " + entry.getValue(), p.x + 2, texty);
1005                        texty += 1 + fontHeight;
1006                    }
1007                }
1008            }
1009    
1010            /*String tileStatus = tile.getStatus();
1011            if (!tile.isLoaded() && PROP_DRAW_DEBUG.get()) {
1012                myDrawString(g, tr("image " + tileStatus), p.x + 2, texty);
1013                texty += 1 + fontHeight;
1014            }*/
1015    
1016            if (tile.hasError() && showErrors) {
1017                myDrawString(g, tr("Error") + ": " + tr(tile.getErrorMessage()), p.x + 2, texty);
1018                texty += 1 + fontHeight;
1019            }
1020    
1021            /*int xCursor = -1;
1022            int yCursor = -1;
1023            if (PROP_DRAW_DEBUG.get()) {
1024                if (yCursor < t.getYtile()) {
1025                    if (t.getYtile() % 32 == 31) {
1026                        g.fillRect(0, p.y - 1, mv.getWidth(), 3);
1027                    } else {
1028                        g.drawLine(0, p.y, mv.getWidth(), p.y);
1029                    }
1030                    yCursor = t.getYtile();
1031                }
1032                // This draws the vertical lines for the entire
1033                // column. Only draw them for the top tile in
1034                // the column.
1035                if (xCursor < t.getXtile()) {
1036                    if (t.getXtile() % 32 == 0) {
1037                        // level 7 tile boundary
1038                        g.fillRect(p.x - 1, 0, 3, mv.getHeight());
1039                    } else {
1040                        g.drawLine(p.x, 0, p.x, mv.getHeight());
1041                    }
1042                    xCursor = t.getXtile();
1043                }
1044            }*/
1045        }
1046    
1047        private Point pixelPos(LatLon ll) {
1048            return Main.map.mapView.getPoint(Main.getProjection().latlon2eastNorth(ll).add(getDx(), getDy()));
1049        }
1050        
1051        private Point pixelPos(Tile t) {
1052            double lon = tileSource.tileXToLon(t.getXtile(), t.getZoom());
1053            LatLon tmpLL = new LatLon(tileSource.tileYToLat(t.getYtile(), t.getZoom()), lon);
1054            return pixelPos(tmpLL);
1055        }
1056        
1057        private LatLon getShiftedLatLon(EastNorth en) {
1058            return Main.getProjection().eastNorth2latlon(en.add(-getDx(), -getDy()));
1059        }
1060        
1061        private Coordinate getShiftedCoord(EastNorth en) {
1062            LatLon ll = getShiftedLatLon(en);
1063            return new Coordinate(ll.lat(),ll.lon());
1064        }
1065    
1066        private final TileSet nullTileSet = new TileSet((LatLon)null, (LatLon)null, 0);
1067        private class TileSet {
1068            int x0, x1, y0, y1;
1069            int zoom;
1070            int tileMax = -1;
1071    
1072            /**
1073             * Create a TileSet by EastNorth bbox taking a layer shift in account
1074             */
1075            TileSet(EastNorth topLeft, EastNorth botRight, int zoom) {
1076                this(getShiftedLatLon(topLeft), getShiftedLatLon(botRight),zoom);
1077            }
1078    
1079            /**
1080             * Create a TileSet by known LatLon bbox without layer shift correction
1081             */
1082            TileSet(LatLon topLeft, LatLon botRight, int zoom) {
1083                this.zoom = zoom;
1084                if (zoom == 0)
1085                    return;
1086    
1087                x0 = (int)tileSource.lonToTileX(topLeft.lon(),  zoom);
1088                y0 = (int)tileSource.latToTileY(topLeft.lat(),  zoom);
1089                x1 = (int)tileSource.lonToTileX(botRight.lon(), zoom);
1090                y1 = (int)tileSource.latToTileY(botRight.lat(), zoom);
1091                if (x0 > x1) {
1092                    int tmp = x0;
1093                    x0 = x1;
1094                    x1 = tmp;
1095                }
1096                if (y0 > y1) {
1097                    int tmp = y0;
1098                    y0 = y1;
1099                    y1 = tmp;
1100                }
1101                tileMax = (int)Math.pow(2.0, zoom);
1102                if (x0 < 0) {
1103                    x0 = 0;
1104                }
1105                if (y0 < 0) {
1106                    y0 = 0;
1107                }
1108                if (x1 > tileMax) {
1109                    x1 = tileMax;
1110                }
1111                if (y1 > tileMax) {
1112                    y1 = tileMax;
1113                }
1114            }
1115            
1116            boolean tooSmall() {
1117                return this.tilesSpanned() < 2.1;
1118            }
1119            
1120            boolean tooLarge() {
1121                return this.tilesSpanned() > 10;
1122            }
1123            
1124            boolean insane() {
1125                return this.tilesSpanned() > 100;
1126            }
1127            
1128            double tilesSpanned() {
1129                return Math.sqrt(1.0 * this.size());
1130            }
1131    
1132            int size() {
1133                int x_span = x1 - x0 + 1;
1134                int y_span = y1 - y0 + 1;
1135                return x_span * y_span;
1136            }
1137    
1138            /*
1139             * Get all tiles represented by this TileSet that are
1140             * already in the tileCache.
1141             */
1142            List<Tile> allExistingTiles() {
1143                return this.__allTiles(false);
1144            }
1145            
1146            List<Tile> allTilesCreate() {
1147                return this.__allTiles(true);
1148            }
1149            
1150            private List<Tile> __allTiles(boolean create) {
1151                // Tileset is either empty or too large
1152                if (zoom == 0 || this.insane())
1153                    return Collections.emptyList();
1154                List<Tile> ret = new ArrayList<Tile>();
1155                for (int x = x0; x <= x1; x++) {
1156                    for (int y = y0; y <= y1; y++) {
1157                        Tile t;
1158                        if (create) {
1159                            t = getOrCreateTile(x % tileMax, y % tileMax, zoom);
1160                        } else {
1161                            t = getTile(x % tileMax, y % tileMax, zoom);
1162                        }
1163                        if (t != null) {
1164                            ret.add(t);
1165                        }
1166                    }
1167                }
1168                return ret;
1169            }
1170            
1171            private List<Tile> allLoadedTiles() {
1172                List<Tile> ret = new ArrayList<Tile>();
1173                for (Tile t : this.allExistingTiles()) {
1174                    if (t.isLoaded())
1175                        ret.add(t);
1176                }
1177                return ret;
1178            }
1179    
1180            void loadAllTiles(boolean force) {
1181                if (!autoLoad && !force)
1182                    return;
1183                for (Tile t : this.allTilesCreate()) {
1184                    loadTile(t, false);
1185                }
1186            }
1187    
1188            void loadAllErrorTiles(boolean force) {
1189                if (!autoLoad && !force)
1190                    return;
1191                for (Tile t : this.allTilesCreate()) {
1192                    if (t.hasError()) {
1193                        loadTile(t, true);
1194                    }
1195                }
1196            }
1197        }
1198    
1199    
1200        private static class TileSetInfo {
1201            public boolean hasVisibleTiles = false;
1202            public boolean hasOverzoomedTiles = false;
1203            public boolean hasLoadingTiles = false;
1204        }
1205    
1206        private static TileSetInfo getTileSetInfo(TileSet ts) {
1207            List<Tile> allTiles = ts.allExistingTiles();
1208            TileSetInfo result = new TileSetInfo();
1209            result.hasLoadingTiles = allTiles.size() < ts.size();
1210            for (Tile t : allTiles) {
1211                if (t.isLoaded()) {
1212                    if (!t.hasError()) {
1213                        result.hasVisibleTiles = true;
1214                    }
1215                    if ("no-tile".equals(t.getValue("tile-info"))) {
1216                        result.hasOverzoomedTiles = true;
1217                    }
1218                } else {
1219                    result.hasLoadingTiles = true;
1220                }
1221            }
1222            return result;
1223        }
1224    
1225        private class DeepTileSet {
1226            final EastNorth topLeft, botRight;
1227            final int minZoom, maxZoom;
1228            private final TileSet[] tileSets;
1229            private final TileSetInfo[] tileSetInfos;
1230            public DeepTileSet(EastNorth topLeft, EastNorth botRight, int minZoom, int maxZoom) {
1231                this.topLeft = topLeft;
1232                this.botRight = botRight;
1233                this.minZoom = minZoom;
1234                this.maxZoom = maxZoom;
1235                this.tileSets = new TileSet[maxZoom - minZoom + 1];
1236                this.tileSetInfos = new TileSetInfo[maxZoom - minZoom + 1];
1237            }
1238            public TileSet getTileSet(int zoom) {
1239                if (zoom < minZoom)
1240                    return nullTileSet;
1241                TileSet ts = tileSets[zoom-minZoom];
1242                if (ts == null) {
1243                    ts = new TileSet(topLeft, botRight, zoom);
1244                    tileSets[zoom-minZoom] = ts;
1245                }
1246                return ts;
1247            }
1248            public TileSetInfo getTileSetInfo(int zoom) {
1249                if (zoom < minZoom)
1250                    return new TileSetInfo();
1251                TileSetInfo tsi = tileSetInfos[zoom-minZoom];
1252                if (tsi == null) {
1253                    tsi = TMSLayer.getTileSetInfo(getTileSet(zoom));
1254                    tileSetInfos[zoom-minZoom] = tsi;
1255                }
1256                return tsi;
1257            }
1258        }
1259    
1260        @Override
1261        public void paint(Graphics2D g, MapView mv, Bounds bounds) {
1262            //long start = System.currentTimeMillis();
1263            EastNorth topLeft = mv.getEastNorth(0, 0);
1264            EastNorth botRight = mv.getEastNorth(mv.getWidth(), mv.getHeight());
1265    
1266            if (botRight.east() == 0.0 || botRight.north() == 0) {
1267                /*Main.debug("still initializing??");*/
1268                // probably still initializing
1269                return;
1270            }
1271    
1272            needRedraw = false;
1273    
1274            int zoom = currentZoomLevel;
1275            if (autoZoom) {
1276                double pixelScaling = getScaleFactor(zoom);
1277                if (pixelScaling > 3 || pixelScaling < 0.7) {
1278                    zoom = getBestZoom();
1279                }
1280            }
1281    
1282            DeepTileSet dts = new DeepTileSet(topLeft, botRight, getMinZoomLvl(), zoom);
1283            TileSet ts = dts.getTileSet(zoom);
1284    
1285            int displayZoomLevel = zoom;
1286    
1287            boolean noTilesAtZoom = false;
1288            if (autoZoom && autoLoad) {
1289                // Auto-detection of tilesource maxzoom (currently fully works only for Bing)
1290                TileSetInfo tsi = dts.getTileSetInfo(zoom);
1291                if (!tsi.hasVisibleTiles && (!tsi.hasLoadingTiles || tsi.hasOverzoomedTiles)) {
1292                    noTilesAtZoom = true;
1293                }
1294                // Find highest zoom level with at least one visible tile
1295                for (int tmpZoom = zoom; tmpZoom > dts.minZoom; tmpZoom--) {
1296                    if (dts.getTileSetInfo(tmpZoom).hasVisibleTiles) {
1297                        displayZoomLevel = tmpZoom;
1298                        break;
1299                    }
1300                }
1301                // Do binary search between currentZoomLevel and displayZoomLevel
1302                while (zoom > displayZoomLevel && !tsi.hasVisibleTiles && tsi.hasOverzoomedTiles){
1303                    zoom = (zoom + displayZoomLevel)/2;
1304                    tsi = dts.getTileSetInfo(zoom);
1305                }
1306    
1307                setZoomLevel(zoom);
1308    
1309                // If all tiles at displayZoomLevel is loaded, load all tiles at next zoom level
1310                // to make sure there're really no more zoom levels
1311                if (zoom == displayZoomLevel && !tsi.hasLoadingTiles && zoom < dts.maxZoom) {
1312                    zoom++;
1313                    tsi = dts.getTileSetInfo(zoom);
1314                }
1315                // When we have overzoomed tiles and all tiles at current zoomlevel is loaded,
1316                // load tiles at previovus zoomlevels until we have all tiles on screen is loaded.
1317                while (zoom > dts.minZoom && tsi.hasOverzoomedTiles && !tsi.hasLoadingTiles) {
1318                    zoom--;
1319                    tsi = dts.getTileSetInfo(zoom);
1320                }
1321                ts = dts.getTileSet(zoom);
1322            } else if (autoZoom) {
1323                setZoomLevel(zoom);
1324            }
1325    
1326            // Too many tiles... refuse to download
1327            if (!ts.tooLarge()) {
1328                //Main.debug("size: " + ts.size() + " spanned: " + ts.tilesSpanned());
1329                ts.loadAllTiles(false);
1330            }
1331    
1332            if (displayZoomLevel != zoom) {
1333                ts = dts.getTileSet(displayZoomLevel);
1334            }
1335    
1336            g.setColor(Color.DARK_GRAY);
1337    
1338            List<Tile> missedTiles = this.paintTileImages(g, ts, displayZoomLevel, null);
1339            int otherZooms[] = { -1, 1, -2, 2, -3, -4, -5};
1340            for (int zoomOffset : otherZooms) {
1341                if (!autoZoom) {
1342                    break;
1343                }
1344                int newzoom = displayZoomLevel + zoomOffset;
1345                if (newzoom < MIN_ZOOM) {
1346                    continue;
1347                }
1348                if (missedTiles.size() <= 0) {
1349                    break;
1350                }
1351                List<Tile> newlyMissedTiles = new LinkedList<Tile>();
1352                for (Tile missed : missedTiles) {
1353                    if ("no-tile".equals(missed.getValue("tile-info")) && zoomOffset > 0) {
1354                        // Don't try to paint from higher zoom levels when tile is overzoomed
1355                        newlyMissedTiles.add(missed);
1356                        continue;
1357                    }
1358                    Tile t2 = tempCornerTile(missed);
1359                    LatLon topLeft2  = tileLatLon(missed);
1360                    LatLon botRight2 = tileLatLon(t2);
1361                    TileSet ts2 = new TileSet(topLeft2, botRight2, newzoom);
1362                    // Instantiating large TileSets is expensive.  If there
1363                    // are no loaded tiles, don't bother even trying.
1364                    if (ts2.allLoadedTiles().size() == 0) {
1365                        newlyMissedTiles.add(missed);
1366                        continue;
1367                    }
1368                    if (ts2.tooLarge()) {
1369                        continue;
1370                    }
1371                    newlyMissedTiles.addAll(this.paintTileImages(g, ts2, newzoom, missed));
1372                }
1373                missedTiles = newlyMissedTiles;
1374            }
1375            /*if (debug && missedTiles.size() > 0) {
1376                Main.debug("still missed "+missedTiles.size()+" in the end");
1377            }*/
1378            g.setColor(Color.red);
1379            g.setFont(InfoFont);
1380    
1381            // The current zoom tileset should have all of its tiles
1382            // due to the loadAllTiles(), unless it to tooLarge()
1383            for (Tile t : ts.allExistingTiles()) {
1384                this.paintTileText(ts, t, g, mv, displayZoomLevel, t);
1385            }
1386    
1387            attribution.paintAttribution(g, mv.getWidth(), mv.getHeight(), getShiftedCoord(topLeft), getShiftedCoord(botRight), displayZoomLevel, this);
1388    
1389            //g.drawString("currentZoomLevel=" + currentZoomLevel, 120, 120);
1390            g.setColor(Color.lightGray);
1391            if (!autoZoom) {
1392                if (ts.insane()) {
1393                    myDrawString(g, tr("zoom in to load any tiles"), 120, 120);
1394                } else if (ts.tooLarge()) {
1395                    myDrawString(g, tr("zoom in to load more tiles"), 120, 120);
1396                } else if (ts.tooSmall()) {
1397                    myDrawString(g, tr("increase zoom level to see more detail"), 120, 120);
1398                }
1399            }
1400            if (noTilesAtZoom) {
1401                myDrawString(g, tr("No tiles at this zoom level"), 120, 120);
1402            }
1403            /*if (debug) {
1404                myDrawString(g, tr("Current zoom: {0}", currentZoomLevel), 50, 140);
1405                myDrawString(g, tr("Display zoom: {0}", displayZoomLevel), 50, 155);
1406                myDrawString(g, tr("Pixel scale: {0}", getScaleFactor(currentZoomLevel)), 50, 170);
1407                myDrawString(g, tr("Best zoom: {0}", Math.log(getScaleFactor(1))/Math.log(2)/2+1), 50, 185);
1408            }*/
1409        }
1410    
1411        /**
1412         * This isn't very efficient, but it is only used when the
1413         * user right-clicks on the map.
1414         */
1415        Tile getTileForPixelpos(int px, int py) {
1416            /*if (debug) {
1417                Main.debug("getTileForPixelpos("+px+", "+py+")");
1418            }*/
1419            MapView mv = Main.map.mapView;
1420            Point clicked = new Point(px, py);
1421            EastNorth topLeft = mv.getEastNorth(0, 0);
1422            EastNorth botRight = mv.getEastNorth(mv.getWidth(), mv.getHeight());
1423            int z = currentZoomLevel;
1424            TileSet ts = new TileSet(topLeft, botRight, z);
1425    
1426            if (!ts.tooLarge()) {
1427                ts.loadAllTiles(false); // make sure there are tile objects for all tiles
1428            }
1429            Tile clickedTile = null;
1430            for (Tile t1 : ts.allExistingTiles()) {
1431                Tile t2 = tempCornerTile(t1);
1432                Rectangle r = new Rectangle(pixelPos(t1));
1433                r.add(pixelPos(t2));
1434                /*if (debug) {
1435                    Main.debug("r: " + r + " clicked: " + clicked);
1436                }*/
1437                if (!r.contains(clicked)) {
1438                    continue;
1439                }
1440                clickedTile  = t1;
1441                break;
1442            }
1443            if (clickedTile == null)
1444                return null;
1445            /*Main.debug("Clicked on tile: " + clickedTile.getXtile() + " " + clickedTile.getYtile() +
1446                    " currentZoomLevel: " + currentZoomLevel);*/
1447            return clickedTile;
1448        }
1449    
1450        @Override
1451        public Action[] getMenuEntries() {
1452            return new Action[] {
1453                    LayerListDialog.getInstance().createShowHideLayerAction(),
1454                    LayerListDialog.getInstance().createDeleteLayerAction(),
1455                    SeparatorLayerAction.INSTANCE,
1456                    // color,
1457                    new OffsetAction(),
1458                    new RenameLayerAction(this.getAssociatedFile(), this),
1459                    SeparatorLayerAction.INSTANCE,
1460                    new LayerListPopup.InfoAction(this) };
1461        }
1462    
1463        @Override
1464        public String getToolTipText() {
1465            return null;
1466        }
1467    
1468        @Override
1469        public void visitBoundingBox(BoundingXYVisitor v) {
1470        }
1471    
1472        @Override
1473        public boolean isChanged() {
1474            return needRedraw;
1475        }
1476    
1477        @Override
1478        public boolean isProjectionSupported(Projection proj) {
1479            return "EPSG:3857".equals(proj.toCode()) || "EPSG:4326".equals(proj.toCode());
1480        }
1481    
1482        @Override
1483        public String nameSupportedProjections() {
1484            return tr("EPSG:4326 and Mercator projection are supported");
1485        }
1486    }