[3843] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.gui.mappaint;
|
---|
| 3 |
|
---|
[3863] | 4 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
| 5 |
|
---|
[3896] | 6 | import java.awt.Color;
|
---|
[3843] | 7 | import java.io.File;
|
---|
[3863] | 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;
|
---|
[7450] | 13 | import java.util.HashMap;
|
---|
[3863] | 14 | import java.util.List;
|
---|
[7450] | 15 | import java.util.Map;
|
---|
[3843] | 16 |
|
---|
[3863] | 17 | import javax.swing.ImageIcon;
|
---|
| 18 |
|
---|
[3843] | 19 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
[3894] | 20 | import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.IconReference;
|
---|
[3843] | 21 | import org.openstreetmap.josm.gui.preferences.SourceEntry;
|
---|
[7248] | 22 | import org.openstreetmap.josm.io.CachedFile;
|
---|
[8097] | 23 | import org.openstreetmap.josm.tools.ImageOverlay;
|
---|
[3863] | 24 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
[6289] | 25 | import org.openstreetmap.josm.tools.Utils;
|
---|
[3843] | 26 |
|
---|
[6896] | 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 | */
|
---|
[6889] | 33 | public abstract class StyleSource extends SourceEntry {
|
---|
[3863] | 34 |
|
---|
[7005] | 35 | private List<Throwable> errors = new ArrayList<>();
|
---|
[3843] | 36 | public File zipIcons;
|
---|
| 37 |
|
---|
[8097] | 38 | /** image provider returning the icon for this style */
|
---|
| 39 | private ImageProvider imageIconProvider;
|
---|
[3894] | 40 |
|
---|
[8097] | 41 | /** image provider returning the default icon */
|
---|
| 42 | private static ImageProvider defaultIconProvider;
|
---|
| 43 |
|
---|
[3894] | 44 | /******
|
---|
| 45 | * The following fields is additional information found in the header
|
---|
| 46 | * of the source file.
|
---|
| 47 | */
|
---|
| 48 | public String icon;
|
---|
| 49 |
|
---|
[7450] | 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 |
|
---|
[3894] | 59 | public StyleSource(String url, String name, String title) {
|
---|
| 60 | super(url, name, title, true);
|
---|
[3843] | 61 | }
|
---|
| 62 |
|
---|
| 63 | public StyleSource(SourceEntry entry) {
|
---|
[6148] | 64 | super(entry);
|
---|
[3843] | 65 | }
|
---|
| 66 |
|
---|
[6896] | 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 | */
|
---|
[7563] | 80 | public abstract void apply(MultiCascade mc, OsmPrimitive osm, double scale, boolean pretendWayIsClosed);
|
---|
[3843] | 81 |
|
---|
[7081] | 82 | /**
|
---|
| 83 | * Loads the style source.
|
---|
| 84 | */
|
---|
[6889] | 85 | public abstract void loadStyleSource();
|
---|
[3863] | 86 |
|
---|
[6289] | 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 | */
|
---|
[6889] | 93 | public abstract InputStream getSourceInputStream() throws IOException;
|
---|
[7185] | 94 |
|
---|
[7081] | 95 | /**
|
---|
[7248] | 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
|
---|
[7081] | 98 | * @throws IOException if any I/O error occurs.
|
---|
| 99 | * @since 7081
|
---|
| 100 | */
|
---|
[7248] | 101 | public abstract CachedFile getCachedFile() throws IOException;
|
---|
[3863] | 102 |
|
---|
[6289] | 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 |
|
---|
[3863] | 113 | public void logError(Throwable e) {
|
---|
| 114 | errors.add(e);
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | public Collection<Throwable> getErrors() {
|
---|
| 118 | return Collections.unmodifiableCollection(errors);
|
---|
| 119 | }
|
---|
| 120 |
|
---|
[8097] | 121 | /**
|
---|
| 122 | * Initialize the class.
|
---|
| 123 | */
|
---|
[3894] | 124 | protected void init() {
|
---|
[3863] | 125 | errors.clear();
|
---|
[8097] | 126 | imageIconProvider = null;
|
---|
[3894] | 127 | icon = null;
|
---|
[3863] | 128 | }
|
---|
| 129 |
|
---|
[8097] | 130 | /**
|
---|
| 131 | * Image provider for default icon.
|
---|
| 132 | *
|
---|
| 133 | * @return image provider for default styles icon
|
---|
| 134 | * @since 8097
|
---|
| 135 | * @see #getIconProvider()
|
---|
| 136 | */
|
---|
[8126] | 137 | private static synchronized ImageProvider getDefaultIconProvider() {
|
---|
[8097] | 138 | if (defaultIconProvider == null) {
|
---|
| 139 | defaultIconProvider = new ImageProvider("dialogs/mappaint", "pencil");
|
---|
[3894] | 140 | }
|
---|
[8097] | 141 | return defaultIconProvider;
|
---|
[3894] | 142 | }
|
---|
| 143 |
|
---|
[8097] | 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) {
|
---|
[3894] | 153 | if (icon != null) {
|
---|
[8097] | 154 | imageIconProvider = MapPaintStyles.getIconProvider(new IconReference(icon, this), true);
|
---|
[3894] | 155 | }
|
---|
[8097] | 156 | if (imageIconProvider == null) {
|
---|
| 157 | imageIconProvider = getDefaultIconProvider();
|
---|
[3894] | 158 | }
|
---|
[3876] | 159 | }
|
---|
[8097] | 160 | return imageIconProvider;
|
---|
[3863] | 161 | }
|
---|
| 162 |
|
---|
[8097] | 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 | */
|
---|
[6889] | 182 | public final ImageIcon getIcon() {
|
---|
[8097] | 183 | return getIconProvider().setMaxSize(ImageProvider.ImageSizes.MENU).get();
|
---|
[3863] | 184 | }
|
---|
| 185 |
|
---|
[8097] | 186 | /**
|
---|
| 187 | * Return text to display as ToolTip.
|
---|
| 188 | *
|
---|
| 189 | * @return tooltip text containing error status
|
---|
| 190 | */
|
---|
[3863] | 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 | }
|
---|
[3896] | 199 |
|
---|
| 200 | public Color getBackgroundColorOverride() {
|
---|
| 201 | return null;
|
---|
| 202 | }
|
---|
[3843] | 203 | }
|
---|