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.List; 014 015import javax.swing.ImageIcon; 016 017import org.openstreetmap.josm.data.osm.OsmPrimitive; 018import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.IconReference; 019import org.openstreetmap.josm.gui.preferences.SourceEntry; 020import org.openstreetmap.josm.tools.ImageProvider; 021import org.openstreetmap.josm.tools.Utils; 022 023abstract public class StyleSource extends SourceEntry { 024 025 private List<Throwable> errors = new ArrayList<Throwable>(); 026 public File zipIcons; 027 028 private ImageIcon imageIcon; 029 private long lastMTime = 0L; 030 031 /****** 032 * The following fields is additional information found in the header 033 * of the source file. 034 */ 035 036 public String icon; 037 038 public StyleSource(String url, String name, String title) { 039 super(url, name, title, true); 040 } 041 042 public StyleSource(SourceEntry entry) { 043 super(entry); 044 } 045 046 abstract public void apply(MultiCascade mc, OsmPrimitive osm, double scale, OsmPrimitive multipolyOuterWay, boolean pretendWayIsClosed); 047 048 abstract public void loadStyleSource(); 049 050 /** 051 * Returns a new {@code InputStream} to the style source. When finished, {@link #closeSourceInputStream(InputStream)} must be called. 052 * @return A new {@code InputStream} to the style source that must be closed by the caller 053 * @throws IOException if any I/O error occurs. 054 * @see #closeSourceInputStream(InputStream) 055 */ 056 abstract public InputStream getSourceInputStream() throws IOException; 057 058 /** 059 * Closes the source input stream previously returned by {@link #getSourceInputStream()} and other linked resources, if applicable. 060 * @param is The source input stream that must be closed 061 * @since 6289 062 * @see #getSourceInputStream() 063 */ 064 public void closeSourceInputStream(InputStream is) { 065 Utils.close(is); 066 } 067 068 public void logError(Throwable e) { 069 errors.add(e); 070 } 071 072 public Collection<Throwable> getErrors() { 073 return Collections.unmodifiableCollection(errors); 074 } 075 076 protected void init() { 077 errors.clear(); 078 imageIcon = null; 079 icon = null; 080 } 081 082 private static ImageIcon defaultIcon; 083 084 private static ImageIcon getDefaultIcon() { 085 if (defaultIcon == null) { 086 defaultIcon = ImageProvider.get("dialogs/mappaint", "pencil"); 087 } 088 return defaultIcon; 089 } 090 091 protected ImageIcon getSourceIcon() { 092 if (imageIcon == null) { 093 if (icon != null) { 094 imageIcon = MapPaintStyles.getIcon(new IconReference(icon, this), -1, -1); 095 } 096 if (imageIcon == null) { 097 imageIcon = getDefaultIcon(); 098 } 099 } 100 return imageIcon; 101 } 102 103 final public ImageIcon getIcon() { 104 if (getErrors().isEmpty()) 105 return getSourceIcon(); 106 else 107 return ImageProvider.overlay(getSourceIcon(), 108 ImageProvider.get("dialogs/mappaint/error_small"), 109 ImageProvider.OverlayPosition.SOUTHEAST); 110 } 111 112 public String getToolTipText() { 113 if (errors.isEmpty()) 114 return null; 115 else 116 return trn("There was an error when loading this style. Select ''Info'' from the right click menu for details.", 117 "There were {0} errors when loading this style. Select ''Info'' from the right click menu for details.", 118 errors.size(), errors.size()); 119 } 120 121 public Color getBackgroundColorOverride() { 122 return null; 123 } 124 125 public long getLastMTime() { 126 return lastMTime; 127 } 128 129 public void setLastMTime(long lastMTime) { 130 this.lastMTime = lastMTime; 131 } 132 133 134}