source: josm/trunk/src/org/openstreetmap/josm/gui/mappaint/loader/MapPaintStyleLoader.java@ 13691

Last change on this file since 13691 was 12825, checked in by Don-vip, 7 years ago

see #15229 - see #15182 - make FileWatcher generic so it has no more dependence on MapCSS

  • Property svn:eol-style set to native
File size: 2.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.mappaint.loader;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.util.ArrayList;
7import java.util.Collection;
8import java.util.Collections;
9import java.util.List;
10
11import org.openstreetmap.josm.data.preferences.sources.SourceEntry;
12import org.openstreetmap.josm.gui.MainApplication;
13import org.openstreetmap.josm.gui.PleaseWaitRunnable;
14import org.openstreetmap.josm.gui.mappaint.MapPaintStyles;
15import org.openstreetmap.josm.gui.mappaint.StyleSource;
16import org.openstreetmap.josm.gui.progress.ProgressMonitor;
17
18/**
19 * This class loads the map paint styles
20 * @since 12651 (extracted from {@link MapPaintStyles}).
21 */
22public class MapPaintStyleLoader extends PleaseWaitRunnable {
23 private boolean canceled;
24 private final Collection<StyleSource> sources;
25
26 /**
27 * Create a new {@link MapPaintStyleLoader}
28 * @param sources The styles to load
29 */
30 public MapPaintStyleLoader(Collection<StyleSource> sources) {
31 super(tr("Reloading style sources"));
32 this.sources = sources;
33 }
34
35 @Override
36 protected void cancel() {
37 canceled = true;
38 }
39
40 @Override
41 protected void finish() {
42 MapPaintStyles.fireMapPaintSylesUpdated();
43 }
44
45 @Override
46 protected void realRun() {
47 ProgressMonitor monitor = getProgressMonitor();
48 monitor.setTicksCount(sources.size());
49 for (StyleSource s : sources) {
50 if (canceled)
51 return;
52 monitor.subTask(tr("loading style ''{0}''...", s.getDisplayString()));
53 s.loadStyleSource();
54 monitor.worked(1);
55 }
56 }
57
58 /**
59 * Reload styles
60 * preferences are the same, but the file source may have changed
61 * @param sel the indices of styles to reload
62 */
63 public static void reloadStyles(final int... sel) {
64 List<StyleSource> toReload = new ArrayList<>();
65 List<StyleSource> data = MapPaintStyles.getStyles().getStyleSources();
66 for (int i : sel) {
67 toReload.add(data.get(i));
68 }
69 MainApplication.worker.submit(new MapPaintStyleLoader(toReload));
70 }
71
72 /**
73 * Reload style.
74 * @param style {@link StyleSource} to reload
75 * @throws IllegalArgumentException if {@code style} is not a {@code StyleSource} instance
76 * @since 12825
77 */
78 public static void reloadStyle(SourceEntry style) {
79 if (style instanceof StyleSource) {
80 MainApplication.worker.submit(new MapPaintStyleLoader(Collections.singleton((StyleSource) style)));
81 } else {
82 throw new IllegalArgumentException(style + " is not a StyleSource");
83 }
84 }
85}
Note: See TracBrowser for help on using the repository browser.