1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.mappaint.loader;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.util.ArrayList;
|
---|
7 | import java.util.Collection;
|
---|
8 | import java.util.Collections;
|
---|
9 | import java.util.List;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.data.preferences.sources.SourceEntry;
|
---|
12 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
13 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
14 | import org.openstreetmap.josm.gui.mappaint.MapPaintStyles;
|
---|
15 | import org.openstreetmap.josm.gui.mappaint.StyleSource;
|
---|
16 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * This class loads the map paint styles
|
---|
20 | * @since 12651 (extracted from {@link MapPaintStyles}).
|
---|
21 | */
|
---|
22 | public 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 | }
|
---|