001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.gui.mappaint; 003 004import static org.openstreetmap.josm.tools.I18n.trn; 005 006import java.awt.Color; 007import java.io.File; 008import java.io.IOException; 009import java.io.InputStream; 010import java.util.ArrayList; 011import java.util.Collection; 012import java.util.Collections; 013import java.util.HashMap; 014import java.util.List; 015import java.util.Map; 016import java.util.Set; 017import java.util.concurrent.CopyOnWriteArrayList; 018import java.util.concurrent.CopyOnWriteArraySet; 019 020import javax.swing.ImageIcon; 021 022import org.openstreetmap.josm.data.osm.OsmPrimitive; 023import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.IconReference; 024import org.openstreetmap.josm.gui.preferences.SourceEntry; 025import org.openstreetmap.josm.io.CachedFile; 026import org.openstreetmap.josm.tools.ImageOverlay; 027import org.openstreetmap.josm.tools.ImageProvider; 028import org.openstreetmap.josm.tools.Utils; 029 030/** 031 * A mappaint style (abstract class). 032 * 033 * Handles everything from parsing the style definition to application 034 * of the style to an osm primitive. 035 */ 036public abstract class StyleSource extends SourceEntry { 037 038 private final List<Throwable> errors = new CopyOnWriteArrayList<>(); 039 private final Set<String> warnings = new CopyOnWriteArraySet<>(); 040 public File zipIcons; 041 042 /** image provider returning the icon for this style */ 043 private ImageProvider imageIconProvider; 044 045 /** image provider returning the default icon */ 046 private static ImageProvider defaultIconProvider; 047 048 /** 049 * The following fields is additional information found in the header of the source file. 050 */ 051 public String icon; 052 053 /** 054 * List of settings for user customization. 055 */ 056 public final List<StyleSetting> settings = new ArrayList<>(); 057 /** 058 * Values of the settings for efficient lookup. 059 */ 060 public Map<String, Object> settingValues = new HashMap<>(); 061 062 /** 063 * Constructs a new, active {@link StyleSource}. 064 * @param url URL that {@link org.openstreetmap.josm.io.CachedFile} understands 065 * @param name The name for this StyleSource 066 * @param title The title that can be used as menu entry 067 */ 068 public StyleSource(String url, String name, String title) { 069 super(url, name, title, true); 070 } 071 072 /** 073 * Constructs a new {@link StyleSource} 074 * @param entry The entry to copy the data (url, name, ...) from. 075 */ 076 public StyleSource(SourceEntry entry) { 077 super(entry); 078 } 079 080 /** 081 * Apply style to osm primitive. 082 * 083 * Adds properties to a MultiCascade. All active {@link StyleSource}s add 084 * their properties on after the other. At a later stage, concrete painting 085 * primitives (lines, icons, text, ...) are derived from the MultiCascade. 086 * @param mc the current MultiCascade, empty for the first StyleSource 087 * @param osm the primitive 088 * @param scale the map scale 089 * @param pretendWayIsClosed For styles that require the way to be closed, 090 * we pretend it is. This is useful for generating area styles from the (segmented) 091 * outer ways of a multipolygon. 092 */ 093 public abstract void apply(MultiCascade mc, OsmPrimitive osm, double scale, boolean pretendWayIsClosed); 094 095 /** 096 * Loads the style source. 097 */ 098 public abstract void loadStyleSource(); 099 100 /** 101 * Returns a new {@code InputStream} to the style source. When finished, {@link #closeSourceInputStream(InputStream)} must be called. 102 * @return A new {@code InputStream} to the style source that must be closed by the caller 103 * @throws IOException if any I/O error occurs. 104 * @see #closeSourceInputStream(InputStream) 105 */ 106 public abstract InputStream getSourceInputStream() throws IOException; 107 108 /** 109 * Returns a new {@code CachedFile} to the local file containing style source (can be a text file or an archive). 110 * @return A new {@code CachedFile} to the local file containing style source 111 * @throws IOException if any I/O error occurs. 112 * @since 7081 113 */ 114 public abstract CachedFile getCachedFile() throws IOException; 115 116 /** 117 * Closes the source input stream previously returned by {@link #getSourceInputStream()} and other linked resources, if applicable. 118 * @param is The source input stream that must be closed 119 * @see #getSourceInputStream() 120 * @since 6289 121 */ 122 public void closeSourceInputStream(InputStream is) { 123 Utils.close(is); 124 } 125 126 /** 127 * Log an error that occured with this style. 128 * @param e error 129 */ 130 public void logError(Throwable e) { 131 errors.add(e); 132 } 133 134 /** 135 * Log a warning that occured with this style. 136 * @param w warnings 137 */ 138 public void logWarning(String w) { 139 warnings.add(w); 140 } 141 142 /** 143 * Replies the collection of errors that occured with this style. 144 * @return collection of errors 145 */ 146 public Collection<Throwable> getErrors() { 147 return Collections.unmodifiableCollection(errors); 148 } 149 150 /** 151 * Replies the collection of warnings that occured with this style. 152 * @return collection of warnings 153 */ 154 public Collection<String> getWarnings() { 155 return Collections.unmodifiableCollection(warnings); 156 } 157 158 /** 159 * Determines if this style is valid (no error, no warning). 160 * @return {@code true} if this style has 0 errors and 0 warnings 161 */ 162 public boolean isValid() { 163 return errors.isEmpty() && warnings.isEmpty(); 164 } 165 166 /** 167 * Initialize the class. 168 */ 169 protected void init() { 170 errors.clear(); 171 imageIconProvider = null; 172 icon = null; 173 } 174 175 /** 176 * Image provider for default icon. 177 * 178 * @return image provider for default styles icon 179 * @see #getIconProvider() 180 * @since 8097 181 */ 182 private static synchronized ImageProvider getDefaultIconProvider() { 183 if (defaultIconProvider == null) { 184 defaultIconProvider = new ImageProvider("dialogs/mappaint", "pencil"); 185 } 186 return defaultIconProvider; 187 } 188 189 /** 190 * Image provider for source icon. Uses default icon, when not else available. 191 * 192 * @return image provider for styles icon 193 * @see #getIconProvider() 194 * @since 8097 195 */ 196 protected ImageProvider getSourceIconProvider() { 197 if (imageIconProvider == null) { 198 if (icon != null) { 199 imageIconProvider = MapPaintStyles.getIconProvider(new IconReference(icon, this), true); 200 } 201 if (imageIconProvider == null) { 202 imageIconProvider = getDefaultIconProvider(); 203 } 204 } 205 return imageIconProvider; 206 } 207 208 /** 209 * Image provider for source icon. 210 * 211 * @return image provider for styles icon 212 * @since 8097 213 */ 214 public final ImageProvider getIconProvider() { 215 ImageProvider i = getSourceIconProvider(); 216 if (!getErrors().isEmpty()) { 217 i = new ImageProvider(i).addOverlay(new ImageOverlay(new ImageProvider("misc", "error"), 0.5, 0.5, 1, 1)); 218 } else if (!getWarnings().isEmpty()) { 219 i = new ImageProvider(i).addOverlay(new ImageOverlay(new ImageProvider("warning-small"), 0.5, 0.5, 1, 1)); 220 } 221 return i; 222 } 223 224 /** 225 * Image for source icon. 226 * 227 * @return styles icon for display 228 */ 229 public final ImageIcon getIcon() { 230 return getIconProvider().setMaxSize(ImageProvider.ImageSizes.MENU).get(); 231 } 232 233 /** 234 * Return text to display as ToolTip. 235 * 236 * @return tooltip text containing error status 237 */ 238 public String getToolTipText() { 239 if (errors.isEmpty() && warnings.isEmpty()) 240 return null; 241 int n = errors.size() + warnings.size(); 242 return trn("There was an error when loading this style. Select ''Info'' from the right click menu for details.", 243 "There were {0} errors when loading this style. Select ''Info'' from the right click menu for details.", 244 n, n); 245 } 246 247 public Color getBackgroundColorOverride() { 248 return null; 249 } 250}