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