001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.gui.mappaint.loader; 003 004import static org.openstreetmap.josm.tools.I18n.tr; 005 006import java.util.Arrays; 007import java.util.Collection; 008import java.util.Collections; 009import java.util.List; 010import java.util.stream.Collectors; 011 012import org.openstreetmap.josm.data.preferences.sources.SourceEntry; 013import org.openstreetmap.josm.gui.MainApplication; 014import org.openstreetmap.josm.gui.PleaseWaitRunnable; 015import org.openstreetmap.josm.gui.mappaint.MapPaintStyles; 016import org.openstreetmap.josm.gui.mappaint.StyleSource; 017import org.openstreetmap.josm.gui.progress.ProgressMonitor; 018 019/** 020 * This class loads the map paint styles 021 * @since 12651 (extracted from {@link MapPaintStyles}). 022 */ 023public class MapPaintStyleLoader extends PleaseWaitRunnable { 024 private boolean canceled; 025 private final Collection<StyleSource> sources; 026 027 /** 028 * Create a new {@link MapPaintStyleLoader} 029 * @param sources The styles to load 030 */ 031 public MapPaintStyleLoader(Collection<StyleSource> sources) { 032 super(tr("Reloading style sources")); 033 this.sources = sources; 034 } 035 036 @Override 037 protected void cancel() { 038 canceled = true; 039 } 040 041 @Override 042 protected void finish() { 043 MapPaintStyles.fireMapPaintStylesUpdated(); 044 } 045 046 @Override 047 protected void realRun() { 048 ProgressMonitor monitor = getProgressMonitor(); 049 monitor.setTicksCount(sources.size()); 050 for (StyleSource s : sources) { 051 if (canceled) 052 return; 053 monitor.subTask(tr("loading style ''{0}''...", s.getDisplayString())); 054 s.loadStyleSource(); 055 monitor.worked(1); 056 } 057 } 058 059 /** 060 * Reload styles 061 * preferences are the same, but the file source may have changed 062 * @param sel the indices of styles to reload 063 */ 064 public static void reloadStyles(final int... sel) { 065 List<StyleSource> data = MapPaintStyles.getStyles().getStyleSources(); 066 List<StyleSource> toReload = Arrays.stream(sel).mapToObj(data::get).collect(Collectors.toList()); 067 MainApplication.worker.submit(new MapPaintStyleLoader(toReload)); 068 } 069 070 /** 071 * Reload style. 072 * @param style {@link StyleSource} to reload 073 * @throws IllegalArgumentException if {@code style} is not a {@code StyleSource} instance 074 * @since 12825 075 */ 076 public static void reloadStyle(SourceEntry style) { 077 if (style instanceof StyleSource) { 078 MainApplication.worker.submit(new MapPaintStyleLoader(Collections.singleton((StyleSource) style))); 079 } else { 080 throw new IllegalArgumentException(style + " is not a StyleSource"); 081 } 082 } 083}