jdk/src/java.desktop/macosx/classes/com/apple/laf/AquaUtils.java
author alanb
Thu, 17 Mar 2016 19:04:16 +0000
changeset 36511 9d0388c6b336
parent 25859 3317bb8137f4
child 40707 8b0d53520355
permissions -rw-r--r--
8142968: Module System implementation Summary: Initial integration of JEP 200, JEP 260, JEP 261, and JEP 282 Reviewed-by: alanb, mchung, naoto, rriggs, psandoz, plevart, mullan, ascarpino, vinnie, prr, sherman, dfuchs, mhaupt Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, chris.hegarty@oracle.com, alexandr.scherbatiy@oracle.com, amy.lu@oracle.com, calvin.cheung@oracle.com, daniel.fuchs@oracle.com, erik.joelsson@oracle.com, harold.seigel@oracle.com, jaroslav.bachorik@oracle.com, jean-francois.denise@oracle.com, jan.lahoda@oracle.com, james.laskey@oracle.com, lois.foltan@oracle.com, miroslav.kos@oracle.com, huaming.li@oracle.com, sean.mullan@oracle.com, naoto.sato@oracle.com, masayoshi.okutsu@oracle.com, peter.levart@gmail.com, philip.race@oracle.com, claes.redestad@oracle.com, sergey.bylokhov@oracle.com, alexandre.iline@oracle.com, volker.simonis@gmail.com, staffan.larsen@oracle.com, stuart.marks@oracle.com, semyon.sadetsky@oracle.com, serguei.spitsyn@oracle.com, sundararajan.athijegannathan@oracle.com, valerie.peng@oracle.com, vincent.x.ryan@oracle.com, weijun.wang@oracle.com, yuri.nesterenko@oracle.com, yekaterina.kantserova@oracle.com, alexander.kulyakhtin@oracle.com, felix.yang@oracle.com, andrei.eremeev@oracle.com, frank.yuan@oracle.com, sergei.pikalev@oracle.com, sibabrata.sahoo@oracle.com, tiantian.du@oracle.com, sha.jiang@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     1
/*
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
     2
 * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     4
 *
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    10
 *
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    15
 * accompanied this code).
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    16
 *
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    20
 *
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    23
 * questions.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    24
 */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    25
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    26
package com.apple.laf;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    27
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    28
import java.awt.*;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    29
import java.awt.image.*;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    30
import java.lang.ref.SoftReference;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    31
import java.lang.reflect.Method;
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
    32
import java.security.AccessController;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    33
import java.security.PrivilegedAction;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    34
import java.util.*;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    35
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    36
import javax.swing.*;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    37
import javax.swing.border.Border;
13234
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
    38
import javax.swing.plaf.UIResource;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    39
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 25859
diff changeset
    40
import jdk.internal.loader.ClassLoaders;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 25859
diff changeset
    41
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    42
import sun.awt.AppContext;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    43
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    44
import sun.lwawt.macosx.CImage;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    45
import sun.lwawt.macosx.CImage.Creator;
13234
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
    46
import sun.lwawt.macosx.CPlatformWindow;
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
    47
import sun.reflect.misc.ReflectUtil;
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
    48
import sun.security.action.GetPropertyAction;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    49
import sun.swing.SwingUtilities2;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    50
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    51
import com.apple.laf.AquaImageFactory.SlicedImageControl;
24532
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents: 23666
diff changeset
    52
import sun.awt.image.MultiResolutionCachedImage;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    53
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
    54
