Changeset 3602 in josm
- Timestamp:
- 2010-10-12T21:09:37+02:00 (14 years ago)
- 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 8 8 import java.awt.Rectangle; 9 9 import java.awt.Toolkit; 10 import java.util.ArrayList; 11 import java.util.Arrays; 12 import java.util.List; 10 13 import java.util.Vector; 14 import java.util.concurrent.CopyOnWriteArrayList; 11 15 12 16 import org.openstreetmap.gui.jmapviewer.Coordinate; … … 24 28 import org.openstreetmap.josm.data.Bounds; 25 29 import org.openstreetmap.josm.data.coor.LatLon; 30 import org.openstreetmap.josm.data.preferences.StringProperty; 26 31 27 32 public 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"); 30 91 31 92 // standard dimension … … 35 96 private TileLoader uncachedLoader; 36 97 37 private SizeButton iSizeButton = new SizeButton(); 38 private SourceButton iSourceButton = new SourceButton();98 private final SizeButton iSizeButton = new SizeButton(); 99 private final SourceButton iSourceButton; 39 100 private Bounds bbox; 40 101 … … 69 130 setMaxTilesInMemory(Main.pref.getInteger("slippy_map_chooser.max_tiles", 1000)); 70 131 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; 81 147 } 148 } 149 if (!foundSource) { 150 setTileSource(tileSources.get(0)); 151 iSourceButton.setCurrentMap(tileSources.get(0)); 82 152 } 83 153 … … 207 277 } 208 278 209 public void toggleMapSource( int mapSource) {279 public void toggleMapSource(TileSource tileSource) { 210 280 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? 221 283 } 222 284 -
trunk/src/org/openstreetmap/josm/gui/bbox/SlippyMapControler.java
r3105 r3602 126 126 } 127 127 128 @Override 128 129 public void mouseDragged(MouseEvent e) { 129 130 if ((e.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK) == MouseEvent.BUTTON1_DOWN_MASK) { … … 152 153 iSlippyMapChooser.repaint(); 153 154 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)); 157 157 } else { 158 158 if (e.getClickCount() == 1) { … … 168 168 } 169 169 170 @Override 170 171 public void mouseMoved(MouseEvent e) { 171 172 } -
trunk/src/org/openstreetmap/josm/gui/bbox/SourceButton.java
r3597 r3602 7 7 import java.awt.Point; 8 8 import java.awt.RenderingHints; 9 import java.util.List; 9 10 10 11 import javax.swing.ImageIcon; 11 12 13 import org.openstreetmap.gui.jmapviewer.interfaces.TileSource; 12 14 import org.openstreetmap.josm.tools.ImageProvider; 13 15 … … 20 22 private int layerHeight; 21 23 22 private String[] sources= new String[] {"Mapnik", "Osmarender", "Cyclemap"};24 private final TileSource[] sources; 23 25 24 26 private ImageIcon enlargeImage; … … 27 29 private boolean isEnlarged = false; 28 30 29 private int currentMap = MAPNIK;31 private int currentMap; 30 32 31 33 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;35 34 36 public SourceButton() { 35 public SourceButton(List<TileSource> sources) { 36 this.sources = sources.toArray(new TileSource[sources.size()]); 37 this.currentMap = 2; 37 38 enlargeImage = ImageProvider.get("layer-switcher-maximize.png"); 38 39 shrinkImage = ImageProvider.get("layer-switcher-minimize.png"); … … 51 52 g.setFont(g.getFont().deriveFont(Font.BOLD).deriveFont(15.0f)); 52 53 FontMetrics fm = g.getFontMetrics(); 53 for ( Stringsource: sources) {54 int width = fm.stringWidth(source); 54 for (TileSource source: sources) { 55 int width = fm.stringWidth(source.getName()); 55 56 if (width > textWidth) { 56 57 textWidth = width; … … 68 69 g.setColor(Color.WHITE); 69 70 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()); 71 72 if (currentMap == i + 2) { 72 73 g.setColor(Color.BLACK); … … 111 112 } 112 113 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; 118 129 } 119 130 }
Note:
See TracChangeset
for help on using the changeset viewer.