001    // License: GPL. For details, see LICENSE file.
002    package org.openstreetmap.josm.gui.mappaint;
003    
004    import static org.openstreetmap.josm.tools.I18n.trn;
005    
006    import java.awt.Color;
007    import java.io.File;
008    import java.io.IOException;
009    import java.io.InputStream;
010    import java.util.ArrayList;
011    import java.util.Collection;
012    import java.util.Collections;
013    import java.util.List;
014    
015    import javax.swing.ImageIcon;
016    
017    import org.openstreetmap.josm.data.osm.OsmPrimitive;
018    import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.IconReference;
019    import org.openstreetmap.josm.gui.preferences.SourceEntry;
020    import org.openstreetmap.josm.tools.ImageProvider;
021    
022    abstract public class StyleSource extends SourceEntry {
023    
024        private List<Throwable> errors = new ArrayList<Throwable>();
025        public File zipIcons;
026    
027        private ImageIcon imageIcon;
028        private long lastMTime = 0l;
029    
030        /******
031         * The following fields is additional information found in the header
032         * of the source file.
033         */
034    
035        public String icon;
036    
037        public StyleSource(String url, String name, String title) {
038            super(url, name, title, true);
039        }
040    
041        public StyleSource(SourceEntry entry) {
042            super(entry.url, entry.name, entry.title, entry.active);
043        }
044    
045        abstract public void apply(MultiCascade mc, OsmPrimitive osm, double scale, OsmPrimitive multipolyOuterWay, boolean pretendWayIsClosed);
046    
047        abstract public void loadStyleSource();
048    
049        abstract public InputStream getSourceInputStream() throws IOException;
050    
051        public void logError(Throwable e) {
052            errors.add(e);
053        }
054    
055        public Collection<Throwable> getErrors() {
056            return Collections.unmodifiableCollection(errors);
057        }
058    
059        protected void init() {
060            errors.clear();
061            imageIcon = null;
062            icon = null;
063        }
064    
065        private static ImageIcon defaultIcon;
066    
067        private static ImageIcon getDefaultIcon() {
068            if (defaultIcon == null) {
069                defaultIcon = ImageProvider.get("dialogs/mappaint", "pencil");
070            }
071            return defaultIcon;
072        }
073    
074        protected ImageIcon getSourceIcon() {
075            if (imageIcon == null) {
076                if (icon != null) {
077                    imageIcon = MapPaintStyles.getIcon(new IconReference(icon, this), -1, -1);
078                }
079                if (imageIcon == null) {
080                    imageIcon = getDefaultIcon();
081                }
082            }
083            return imageIcon;
084        }
085    
086        final public ImageIcon getIcon() {
087            if (getErrors().isEmpty())
088                return getSourceIcon();
089            else
090                return ImageProvider.overlay(getSourceIcon(),
091                        ImageProvider.get("dialogs/mappaint/error_small"),
092                        ImageProvider.OverlayPosition.SOUTHEAST);
093        }
094    
095        public String getToolTipText() {
096            if (errors.isEmpty())
097                return null;
098            else
099                return trn("There was an error when loading this style. Select ''Info'' from the right click menu for details.",
100                        "There were {0} errors when loading this style. Select ''Info'' from the right click menu for details.",
101                        errors.size(), errors.size());
102        }
103    
104        public Color getBackgroundColorOverride() {
105            return null;
106        }
107    
108        public long getLastMTime() {
109            return lastMTime;
110        }
111    
112        public void setLastMTime(long lastMTime) {
113            this.lastMTime = lastMTime;
114        }
115    
116    
117    }