final class AquaUtils {
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
    55
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
    56
    private static final String ANIMATIONS_PROPERTY = "swing.enableAnimations";
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    57
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
    58
    /**
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
    59
     * Suppresses default constructor, ensuring non-instantiability.
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
    60
     */
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
    61
    private AquaUtils() {
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
    62
    }
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
    63
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
    64
    /**
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    65
     * Convenience function for determining ComponentOrientation.  Helps us
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    66
     * avoid having Munge directives throughout the code.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    67
     */
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
    68
    static boolean isLeftToRight(final Component c) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    69
        return c.getComponentOrientation().isLeftToRight();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    70
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    71
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
    72
    static void enforceComponentOrientation(final Component c, final ComponentOrientation orientation) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    73
        c.setComponentOrientation(orientation);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    74
        if (c instanceof Container) {
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
    75
            for (final Component child : ((Container)c).getComponents()) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    76
                enforceComponentOrientation(child, orientation);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    77
            }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    78
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    79
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    80
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
    81
    private static Creator getCImageCreatorInternal() {
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
    82
        return AccessController.doPrivileged(new PrivilegedAction<Creator>() {
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
    83
            @Override
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    84
            public Creator run() {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    85
                try {
25762
c4a3548120c6 8044862: Fix raw and unchecked lint warnings in macosx specific code
henryjen
parents: 24532
diff changeset
    86
                    final Method getCreatorMethod = CImage.class.getDeclaredMethod(
c4a3548120c6 8044862: Fix raw and unchecked lint warnings in macosx specific code
henryjen
parents: 24532
diff changeset
    87
                                "getCreator", new Class<?>[] {});
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    88
                    getCreatorMethod.setAccessible(true);
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
    89
                    return (Creator)getCreatorMethod.invoke(null, new Object[] {});
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
    90
                } catch (final Exception ignored) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    91
                    return null;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    92
                }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    93
            }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    94
        });
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    95
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    96
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
    97
    private static final RecyclableSingleton<Creator> cImageCreator = new RecyclableSingleton<Creator>() {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    98
        @Override
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    99
        protected Creator getInstance() {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   100
            return getCImageCreatorInternal();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   101
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   102
    };
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   103
    static Creator getCImageCreator() {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   104
        return cImageCreator.get();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   105
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   106
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   107
    static Image generateSelectedDarkImage(final Image image) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   108
        final ImageProducer prod = new FilteredImageSource(image.getSource(), new IconImageFilter() {
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   109
            @Override
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   110
            int getGreyFor(final int gray) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   111
                return gray * 75 / 100;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   112
            }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   113
        });
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   114
        return Toolkit.getDefaultToolkit().createImage(prod);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   115
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   116
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   117
    static Image generateDisabledImage(final Image image) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   118
        final ImageProducer prod = new FilteredImageSource(image.getSource(), new IconImageFilter() {
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   119
            @Override
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   120
            int getGreyFor(final int gray) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   121
                return 255 - ((255 - gray) * 65 / 100);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   122
            }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   123
        });
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   124
        return Toolkit.getDefaultToolkit().createImage(prod);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   125
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   126
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   127
    static Image generateLightenedImage(final Image image, final int percent) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   128
        final GrayFilter filter = new GrayFilter(true, percent);
24532
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents: 23666
diff changeset
   129
        return (image instanceof MultiResolutionCachedImage)
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents: 23666
diff changeset
   130
                ? ((MultiResolutionCachedImage) image).map(
23318
7da69afd4ffe 8031573: [macosx] Checkmarks of JCheckBoxMenuItems aren't rendered in high resolution on Retina
alexsch
parents: 20835
diff changeset
   131
                        rv -> generateLightenedImage(rv, filter))
7da69afd4ffe 8031573: [macosx] Checkmarks of JCheckBoxMenuItems aren't rendered in high resolution on Retina
alexsch
parents: 20835
diff changeset
   132
                : generateLightenedImage(image, filter);
7da69afd4ffe 8031573: [macosx] Checkmarks of JCheckBoxMenuItems aren't rendered in high resolution on Retina
alexsch
parents: 20835
diff changeset
   133
    }
7da69afd4ffe 8031573: [macosx] Checkmarks of JCheckBoxMenuItems aren't rendered in high resolution on Retina
alexsch
parents: 20835
diff changeset
   134
