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