Changeset 14300 in josm for trunk/src/org/openstreetmap
- Timestamp:
- 2018-10-06T22:01:03+02:00 (6 years ago)
- Location:
- trunk/src/org/openstreetmap/josm/gui
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/bbox/SlippyMapBBoxChooser.java
r14273 r14300 16 16 import java.util.Collections; 17 17 import java.util.HashMap; 18 import java.util. HashSet;18 import java.util.LinkedHashMap; 19 19 import java.util.List; 20 20 import java.util.Map; 21 import java.util.Set;22 21 import java.util.concurrent.CopyOnWriteArrayList; 23 22 import java.util.concurrent.TimeUnit; 23 import java.util.stream.Collectors; 24 24 25 25 import javax.swing.ButtonModel; … … 53 53 import org.openstreetmap.josm.gui.MainApplication; 54 54 import org.openstreetmap.josm.gui.layer.AbstractCachedTileSourceLayer; 55 import org.openstreetmap.josm.gui.layer.ImageryLayer; 55 56 import org.openstreetmap.josm.gui.layer.MainLayerManager; 56 57 import org.openstreetmap.josm.gui.layer.TMSLayer; … … 61 62 * This panel displays a map and lets the user chose a {@link BBox}. 62 63 */ 63 public class SlippyMapBBoxChooser extends JMapViewer implements BBoxChooser, ChangeListener, MainLayerManager.ActiveLayerChangeListener {64 64 public class SlippyMapBBoxChooser extends JMapViewer implements BBoxChooser, ChangeListener, 65 MainLayerManager.ActiveLayerChangeListener, MainLayerManager.LayerChangeListener { 65 66 /** 66 67 * A list of tile sources that can be used for displaying the map. … … 76 77 77 78 /** 78 * TMS TileSource provider for the slippymap chooser 79 */ 80 public static class TMSTileSourceProvider implements TileSourceProvider { 81 private static final Set<String> existingSlippyMapUrls = new HashSet<>(); 82 static { 83 // Urls that already exist in the slippymap chooser and shouldn't be copied from TMS layer list 84 existingSlippyMapUrls.add("https://{switch:a,b,c}.tile.openstreetmap.org/{zoom}/{x}/{y}.png"); // Mapnik 85 } 79 * TileSource provider for the slippymap chooser. 80 * @since 14300 81 */ 82 public abstract static class AbstractImageryInfoBasedTileSourceProvider implements TileSourceProvider { 83 /** 84 * Returns the list of imagery infos backing tile sources. 85 * @return the list of imagery infos backing tile sources 86 */ 87 public abstract List<ImageryInfo> getImageryInfos(); 86 88 87 89 @Override … … 89 91 if (!TMSLayer.PROP_ADD_TO_SLIPPYMAP_CHOOSER.get()) return Collections.<TileSource>emptyList(); 90 92 List<TileSource> sources = new ArrayList<>(); 91 for (ImageryInfo info : ImageryLayerInfo.instance.getLayers()) { 92 if (existingSlippyMapUrls.contains(info.getUrl())) { 93 continue; 94 } 93 for (ImageryInfo info : this.getImageryInfos()) { 95 94 try { 96 95 TileSource source = TMSLayer.getTileSourceStatic(info); … … 112 111 113 112 /** 113 * TileSource provider for the slippymap chooser - providing sources from imagery sources menu 114 * @since 14300 115 */ 116 public static class TMSTileSourceProvider extends AbstractImageryInfoBasedTileSourceProvider { 117 @Override 118 public List<ImageryInfo> getImageryInfos() { 119 return ImageryLayerInfo.instance.getLayers(); 120 } 121 } 122 123 /** 124 * TileSource provider for the slippymap chooser - providing sources from current layers 125 * @since 14300 126 */ 127 public static class CurrentLayersTileSourceProvider extends AbstractImageryInfoBasedTileSourceProvider { 128 @Override 129 public List<ImageryInfo> getImageryInfos() { 130 return MainApplication.getLayerManager().getLayers().stream().filter( 131 layer -> layer instanceof ImageryLayer 132 ).map( 133 layer -> ((ImageryLayer) layer).getInfo() 134 ).collect(Collectors.toList()); 135 } 136 } 137 138 /** 114 139 * Plugins that wish to add custom tile sources to slippy map choose should call this method 115 140 * @param tileSourceProvider new tile source provider … … 123 148 addTileSourceProvider(() -> Arrays.<TileSource>asList(new OsmTileSource.Mapnik())); 124 149 addTileSourceProvider(new TMSTileSourceProvider()); 150 addTileSourceProvider(new CurrentLayersTileSourceProvider()); 125 151 } 126 152 … … 178 204 setMaxTilesInMemory(Config.getPref().getInt("slippy_map_chooser.max_tiles", 1000)); 179 205 180 List<TileSource> tileSources = getAllTileSources();206 List<TileSource> tileSources = new ArrayList<>(getAllTileSources().values()); 181 207 182 208 this.showDownloadAreaButtonModel = new JToggleButton.ToggleButtonModel(); … … 211 237 } 212 238 213 private static List<TileSource> getAllTileSources() { 214 List<TileSource> tileSources = new ArrayList<>(); 215 for (TileSourceProvider provider: providers) { 216 tileSources.addAll(provider.getTileSources()); 217 } 218 return tileSources; 239 private static LinkedHashMap<String, TileSource> getAllTileSources() { 240 // using a LinkedHashMap of <id, TileSource> to retain ordering but provide deduplication 241 return providers.stream().flatMap( 242 provider -> provider.getTileSources().stream() 243 ).collect(Collectors.toMap( 244 TileSource::getId, 245 ts -> ts, 246 (oldTs, newTs) -> oldTs, 247 LinkedHashMap::new 248 )); 219 249 } 220 250 … … 359 389 this.setTileSource(tileSource); 360 390 PROP_MAPSTYLE.put(tileSource.getName()); // TODO Is name really unique? 361 if (this.iSourceButton.getCurrentSource() != tileSource) { // prevent infinite recursion 362 this.iSourceButton.setCurrentMap(tileSource); 363 } 391 392 // we need to refresh the tile sources in case the deselected source should no longer be present 393 // (and only remained there because its removal was deferred while the source was still the 394 // selected one). this should also have the effect of propagating the new selection to the 395 // iSourceButton & menu: it attempts to re-select the current source when rebuilding its menu. 396 this.refreshTileSources(); 364 397 } 365 398 … … 417 450 */ 418 451 public final void refreshTileSources() { 419 iSourceButton.setSources(getAllTileSources()); 420 } 452 final LinkedHashMap<String, TileSource> newTileSources = getAllTileSources(); 453 final TileSource currentTileSource = this.getTileController().getTileSource(); 454 455 // re-add the currently active TileSource to prevent inconsistent display of menu 456 newTileSources.putIfAbsent(currentTileSource.getId(), currentTileSource); 457 458 this.iSourceButton.setSources(new ArrayList<>(newTileSources.values())); 459 } 460 461 @Override 462 public void layerAdded(MainLayerManager.LayerAddEvent e) { 463 if (e.getAddedLayer() instanceof ImageryLayer) { 464 this.refreshTileSources(); 465 } 466 } 467 468 @Override 469 public void layerRemoving(MainLayerManager.LayerRemoveEvent e) { 470 if (e.getRemovedLayer() instanceof ImageryLayer) { 471 this.refreshTileSources(); 472 } 473 } 474 475 @Override 476 public void layerOrderChanged(MainLayerManager.LayerOrderChangeEvent e) {} 421 477 } -
trunk/src/org/openstreetmap/josm/gui/bbox/SourceButton.java
r12962 r14300 82 82 83 83 // attempt to initialize button group matching current state of slippyMapBBoxChooser 84 buttonModel.setSelected(this.slippyMapBBoxChooser.getTileController().getTileSource() == ts);84 buttonModel.setSelected(this.slippyMapBBoxChooser.getTileController().getTileSource().getId().equals(ts.getId())); 85 85 } 86 86 -
trunk/src/org/openstreetmap/josm/gui/dialogs/MinimapDialog.java
r12630 r14300 38 38 slippyMap.setSizeButtonVisible(false); 39 39 slippyMap.addPropertyChangeListener(BBoxChooser.BBOX_PROP, this); 40 MainApplication.getLayerManager().addLayerChangeListener(slippyMap); 40 41 } 41 42
Note:
See TracChangeset
for help on using the changeset viewer.