7da69afd4ffe 8031573: [macosx] Checkmarks of JCheckBoxMenuItems aren't rendered in high resolution on Retina
alexsch
parents: 20835
diff changeset
   135
    static Image generateLightenedImage(Image image, ImageFilter filter) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   136
        final ImageProducer prod = new FilteredImageSource(image.getSource(), filter);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   137
        return Toolkit.getDefaultToolkit().createImage(prod);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   138
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   139
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   140
    private abstract static class IconImageFilter extends RGBImageFilter {
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   141
        IconImageFilter() {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   142
            super();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   143
            canFilterIndexColorModel = true;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   144
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   145
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   146
        @Override
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   147
        public final int filterRGB(final int x, final int y, final int rgb) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   148
            final int red = (rgb >> 16) & 0xff;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   149
            final int green = (rgb >> 8) & 0xff;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   150
            final int blue = rgb & 0xff;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   151
            final int gray = getGreyFor((int)((0.30 * red + 0.59 * green + 0.11 * blue) / 3));
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   152
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   153
            return (rgb & 0xff000000) | (grayTransform(red, gray) << 16) | (grayTransform(green, gray) << 8) | (grayTransform(blue, gray) << 0);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   154
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   155
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   156
        private static int grayTransform(final int color, final int gray) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   157
            int result = color - gray;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   158
            if (result < 0) result = 0;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   159
            if (result > 255) result = 255;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   160
            return result;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   161
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   162
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   163
        abstract int getGreyFor(int gray);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   164
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   165
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   166
    abstract static class RecyclableObject<T> {
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   167
        private SoftReference<T> objectRef;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   168
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   169
        T get() {
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   170
            T referent;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   171
            if (objectRef != null && (referent = objectRef.get()) != null) return referent;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   172
            referent = create();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   173
            objectRef = new SoftReference<T>(referent);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   174
            return referent;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   175
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   176
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   177
        protected abstract T create();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   178
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   179
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   180
    abstract static class RecyclableSingleton<T> {
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   181
        final T get() {
23666
a54bf43b5ba9 8035069: [macosx] Loading resolution variants by demand
alexsch
parents: 23318
diff changeset
   182
            return AppContext.getSoftReferenceValue(this, () -> getInstance());
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   183
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   184
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   185
        void reset() {
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   186
            AppContext.getAppContext().remove(this);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   187
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   188
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   189
        abstract T getInstance();
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   190
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   191
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   192
    static class RecyclableSingletonFromDefaultConstructor<T> extends RecyclableSingleton<T> {
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   193
        private final Class<T> clazz;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   194
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   195
        RecyclableSingletonFromDefaultConstructor(final Class<T> clazz) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   196
            this.clazz = clazz;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   197
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   198
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   199
        @Override
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   200
        T getInstance() {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   201
            try {
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   202
                ReflectUtil.checkPackageAccess(clazz);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   203
                return clazz.newInstance();
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   204
            } catch (InstantiationException | IllegalAccessException ignored) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   205
            }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   206
            return null;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   207
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   208
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   209
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   210
    abstract static class LazyKeyedSingleton<K, V> {
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   211
        private Map<K, V> refs;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   212
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   213
        V get(final K key) {
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   214
            if (refs == null) refs = new HashMap<>();
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   215
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   216
            final V cachedValue = refs.get(key);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   217
            if (cachedValue != null) return cachedValue;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   218
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   219
            final V value = getInstance(key);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   220
            refs.put(key, value);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   221
            return value;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   222
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   223
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   224
        protected abstract V getInstance(K key);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   225
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   226
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   227
    private static final RecyclableSingleton<Boolean> enableAnimations = new RecyclableSingleton<Boolean>() {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   228
        @Override
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   229
        protected Boolean getInstance() {
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   230
            final String sizeProperty = (String) AccessController.doPrivileged((PrivilegedAction<?>)new GetPropertyAction(
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   231
                    ANIMATIONS_PROPERTY));
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   232
            return !"false".equals(sizeProperty); // should be true by default
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   233
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   234
    };
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   235
    private static boolean animationsEnabled() {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   236
        return enableAnimations.get();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   237
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   238
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   239
    private static final int MENU_BLINK_DELAY = 50; // 50ms == 3/60 sec, according to the spec
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   240
    static void blinkMenu(final Selectable selectable) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   241
        if (!animationsEnabled()) return;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   242
        try {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   243
            selectable.paintSelected(false);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   244
            Thread.sleep(MENU_BLINK_DELAY);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   245
            selectable.paintSelected(true);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   246
            Thread.sleep(MENU_BLINK_DELAY);
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   247
        } catch (final InterruptedException ignored) { }
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   248
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   249
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   250
    interface Selectable {
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   251
        void paintSelected(boolean selected);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   252
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   253
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   254
    interface JComponentPainter {
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   255
        void paint(JComponent c, Graphics g, int x, int y, int w, int h);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   256
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   257
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   258
    interface Painter {
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   259
        void paint(Graphics g, int x, int y, int w, int h);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   260
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   261
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   262
    static void paintDropShadowText(final Graphics g, final JComponent c, final Font font, final FontMetrics metrics, final int x, final int y, final int offsetX, final int offsetY, final Color textColor, final Color shadowColor, final String text) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   263
        g.setFont(font);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   264
        g.setColor(shadowColor);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   265
        SwingUtilities2.drawString(c, g, text, x + offsetX, y + offsetY + metrics.getAscent());
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   266
        g.setColor(textColor);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   267
        SwingUtilities2.drawString(c, g, text, x, y + metrics.getAscent());
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   268
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   269
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   270
    static class ShadowBorder implements Border {
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   271
        private final Painter prePainter;
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   272
        private final Painter postPainter;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   273
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   274
        private final int offsetX;
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   275
        private final int offsetY;
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   276
        private final float distance;
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   277
        private final int blur;
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   278
        private final Insets insets;
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   279
        private final ConvolveOp blurOp;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   280
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   281
        ShadowBorder(final Painter prePainter, final Painter postPainter, final int offsetX, final int offsetY, final float distance, final float intensity, final int blur) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   282
            this.prePainter = prePainter; this.postPainter = postPainter;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   283
            this.offsetX = offsetX; this.offsetY = offsetY; this.distance = distance; this.blur = blur;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   284
            final int halfBlur = blur / 2;
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   285
            insets = new Insets(halfBlur - offsetY, halfBlur - offsetX, halfBlur + offsetY, halfBlur + offsetX);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   286
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   287
            final float blurry = intensity / (blur * blur);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   288
            final float[] blurKernel = new float[blur * blur];
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   289
            for (int i = 0; i < blurKernel.length; i++) blurKernel[i] = blurry;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   290
            blurOp = new ConvolveOp(new Kernel(blur, blur, blurKernel));
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   291
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   292
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   293
        @Override
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   294
        public final boolean isBorderOpaque() {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   295
            return false;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   296
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   297
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   298
        @Override
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   299
        public final Insets getBorderInsets(final Component c) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   300
            return insets;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   301
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   302
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   303
        @Override
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   304
        public void paintBorder(final Component c, final Graphics g, final int x, final int y, final int width, final int height) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   305
            final BufferedImage img = new BufferedImage(width + blur * 2, height + blur * 2, BufferedImage.TYPE_INT_ARGB_PRE);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   306
            paintToImage(img, x, y, width, height);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   307
//            debugFrame("border", img);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   308
            g.drawImage(img, -blur, -blur, null);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   309
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   310
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   311
        private void paintToImage(final BufferedImage img, final int x, final int y, final int width, final int height) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   312
            // clear the prior image
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   313
            Graphics2D imgG = (Graphics2D)img.getGraphics();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   314
            imgG.setComposite(AlphaComposite.Clear);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   315
            imgG.setColor(Color.black);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   316
            imgG.fillRect(0, 0, width + blur * 2, height + blur * 2);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   317
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   318
            final int adjX = (int)(x + blur + offsetX + (insets.left * distance));
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   319
            final int adjY = (int)(y + blur + offsetY + (insets.top * distance));
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   320
            final int adjW = (int)(width - (insets.left + insets.right) * distance);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   321
            final int adjH = (int)(height - (insets.top + insets.bottom) * distance);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   322
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   323
            // let the delegate paint whatever they want to be blurred
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   324
            imgG.setComposite(AlphaComposite.DstAtop);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   325
            if (prePainter != null) prePainter.paint(imgG, adjX, adjY, adjW, adjH);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   326
            imgG.dispose();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   327
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   328
            // blur the prior image back into the same pixels
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   329
            imgG = (Graphics2D)img.getGraphics();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   330
            imgG.setComposite(AlphaComposite.DstAtop);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   331
            imgG.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   332
            imgG.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   333
            imgG.drawImage(img, blurOp, 0, 0);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   334
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   335
            if (postPainter != null) postPainter.paint(imgG, adjX, adjY, adjW, adjH);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   336
            imgG.dispose();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   337
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   338
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   339
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   340
    static class SlicedShadowBorder extends ShadowBorder {
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   341
        private final SlicedImageControl slices;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   342
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   343
        SlicedShadowBorder(final Painter prePainter, final Painter postPainter, final int offsetX, final int offsetY, final float distance, final float intensity, final int blur, final int templateWidth, final int templateHeight, final int leftCut, final int topCut, final int rightCut, final int bottomCut) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   344
            super(prePainter, postPainter, offsetX, offsetY, distance, intensity, blur);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   345
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   346
            final BufferedImage i = new BufferedImage(templateWidth, templateHeight, BufferedImage.TYPE_INT_ARGB_PRE);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   347
            super.paintBorder(null, i.getGraphics(), 0, 0, templateWidth, templateHeight);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   348
//            debugFrame("slices", i);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   349
            slices = new SlicedImageControl(i, leftCut, topCut, rightCut, bottomCut, false);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   350
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   351
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   352
        @Override
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   353
        public void paintBorder(final Component c, final Graphics g, final int x, final int y, final int width, final int height) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   354
            slices.paint(g, x, y, width, height);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   355
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   356
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   357
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   358
//    static void debugFrame(String name, Image image) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   359
//        JFrame f = new JFrame(name);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   360
//        f.setContentPane(new JLabel(new ImageIcon(image)));
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   361
//        f.pack();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   362
//        f.setVisible(true);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   363
//    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   364
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   365
    // special casing naughty applications, like InstallAnywhere
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   366
    // <rdar://problem/4851533> REGR: JButton: Myst IV: the buttons of 1.0.3 updater have redraw issue
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   367
    static boolean shouldUseOpaqueButtons() {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 25859
diff changeset
   368
        // can we use ClassLoader.getSystemClassLoader here?
9d0388c6b336 8142968: Module System implementation
alanb
parents: 25859
diff changeset
   369
        final ClassLoader launcherClassLoader = ClassLoaders.appClassLoader();
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   370
        if (classExists(launcherClassLoader, "com.installshield.wizard.platform.macosx.MacOSXUtils")) return true;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   371
        return false;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   372
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   373
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   374
    private static boolean classExists(final ClassLoader classLoader, final String clazzName) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   375
        try {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   376
            return Class.forName(clazzName, false, classLoader) != null;
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   377
        } catch (final Throwable ignored) { }
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   378
        return false;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   379
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   380
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   381
    private static final RecyclableSingleton<Method> getJComponentGetFlagMethod = new RecyclableSingleton<Method>() {
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   382
        @Override
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   383
        protected Method getInstance() {
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   384
            return AccessController.doPrivileged(
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   385
                new PrivilegedAction<Method>() {
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   386
                    @Override
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   387
                    public Method run() {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   388
                        try {
25762
c4a3548120c6 8044862: Fix raw and unchecked lint warnings in macosx specific code
henryjen
parents: 24532
diff changeset
   389
                            final Method method = JComponent.class.getDeclaredMethod(
c4a3548120c6 8044862: Fix raw and unchecked lint warnings in macosx specific code
henryjen
parents: 24532
diff changeset
   390
                                    "getFlag", new Class<?>[] { int.class });
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   391
                            method.setAccessible(true);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   392
                            return method;
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   393
                        } catch (final Throwable ignored) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   394
                            return null;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   395
                        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   396
                    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   397
                }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   398
            );
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   399
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   400
    };
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   401
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   402
    private static final Integer OPAQUE_SET_FLAG = 24; // private int JComponent.OPAQUE_SET
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   403
    static boolean hasOpaqueBeenExplicitlySet(final JComponent c) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   404
        final Method method = getJComponentGetFlagMethod.get();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   405
        if (method == null) return false;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   406
        try {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   407
            return Boolean.TRUE.equals(method.invoke(c, OPAQUE_SET_FLAG));
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   408
        } catch (final Throwable ignored) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   409
            return false;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   410
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   411
    }
13234
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   412
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   413
    private static boolean isWindowTextured(final Component c) {
13234
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   414
        if (!(c instanceof JComponent)) {
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   415
            return false;
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   416
        }
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   417
        final JRootPane pane = ((JComponent) c).getRootPane();
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   418
        if (pane == null) {
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   419
            return false;
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   420
        }
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   421
        Object prop = pane.getClientProperty(
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   422
                CPlatformWindow.WINDOW_BRUSH_METAL_LOOK);
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   423
        if (prop != null) {
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   424
            return Boolean.parseBoolean(prop.toString());
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   425
        }
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   426
        prop = pane.getClientProperty(CPlatformWindow.WINDOW_STYLE);
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   427
        return prop != null && "textured".equals(prop);
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   428
    }
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   429
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   430
    private static Color resetAlpha(final Color color) {
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   431
        return new Color(color.getRed(), color.getGreen(), color.getBlue(), 0);
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   432
    }
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   433
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   434
    static void fillRect(final Graphics g, final Component c) {
13234
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   435
        fillRect(g, c, c.getBackground(), 0, 0, c.getWidth(), c.getHeight());
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   436
    }
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   437
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   438
    static void fillRect(final Graphics g, final Component c, final Color color,
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   439
                         final int x, final int y, final int w, final int h) {
13234
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   440
        if (!(g instanceof Graphics2D)) {
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   441
            return;
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   442
        }
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   443
        final Graphics2D cg = (Graphics2D) g.create();
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   444
        try {
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   445
            if (color instanceof UIResource && isWindowTextured(c)
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   446
                    && color.equals(SystemColor.window)) {
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   447
                cg.setComposite(AlphaComposite.Src);
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   448
                cg.setColor(resetAlpha(color));
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   449
            } else {
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   450
                cg.setColor(color);
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   451
            }
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   452
            cg.fillRect(x, y, w, h);
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   453
        } finally {
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   454
            cg.dispose();
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   455
        }
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   456
    }
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   457
}
13234
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   458