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 | import java.util.Set;
|
---|
17 | import java.util.concurrent.CopyOnWriteArrayList;
|
---|
18 | import java.util.concurrent.CopyOnWriteArraySet;
|
---|
19 |
|
---|
20 | import javax.swing.ImageIcon;
|
---|
21 |
|
---|
22 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
23 | import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.IconReference;
|
---|
24 | import org.openstreetmap.josm.gui.preferences.SourceEntry;
|
---|
25 | import org.openstreetmap.josm.io.CachedFile;
|
---|
26 | import org.openstreetmap.josm.tools.ImageOverlay;
|
---|
27 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
28 | import org.openstreetmap.josm.tools.Utils;
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * A mappaint style (abstract class).
|
---|
32 | *
|
---|
33 | * Handles everything from parsing the style definition to application
|
---|
34 | * of the style to an osm primitive.
|
---|
35 | */
|
---|
36 | public abstract class StyleSource extends SourceEntry {
|
---|
37 |
|
---|
38 | private final List<Throwable> errors = new CopyOnWriteArrayList<>();
|
---|
39 | private final Set<String> warnings = new CopyOnWriteArraySet<>();
|
---|
40 | public File zipIcons;
|
---|
41 |
|
---|
42 | /** image provider returning the icon for this style */
|
---|
43 | private ImageProvider imageIconProvider;
|
---|
44 |
|
---|
45 | /** image provider returning the default icon */
|
---|
46 | private static ImageProvider defaultIconProvider;
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * The following fields is additional information found in the header of the source file.
|
---|
50 | */
|
---|
51 | public String icon;
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * List of settings for user customization.
|
---|
55 | */
|
---|
56 | public final List<StyleSetting> settings = new ArrayList<>();
|
---|
57 | /**
|
---|
58 | * Values of the settings for efficient lookup.
|
---|
59 | */
|
---|
60 | public Map<String, Object> settingValues = new HashMap<>();
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Constructs a new, active {@link StyleSource}.
|
---|
64 | * @param url URL that {@link org.openstreetmap.josm.io.CachedFile} understands
|
---|
65 | * @param name The name for this StyleSource
|
---|
66 | * @param title The title that can be used as menu entry
|
---|
67 | */
|
---|
68 | public StyleSource(String url, String name, String title) {
|
---|
69 | super(url, name, title, true);
|
---|
70 | }
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Constructs a new {@link StyleSource}
|
---|
74 | * @param entry The entry to copy the data (url, name, ...) from.
|
---|
75 | */
|
---|
76 | public StyleSource(SourceEntry entry) {
|
---|
77 | super(entry);
|
---|
78 | }
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * Apply style to osm primitive.
|
---|
82 | *
|
---|
83 | * Adds properties to a MultiCascade. All active {@link StyleSource}s add
|
---|
84 | * their properties on after the other. At a later stage, concrete painting
|
---|
85 | * primitives (lines, icons, text, ...) are derived from the MultiCascade.
|
---|
86 | * @param mc the current MultiCascade, empty for the first StyleSource
|
---|
87 | * @param osm the primitive
|
---|
88 | * @param scale the map scale
|
---|
89 | * @param pretendWayIsClosed For styles that require the way to be closed,
|
---|
90 | * we pretend it is. This is useful for generating area styles from the (segmented)
|
---|
91 | * outer ways of a multipolygon.
|
---|
92 | */
|
---|
93 | public abstract void apply(MultiCascade mc, OsmPrimitive osm, double scale, boolean pretendWayIsClosed);
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * Loads the style source.
|
---|
97 | */
|
---|
98 | public abstract void loadStyleSource();
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * Returns a new {@code InputStream} to the style source. When finished, {@link #closeSourceInputStream(InputStream)} must be called.
|
---|
102 | * @return A new {@code InputStream} to the style source that must be closed by the caller
|
---|
103 | * @throws IOException if any I/O error occurs.
|
---|
104 | * @see #closeSourceInputStream(InputStream)
|
---|
105 | */
|
---|
106 | public abstract InputStream getSourceInputStream() throws IOException;
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * Returns a new {@code CachedFile} to the local file containing style source (can be a text file or an archive).
|
---|
110 | * @return A new {@code CachedFile} to the local file containing style source
|
---|
111 | * @throws IOException if any I/O error occurs.
|
---|
112 | * @since 7081
|
---|
113 | */
|
---|
114 | public abstract CachedFile getCachedFile() throws IOException;
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * Closes the source input stream previously returned by {@link #getSourceInputStream()} and other linked resources, if applicable.
|
---|
118 | * @param is The source input stream that must be closed
|
---|
119 | * @see #getSourceInputStream()
|
---|
120 | * @since 6289
|
---|
121 | */
|
---|
122 | public void closeSourceInputStream(InputStream is) {
|
---|
123 | Utils.close(is);
|
---|
124 | }
|
---|
125 |
|
---|
126 | /**
|
---|
127 | * Log an error that occured with this style.
|
---|
128 | * @param e error
|
---|
129 | */
|
---|
130 | public void logError(Throwable e) {
|
---|
131 | errors.add(e);
|
---|
132 | }
|
---|
133 |
|
---|
134 | /**
|
---|
135 | * Log a warning that occured with this style.
|
---|
136 | * @param w warnings
|
---|
137 | */
|
---|
138 | public void logWarning(String w) {
|
---|
139 | warnings.add(w);
|
---|
140 | }
|
---|
141 |
|
---|
142 | /**
|
---|
143 | * Replies the collection of errors that occured with this style.
|
---|
144 | * @return collection of errors
|
---|
145 | */
|
---|
146 | public Collection<Throwable> getErrors() {
|
---|
147 | return Collections.unmodifiableCollection(errors);
|
---|
148 | }
|
---|
149 |
|
---|
150 | /**
|
---|
151 | * Replies the collection of warnings that occured with this style.
|
---|
152 | * @return collection of warnings
|
---|
153 | */
|
---|
154 | public Collection<String> getWarnings() {
|
---|
155 | return Collections.unmodifiableCollection(warnings);
|
---|
156 | }
|
---|
157 |
|
---|
158 | /**
|
---|
159 | * Determines if this style is valid (no error, no warning).
|
---|
160 | * @return {@code true} if this style has 0 errors and 0 warnings
|
---|
161 | */
|
---|
162 | public boolean isValid() {
|
---|
163 | return errors.isEmpty() && warnings.isEmpty();
|
---|
164 | }
|
---|
165 |
|
---|
166 | /**
|
---|
167 | * Initialize the class.
|
---|
168 | */
|
---|
169 | protected void init() {
|
---|
170 | errors.clear();
|
---|
171 | imageIconProvider = null;
|
---|
172 | icon = null;
|
---|
173 | }
|
---|
174 |
|
---|
175 | /**
|
---|
176 | * Image provider for default icon.
|
---|
177 | *
|
---|
178 | * @return image provider for default styles icon
|
---|
179 | * @see #getIconProvider()
|
---|
180 | * @since 8097
|
---|
181 | */
|
---|
182 | private static synchronized ImageProvider getDefaultIconProvider() {
|
---|
183 | if (defaultIconProvider == null) {
|
---|
184 | defaultIconProvider = new ImageProvider("dialogs/mappaint", "pencil");
|
---|
185 | }
|
---|
186 | return defaultIconProvider;
|
---|
187 | }
|
---|
188 |
|
---|
189 | /**
|
---|
190 | * Image provider for source icon. Uses default icon, when not else available.
|
---|
191 | *
|
---|
192 | * @return image provider for styles icon
|
---|
193 | * @see #getIconProvider()
|
---|
194 | * @since 8097
|
---|
195 | */
|
---|
196 | protected ImageProvider getSourceIconProvider() {
|
---|
197 | if (imageIconProvider == null) {
|
---|
198 | if (icon != null) {
|
---|
199 | imageIconProvider = MapPaintStyles.getIconProvider(new IconReference(icon, this), true);
|
---|
200 | }
|
---|
201 | if (imageIconProvider == null) {
|
---|
202 | imageIconProvider = getDefaultIconProvider();
|
---|
203 | }
|
---|
204 | }
|
---|
205 | return imageIconProvider;
|
---|
206 | }
|
---|
207 |
|
---|
208 | /**
|
---|
209 | * Image provider for source icon.
|
---|
210 | *
|
---|
211 | * @return image provider for styles icon
|
---|
212 | * @since 8097
|
---|
213 | */
|
---|
214 | public final ImageProvider getIconProvider() {
|
---|
215 | ImageProvider i = getSourceIconProvider();
|
---|
216 | if (!getErrors().isEmpty()) {
|
---|
217 | i = new ImageProvider(i).addOverlay(new ImageOverlay(new ImageProvider("misc", "error"), 0.5, 0.5, 1, 1));
|
---|
218 | } else if (!getWarnings().isEmpty()) {
|
---|
219 | i = new ImageProvider(i).addOverlay(new ImageOverlay(new ImageProvider("warning-small"), 0.5, 0.5, 1, 1));
|
---|
220 | }
|
---|
221 | return i;
|
---|
222 | }
|
---|
223 |
|
---|
224 | /**
|
---|
225 | * Image for source icon.
|
---|
226 | *
|
---|
227 | * @return styles icon for display
|
---|
228 | */
|
---|
229 | public final ImageIcon getIcon() {
|
---|
230 | return getIconProvider().setMaxSize(ImageProvider.ImageSizes.MENU).get();
|
---|
231 | }
|
---|
232 |
|
---|
233 | /**
|
---|
234 | * Return text to display as ToolTip.
|
---|
235 | *
|
---|
236 | * @return tooltip text containing error status
|
---|
237 | */
|
---|
238 | public String getToolTipText() {
|
---|
239 | if (errors.isEmpty() && warnings.isEmpty())
|
---|
240 | return null;
|
---|
241 | int n = errors.size() + warnings.size();
|
---|
242 | return trn("There was an error when loading this style. Select ''Info'' from the right click menu for details.",
|
---|
243 | "There were {0} errors when loading this style. Select ''Info'' from the right click menu for details.",
|
---|
244 | n, n);
|
---|
245 | }
|
---|
246 |
|
---|
247 | public Color getBackgroundColorOverride() {
|
---|
248 | return null;
|
---|
249 | }
|
---|
250 | }
|
---|