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.List;
|
---|
14 |
|
---|
15 | import javax.swing.ImageIcon;
|
---|
16 |
|
---|
17 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
18 | import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.IconReference;
|
---|
19 | import org.openstreetmap.josm.gui.preferences.SourceEntry;
|
---|
20 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
21 | import org.openstreetmap.josm.tools.Utils;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * A mappaint style (abstract class).
|
---|
25 | *
|
---|
26 | * Handles everything from parsing the style definition to application
|
---|
27 | * of the style to an osm primitive.
|
---|
28 | */
|
---|
29 | public abstract class StyleSource extends SourceEntry {
|
---|
30 |
|
---|
31 | private List<Throwable> errors = new ArrayList<>();
|
---|
32 | public File zipIcons;
|
---|
33 |
|
---|
34 | private ImageIcon imageIcon;
|
---|
35 | private long lastMTime = 0L;
|
---|
36 |
|
---|
37 | /******
|
---|
38 | * The following fields is additional information found in the header
|
---|
39 | * of the source file.
|
---|
40 | */
|
---|
41 |
|
---|
42 | public String icon;
|
---|
43 |
|
---|
44 | public StyleSource(String url, String name, String title) {
|
---|
45 | super(url, name, title, true);
|
---|
46 | }
|
---|
47 |
|
---|
48 | public StyleSource(SourceEntry entry) {
|
---|
49 | super(entry);
|
---|
50 | }
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Apply style to osm primitive.
|
---|
54 | *
|
---|
55 | * Adds properties to a MultiCascade. All active {@link StyleSource}s add
|
---|
56 | * their properties on after the other. At a later stage, concrete painting
|
---|
57 | * primitives (lines, icons, text, ...) are derived from the MultiCascade.
|
---|
58 | * @param mc the current MultiCascade, empty for the first StyleSource
|
---|
59 | * @param osm the primitive
|
---|
60 | * @param scale the map scale
|
---|
61 | * @param multipolyOuterWay support for a very old multipolygon tagging style
|
---|
62 | * where you add the tags both to the outer and the inner way.
|
---|
63 | * However, independent inner way style is also possible.
|
---|
64 | * @param pretendWayIsClosed For styles that require the way to be closed,
|
---|
65 | * we pretend it is. This is useful for generating area styles from the (segmented)
|
---|
66 | * outer ways of a multipolygon.
|
---|
67 | */
|
---|
68 | public abstract void apply(MultiCascade mc, OsmPrimitive osm, double scale, OsmPrimitive multipolyOuterWay, boolean pretendWayIsClosed);
|
---|
69 |
|
---|
70 | public abstract void loadStyleSource();
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Returns a new {@code InputStream} to the style source. When finished, {@link #closeSourceInputStream(InputStream)} must be called.
|
---|
74 | * @return A new {@code InputStream} to the style source that must be closed by the caller
|
---|
75 | * @throws IOException if any I/O error occurs.
|
---|
76 | * @see #closeSourceInputStream(InputStream)
|
---|
77 | */
|
---|
78 | public abstract InputStream getSourceInputStream() throws IOException;
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * Closes the source input stream previously returned by {@link #getSourceInputStream()} and other linked resources, if applicable.
|
---|
82 | * @param is The source input stream that must be closed
|
---|
83 | * @since 6289
|
---|
84 | * @see #getSourceInputStream()
|
---|
85 | */
|
---|
86 | public void closeSourceInputStream(InputStream is) {
|
---|
87 | Utils.close(is);
|
---|
88 | }
|
---|
89 |
|
---|
90 | public void logError(Throwable e) {
|
---|
91 | errors.add(e);
|
---|
92 | }
|
---|
93 |
|
---|
94 | public Collection<Throwable> getErrors() {
|
---|
95 | return Collections.unmodifiableCollection(errors);
|
---|
96 | }
|
---|
97 |
|
---|
98 | protected void init() {
|
---|
99 | errors.clear();
|
---|
100 | imageIcon = null;
|
---|
101 | icon = null;
|
---|
102 | }
|
---|
103 |
|
---|
104 | private static ImageIcon defaultIcon;
|
---|
105 |
|
---|
106 | private static ImageIcon getDefaultIcon() {
|
---|
107 | if (defaultIcon == null) {
|
---|
108 | defaultIcon = ImageProvider.get("dialogs/mappaint", "pencil");
|
---|
109 | }
|
---|
110 | return defaultIcon;
|
---|
111 | }
|
---|
112 |
|
---|
113 | protected ImageIcon getSourceIcon() {
|
---|
114 | if (imageIcon == null) {
|
---|
115 | if (icon != null) {
|
---|
116 | imageIcon = MapPaintStyles.getIcon(new IconReference(icon, this), -1, -1);
|
---|
117 | }
|
---|
118 | if (imageIcon == null) {
|
---|
119 | imageIcon = getDefaultIcon();
|
---|
120 | }
|
---|
121 | }
|
---|
122 | return imageIcon;
|
---|
123 | }
|
---|
124 |
|
---|
125 | public final ImageIcon getIcon() {
|
---|
126 | if (getErrors().isEmpty())
|
---|
127 | return getSourceIcon();
|
---|
128 | else
|
---|
129 | return ImageProvider.overlay(getSourceIcon(),
|
---|
130 | ImageProvider.get("dialogs/mappaint/error_small"),
|
---|
131 | ImageProvider.OverlayPosition.SOUTHEAST);
|
---|
132 | }
|
---|
133 |
|
---|
134 | public String getToolTipText() {
|
---|
135 | if (errors.isEmpty())
|
---|
136 | return null;
|
---|
137 | else
|
---|
138 | return trn("There was an error when loading this style. Select ''Info'' from the right click menu for details.",
|
---|
139 | "There were {0} errors when loading this style. Select ''Info'' from the right click menu for details.",
|
---|
140 | errors.size(), errors.size());
|
---|
141 | }
|
---|
142 |
|
---|
143 | public Color getBackgroundColorOverride() {
|
---|
144 | return null;
|
---|
145 | }
|
---|
146 |
|
---|
147 | public long getLastMTime() {
|
---|
148 | return lastMTime;
|
---|
149 | }
|
---|
150 |
|
---|
151 | public void setLastMTime(long lastMTime) {
|
---|
152 | this.lastMTime = lastMTime;
|
---|
153 | }
|
---|
154 |
|
---|
155 |
|
---|
156 | }
|
---|