source: josm/trunk/src/org/openstreetmap/josm/gui/mappaint/MapPaintStyles.java@ 12967

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

see #15273 - fix initialization of MapPaintStyles (NPE)

  • Property svn:eol-style set to native
File size: 17.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.mappaint;
3
4import java.io.File;
5import java.io.IOException;
6import java.util.ArrayList;
7import java.util.Arrays;
8import java.util.Collection;
9import java.util.HashSet;
10import java.util.LinkedList;
11import java.util.List;
12import java.util.Set;
13
14import javax.swing.ImageIcon;
15import javax.swing.SwingUtilities;
16
17import org.openstreetmap.josm.Main;
18import org.openstreetmap.josm.data.coor.LatLon;
19import org.openstreetmap.josm.data.osm.DataSet;
20import org.openstreetmap.josm.data.osm.Node;
21import org.openstreetmap.josm.data.osm.Tag;
22import org.openstreetmap.josm.data.preferences.sources.MapPaintPrefHelper;
23import org.openstreetmap.josm.data.preferences.sources.SourceEntry;
24import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSStyleSource;
25import org.openstreetmap.josm.gui.mappaint.styleelement.MapImage;
26import org.openstreetmap.josm.gui.mappaint.styleelement.NodeElement;
27import org.openstreetmap.josm.gui.mappaint.styleelement.StyleElement;
28import org.openstreetmap.josm.io.CachedFile;
29import org.openstreetmap.josm.spi.preferences.Config;
30import org.openstreetmap.josm.tools.ImageProvider;
31import org.openstreetmap.josm.tools.ListenerList;
32import org.openstreetmap.josm.tools.Logging;
33import org.openstreetmap.josm.tools.Utils;
34
35/**
36 * This class manages the list of available map paint styles and gives access to
37 * the ElemStyles singleton.
38 *
39 * On change, {@link MapPaintSylesUpdateListener#mapPaintStylesUpdated()} is fired
40 * for all listeners.
41 */
42public final class MapPaintStyles {
43
44 private static final Collection<String> DEPRECATED_IMAGE_NAMES = Arrays.asList(
45 "presets/misc/deprecated.svg",
46 "misc/deprecated.png");
47
48 private static final ListenerList<MapPaintSylesUpdateListener> listeners = ListenerList.createUnchecked();
49
50 static {
51 listeners.addListener(new MapPaintSylesUpdateListener() {
52 @Override
53 public void mapPaintStylesUpdated() {
54 SwingUtilities.invokeLater(styles::clearCached);
55 }
56
57 @Override
58 public void mapPaintStyleEntryUpdated(int index) {
59 mapPaintStylesUpdated();
60 }
61 });
62 }
63
64 private static ElemStyles styles = new ElemStyles();
65
66 /**
67 * Returns the {@link ElemStyles} singleton instance.
68 *
69 * The returned object is read only, any manipulation happens via one of
70 * the other wrapper methods in this class. ({@link #readFromPreferences},
71 * {@link #moveStyles}, ...)
72 * @return the {@code ElemStyles} singleton instance
73 */
74 public static ElemStyles getStyles() {
75 return styles;
76 }
77
78 private MapPaintStyles() {
79 // Hide default constructor for utils classes
80 }
81
82 /**
83 * Value holder for a reference to a tag name. A style instruction
84 * <pre>
85 * text: a_tag_name;
86 * </pre>
87 * results in a tag reference for the tag <tt>a_tag_name</tt> in the
88 * style cascade.
89 */
90 public static class TagKeyReference {
91 /**
92 * The tag name
93 */
94 public final String key;
95
96 /**
97 * Create a new {@link TagKeyReference}
98 * @param key The tag name
99 */
100 public TagKeyReference(String key) {
101 this.key = key;
102 }
103
104 @Override
105 public String toString() {
106 return "TagKeyReference{" + "key='" + key + "'}";
107 }
108 }
109
110 /**
111 * IconReference is used to remember the associated style source for each icon URL.
112 * This is necessary because image URLs can be paths relative
113 * to the source file and we have cascading of properties from different source files.
114 */
115 public static class IconReference {
116
117 /**
118 * The name of the icon
119 */
120 public final String iconName;
121 /**
122 * The style source this reference occurred in
123 */
124 public final StyleSource source;
125
126 /**
127 * Create a new {@link IconReference}
128 * @param iconName The icon name
129 * @param source The current style source
130 */
131 public IconReference(String iconName, StyleSource source) {
132 this.iconName = iconName;
133 this.source = source;
134 }
135
136 @Override
137 public String toString() {
138 return "IconReference{" + "iconName='" + iconName + "' source='" + source.getDisplayString() + "'}";
139 }
140
141 /**
142 * Determines whether this icon represents a deprecated icon
143 * @return whether this icon represents a deprecated icon
144 * @since 10927
145 */
146 public boolean isDeprecatedIcon() {
147 return DEPRECATED_IMAGE_NAMES.contains(iconName);
148 }
149 }
150
151 /**
152 * Image provider for icon. Note that this is a provider only. A @link{ImageProvider#get()} call may still fail!
153 *
154 * @param ref reference to the requested icon
155 * @param test if <code>true</code> than the icon is request is tested
156 * @return image provider for icon (can be <code>null</code> when <code>test</code> is <code>true</code>).
157 * @see #getIcon(IconReference, int,int)
158 * @since 8097
159 */
160 public static ImageProvider getIconProvider(IconReference ref, boolean test) {
161 final String namespace = ref.source.getPrefName();
162 ImageProvider i = new ImageProvider(ref.iconName)
163 .setDirs(getIconSourceDirs(ref.source))
164 .setId("mappaint."+namespace)
165 .setArchive(ref.source.zipIcons)
166 .setInArchiveDir(ref.source.getZipEntryDirName())
167 .setOptional(true);
168 if (test && i.get() == null) {
169 String msg = "Mappaint style \""+namespace+"\" ("+ref.source.getDisplayString()+") icon \"" + ref.iconName + "\" not found.";
170 ref.source.logWarning(msg);
171 Logging.warn(msg);
172 return null;
173 }
174 return i;
175 }
176
177 /**
178 * Return scaled icon.
179 *
180 * @param ref reference to the requested icon
181 * @param width icon width or -1 for autoscale
182 * @param height icon height or -1 for autoscale
183 * @return image icon or <code>null</code>.
184 * @see #getIconProvider(IconReference, boolean)
185 */
186 public static ImageIcon getIcon(IconReference ref, int width, int height) {
187 final String namespace = ref.source.getPrefName();
188 ImageIcon i = getIconProvider(ref, false).setSize(width, height).get();
189 if (i == null) {
190 Logging.warn("Mappaint style \""+namespace+"\" ("+ref.source.getDisplayString()+") icon \"" + ref.iconName + "\" not found.");
191 return null;
192 }
193 return i;
194 }
195
196 /**
197 * No icon with the given name was found, show a dummy icon instead
198 * @param source style source
199 * @return the icon misc/no_icon.png, in descending priority:
200 * - relative to source file
201 * - from user icon paths
202 * - josm's default icon
203 * can be null if the defaults are turned off by user
204 */
205 public static ImageIcon getNoIconIcon(StyleSource source) {
206 return new ImageProvider("presets/misc/no_icon")
207 .setDirs(getIconSourceDirs(source))
208 .setId("mappaint."+source.getPrefName())
209 .setArchive(source.zipIcons)
210 .setInArchiveDir(source.getZipEntryDirName())
211 .setOptional(true).get();
212 }
213
214 /**
215 * Returns the node icon that would be displayed for the given tag.
216 * @param tag The tag to look an icon for
217 * @return {@code null} if no icon found
218 */
219 public static ImageIcon getNodeIcon(Tag tag) {
220 return getNodeIcon(tag, true);
221 }
222
223 /**
224 * Returns the node icon that would be displayed for the given tag.
225 * @param tag The tag to look an icon for
226 * @param includeDeprecatedIcon if {@code true}, the special deprecated icon will be returned if applicable
227 * @return {@code null} if no icon found, or if the icon is deprecated and not wanted
228 */
229 public static ImageIcon getNodeIcon(Tag tag, boolean includeDeprecatedIcon) {
230 if (tag != null) {
231 DataSet ds = new DataSet();
232 Node virtualNode = new Node(LatLon.ZERO);
233 virtualNode.put(tag.getKey(), tag.getValue());
234 StyleElementList styleList;
235 MapCSSStyleSource.STYLE_SOURCE_LOCK.readLock().lock();
236 try {
237 // Add primitive to dataset to avoid DataIntegrityProblemException when evaluating selectors
238 ds.addPrimitive(virtualNode);
239 styleList = getStyles().generateStyles(virtualNode, 0.5, false).a;
240 ds.removePrimitive(virtualNode);
241 } finally {
242 MapCSSStyleSource.STYLE_SOURCE_LOCK.readLock().unlock();
243 }
244 if (styleList != null) {
245 for (StyleElement style : styleList) {
246 if (style instanceof NodeElement) {
247 MapImage mapImage = ((NodeElement) style).mapImage;
248 if (mapImage != null) {
249 if (includeDeprecatedIcon || mapImage.name == null || !DEPRECATED_IMAGE_NAMES.contains(mapImage.name)) {
250 return new ImageIcon(mapImage.getImage(false));
251 } else {
252 return null; // Deprecated icon found but not wanted
253 }
254 }
255 }
256 }
257 }
258 }
259 return null;
260 }
261
262 /**
263 * Gets the directories that should be searched for icons
264 * @param source The style source the icon is from
265 * @return A list of directory names
266 */
267 public static List<String> getIconSourceDirs(StyleSource source) {
268 List<String> dirs = new LinkedList<>();
269
270 File sourceDir = source.getLocalSourceDir();
271 if (sourceDir != null) {
272 dirs.add(sourceDir.getPath());
273 }
274
275 Collection<String> prefIconDirs = Config.getPref().getList("mappaint.icon.sources");
276 for (String fileset : prefIconDirs) {
277 String[] a;
278 if (fileset.indexOf('=') >= 0) {
279 a = fileset.split("=", 2);
280 } else {
281 a = new String[] {"", fileset};
282 }
283
284 /* non-prefixed path is generic path, always take it */
285 if (a[0].isEmpty() || source.getPrefName().equals(a[0])) {
286 dirs.add(a[1]);
287 }
288 }
289
290 if (Config.getPref().getBoolean("mappaint.icon.enable-defaults", true)) {
291 /* don't prefix icon path, as it should be generic */
292 dirs.add("resource://images/");
293 }
294
295 return dirs;
296 }
297
298 /**
299 * Reloads all styles from the preferences.
300 */
301 public static void readFromPreferences() {
302 styles.clear();
303
304 Collection<? extends SourceEntry> sourceEntries = MapPaintPrefHelper.INSTANCE.get();
305
306 for (SourceEntry entry : sourceEntries) {
307 styles.add(fromSourceEntry(entry));
308 }
309 for (StyleSource source : styles.getStyleSources()) {
310 loadStyleForFirstTime(source);
311 }
312 fireMapPaintSylesUpdated();
313 }
314
315 private static void loadStyleForFirstTime(StyleSource source) {
316 final long startTime = System.currentTimeMillis();
317 source.loadStyleSource();
318 if (Config.getPref().getBoolean("mappaint.auto_reload_local_styles", true) && source.isLocal()) {
319 try {
320 Main.fileWatcher.registerSource(source);
321 } catch (IOException | IllegalStateException | IllegalArgumentException e) {
322 Logging.error(e);
323 }
324 }
325 if (Logging.isDebugEnabled() || !source.isValid()) {
326 final long elapsedTime = System.currentTimeMillis() - startTime;
327 String message = "Initializing map style " + source.url + " completed in " + Utils.getDurationString(elapsedTime);
328 if (!source.isValid()) {
329 Logging.warn(message + " (" + source.getErrors().size() + " errors, " + source.getWarnings().size() + " warnings)");
330 } else {
331 Logging.debug(message);
332 }
333 }
334 }
335
336 private static StyleSource fromSourceEntry(SourceEntry entry) {
337 if (entry.url == null && entry instanceof MapCSSStyleSource) {
338 return (MapCSSStyleSource) entry;
339 }
340 Set<String> mimes = new HashSet<>(Arrays.asList(MapCSSStyleSource.MAPCSS_STYLE_MIME_TYPES.split(", ")));
341 try (CachedFile cf = new CachedFile(entry.url).setHttpAccept(Utils.join(", ", mimes))) {
342 String zipEntryPath = cf.findZipEntryPath("mapcss", "style");
343 if (zipEntryPath != null) {
344 entry.isZip = true;
345 entry.zipEntryPath = zipEntryPath;
346 }
347 return new MapCSSStyleSource(entry);
348 }
349 }
350
351 /**
352 * Move position of entries in the current list of StyleSources
353 * @param sel The indices of styles to be moved.
354 * @param delta The number of lines it should move. positive int moves
355 * down and negative moves up.
356 */
357 public static void moveStyles(int[] sel, int delta) {
358 if (!canMoveStyles(sel, delta))
359 return;
360 int[] selSorted = Utils.copyArray(sel);
361 Arrays.sort(selSorted);
362 List<StyleSource> data = new ArrayList<>(styles.getStyleSources());
363 for (int row: selSorted) {
364 StyleSource t1 = data.get(row);
365 StyleSource t2 = data.get(row + delta);
366 data.set(row, t2);
367 data.set(row + delta, t1);
368 }
369 styles.setStyleSources(data);
370 MapPaintPrefHelper.INSTANCE.put(data);
371 fireMapPaintSylesUpdated();
372 }
373
374 /**
375 * Check if the styles can be moved
376 * @param sel The indexes of the selected styles
377 * @param i The number of places to move the styles
378 * @return <code>true</code> if that movement is possible
379 */
380 public static boolean canMoveStyles(int[] sel, int i) {
381 if (sel.length == 0)
382 return false;
383 int[] selSorted = Utils.copyArray(sel);
384 Arrays.sort(selSorted);
385
386 if (i < 0) // Up
387 return selSorted[0] >= -i;
388 else if (i > 0) // Down
389 return selSorted[selSorted.length-1] <= styles.getStyleSources().size() - 1 - i;
390 else
391 return true;
392 }
393
394 /**
395 * Toggles the active state of several styles
396 * @param sel The style indexes
397 */
398 public static void toggleStyleActive(int... sel) {
399 List<StyleSource> data = styles.getStyleSources();
400 for (int p : sel) {
401 StyleSource s = data.get(p);
402 s.active = !s.active;
403 }
404 MapPaintPrefHelper.INSTANCE.put(data);
405 if (sel.length == 1) {
406 fireMapPaintStyleEntryUpdated(sel[0]);
407 } else {
408 fireMapPaintSylesUpdated();
409 }
410 }
411
412 /**
413 * Add a new map paint style.
414 * @param entry map paint style
415 * @return loaded style source, or {@code null}
416 */
417 public static StyleSource addStyle(SourceEntry entry) {
418 StyleSource source = fromSourceEntry(entry);
419 styles.add(source);
420 loadStyleForFirstTime(source);
421 refreshStyles();
422 return source;
423 }
424
425 /**
426 * Remove a map paint style.
427 * @param entry map paint style
428 * @since 11493
429 */
430 public static void removeStyle(SourceEntry entry) {
431 StyleSource source = fromSourceEntry(entry);
432 if (styles.remove(source)) {
433 refreshStyles();
434 }
435 }
436
437 private static void refreshStyles() {
438 MapPaintPrefHelper.INSTANCE.put(styles.getStyleSources());
439 fireMapPaintSylesUpdated();
440 }
441
442 /***********************************
443 * MapPaintSylesUpdateListener &amp; related code
444 * (get informed when the list of MapPaint StyleSources changes)
445 */
446 public interface MapPaintSylesUpdateListener {
447 /**
448 * Called on any style source changes that are not handled by {@link #mapPaintStyleEntryUpdated(int)}
449 */
450 void mapPaintStylesUpdated();
451
452 /**
453 * Called whenever a single style source entry was changed.
454 * @param index The index of the entry.
455 */
456 void mapPaintStyleEntryUpdated(int index);
457 }
458
459 /**
460 * Add a listener that listens to global style changes.
461 * @param listener The listener
462 */
463 public static void addMapPaintSylesUpdateListener(MapPaintSylesUpdateListener listener) {
464 listeners.addListener(listener);
465 }
466
467 /**
468 * Removes a listener that listens to global style changes.
469 * @param listener The listener
470 */
471 public static void removeMapPaintSylesUpdateListener(MapPaintSylesUpdateListener listener) {
472 listeners.removeListener(listener);
473 }
474
475 /**
476 * Notifies all listeners that there was any update to the map paint styles
477 */
478 public static void fireMapPaintSylesUpdated() {
479 listeners.fireEvent(MapPaintSylesUpdateListener::mapPaintStylesUpdated);
480 }
481
482 /**
483 * Notifies all listeners that there was an update to a specific map paint style
484 * @param index The style index
485 */
486 public static void fireMapPaintStyleEntryUpdated(int index) {
487 listeners.fireEvent(l -> l.mapPaintStyleEntryUpdated(index));
488 }
489}
Note: See TracBrowser for help on using the repository browser.