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

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

Sonar: various code style cleanup:

  • fix copyright
  • classes that should be final
  • order of javadoc At-clauses
  • unexpected spaces before parenthesis
  • Property svn:eol-style set to native
File size: 6.8 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;
16
17import javax.swing.ImageIcon;
18
19import org.openstreetmap.josm.data.osm.OsmPrimitive;
20import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.IconReference;
21import org.openstreetmap.josm.gui.preferences.SourceEntry;
22import org.openstreetmap.josm.io.CachedFile;
23import org.openstreetmap.josm.tools.ImageOverlay;
24import org.openstreetmap.josm.tools.ImageProvider;
25import 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 */
33public 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 /**
60 * Constructs a new, active {@link StyleSource}.
61 * @param url URL that {@link org.openstreetmap.josm.io.CachedFile} understands
62 * @param name The name for this StyleSource
63 * @param title The title that can be used as menu entry
64 */
65 public StyleSource(String url, String name, String title) {
66 super(url, name, title, true);
67 }
68
69 /**
70 * Constructs a new {@link StyleSource}
71 * @param entry The entry to copy the data (url, name, ...) from.
72 */
73 public StyleSource(SourceEntry entry) {
74 super(entry);
75 }
76
77 /**
78 * Apply style to osm primitive.
79 *
80 * Adds properties to a MultiCascade. All active {@link StyleSource}s add
81 * their properties on after the other. At a later stage, concrete painting
82 * primitives (lines, icons, text, ...) are derived from the MultiCascade.
83 * @param mc the current MultiCascade, empty for the first StyleSource
84 * @param osm the primitive
85 * @param scale the map scale
86 * @param pretendWayIsClosed For styles that require the way to be closed,
87 * we pretend it is. This is useful for generating area styles from the (segmented)
88 * outer ways of a multipolygon.
89 */
90 public abstract void apply(MultiCascade mc, OsmPrimitive osm, double scale, boolean pretendWayIsClosed);
91
92 /**
93 * Loads the style source.
94 */
95 public abstract void loadStyleSource();
96
97 /**
98 * Returns a new {@code InputStream} to the style source. When finished, {@link #closeSourceInputStream(InputStream)} must be called.
99 * @return A new {@code InputStream} to the style source that must be closed by the caller
100 * @throws IOException if any I/O error occurs.
101 * @see #closeSourceInputStream(InputStream)
102 */
103 public abstract InputStream getSourceInputStream() throws IOException;
104
105 /**
106 * Returns a new {@code CachedFile} to the local file containing style source (can be a text file or an archive).
107 * @return A new {@code CachedFile} to the local file containing style source
108 * @throws IOException if any I/O error occurs.
109 * @since 7081
110 */
111 public abstract CachedFile getCachedFile() throws IOException;
112
113 /**
114 * Closes the source input stream previously returned by {@link #getSourceInputStream()} and other linked resources, if applicable.
115 * @param is The source input stream that must be closed
116 * @see #getSourceInputStream()
117 * @since 6289
118 */
119 public void closeSourceInputStream(InputStream is) {
120 Utils.close(is);
121 }
122
123 public void logError(Throwable e) {
124 errors.add(e);
125 }
126
127 public Collection<Throwable> getErrors() {
128 return Collections.unmodifiableCollection(errors);
129 }
130
131 /**
132 * Initialize the class.
133 */
134 protected void init() {
135 errors.clear();
136 imageIconProvider = null;
137 icon = null;
138 }
139
140 /**
141 * Image provider for default icon.
142 *
143 * @return image provider for default styles icon
144 * @see #getIconProvider()
145 * @since 8097
146 */
147 private static synchronized ImageProvider getDefaultIconProvider() {
148 if (defaultIconProvider == null) {
149 defaultIconProvider = new ImageProvider("dialogs/mappaint", "pencil");
150 }
151 return defaultIconProvider;
152 }
153
154 /**
155 * Image provider for source icon. Uses default icon, when not else available.
156 *
157 * @return image provider for styles icon
158 * @see #getIconProvider()
159 * @since 8097
160 */
161 protected ImageProvider getSourceIconProvider() {
162 if (imageIconProvider == null) {
163 if (icon != null) {
164 imageIconProvider = MapPaintStyles.getIconProvider(new IconReference(icon, this), true);
165 }
166 if (imageIconProvider == null) {
167 imageIconProvider = getDefaultIconProvider();
168 }
169 }
170 return imageIconProvider;
171 }
172
173 /**
174 * Image provider for source icon.
175 *
176 * @return image provider for styles icon
177 * @since 8097
178 */
179 public final ImageProvider getIconProvider() {
180 ImageProvider i = getSourceIconProvider();
181 if (!getErrors().isEmpty()) {
182 i = new ImageProvider(i).addOverlay(new ImageOverlay(new ImageProvider("dialogs/mappaint/error_small")));
183 }
184 return i;
185 }
186
187 /**
188 * Image for source icon.
189 *
190 * @return styles icon for display
191 */
192 public final ImageIcon getIcon() {
193 return getIconProvider().setMaxSize(ImageProvider.ImageSizes.MENU).get();
194 }
195
196 /**
197 * Return text to display as ToolTip.
198 *
199 * @return tooltip text containing error status
200 */
201 public String getToolTipText() {
202 if (errors.isEmpty())
203 return null;
204 else
205 return trn("There was an error when loading this style. Select ''Info'' from the right click menu for details.",
206 "There were {0} errors when loading this style. Select ''Info'' from the right click menu for details.",
207 errors.size(), errors.size());
208 }
209
210 public Color getBackgroundColorOverride() {
211 return null;
212 }
213}
Note: See TracBrowser for help on using the repository browser.