1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.mappaint;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
5 |
|
---|
6 | import java.awt.Color;
|
---|
7 | import java.io.File;
|
---|
8 | import java.io.IOException;
|
---|
9 | import java.io.InputStream;
|
---|
10 | import java.util.ArrayList;
|
---|
11 | import java.util.Collection;
|
---|
12 | import java.util.Collections;
|
---|
13 | import java.util.HashMap;
|
---|
14 | import java.util.List;
|
---|
15 | import java.util.Map;
|
---|
16 |
|
---|
17 | import javax.swing.ImageIcon;
|
---|
18 |
|
---|
19 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
20 | import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.IconReference;
|
---|
21 | import org.openstreetmap.josm.gui.preferences.SourceEntry;
|
---|
22 | import org.openstreetmap.josm.io.CachedFile;
|
---|
23 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
24 | import org.openstreetmap.josm.tools.Utils;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * A mappaint style (abstract class).
|
---|
28 | *
|
---|
29 | * Handles everything from parsing the style definition to application
|
---|
30 | * of the style to an osm primitive.
|
---|
31 | */
|
---|
32 | public abstract class StyleSource extends SourceEntry {
|
---|
33 |
|
---|
34 | private List<Throwable> errors = new ArrayList<>();
|
---|
35 | public File zipIcons;
|
---|
36 |
|
---|
37 | private ImageIcon imageIcon;
|
---|
38 |
|
---|
39 | /******
|
---|
40 | * The following fields is additional information found in the header
|
---|
41 | * of the source file.
|
---|
42 | */
|
---|
43 |
|
---|
44 | public String icon;
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * List of settings for user customization.
|
---|
48 | */
|
---|
49 | public final List<StyleSetting> settings = new ArrayList<>();
|
---|
50 | /**
|
---|
51 | * Values of the settings for efficient lookup.
|
---|
52 | */
|
---|
53 | public Map<String, Object> settingValues = new HashMap<>();
|
---|
54 |
|
---|
55 | public StyleSource(String url, String name, String title) {
|
---|
56 | super(url, name, title, true);
|
---|
57 | }
|
---|
58 |
|
---|
59 | public StyleSource(SourceEntry entry) {
|
---|
60 | super(entry);
|
---|
61 | }
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * Apply style to osm primitive.
|
---|
65 | *
|
---|
66 | * Adds properties to a MultiCascade. All active {@link StyleSource}s add
|
---|
67 | * their properties on after the other. At a later stage, concrete painting
|
---|
68 | * primitives (lines, icons, text, ...) are derived from the MultiCascade.
|
---|
69 | * @param mc the current MultiCascade, empty for the first StyleSource
|
---|
70 | * @param osm the primitive
|
---|
71 | * @param scale the map scale
|
---|
72 | * @param pretendWayIsClosed For styles that require the way to be closed,
|
---|
73 | * we pretend it is. This is useful for generating area styles from the (segmented)
|
---|
74 | * outer ways of a multipolygon.
|
---|
75 | */
|
---|
76 | public abstract void apply(MultiCascade mc, OsmPrimitive osm, double scale, boolean pretendWayIsClosed);
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Loads the style source.
|
---|
80 | */
|
---|
81 | public abstract void loadStyleSource();
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * Returns a new {@code InputStream} to the style source. When finished, {@link #closeSourceInputStream(InputStream)} must be called.
|
---|
85 | * @return A new {@code InputStream} to the style source that must be closed by the caller
|
---|
86 | * @throws IOException if any I/O error occurs.
|
---|
87 | * @see #closeSourceInputStream(InputStream)
|
---|
88 | */
|
---|
89 | public abstract InputStream getSourceInputStream() throws IOException;
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * Returns a new {@code CachedFile} to the local file containing style source (can be a text file or an archive).
|
---|
93 | * @return A new {@code CachedFile} to the local file containing style source
|
---|
94 | * @throws IOException if any I/O error occurs.
|
---|
95 | * @since 7081
|
---|
96 | */
|
---|
97 | public abstract CachedFile getCachedFile() throws IOException;
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * Closes the source input stream previously returned by {@link #getSourceInputStream()} and other linked resources, if applicable.
|
---|
101 | * @param is The source input stream that must be closed
|
---|
102 | * @since 6289
|
---|
103 | * @see #getSourceInputStream()
|
---|
104 | */
|
---|
105 | public void closeSourceInputStream(InputStream is) {
|
---|
106 | Utils.close(is);
|
---|
107 | }
|
---|
108 |
|
---|
109 | public void logError(Throwable e) {
|
---|
110 | errors.add(e);
|
---|
111 | }
|
---|
112 |
|
---|
113 | public Collection<Throwable> getErrors() {
|
---|
114 | return Collections.unmodifiableCollection(errors);
|
---|
115 | }
|
---|
116 |
|
---|
117 | protected void init() {
|
---|
118 | errors.clear();
|
---|
119 | imageIcon = null;
|
---|
120 | icon = null;
|
---|
121 | }
|
---|
122 |
|
---|
123 | private static ImageIcon defaultIcon;
|
---|
124 |
|
---|
125 | private static ImageIcon getDefaultIcon() {
|
---|
126 | if (defaultIcon == null) {
|
---|
127 | defaultIcon = ImageProvider.get("dialogs/mappaint", "pencil");
|
---|
128 | }
|
---|
129 | return defaultIcon;
|
---|
130 | }
|
---|
131 |
|
---|
132 | protected ImageIcon getSourceIcon() {
|
---|
133 | if (imageIcon == null) {
|
---|
134 | if (icon != null) {
|
---|
135 | imageIcon = MapPaintStyles.getIcon(new IconReference(icon, this), -1, -1);
|
---|
136 | }
|
---|
137 | if (imageIcon == null) {
|
---|
138 | imageIcon = getDefaultIcon();
|
---|
139 | }
|
---|
140 | }
|
---|
141 | return imageIcon;
|
---|
142 | }
|
---|
143 |
|
---|
144 | public final ImageIcon getIcon() {
|
---|
145 | if (getErrors().isEmpty())
|
---|
146 | return getSourceIcon();
|
---|
147 | else
|
---|
148 | return ImageProvider.overlay(getSourceIcon(),
|
---|
149 | ImageProvider.get("dialogs/mappaint/error_small"),
|
---|
150 | ImageProvider.OverlayPosition.SOUTHEAST);
|
---|
151 | }
|
---|
152 |
|
---|
153 | public String getToolTipText() {
|
---|
154 | if (errors.isEmpty())
|
---|
155 | return null;
|
---|
156 | else
|
---|
157 | return trn("There was an error when loading this style. Select ''Info'' from the right click menu for details.",
|
---|
158 | "There were {0} errors when loading this style. Select ''Info'' from the right click menu for details.",
|
---|
159 | errors.size(), errors.size());
|
---|
160 | }
|
---|
161 |
|
---|
162 | public Color getBackgroundColorOverride() {
|
---|
163 | return null;
|
---|
164 | }
|
---|
165 | }
|
---|