Ticket #18941: 18941.patch

File 18941.patch, 2.6 KB (added by simon04, 4 years ago)
  • src/org/openstreetmap/josm/gui/mappaint/styleelement/MapImage.java

    commit 82e763f51c86dacbbea890962ece8804cd6f489b
    Author: Simon Legner <Simon.Legner@gmail.com>
    Date:   2020-03-16 20:24:03 +0100
    
        fix #18941 - MapImage.getImageResource: wait for image to load
    
    diff --git a/src/org/openstreetmap/josm/gui/mappaint/styleelement/MapImage.java b/src/org/openstreetmap/josm/gui/mappaint/styleelement/MapImage.java
    index aef68a6fa..238254cb6 100644
    a b  
    99import java.awt.Rectangle;
    1010import java.awt.image.BufferedImage;
    1111import java.util.Objects;
     12import java.util.concurrent.CompletableFuture;
     13import java.util.concurrent.ExecutionException;
    1214
    1315import javax.swing.ImageIcon;
    1416
     
    2123import org.openstreetmap.josm.gui.util.GuiHelper;
    2224import org.openstreetmap.josm.tools.ImageProvider;
    2325import org.openstreetmap.josm.tools.ImageResource;
     26import org.openstreetmap.josm.tools.Logging;
    2427import org.openstreetmap.josm.tools.Utils;
    2528
    2629/**
    public Image getImage(boolean disabled) {  
    114117
    115118    /**
    116119     * Get the image resource associated with this MapImage object.
     120     * This method blocks until the image has been loaded.
    117121     * @return the image resource
    118122     */
    119123    public ImageResource getImageResource() {
     124        if (imageResource == null) {
     125            try {
     126                // load and wait for the image
     127                loadImage().get();
     128            } catch (ExecutionException | InterruptedException e) {
     129                Logging.warn(e);
     130                Thread.currentThread().interrupt();
     131            }
     132        }
    120133        return imageResource;
    121134    }
    122135
    private Image getImage() {  
    141154        if (img != null)
    142155            return img;
    143156        temporary = false;
    144         new ImageProvider(name)
     157        loadImage();
     158        synchronized (this) {
     159            if (img == null) {
     160                img = ImageProvider.get("clock").getImage();
     161                temporary = true;
     162            }
     163        }
     164        return img;
     165    }
     166
     167    private CompletableFuture<Void> loadImage() {
     168        return new ImageProvider(name)
    145169                .setDirs(MapPaintStyles.getIconSourceDirs(source))
    146170                .setId("mappaint."+source.getPrefName())
    147171                .setArchive(source.zipIcons)
    private Image getImage() {  
    167191                    }
    168192                }
    169193        );
    170         synchronized (this) {
    171             if (img == null) {
    172                 img = ImageProvider.get("clock").getImage();
    173                 temporary = true;
    174             }
    175         }
    176         return img;
    177194    }
    178195
    179196    /**