Changeset 3602 in josm for trunk/src


Ignore:
Timestamp:
2010-10-12T21:09:37+02:00 (14 years ago)
Author:
jttt
Message:

Allow to use custom tile sources in slippy map chooser (see #3744)

Location:
trunk/src/org/openstreetmap/josm/gui/bbox
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/bbox/SlippyMapBBoxChooser.java

    r3597 r3602  
    88import java.awt.Rectangle;
    99import java.awt.Toolkit;
     10import java.util.ArrayList;
     11import java.util.Arrays;
     12import java.util.List;
    1013import java.util.Vector;
     14import java.util.concurrent.CopyOnWriteArrayList;
    1115
    1216import org.openstreetmap.gui.jmapviewer.Coordinate;
     
    2428import org.openstreetmap.josm.data.Bounds;
    2529import org.openstreetmap.josm.data.coor.LatLon;
     30import org.openstreetmap.josm.data.preferences.StringProperty;
    2631
    2732public class SlippyMapBBoxChooser extends JMapViewer implements BBoxChooser{
    28     static private TileSource[] TILE_SOURCES = { new OsmTileSource.Mapnik(),
    29         new OsmTileSource.TilesAtHome(), new OsmTileSource.CycleMap() };
     33
     34    public interface TileSourceProvider {
     35        List<TileSource> getTileSources();
     36    }
     37
     38    public static class RenamedSourceDecorator implements TileSource {
     39
     40        private final TileSource source;
     41        private final String name;
     42
     43        public RenamedSourceDecorator(TileSource source, String name) {
     44            this.source = source;
     45            this.name = name;
     46        }
     47
     48        @Override public String getName() {
     49            return name;
     50        }
     51
     52        @Override public int getMaxZoom() { return source.getMaxZoom(); }
     53
     54        @Override public int getMinZoom() { return source.getMinZoom(); }
     55
     56        @Override public int getTileSize() { return source.getTileSize(); }
     57
     58        @Override public String getTileType() { return source.getTileType(); }
     59
     60        @Override public TileUpdate getTileUpdate() { return source.getTileUpdate(); }
     61
     62        @Override public String getTileUrl(int zoom, int tilex, int tiley) { return source.getTileUrl(zoom, tilex, tiley); }
     63
     64
     65    }
     66
     67    /**
     68     * Plugins that wish to add custom tile sources to slippy map choose should call this method
     69     * @param tileSourceProvider
     70     */
     71    public static void addTileSourceProvider(TileSourceProvider tileSourceProvider) {
     72        providers.addIfAbsent(tileSourceProvider);
     73    }
     74
     75    private static CopyOnWriteArrayList<TileSourceProvider> providers = new CopyOnWriteArrayList<TileSourceProvider>();
     76
     77    static {
     78        addTileSourceProvider(new TileSourceProvider() {
     79            @Override
     80            public List<TileSource> getTileSources() {
     81                return Arrays.<TileSource>asList(
     82                        new RenamedSourceDecorator(new OsmTileSource.Mapnik(), "Mapnik"),
     83                        new RenamedSourceDecorator(new OsmTileSource.TilesAtHome(), "Osmarender"),
     84                        new RenamedSourceDecorator(new OsmTileSource.CycleMap(), "Cyclemap")
     85                );
     86            }
     87        });
     88    }
     89
     90    private static final StringProperty PROP_MAPSTYLE = new StringProperty("slippy_map_chooser.mapstyle", "Mapnik");
    3091
    3192    // standard dimension
     
    3596    private TileLoader uncachedLoader;
    3697
    37     private SizeButton iSizeButton = new SizeButton();
    38     private SourceButton iSourceButton = new SourceButton();
     98    private final SizeButton iSizeButton = new SizeButton();
     99    private final SourceButton iSourceButton;
    39100    private Bounds bbox;
    40101
     
    69130        setMaxTilesInMemory(Main.pref.getInteger("slippy_map_chooser.max_tiles", 1000));
    70131
    71         String mapStyle = Main.pref.get("slippy_map_chooser.mapstyle", "mapnik");
    72         if (mapStyle.equals("osmarender")) {
    73             iSourceButton.setMapStyle(SourceButton.OSMARENDER);
    74             this.setTileSource(TILE_SOURCES[1]);
    75         } else if (mapStyle.equals("cyclemap")) {
    76             iSourceButton.setMapStyle(SourceButton.CYCLEMAP);
    77             this.setTileSource(TILE_SOURCES[2]);
    78         } else {
    79             if (!mapStyle.equals("mapnik")) {
    80                 Main.pref.put("slippy_map_chooser", "mapnik");
     132        List<TileSource> tileSources = new ArrayList<TileSource>();
     133        for (TileSourceProvider provider: providers) {
     134            tileSources.addAll(provider.getTileSources());
     135        }
     136
     137        iSourceButton = new SourceButton(tileSources);
     138
     139        String mapStyle = PROP_MAPSTYLE.get();
     140        boolean foundSource = false;
     141        for (TileSource source: tileSources) {
     142            if (source.getName().equals(mapStyle)) {
     143                this.setTileSource(source);
     144                iSourceButton.setCurrentMap(source);
     145                foundSource = true;
     146                break;
    81147            }
     148        }
     149        if (!foundSource) {
     150            setTileSource(tileSources.get(0));
     151            iSourceButton.setCurrentMap(tileSources.get(0));
    82152        }
    83153
     
    207277    }
    208278
    209     public void toggleMapSource(int mapSource) {
     279    public void toggleMapSource(TileSource tileSource) {
    210280        this.tileController.setTileCache(new MemoryTileCache());
    211         if (mapSource == SourceButton.MAPNIK) {
    212             this.setTileSource(TILE_SOURCES[0]);
    213             Main.pref.put("slippy_map_chooser.mapstyle", "mapnik");
    214         } else if (mapSource == SourceButton.CYCLEMAP) {
    215             this.setTileSource(TILE_SOURCES[2]);
    216             Main.pref.put("slippy_map_chooser.mapstyle", "cyclemap");
    217         } else {
    218             this.setTileSource(TILE_SOURCES[1]);
    219             Main.pref.put("slippy_map_chooser.mapstyle", "osmarender");
    220         }
     281        this.setTileSource(tileSource);
     282        PROP_MAPSTYLE.put(tileSource.getName()); // TODO Is name really unique?
    221283    }
    222284
  • trunk/src/org/openstreetmap/josm/gui/bbox/SlippyMapControler.java

    r3105 r3602  
    126126    }
    127127
     128    @Override
    128129    public void mouseDragged(MouseEvent e) {
    129130        if ((e.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK) == MouseEvent.BUTTON1_DOWN_MASK) {
     
    152153                iSlippyMapChooser.repaint();
    153154
    154             } else if (sourceButton == SourceButton.MAPNIK || sourceButton == SourceButton.OSMARENDER
    155                     || sourceButton == SourceButton.CYCLEMAP) {
    156                 iSlippyMapChooser.toggleMapSource(sourceButton);
     155            } else if (sourceButton != 0) {
     156                iSlippyMapChooser.toggleMapSource(iSourceButton.hitToTileSource(sourceButton));
    157157            } else {
    158158                if (e.getClickCount() == 1) {
     
    168168    }
    169169
     170    @Override
    170171    public void mouseMoved(MouseEvent e) {
    171172    }
  • trunk/src/org/openstreetmap/josm/gui/bbox/SourceButton.java

    r3597 r3602  
    77import java.awt.Point;
    88import java.awt.RenderingHints;
     9import java.util.List;
    910
    1011import javax.swing.ImageIcon;
    1112
     13import org.openstreetmap.gui.jmapviewer.interfaces.TileSource;
    1214import org.openstreetmap.josm.tools.ImageProvider;
    1315
     
    2022    private int layerHeight;
    2123
    22     private String[] sources = new String[] {"Mapnik", "Osmarender", "Cyclemap"};
     24    private final TileSource[] sources;
    2325
    2426    private ImageIcon enlargeImage;
     
    2729    private boolean isEnlarged = false;
    2830
    29     private int currentMap = MAPNIK;
     31    private int currentMap;
    3032
    3133    public static final int HIDE_OR_SHOW = 1;
    32     public static final int MAPNIK = 2;
    33     public static final int OSMARENDER = 3;
    34     public static final int CYCLEMAP = 4;
    3534
    36     public SourceButton() {
     35    public SourceButton(List<TileSource> sources) {
     36        this.sources = sources.toArray(new TileSource[sources.size()]);
     37        this.currentMap = 2;
    3738        enlargeImage = ImageProvider.get("layer-switcher-maximize.png");
    3839        shrinkImage = ImageProvider.get("layer-switcher-minimize.png");
     
    5152            g.setFont(g.getFont().deriveFont(Font.BOLD).deriveFont(15.0f));
    5253            FontMetrics fm = g.getFontMetrics();
    53             for (String source: sources) {
    54                 int width = fm.stringWidth(source);
     54            for (TileSource source: sources) {
     55                int width = fm.stringWidth(source.getName());
    5556                if (width > textWidth) {
    5657                    textWidth = width;
     
    6869                g.setColor(Color.WHITE);
    6970                g.fillOval(barX + leftPadding, barY + topPadding + i * layerHeight + 6, radioButtonSize, radioButtonSize);
    70                 g.drawString(sources[i], barX + leftPadding + radioButtonSize + leftPadding, barY + topPadding + i * layerHeight + g.getFontMetrics().getHeight());
     71                g.drawString(sources[i].getName(), barX + leftPadding + radioButtonSize + leftPadding, barY + topPadding + i * layerHeight + g.getFontMetrics().getHeight());
    7172                if (currentMap == i + 2) {
    7273                    g.setColor(Color.BLACK);
     
    111112    }
    112113
    113     /**
    114      * One of the constants OSMARENDER,MAPNIK or CYCLEMAP
    115      */
    116     public void setMapStyle (int style) {
    117         currentMap = (style < 2 || style > 4) ? MAPNIK : style;
     114    public TileSource hitToTileSource(int hit) {
     115        if (hit >= 2 && hit < sources.length + 2)
     116            return sources[hit - 2];
     117        else
     118            return null;
     119    }
     120
     121    public void setCurrentMap(TileSource tileSource) {
     122        for (int i=0; i<sources.length; i++) {
     123            if (sources[i].equals(tileSource)) {
     124                currentMap = i + 2;
     125                return;
     126            }
     127        }
     128        currentMap = 2;
    118129    }
    119130}
Note: See TracChangeset for help on using the changeset viewer.