Ignore:
Timestamp:
2016-06-19T18:28:26+02:00 (7 years ago)
Author:
stoecker
Message:

see #9995 - patch mainly by strump - improve HIDPI behaviour

Location:
trunk/src/org/openstreetmap/josm/tools
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/ImageProvider.java

    r10410 r10428  
    272272    /** ordered list of overlay images */
    273273    protected List<ImageOverlay> overlayInfo;
     274    /** <code>true</code> if icon must be grayed out */
     275    protected boolean isDisabled = false;
    274276
    275277    private static SVGUniverse svgUniverse;
     
    351353        this.additionalClassLoaders = image.additionalClassLoaders;
    352354        this.overlayInfo = image.overlayInfo;
     355        this.isDisabled = image.isDisabled;
    353356    }
    354357
     
    601604
    602605    /**
     606     * Set, if image must be filtered to grayscale so it will look like disabled icon.
     607     *
     608     * @param disabled true, if image must be grayed out for disabled state
     609     * @return the current object, for convenience
     610     * @since 10428
     611     */
     612    public ImageProvider setDisabled(boolean disabled) {
     613        this.isDisabled = disabled;
     614        return this;
     615    }
     616
     617    /**
    603618     * Execute the image request and scale result.
    604619     * @return the requested image or null if the request failed
     
    606621    public ImageIcon get() {
    607622        ImageResource ir = getResource();
    608         if (ir == null)
     623
     624        if (ir == null) {
    609625            return null;
     626        }
    610627        if (virtualMaxWidth != -1 || virtualMaxHeight != -1)
    611628            return ir.getImageIconBounded(new Dimension(virtualMaxWidth, virtualMaxHeight));
     
    638655            ir = new ImageResource(ir, overlayInfo);
    639656        }
     657        if (isDisabled) {
     658            ir.setDisabled(true);
     659        }
    640660        return ir;
    641661    }
     
    718738
    719739    /**
     740     * Load an image from directory with a given file name and size.
     741     *
     742     * @param subdir subdirectory the image lies in
     743     * @param name The icon name (base name with or without '.png' or '.svg' extension)
     744     * @param size Target icon size
     745     * @return The requested Image.
     746     * @throws RuntimeException if the image cannot be located
     747     * @since 10428
     748     */
     749    public static ImageIcon get(String subdir, String name, ImageSizes size) {
     750        return new ImageProvider(subdir, name).setSize(size).get();
     751    }
     752
     753    /**
    720754     * Load an empty image with a given size.
    721755     *
     
    741775    public static ImageIcon getIfAvailable(String subdir, String name) {
    742776        return new ImageProvider(subdir, name).setOptional(true).get();
     777    }
     778
     779    /**
     780     * Load an image with a given file name and size.
     781     *
     782     * @param name The icon name (base name with or without '.png' or '.svg' extension)
     783     * @param size Target icon size
     784     * @return the requested image or null if the request failed
     785     * @see #get(String, String)
     786     * @since 10428
     787     */
     788    public static ImageIcon get(String name, ImageSizes size) {
     789        return new ImageProvider(name).setSize(size).get();
    743790    }
    744791
     
    776823                return null;
    777824
     825            String prefix = "";
     826            if(isDisabled)
     827              prefix = "dis:"+prefix;
    778828            if (name.startsWith("data:")) {
    779829                String url = name;
    780                 ImageResource ir = cache.get(url);
     830                ImageResource ir = cache.get(prefix+url);
    781831                if (ir != null) return ir;
    782832                ir = getIfAvailableDataUrl(url);
    783833                if (ir != null) {
    784                     cache.put(url, ir);
     834                    cache.put(prefix+url, ir);
    785835                }
    786836                return ir;
     
    791841            if (name.startsWith(HTTP_PROTOCOL) || name.startsWith(HTTPS_PROTOCOL)) {
    792842                String url = name;
    793                 ImageResource ir = cache.get(url);
     843                ImageResource ir = cache.get(prefix+url);
    794844                if (ir != null) return ir;
    795845                ir = getIfAvailableHttp(url, type);
    796846                if (ir != null) {
    797                     cache.put(url, ir);
     847                    cache.put(prefix+url, ir);
    798848                }
    799849                return ir;
    800850            } else if (name.startsWith(WIKI_PROTOCOL)) {
    801                 ImageResource ir = cache.get(name);
     851                ImageResource ir = cache.get(prefix+name);
    802852                if (ir != null) return ir;
    803853                ir = getIfAvailableWiki(name, type);
    804854                if (ir != null) {
    805                     cache.put(name, ir);
     855                    cache.put(prefix+name, ir);
    806856                }
    807857                return ir;
     
    831881
    832882                    String fullName = subdir + name + ext;
    833                     String cacheName = fullName;
     883                    String cacheName = prefix + fullName;
    834884                    /* cache separately */
    835885                    if (dirs != null && !dirs.isEmpty()) {
  • trunk/src/org/openstreetmap/josm/tools/ImageResource.java

    r10409 r10428  
    1111import javax.swing.AbstractAction;
    1212import javax.swing.Action;
     13import javax.swing.Icon;
    1314import javax.swing.ImageIcon;
     15import javax.swing.JPanel;
     16import javax.swing.UIManager;
    1417
    1518import org.openstreetmap.josm.gui.util.GuiSizesHelper;
     
    4346     */
    4447    protected List<ImageOverlay> overlayInfo;
     48    /**
     49     * <code>true</code> if icon must be grayed out
     50     */
     51    protected boolean isDisabled = false;
     52    /**
     53     * The base raster image for the final output
     54     */
    4555    private Image baseImage;
    4656
     
    5161    public ImageResource(Image img) {
    5262        CheckParameterUtil.ensureParameterNotNull(img);
    53         baseImage = img;
    54         imgCache.put(DEFAULT_DIMENSION, scaleBaseImageIfNeeded(img));
     63        baseImage = scaleBaseImageIfNeeded(img);
    5564    }
    5665
     
    96105
    97106    /**
     107     * Set, if image must be filtered to grayscale so it will look like disabled icon.
     108     *
     109     * @param disabled true, if image must be grayed out for disabled state
     110     * @return the current object, for convenience
     111     * @since 10428
     112     */
     113    public ImageResource setDisabled(boolean disabled) {
     114        this.isDisabled = disabled;
     115        return this;
     116    }
     117
     118    /**
    98119     * Set both icons of an Action
    99120     * @param a The action for the icons
     
    145166            return new ImageIcon(img);
    146167        }
     168        BufferedImage bimg;
    147169        if (svg != null) {
    148170            Dimension realDim = GuiSizesHelper.getDimensionDpiAdjusted(dim);
    149             BufferedImage bimg = ImageProvider.createImageFromSvg(svg, realDim);
     171            bimg = ImageProvider.createImageFromSvg(svg, realDim);
    150172            if (bimg == null) {
    151173                return null;
    152174            }
    153             if (overlayInfo != null) {
    154                 for (ImageOverlay o : overlayInfo) {
    155                     o.process(bimg);
    156                 }
    157             }
    158             imgCache.put(dim, bimg);
    159             return new ImageIcon(bimg);
    160175        } else {
    161176            if (baseImage == null) throw new AssertionError();
     
    173188            }
    174189            Image i = icon.getImage().getScaledInstance(realWidth, realHeight, Image.SCALE_SMOOTH);
    175             BufferedImage bimg = new BufferedImage(realWidth, realHeight, BufferedImage.TYPE_INT_ARGB);
     190            bimg = new BufferedImage(realWidth, realHeight, BufferedImage.TYPE_INT_ARGB);
    176191            bimg.getGraphics().drawImage(i, 0, 0, null);
    177             if (overlayInfo != null) {
    178                 for (ImageOverlay o : overlayInfo) {
    179                     o.process(bimg);
    180                 }
    181             }
    182             imgCache.put(dim, bimg);
    183             return new ImageIcon(bimg);
    184         }
     192        }
     193        if (overlayInfo != null) {
     194            for (ImageOverlay o : overlayInfo) {
     195                o.process(bimg);
     196            }
     197        }
     198        if (isDisabled) {
     199            //Use default Swing functionality to make icon look disabled by applying grayscaling filter.
     200            Icon disabledIcon = UIManager.getLookAndFeel().getDisabledIcon(null, new ImageIcon(bimg));
     201
     202            //Convert Icon to ImageIcon with BufferedImage inside
     203            bimg = new BufferedImage(bimg.getWidth(), bimg.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
     204            disabledIcon.paintIcon(new JPanel(), bimg.getGraphics(), 0, 0);
     205        }
     206        imgCache.put(dim, bimg);
     207        return new ImageIcon(bimg);
    185208    }
    186209
Note: See TracChangeset for help on using the changeset viewer.