Changeset 34760 in osm for applications/viewer


Ignore:
Timestamp:
2018-12-01T21:15:59+01:00 (6 years ago)
Author:
donvip
Message:

see #josm16937 - make sure images are loaded using JOSM bullet-proof engine

Location:
applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/FeatureAdapter.java

    r31438 r34760  
    33
    44import java.awt.Desktop;
     5import java.awt.image.BufferedImage;
    56import java.io.IOException;
    67import java.net.URI;
    78import java.net.URISyntaxException;
     9import java.net.URL;
    810import java.text.MessageFormat;
     11import java.util.Objects;
    912import java.util.logging.Logger;
     13
     14import javax.imageio.ImageIO;
    1015
    1116public final class FeatureAdapter {
    1217
    1318    private static BrowserAdapter browserAdapter = new DefaultBrowserAdapter();
     19    private static ImageAdapter imageAdapter = new DefaultImageAdapter();
    1420    private static TranslationAdapter translationAdapter = new DefaultTranslationAdapter();
    1521    private static LoggingAdapter loggingAdapter = new DefaultLoggingAdapter();
     
    3238    }
    3339
     40    public interface ImageAdapter {
     41        BufferedImage read(URL input, boolean readMetadata, boolean enforceTransparency) throws IOException;
     42    }
     43
    3444    public static void registerBrowserAdapter(BrowserAdapter browserAdapter) {
    35         FeatureAdapter.browserAdapter = browserAdapter;
     45        FeatureAdapter.browserAdapter = Objects.requireNonNull(browserAdapter);
     46    }
     47
     48    public static void registerImageAdapter(ImageAdapter imageAdapter) {
     49        FeatureAdapter.imageAdapter = Objects.requireNonNull(imageAdapter);
    3650    }
    3751
    3852    public static void registerTranslationAdapter(TranslationAdapter translationAdapter) {
    39         FeatureAdapter.translationAdapter = translationAdapter;
     53        FeatureAdapter.translationAdapter = Objects.requireNonNull(translationAdapter);
    4054    }
    4155
    4256    public static void registerLoggingAdapter(LoggingAdapter loggingAdapter) {
    43         FeatureAdapter.loggingAdapter = loggingAdapter;
     57        FeatureAdapter.loggingAdapter = Objects.requireNonNull(loggingAdapter);
    4458    }
    4559
    4660    public static void openLink(String url) {
    4761        browserAdapter.openLink(url);
     62    }
     63
     64    public static BufferedImage readImage(URL url) throws IOException {
     65        return imageAdapter.read(url, false, false);
    4866    }
    4967
     
    7391    }
    7492
     93    public static class DefaultImageAdapter implements ImageAdapter {
     94        @Override
     95        public BufferedImage read(URL input, boolean readMetadata, boolean enforceTransparency) throws IOException {
     96            return ImageIO.read(input);
     97        }
     98    }
     99
    75100    public static class DefaultTranslationAdapter implements TranslationAdapter {
    76101        @Override
  • applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/JMapViewer.java

    r34759 r34760  
    88import java.awt.Point;
    99import java.awt.event.MouseEvent;
     10import java.io.IOException;
    1011import java.net.URL;
    1112import java.util.ArrayList;
     
    164165        add(zoomSlider);
    165166        int size = 18;
    166         URL url = JMapViewer.class.getResource("images/plus.png");
    167         if (url != null) {
    168             ImageIcon icon = new ImageIcon(url);
     167        ImageIcon icon = getImageIcon("images/plus.png");
     168        if (icon != null) {
    169169            zoomInButton = new JButton(icon);
    170170        } else {
     
    177177        zoomInButton.setFocusable(false);
    178178        add(zoomInButton);
    179         url = JMapViewer.class.getResource("images/minus.png");
    180         if (url != null) {
    181             ImageIcon icon = new ImageIcon(url);
     179        icon = getImageIcon("images/minus.png");
     180        if (icon != null) {
    182181            zoomOutButton = new JButton(icon);
    183182        } else {
     
    190189        zoomOutButton.setFocusable(false);
    191190        add(zoomOutButton);
     191    }
     192
     193    private static ImageIcon getImageIcon(String name) {
     194        URL url = JMapViewer.class.getResource(name);
     195        if (url != null) {
     196            try {
     197                return new ImageIcon(FeatureAdapter.readImage(url));
     198            } catch (IOException e) {
     199                e.printStackTrace();
     200            }
     201        }
     202        return null;
    192203    }
    193204
  • applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/Tile.java

    r34616 r34760  
    8282    private static BufferedImage loadImage(String path) {
    8383        try {
    84             return ImageIO.read(JMapViewer.class.getResourceAsStream(path));
     84            return FeatureAdapter.readImage(JMapViewer.class.getResource(path));
    8585        } catch (IOException | IllegalArgumentException ex) {
    8686            ex.printStackTrace();
  • applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/tilesources/BingAerialTileSource.java

    r34094 r34760  
    44import java.awt.Image;
    55import java.io.IOException;
    6 import java.io.InputStream;
    76import java.net.MalformedURLException;
    87import java.net.URL;
     
    1817import java.util.regex.Pattern;
    1918
    20 import javax.imageio.ImageIO;
    2119import javax.xml.parsers.DocumentBuilder;
    2220import javax.xml.parsers.DocumentBuilderFactory;
     
    2927
    3028import org.openstreetmap.gui.jmapviewer.Coordinate;
     29import org.openstreetmap.gui.jmapviewer.FeatureAdapter;
    3130import org.openstreetmap.gui.jmapviewer.JMapViewer;
    3231import org.openstreetmap.gui.jmapviewer.interfaces.ICoordinate;
     
    191190    public Image getAttributionImage() {
    192191        try {
    193             final InputStream imageResource = JMapViewer.class.getResourceAsStream("images/bing_maps.png");
     192            final URL imageResource = JMapViewer.class.getResource("images/bing_maps.png");
    194193            if (imageResource != null) {
    195                 return ImageIO.read(imageResource);
     194                return FeatureAdapter.readImage(imageResource);
    196195            } else {
    197196                // Some Linux distributions (like Debian) will remove Bing logo from sources, so get it at runtime
     
    204203                if (brandLogoUri != null && !brandLogoUri.isEmpty()) {
    205204                    System.out.println("Reading Bing logo from "+brandLogoUri);
    206                     return ImageIO.read(new URL(brandLogoUri));
     205                    return FeatureAdapter.readImage(new URL(brandLogoUri));
    207206                }
    208207            }
Note: See TracChangeset for help on using the changeset viewer.