Ticket #6797: async2.diff

File async2.diff, 19.1 KB (added by bastiK, 14 years ago)
  • src/org/openstreetmap/josm/tools/ImageProvider.java

     
    3030import java.util.Collection;
    3131import java.util.HashMap;
    3232import java.util.Map;
     33import java.util.concurrent.Executors;
     34import java.util.concurrent.ExecutorService;
    3335import java.util.regex.Matcher;
    3436import java.util.regex.Pattern;
    3537import java.util.zip.ZipEntry;
     
    107109     */
    108110    private static Map<String, ImageResource> cache = new HashMap<String, ImageResource>();
    109111
     112    private final static ExecutorService imageFetcher = Executors.newSingleThreadExecutor();
     113
     114    public interface ImageCallback {
     115        void finished(ImageIcon result);
     116    }
     117
    110118    /**
    111119     * @param subdir    Subdirectory the image lies in.
    112120     * @param name      The name of the image. If it does not end with '.png' or '.svg',
     
    243251    }
    244252
    245253    /**
     254     * Load the image in a background thread.
     255     */
     256    public void getInBackground(final ImageCallback callback) {
     257        if (name.startsWith("http://") || name.startsWith("wiki://")) {
     258            Runnable fetch = new Runnable() {
     259                @Override
     260                public void run() {
     261                    ImageIcon result = get();
     262                    callback.finished(result);
     263                }
     264            };
     265            imageFetcher.submit(fetch);
     266        } else {
     267            ImageIcon result = get();
     268            callback.finished(result);
     269        }
     270    }
     271
     272    /**
    246273     * Return an image from the specified location. Throws a RuntimeException if
    247274     * the image cannot be located.
    248275     *
  • src/org/openstreetmap/josm/gui/mappaint/LinePatternElemStyle.java

     
    11// License: GPL. For details, see LICENSE file.
    22package org.openstreetmap.josm.gui.mappaint;
    33
    4 import java.awt.Image;
    5 
    6 import javax.swing.ImageIcon;
    7 
    84import org.openstreetmap.josm.data.osm.OsmPrimitive;
    95import org.openstreetmap.josm.data.osm.Way;
    106import org.openstreetmap.josm.data.osm.visitor.paint.MapPaintSettings;
     
    1612 */
    1713public class LinePatternElemStyle extends ElemStyle {
    1814
    19     public MapImage<Image> pattern;
     15    public MapImage pattern;
    2016
    21     public LinePatternElemStyle(Cascade c, MapImage<Image> pattern) {
     17    public LinePatternElemStyle(Cascade c, MapImage pattern) {
    2218        super(c, -1f);
    2319        this.pattern = pattern;
    2420    }
     
    2925        IconReference iconRef = c.get("pattern-image", null, IconReference.class);
    3026        if (iconRef == null)
    3127            return null;
    32         ImageIcon icon = MapPaintStyles.getIcon(iconRef, -1, -1);
    33         if (icon == null)
    34             return null;
    35         MapImage<Image> pattern = new MapImage<Image>(iconRef.iconName, iconRef.source);
    36         pattern.img = icon.getImage();
     28        MapImage pattern = new MapImage(iconRef.iconName, iconRef.source);
    3729        return new LinePatternElemStyle(c, pattern);
    3830    }
    3931
    4032    @Override
    4133    public void paintPrimitive(OsmPrimitive primitive, MapPaintSettings paintSettings, MapPainter painter, boolean selected, boolean member) {
    4234        Way w = (Way)primitive;
    43         painter.drawLinePattern(w, pattern.img);
     35        painter.drawLinePattern(w, pattern.getImage());
    4436    }
    4537
    4638    @Override
  • src/org/openstreetmap/josm/gui/mappaint/NodeElemStyle.java

     
    55
    66import java.awt.BasicStroke;
    77import java.awt.Color;
    8 import java.awt.Image;
    98import java.awt.Rectangle;
    109import java.awt.Stroke;
    1110
    12 import javax.swing.ImageIcon;
    13 
    1411import org.openstreetmap.josm.Main;
    1512import org.openstreetmap.josm.data.osm.Node;
    1613import org.openstreetmap.josm.data.osm.OsmPrimitive;
     
    2623 * applies for Nodes and turn restriction relations
    2724 */
    2825public class NodeElemStyle extends ElemStyle {
    29     public MapImage<Image> mapImage;
     26    public MapImage mapImage;
    3027    public Symbol symbol;
    3128
    32     private ImageIcon disabledIcon;
    33 
    3429    public enum SymbolShape { SQUARE, CIRCLE, TRIANGLE, PENTAGON, HEXAGON, HEPTAGON, OCTAGON, NONAGON, DECAGON }
    3530
    3631    public static class Symbol {
     
    9489    public static final StyleList DEFAULT_NODE_STYLELIST = new StyleList(NodeElemStyle.SIMPLE_NODE_ELEMSTYLE);
    9590    public static final StyleList DEFAULT_NODE_STYLELIST_TEXT = new StyleList(NodeElemStyle.SIMPLE_NODE_ELEMSTYLE, BoxTextElemStyle.SIMPLE_NODE_TEXT_ELEMSTYLE);
    9691
    97     protected NodeElemStyle(Cascade c, MapImage<Image> mapImage, Symbol symbol) {
     92    protected NodeElemStyle(Cascade c, MapImage mapImage, Symbol symbol) {
    9893        super(c, 1000f);
    9994        this.mapImage = mapImage;
    10095        this.symbol = symbol;
     
    107102    private static NodeElemStyle create(Environment env, boolean allowDefault) {
    108103        Cascade c = env.mc.getCascade(env.layer);
    109104
    110         MapImage<Image> mapImage = createIcon(env);
     105        MapImage mapImage = createIcon(env);
    111106        Symbol symbol = null;
    112107        if (mapImage == null) {
    113108            symbol = createSymbol(env);
     
    121116        return new NodeElemStyle(c, mapImage, symbol);
    122117    }
    123118
    124     private static MapImage<Image> createIcon(Environment env) {
     119    private static MapImage createIcon(Environment env) {
    125120        Cascade c = env.mc.getCascade(env.layer);
    126121        Cascade c_def = env.mc.getCascade("default");
    127122
    128         IconReference iconRef = c.get("icon-image", null, IconReference.class);
     123        final IconReference iconRef = c.get("icon-image", null, IconReference.class);
    129124        if (iconRef == null)
    130125            return null;
    131126
     
    144139        int width = widthF == null ? -1 : Math.round(widthF);
    145140        int height = heightF == null ? -1 : Math.round(heightF);
    146141
    147         MapImage<Image> mapImage = new MapImage<Image>(iconRef.iconName, iconRef.source);
     142        final MapImage mapImage = new MapImage(iconRef.iconName, iconRef.source);
    148143
    149         ImageIcon icon = MapPaintStyles.getIcon(iconRef, width, height);
    150         if (icon == null) {
    151             mapImage.img = MapPaintStyles.getNoIcon_Icon(iconRef.source).getImage();
    152         } else {
    153             mapImage.img = icon.getImage();
    154             mapImage.alpha = Math.min(255, Math.max(0, Integer.valueOf(Main.pref.getInteger("mappaint.icon-image-alpha", 255))));
    155             Integer pAlpha = Utils.color_float2int(c.get("icon-opacity", null, float.class));
    156             if (pAlpha != null) {
    157                 mapImage.alpha = pAlpha;
    158             }
    159             mapImage.width = width;
    160             mapImage.height = height;
     144        mapImage.width = width;
     145        mapImage.height = height;
     146
     147        mapImage.alpha = Math.min(255, Math.max(0, Integer.valueOf(Main.pref.getInteger("mappaint.icon-image-alpha", 255))));
     148        Integer pAlpha = Utils.color_float2int(c.get("icon-opacity", null, float.class));
     149        if (pAlpha != null) {
     150            mapImage.alpha = pAlpha;
    161151        }
    162152        return mapImage;
    163153    }
     
    242232        if (primitive instanceof Node) {
    243233            Node n = (Node) primitive;
    244234            if (mapImage != null && painter.isShowIcons()) {
    245                 painter.drawNodeIcon(n, (painter.isInactiveMode() || n.isDisabled()) ? mapImage.getDisabled() : mapImage.img,
     235                painter.drawNodeIcon(n, (painter.isInactiveMode() || n.isDisabled()) ? mapImage.getDisabled() : mapImage.getImage(),
    246236                        Utils.color_int2float(mapImage.alpha), selected, member);
    247237            } else if (symbol != null) {
    248238                Color fillColor = symbol.fillColor;
     
    310300
    311301    public Rectangle getBox() {
    312302        if (mapImage != null) {
    313             int w = mapImage.img.getWidth(null), h = mapImage.img.getHeight(null);
     303            int w = mapImage.getWidth(), h = mapImage.getHeight();
    314304            return new Rectangle(-w/2, -h/2, w, h);
    315305        } else if (symbol != null) {
    316306            return new Rectangle(-symbol.size/2, -symbol.size/2, symbol.size, symbol.size);
  • src/org/openstreetmap/josm/gui/mappaint/MapPaintStyles.java

     
    130130                .setOptional(true).get();
    131131    }
    132132
    133     private static List<String> getIconSourceDirs(StyleSource source) {
     133    public static List<String> getIconSourceDirs(StyleSource source) {
    134134        List<String> dirs = new LinkedList<String>();
    135135
    136136        String sourceDir = source.getLocalSourceDir();
  • src/org/openstreetmap/josm/gui/mappaint/MapImage.java

     
    22package org.openstreetmap.josm.gui.mappaint;
    33
    44import static org.openstreetmap.josm.tools.Utils.equal;
     5
    56import java.awt.Image;
     7import java.awt.image.BufferedImage;
    68
    79import javax.swing.GrayFilter;
    810import javax.swing.ImageIcon;
    911
    10 public class MapImage<I extends Image> {
     12import org.openstreetmap.josm.Main;
     13import org.openstreetmap.josm.tools.ImageProvider;
     14import org.openstreetmap.josm.tools.ImageProvider.ImageCallback;
     15import org.openstreetmap.josm.tools.Utils;
     16
     17public class MapImage {
    1118    /**
    1219     * ImageIcon can chage while the image is loading.
    1320     */
    14     public I img;
     21    private Image img;
    1522
    1623    public int alpha = 255;
    1724
     
    3643        return disabledImg = GrayFilter.createDisabledImage(img);
    3744    }
    3845
     46    public Image getImage() {
     47        if (img != null)
     48            return img;
     49        new ImageProvider(name)
     50                .setDirs(MapPaintStyles.getIconSourceDirs(source))
     51                .setId("mappaint."+source.getPrefName())
     52                .setArchive(source.zipIcons)
     53                .setWidth(width)
     54                .setHeight(height)
     55                .setOptional(true)
     56                .getInBackground(new ImageCallback() {
     57                    @Override
     58                    public void finished(ImageIcon result) {
     59                        synchronized (this) {
     60                            boolean repaint = img != null;
     61                            if (result == null) {
     62                                img = MapPaintStyles.getNoIcon_Icon(source).getImage();
     63                            } else {
     64                                img = result.getImage();
     65                            }
     66                            if (repaint) {
     67                                Main.map.mapView.preferenceChanged(null); // otherwise repaint is ignored, because layer hasn't changed
     68                                Main.map.mapView.repaint();
     69                            }
     70                        }
     71                    }
     72                }
     73        );
     74        synchronized (this) {
     75            if (img == null) {
     76                img = ImageProvider.get("clock").getImage();
     77            }
     78        }
     79        return img;
     80    }
     81
     82    public BufferedImage getBufferedImage() {
     83        if (img == null) {
     84            getImage();
     85        }
     86        if (!(img instanceof BufferedImage)) {
     87            img = ImageProvider.sanitize(img);
     88        }
     89        return (BufferedImage)img;
     90    }
     91
     92    public int getWidth() {
     93        return getImage().getWidth(null);
     94    }
     95
     96    public int getHeight() {
     97        return getImage().getHeight(null);
     98    }
     99
     100    public float getAlphaFloat() {
     101        return Utils.color_int2float(alpha);
     102    }
     103
    39104    // img changes when image is fully loaded and can't be used for equality check.
    40105    @Override
    41106    public boolean equals(Object obj) {
  • src/org/openstreetmap/josm/gui/mappaint/AreaElemStyle.java

     
    44import static org.openstreetmap.josm.tools.Utils.equal;
    55
    66import java.awt.Color;
    7 import java.awt.image.BufferedImage;
    87
    9 import javax.swing.ImageIcon;
    10 
    118import org.openstreetmap.josm.Main;
    129import org.openstreetmap.josm.data.osm.OsmPrimitive;
    1310import org.openstreetmap.josm.data.osm.Relation;
     
    1714import org.openstreetmap.josm.data.osm.visitor.paint.PaintColors;
    1815import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.IconReference;
    1916import org.openstreetmap.josm.tools.CheckParameterUtil;
    20 import org.openstreetmap.josm.tools.ImageProvider.SanitizeMode;
    2117import org.openstreetmap.josm.tools.Utils;
    2218
    2319public class AreaElemStyle extends ElemStyle
     
    2723     * an arbitrary color value sampled from the fillImage
    2824     */
    2925    public Color color;
    30     public MapImage<BufferedImage> fillImage;
     26    public MapImage fillImage;
    3127    public TextElement text;
    3228
    33     protected AreaElemStyle(Cascade c, Color color, MapImage<BufferedImage> fillImage, TextElement text) {
     29    protected AreaElemStyle(Cascade c, Color color, MapImage fillImage, TextElement text) {
    3430        super(c, -1000f);
    3531        CheckParameterUtil.ensureParameterNotNull(color);
    3632        this.color = color;
     
    3935    }
    4036
    4137    public static AreaElemStyle create(Cascade c) {
    42         MapImage<BufferedImage> fillImage = null;
     38        MapImage fillImage = null;
    4339        Color color = null;
    4440
    4541        IconReference iconRef = c.get("fill-image", null, IconReference.class);
    4642        if (iconRef != null) {
    47             ImageIcon icon = MapPaintStyles.getIcon(iconRef, -1, -1, SanitizeMode.MAKE_BUFFEREDIMAGE);
    48             if (icon != null) {
    49                 if (!(icon.getImage() instanceof BufferedImage))
    50                     throw new RuntimeException();
    51                 fillImage = new MapImage<BufferedImage>(iconRef.iconName, iconRef.source);
    52                 fillImage.img = (BufferedImage) icon.getImage();
     43            fillImage = new MapImage(iconRef.iconName, iconRef.source);
     44            fillImage.getBufferedImage();
    5345
    54                 color = new Color(fillImage.img.getRGB(
    55                         fillImage.img.getWidth() / 2, fillImage.img.getHeight() / 2)
    56                 );
     46            color = new Color(fillImage.getBufferedImage().getRGB(
     47                    fillImage.getWidth() / 2, fillImage.getHeight() / 2)
     48            );
    5749
    58                 fillImage.alpha = Math.min(255, Math.max(0, Integer.valueOf(Main.pref.getInteger("mappaint.fill-image-alpha", 255))));
    59                 Integer pAlpha = Utils.color_float2int(c.get("fill-opacity", null, float.class));
    60                 if (pAlpha != null) {
    61                     fillImage.alpha = pAlpha;
    62                 }
     50            fillImage.alpha = Math.min(255, Math.max(0, Integer.valueOf(Main.pref.getInteger("mappaint.fill-image-alpha", 255))));
     51            Integer pAlpha = Utils.color_float2int(c.get("fill-opacity", null, float.class));
     52            if (pAlpha != null) {
     53                fillImage.alpha = pAlpha;
    6354            }
    6455        } else {
    6556            color = c.get("fill-color", null, Color.class);
  • src/org/openstreetmap/josm/gui/MapView.java

     
    459459    /**
    460460     * Draw the component.
    461461     */
    462     @Override public void paint(Graphics g) {
     462    @Override public synchronized void paint(Graphics g) {
    463463        if (BugReportExceptionHandler.exceptionHandlingInProgress())
    464464            return;
    465465
     
    832832        }
    833833    }
    834834
    835     public void preferenceChanged(PreferenceChangeEvent e) {
     835    public synchronized void preferenceChanged(PreferenceChangeEvent e) {
    836836        paintPreferencesChanged = true;
    837837    }
    838838
  • src/org/openstreetmap/josm/data/osm/visitor/paint/MapPainter.java

     
    802802        return path;
    803803    }
    804804
    805     public void drawArea(Way w, Color color, MapImage<BufferedImage> fillImage, TextElement text) {
     805    public void drawArea(Way w, Color color, MapImage fillImage, TextElement text) {
    806806        drawArea(w, getPath(w), color, fillImage, text);
    807807    }
    808808
    809     protected void drawArea(OsmPrimitive osm, Path2D.Double path, Color color, MapImage<BufferedImage> fillImage, TextElement text) {
     809    protected void drawArea(OsmPrimitive osm, Path2D.Double path, Color color, MapImage fillImage, TextElement text) {
    810810
    811811        Shape area = path.createTransformedShape(nc.getAffineTransform());
    812812
     
    815815                g.setColor(color);
    816816                g.fill(area);
    817817            } else {
    818                 TexturePaint texture = new TexturePaint(fillImage.img,
     818                TexturePaint texture = new TexturePaint(fillImage.getBufferedImage(),
    819819                        //                        new Rectangle(polygon.xpoints[0], polygon.ypoints[0], fillImage.getWidth(), fillImage.getHeight()));
    820                         new Rectangle(0, 0, fillImage.img.getWidth(null), fillImage.img.getHeight(null)));
     820                        new Rectangle(0, 0, fillImage.getWidth(), fillImage.getHeight()));
    821821                g.setPaint(texture);
    822                 if (fillImage.alpha != 1f) {
    823                     g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, fillImage.alpha));
     822                if (fillImage.getAlphaFloat() != 1f) {
     823                    g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, fillImage.getAlphaFloat()));
    824824                }
    825825                g.fill(area);
    826826                g.setPaintMode();
     
    871871        }
    872872    }
    873873
    874     public void drawArea(Relation r, Color color, MapImage<BufferedImage> fillImage, TextElement text) {
     874    public void drawArea(Relation r, Color color, MapImage fillImage, TextElement text) {
    875875        Multipolygon multipolygon = MultipolygonCache.getInstance().get(nc, r);
    876876        if (!r.isDisabled() && !multipolygon.getOuterWays().isEmpty()) {
    877877            for (PolyData pd : multipolygon.getCombinedPolygons()) {
     
    913913        }
    914914    }
    915915
    916     public void drawRestriction(Relation r, MapImage<Image> icon) {
     916    public void drawRestriction(Relation r, MapImage icon) {
    917917        Way fromWay = null;
    918918        Way toWay = null;
    919919        OsmPrimitive via = null;
     
    10841084            iconAngle = 270-fromAngleDeg;
    10851085        }
    10861086
    1087         drawRestriction(inactive || r.isDisabled() ? icon.getDisabled() : icon.img,
     1087        drawRestriction(inactive || r.isDisabled() ? icon.getDisabled() : icon.getImage(),
    10881088                pVia, vx, vx2, vy, vy2, iconAngle, r.isSelected());
    10891089    }
    10901090