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

Last change on this file since 7530 was 7450, checked in by bastiK, 10 years ago

fixed #10421 - User-settings for mapcss mappaint styles

  • only boolean setting for now
  • no shortcut support so far
  • Property svn:eol-style set to native
File size: 5.5 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.ImageProvider;
24import 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 */
32public 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 multipolyOuterWay support for a very old multipolygon tagging style
73 * where you add the tags both to the outer and the inner way.
74 * However, independent inner way style is also possible.
75 * @param pretendWayIsClosed For styles that require the way to be closed,
76 * we pretend it is. This is useful for generating area styles from the (segmented)
77 * outer ways of a multipolygon.
78 */
79 public abstract void apply(MultiCascade mc, OsmPrimitive osm, double scale, OsmPrimitive multipolyOuterWay, boolean pretendWayIsClosed);
80
81 /**
82 * Loads the style source.
83 */
84 public abstract void loadStyleSource();
85
86 /**
87 * Returns a new {@code InputStream} to the style source. When finished, {@link #closeSourceInputStream(InputStream)} must be called.
88 * @return A new {@code InputStream} to the style source that must be closed by the caller
89 * @throws IOException if any I/O error occurs.
90 * @see #closeSourceInputStream(InputStream)
91 */
92 public abstract InputStream getSourceInputStream() throws IOException;
93
94 /**
95 * Returns a new {@code CachedFile} to the local file containing style source (can be a text file or an archive).
96 * @return A new {@code CachedFile} to the local file containing style source
97 * @throws IOException if any I/O error occurs.
98 * @since 7081
99 */
100 public abstract CachedFile getCachedFile() throws IOException;
101
102 /**
103 * Closes the source input stream previously returned by {@link #getSourceInputStream()} and other linked resources, if applicable.
104 * @param is The source input stream that must be closed
105 * @since 6289
106 * @see #getSourceInputStream()
107 */
108 public void closeSourceInputStream(InputStream is) {
109 Utils.close(is);
110 }
111
112 public void logError(Throwable e) {
113 errors.add(e);
114 }
115
116 public Collection<Throwable> getErrors() {
117 return Collections.unmodifiableCollection(errors);
118 }
119
120 protected void init() {
121 errors.clear();
122 imageIcon = null;
123 icon = null;
124 }
125
126 private static ImageIcon defaultIcon;
127
128 private static ImageIcon getDefaultIcon() {
129 if (defaultIcon == null) {
130 defaultIcon = ImageProvider.get("dialogs/mappaint", "pencil");
131 }
132 return defaultIcon;
133 }
134
135 protected ImageIcon getSourceIcon() {
136 if (imageIcon == null) {
137 if (icon != null) {
138 imageIcon = MapPaintStyles.getIcon(new IconReference(icon, this), -1, -1);
139 }
140 if (imageIcon == null) {
141 imageIcon = getDefaultIcon();
142 }
143 }
144 return imageIcon;
145 }
146
147 public final ImageIcon getIcon() {
148 if (getErrors().isEmpty())
149 return getSourceIcon();
150 else
151 return ImageProvider.overlay(getSourceIcon(),
152 ImageProvider.get("dialogs/mappaint/error_small"),
153 ImageProvider.OverlayPosition.SOUTHEAST);
154 }
155
156 public String getToolTipText() {
157 if (errors.isEmpty())
158 return null;
159 else
160 return trn("There was an error when loading this style. Select ''Info'' from the right click menu for details.",
161 "There were {0} errors when loading this style. Select ''Info'' from the right click menu for details.",
162 errors.size(), errors.size());
163 }
164
165 public Color getBackgroundColorOverride() {
166 return null;
167 }
168}
Note: See TracBrowser for help on using the repository browser.