source: josm/trunk/src/org/openstreetmap/josm/gui/mappaint/StyleSource.java@ 12378

Last change on this file since 12378 was 12378, checked in by michael2402, 7 years ago

Document the gui.mappaint package

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