jdk/src/java.desktop/share/classes/sun/awt/image/MultiResolutionCachedImage.java
author alexsch
Fri, 19 Aug 2016 16:48:53 +0400
changeset 40707 8b0d53520355
parent 32682 6f1200d8999d
permissions -rw-r--r--
8151303: [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it 8156182: [macosx] HiDPI/Retina icons do not work for disabled JButton/JMenuItem etc. Reviewed-by: flar, prr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24532
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
     1
/*
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
     4
 *
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
    10
 *
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
    15
 * accompanied this code).
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
    16
 *
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
    20
 *
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
    23
 * questions.
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
    24
 */
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
    25
package sun.awt.image;
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
    26
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
    27
import java.awt.Dimension;
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
    28
import java.awt.Image;
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
    29
import java.awt.geom.Dimension2D;
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
    30
import java.awt.image.ImageObserver;
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
    31
import java.util.Arrays;
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
    32
import java.util.List;
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
    33
import java.util.function.Function;
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
    34
import java.util.function.BiFunction;
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
    35
import java.util.stream.Collectors;
40707
8b0d53520355 8151303: [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
alexsch
parents: 32682
diff changeset
    36
import java.awt.image.MultiResolutionImage;
32682
6f1200d8999d 8029339: Custom MultiResolution image support on HiDPI displays
alexsch
parents: 32427
diff changeset
    37
import java.awt.image.AbstractMultiResolutionImage;
24532
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
    38
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
    39
public class MultiResolutionCachedImage extends AbstractMultiResolutionImage {
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
    40
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
    41
    private final int baseImageWidth;
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
    42
    private final int baseImageHeight;
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
    43
    private final Dimension2D[] sizes;
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
    44
    private final BiFunction<Integer, Integer, Image> mapper;
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
    45
    private int availableInfo;
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
    46
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
    47
    public MultiResolutionCachedImage(int baseImageWidth, int baseImageHeight,
40707
8b0d53520355 8151303: [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
alexsch
parents: 32682
diff changeset
    48
                                      BiFunction<Integer, Integer, Image> mapper)
8b0d53520355 8151303: [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
alexsch
parents: 32682
diff changeset
    49
    {
8b0d53520355 8151303: [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
alexsch
parents: 32682
diff changeset
    50
        this(baseImageWidth, baseImageHeight,
8b0d53520355 8151303: [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
alexsch
parents: 32682
diff changeset
    51
             new Dimension[]{new Dimension( baseImageWidth, baseImageHeight)
24532
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
    52
        }, mapper);
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
    53
    }
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
    54
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
    55
    public MultiResolutionCachedImage(int baseImageWidth, int baseImageHeight,
40707
8b0d53520355 8151303: [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
alexsch
parents: 32682
diff changeset
    56
                                      Dimension2D[] sizes,
8b0d53520355 8151303: [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
alexsch
parents: 32682
diff changeset
    57
                                      BiFunction<Integer, Integer, Image> mapper)
8b0d53520355 8151303: [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
alexsch
parents: 32682
diff changeset
    58
    {
8b0d53520355 8151303: [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
alexsch
parents: 32682
diff changeset
    59
        this(baseImageWidth, baseImageHeight, sizes, mapper, true);
8b0d53520355 8151303: [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
alexsch
parents: 32682
diff changeset
    60
    }
8b0d53520355 8151303: [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
alexsch
parents: 32682
diff changeset
    61
8b0d53520355 8151303: [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
alexsch
parents: 32682
diff changeset
    62
    private MultiResolutionCachedImage(int baseImageWidth, int baseImageHeight,
8b0d53520355 8151303: [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
alexsch
parents: 32682
diff changeset
    63
                                       Dimension2D[] sizes,
8b0d53520355 8151303: [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
alexsch
parents: 32682
diff changeset
    64
                                       BiFunction<Integer, Integer, Image> mapper,
8b0d53520355 8151303: [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
alexsch
parents: 32682
diff changeset
    65
                                       boolean copySizes)
8b0d53520355 8151303: [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
alexsch
parents: 32682
diff changeset
    66
    {
24532
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
    67
        this.baseImageWidth = baseImageWidth;
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
    68
        this.baseImageHeight = baseImageHeight;
40707
8b0d53520355 8151303: [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
alexsch
parents: 32682
diff changeset
    69
        this.sizes = (copySizes && sizes != null)
8b0d53520355 8151303: [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
alexsch
parents: 32682
diff changeset
    70
                                ? Arrays.copyOf(sizes, sizes.length)
8b0d53520355 8151303: [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
alexsch
parents: 32682
diff changeset
    71
                                : sizes;
24532
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
    72
        this.mapper = mapper;
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
    73
    }
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
    74
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
    75
    @Override
32682
6f1200d8999d 8029339: Custom MultiResolution image support on HiDPI displays
alexsch
parents: 32427
diff changeset
    76
    public Image getResolutionVariant(double destWidth, double destHeight) {
6f1200d8999d 8029339: Custom MultiResolution image support on HiDPI displays
alexsch
parents: 32427
diff changeset
    77
        checkSize(destWidth, destHeight);
6f1200d8999d 8029339: Custom MultiResolution image support on HiDPI displays
alexsch
parents: 32427
diff changeset
    78
        int width = (int) Math.ceil(destWidth);
6f1200d8999d 8029339: Custom MultiResolution image support on HiDPI displays
alexsch
parents: 32427
diff changeset
    79
        int height = (int) Math.ceil(destHeight);
24532
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
    80
        ImageCache cache = ImageCache.getInstance();
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
    81
        ImageCacheKey key = new ImageCacheKey(this, width, height);
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
    82
        Image resolutionVariant = cache.getImage(key);
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
    83
        if (resolutionVariant == null) {
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
    84
            resolutionVariant = mapper.apply(width, height);
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
    85
            cache.setImage(key, resolutionVariant);
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
    86
        }
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
    87
        preload(resolutionVariant, availableInfo);
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
    88
        return resolutionVariant;
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
    89
    }
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
    90
32682
6f1200d8999d 8029339: Custom MultiResolution image support on HiDPI displays
alexsch
parents: 32427
diff changeset
    91
    private static void checkSize(double width, double height) {
6f1200d8999d 8029339: Custom MultiResolution image support on HiDPI displays
alexsch
parents: 32427
diff changeset
    92
        if (width <= 0 || height <= 0) {
6f1200d8999d 8029339: Custom MultiResolution image support on HiDPI displays
alexsch
parents: 32427
diff changeset
    93
            throw new IllegalArgumentException(String.format(
6f1200d8999d 8029339: Custom MultiResolution image support on HiDPI displays
alexsch
parents: 32427
diff changeset
    94
                    "Width (%s) or height (%s) cannot be <= 0", width, height));
6f1200d8999d 8029339: Custom MultiResolution image support on HiDPI displays
alexsch
parents: 32427
diff changeset
    95
        }
6f1200d8999d 8029339: Custom MultiResolution image support on HiDPI displays
alexsch
parents: 32427
diff changeset
    96
6f1200d8999d 8029339: Custom MultiResolution image support on HiDPI displays
alexsch
parents: 32427
diff changeset
    97
        if (!Double.isFinite(width) || !Double.isFinite(height)) {
6f1200d8999d 8029339: Custom MultiResolution image support on HiDPI displays
alexsch
parents: 32427
diff changeset
    98
            throw new IllegalArgumentException(String.format(
6f1200d8999d 8029339: Custom MultiResolution image support on HiDPI displays
alexsch
parents: 32427
diff changeset
    99
                    "Width (%s) or height (%s) is not finite", width, height));
6f1200d8999d 8029339: Custom MultiResolution image support on HiDPI displays
alexsch
parents: 32427
diff changeset
   100
        }
6f1200d8999d 8029339: Custom MultiResolution image support on HiDPI displays
alexsch
parents: 32427
diff changeset
   101
    }
6f1200d8999d 8029339: Custom MultiResolution image support on HiDPI displays
alexsch
parents: 32427
diff changeset
   102
24532
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   103
    @Override
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   104
    public List<Image> getResolutionVariants() {
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   105
        return Arrays.stream(sizes).map((Function<Dimension2D, Image>) size
32682
6f1200d8999d 8029339: Custom MultiResolution image support on HiDPI displays
alexsch
parents: 32427
diff changeset
   106
                -> getResolutionVariant(size.getWidth(), size.getHeight()))
6f1200d8999d 8029339: Custom MultiResolution image support on HiDPI displays
alexsch
parents: 32427
diff changeset
   107
                .collect(Collectors.toList());
24532
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   108
    }
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   109
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   110
    public MultiResolutionCachedImage map(Function<Image, Image> mapper) {
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   111
        return new MultiResolutionCachedImage(baseImageWidth, baseImageHeight,
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   112
                sizes, (width, height) ->
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   113
                        mapper.apply(getResolutionVariant(width, height)));
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   114
    }
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   115
40707
8b0d53520355 8151303: [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
alexsch
parents: 32682
diff changeset
   116
    public static Image map(MultiResolutionImage mrImage,
8b0d53520355 8151303: [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
alexsch
parents: 32682
diff changeset
   117
                            Function<Image, Image> mapper) {
8b0d53520355 8151303: [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
alexsch
parents: 32682
diff changeset
   118
8b0d53520355 8151303: [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
alexsch
parents: 32682
diff changeset
   119
        if (mrImage instanceof MultiResolutionToolkitImage) {
8b0d53520355 8151303: [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
alexsch
parents: 32682
diff changeset
   120
            MultiResolutionToolkitImage mrtImage =
8b0d53520355 8151303: [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
alexsch
parents: 32682
diff changeset
   121
                    (MultiResolutionToolkitImage) mrImage;
8b0d53520355 8151303: [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
alexsch
parents: 32682
diff changeset
   122
            return MultiResolutionToolkitImage.map(mrtImage, mapper);
8b0d53520355 8151303: [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
alexsch
parents: 32682
diff changeset
   123
        }
8b0d53520355 8151303: [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
alexsch
parents: 32682
diff changeset
   124
8b0d53520355 8151303: [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
alexsch
parents: 32682
diff changeset
   125
        BiFunction<Integer, Integer, Image> sizeMapper
8b0d53520355 8151303: [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
alexsch
parents: 32682
diff changeset
   126
                = (w, h) -> mapper.apply(mrImage.getResolutionVariant(w, h));
8b0d53520355 8151303: [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
alexsch
parents: 32682
diff changeset
   127
8b0d53520355 8151303: [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
alexsch
parents: 32682
diff changeset
   128
        if (mrImage instanceof MultiResolutionCachedImage) {
8b0d53520355 8151303: [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
alexsch
parents: 32682
diff changeset
   129
            MultiResolutionCachedImage mrcImage
8b0d53520355 8151303: [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
alexsch
parents: 32682
diff changeset
   130
                    = (MultiResolutionCachedImage) mrImage;
8b0d53520355 8151303: [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
alexsch
parents: 32682
diff changeset
   131
8b0d53520355 8151303: [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
alexsch
parents: 32682
diff changeset
   132
            return new MultiResolutionCachedImage(mrcImage.baseImageWidth,
8b0d53520355 8151303: [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
alexsch
parents: 32682
diff changeset
   133
                                                  mrcImage.baseImageHeight,
8b0d53520355 8151303: [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
alexsch
parents: 32682
diff changeset
   134
                                                  mrcImage.sizes,
8b0d53520355 8151303: [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
alexsch
parents: 32682
diff changeset
   135
                                                  sizeMapper,
8b0d53520355 8151303: [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
alexsch
parents: 32682
diff changeset
   136
                                                  false);
8b0d53520355 8151303: [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
alexsch
parents: 32682
diff changeset
   137
        }
8b0d53520355 8151303: [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
alexsch
parents: 32682
diff changeset
   138
8b0d53520355 8151303: [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
alexsch
parents: 32682
diff changeset
   139
        Image image = (Image) mrImage;
8b0d53520355 8151303: [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
alexsch
parents: 32682
diff changeset
   140
        int width = image.getWidth(null);
8b0d53520355 8151303: [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
alexsch
parents: 32682
diff changeset
   141
        int height = image.getHeight(null);
8b0d53520355 8151303: [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
alexsch
parents: 32682
diff changeset
   142
        return new MultiResolutionCachedImage(width, height, sizeMapper);
8b0d53520355 8151303: [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
alexsch
parents: 32682
diff changeset
   143
    }
8b0d53520355 8151303: [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
alexsch
parents: 32682
diff changeset
   144
24532
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   145
    @Override
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   146
    public int getWidth(ImageObserver observer) {
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   147
        updateInfo(observer, ImageObserver.WIDTH);
32125
008f0a0d17e0 8132123: MultiResolutionCachedImage unnecessarily creates base image to get its size
alexsch
parents: 26037
diff changeset
   148
        return baseImageWidth;
24532
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   149
    }
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   150
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   151
    @Override
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   152
    public int getHeight(ImageObserver observer) {
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   153
        updateInfo(observer, ImageObserver.HEIGHT);
32125
008f0a0d17e0 8132123: MultiResolutionCachedImage unnecessarily creates base image to get its size
alexsch
parents: 26037
diff changeset
   154
        return baseImageHeight;
24532
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   155
    }
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   156
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   157
    @Override
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   158
    public Object getProperty(String name, ImageObserver observer) {
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   159
        updateInfo(observer, ImageObserver.PROPERTIES);
32125
008f0a0d17e0 8132123: MultiResolutionCachedImage unnecessarily creates base image to get its size
alexsch
parents: 26037
diff changeset
   160
        return Image.UndefinedProperty;
008f0a0d17e0 8132123: MultiResolutionCachedImage unnecessarily creates base image to get its size
alexsch
parents: 26037
diff changeset
   161
    }
008f0a0d17e0 8132123: MultiResolutionCachedImage unnecessarily creates base image to get its size
alexsch
parents: 26037
diff changeset
   162
008f0a0d17e0 8132123: MultiResolutionCachedImage unnecessarily creates base image to get its size
alexsch
parents: 26037
diff changeset
   163
    @Override
008f0a0d17e0 8132123: MultiResolutionCachedImage unnecessarily creates base image to get its size
alexsch
parents: 26037
diff changeset
   164
    public Image getScaledInstance(int width, int height, int hints) {
008f0a0d17e0 8132123: MultiResolutionCachedImage unnecessarily creates base image to get its size
alexsch
parents: 26037
diff changeset
   165
        return getResolutionVariant(width, height);
24532
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   166
    }
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   167
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   168
    @Override
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   169
    protected Image getBaseImage() {
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   170
        return getResolutionVariant(baseImageWidth, baseImageHeight);
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   171
    }
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   172
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   173
    private void updateInfo(ImageObserver observer, int info) {
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   174
        availableInfo |= (observer == null) ? ImageObserver.ALLBITS : info;
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   175
    }
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   176
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   177
    private static int getInfo(Image image) {
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   178
        if (image instanceof ToolkitImage) {
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   179
            return ((ToolkitImage) image).getImageRep().check(
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   180
                    (img, infoflags, x, y, w, h) -> false);
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   181
        }
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   182
        return 0;
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   183
    }
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   184
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   185
    private static void preload(Image image, int availableInfo) {
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   186
        if (availableInfo != 0 && image instanceof ToolkitImage) {
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   187
            ((ToolkitImage) image).preload(new ImageObserver() {
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   188
                int flags = availableInfo;
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   189
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   190
                @Override
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   191
                public boolean imageUpdate(Image img, int infoflags,
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   192
                        int x, int y, int width, int height) {
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   193
                    flags &= ~infoflags;
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   194
                    return (flags != 0) && ((infoflags
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   195
                            & (ImageObserver.ERROR | ImageObserver.ABORT)) == 0);
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   196
                }
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   197
            });
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   198
        }
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   199
    }
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   200
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   201
    private static class ImageCacheKey implements ImageCache.PixelsKey {
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   202
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   203
        private final int pixelCount;
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   204
        private final int hash;
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   205
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   206
        private final int w;
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   207
        private final int h;
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   208
        private final Image baseImage;
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   209
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   210
        ImageCacheKey(final Image baseImage,
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   211
                final int w, final int h) {
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   212
            this.baseImage = baseImage;
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   213
            this.w = w;
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   214
            this.h = h;
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   215
            this.pixelCount = w * h;
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   216
            hash = hash();
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   217
        }
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   218
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   219
        @Override
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   220
        public int getPixelCount() {
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   221
            return pixelCount;
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   222
        }
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   223
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   224
        private int hash() {
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   225
            int hash = baseImage.hashCode();
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   226
            hash = 31 * hash + w;
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   227
            hash = 31 * hash + h;
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   228
            return hash;
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   229
        }
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   230
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   231
        @Override
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   232
        public int hashCode() {
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   233
            return hash;
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   234
        }
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   235
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   236
        @Override
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   237
        public boolean equals(Object obj) {
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   238
            if (obj instanceof ImageCacheKey) {
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   239
                ImageCacheKey key = (ImageCacheKey) obj;
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   240
                return baseImage == key.baseImage && w == key.w && h == key.h;
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   241
            }
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   242
            return false;
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   243
        }
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents:
diff changeset
   244
    }
32427
c22b7e41adf3 8134984: Text files should end in exactly one newline
martin
parents: 32125
diff changeset
   245
}