src/java.desktop/macosx/classes/com/apple/laf/AquaUtils.java
author prr
Wed, 03 Oct 2018 11:10:09 -0700
changeset 52242 7bd9745e8e15
parent 47216 71c04702a3d5
permissions -rw-r--r--
8211031: Remove un-needed qualified export to java.desktop from java.base on macos Reviewed-by: serb, mchung
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
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    40
import sun.awt.AppContext;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    41
13234
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
    42
import sun.lwawt.macosx.CPlatformWindow;
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
    43
import sun.reflect.misc.ReflectUtil;
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
    44
import sun.security.action.GetPropertyAction;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    45
import sun.swing.SwingUtilities2;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    46
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    47
import com.apple.laf.AquaImageFactory.SlicedImageControl;
24532
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents: 23666
diff changeset
    48
import sun.awt.image.MultiResolutionCachedImage;
40719
4ae72a69bd3b 8129854: Remove reflection from AWT/Swing classes
alexsch
parents: 40707
diff changeset
    49
import sun.swing.SwingAccessor;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    50
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
    51
final class AquaUtils {
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
    52
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
    53
    private static final String ANIMATIONS_PROPERTY = "swing.enableAnimations";
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    54
20835
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
     * Suppresses default constructor, ensuring non-instantiability.
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
    57
     */
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
    58
    private AquaUtils() {
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
    59
    }
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
    /**
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    62
     * Convenience function for determining ComponentOrientation.  Helps us
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    63
     * avoid having Munge directives throughout the code.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    64
     */
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
    65
    static boolean isLeftToRight(final Component c) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    66
        return c.getComponentOrientation().isLeftToRight();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    67
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    68
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
    69
    static void enforceComponentOrientation(final Component c, final ComponentOrientation orientation) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    70
        c.setComponentOrientation(orientation);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    71
        if (c instanceof Container) {
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
    72
            for (final Component child : ((Container)c).getComponents()) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    73
                enforceComponentOrientation(child, orientation);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    74
            }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    75
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    76
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    77
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
    78
    static Image generateSelectedDarkImage(final Image image) {
40707
8b0d53520355 8151303: [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
alexsch
parents: 36511
diff changeset
    79
        final ImageFilter filter =  new IconImageFilter() {
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
    80
            @Override
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    81
            int getGreyFor(final int gray) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    82
                return gray * 75 / 100;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    83
            }
40707
8b0d53520355 8151303: [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
alexsch
parents: 36511
diff changeset
    84
        };
8b0d53520355 8151303: [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
alexsch
parents: 36511
diff changeset
    85
        return map(image, filter);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    86
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    87
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
    88
    static Image generateDisabledImage(final Image image) {
40707
8b0d53520355 8151303: [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
alexsch
parents: 36511
diff changeset
    89
        final ImageFilter filter = new IconImageFilter() {
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
    90
            @Override
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    91
            int getGreyFor(final int gray) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    92
                return 255 - ((255 - gray) * 65 / 100);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    93
            }
40707
8b0d53520355 8151303: [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
alexsch
parents: 36511
diff changeset
    94
        };
8b0d53520355 8151303: [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
alexsch
parents: 36511
diff changeset
    95
        return map(image, filter);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    96
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    97
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
    98
    static Image generateLightenedImage(final Image image, final int percent) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    99
        final GrayFilter filter = new GrayFilter(true, percent);
40707
8b0d53520355 8151303: [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
alexsch
parents: 36511
diff changeset
   100
        return map(image, filter);
8b0d53520355 8151303: [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
alexsch
parents: 36511
diff changeset
   101
    }
8b0d53520355 8151303: [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
alexsch
parents: 36511
diff changeset
   102
8b0d53520355 8151303: [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
alexsch
parents: 36511
diff changeset
   103
    static Image generateFilteredImage(Image image, ImageFilter filter) {
8b0d53520355 8151303: [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
alexsch
parents: 36511
diff changeset
   104
        final ImageProducer prod = new FilteredImageSource(image.getSource(), filter);
8b0d53520355 8151303: [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
alexsch
parents: 36511
diff changeset
   105
        return Toolkit.getDefaultToolkit().createImage(prod);
23318
7da69afd4ffe 8031573: [macosx] Checkmarks of JCheckBoxMenuItems aren't rendered in high resolution on Retina
alexsch
parents: 20835
diff changeset
   106
    }
7da69afd4ffe 8031573: [macosx] Checkmarks of JCheckBoxMenuItems aren't rendered in high resolution on Retina
alexsch
parents: 20835
diff changeset
   107
40707
8b0d53520355 8151303: [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
alexsch
parents: 36511
diff changeset
   108
    private static Image map(Image image, ImageFilter filter) {
8b0d53520355 8151303: [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
alexsch
parents: 36511
diff changeset
   109
        if (image instanceof MultiResolutionImage) {
8b0d53520355 8151303: [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
alexsch
parents: 36511
diff changeset
   110
            return MultiResolutionCachedImage
8b0d53520355 8151303: [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
alexsch
parents: 36511
diff changeset
   111
                    .map((MultiResolutionImage) image,
8b0d53520355 8151303: [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
alexsch
parents: 36511
diff changeset
   112
                         (img) -> generateFilteredImage(img, filter));
8b0d53520355 8151303: [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
alexsch
parents: 36511
diff changeset
   113
        }
8b0d53520355 8151303: [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
alexsch
parents: 36511
diff changeset
   114
        return generateFilteredImage(image, filter);
12047
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
    private abstract static class IconImageFilter extends RGBImageFilter {
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   118
        IconImageFilter() {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   119
            super();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   120
            canFilterIndexColorModel = true;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   121
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   122
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   123
        @Override
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   124
        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
   125
            final int red = (rgb >> 16) & 0xff;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   126
            final int green = (rgb >> 8) & 0xff;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   127
            final int blue = rgb & 0xff;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   128
            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
   129
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   130
            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
   131
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   132
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   133
        private static int grayTransform(final int color, final int gray) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   134
            int result = color - gray;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   135
            if (result < 0) result = 0;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   136
            if (result > 255) result = 255;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   137
            return result;
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
        abstract int getGreyFor(int gray);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   141
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   142
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   143
    abstract static class RecyclableObject<T> {
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   144
        private SoftReference<T> objectRef;
12047
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
        T get() {
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   147
            T referent;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   148
            if (objectRef != null && (referent = objectRef.get()) != null) return referent;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   149
            referent = create();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   150
            objectRef = new SoftReference<T>(referent);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   151
            return referent;
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
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   154
        protected abstract T create();
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
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   157
    abstract static class RecyclableSingleton<T> {
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   158
        final T get() {
23666
a54bf43b5ba9 8035069: [macosx] Loading resolution variants by demand
alexsch
parents: 23318
diff changeset
   159
            return AppContext.getSoftReferenceValue(this, () -> getInstance());
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   160
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   161
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   162
        void reset() {
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   163
            AppContext.getAppContext().remove(this);
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 T getInstance();
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   167
    }
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
    static class RecyclableSingletonFromDefaultConstructor<T> extends RecyclableSingleton<T> {
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   170
        private final Class<T> clazz;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   171
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   172
        RecyclableSingletonFromDefaultConstructor(final Class<T> clazz) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   173
            this.clazz = clazz;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   174
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   175
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   176
        @Override
42218
35e0972b2533 8169887: javax/swing/JEditorPane/8080972/TestJEditor.java, javax/swing/text/View/8080972/TestObjectView.java are failing
prr
parents: 42208
diff changeset
   177
        @SuppressWarnings("deprecation")
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   178
        T getInstance() {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   179
            try {
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   180
                ReflectUtil.checkPackageAccess(clazz);
42218
35e0972b2533 8169887: javax/swing/JEditorPane/8080972/TestJEditor.java, javax/swing/text/View/8080972/TestObjectView.java are failing
prr
parents: 42208
diff changeset
   181
                return clazz.newInstance();
42208
7c1017f0ade5 8155874: Fix java.desktop deprecation warnings about Class.newInstance
prr
parents: 40719
diff changeset
   182
            } catch (ReflectiveOperationException ignored) {
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
            return null;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   185
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   186
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   187
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   188
    abstract static class LazyKeyedSingleton<K, V> {
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   189
        private Map<K, V> refs;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   190
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   191
        V get(final K key) {
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   192
            if (refs == null) refs = new HashMap<>();
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   193
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   194
            final V cachedValue = refs.get(key);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   195
            if (cachedValue != null) return cachedValue;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   196
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   197
            final V value = getInstance(key);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   198
            refs.put(key, value);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   199
            return value;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   200
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   201
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   202
        protected abstract V getInstance(K key);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   203
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   204
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   205
    private static final RecyclableSingleton<Boolean> enableAnimations = new RecyclableSingleton<Boolean>() {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   206
        @Override
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   207
        protected Boolean getInstance() {
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   208
            final String sizeProperty = (String) AccessController.doPrivileged((PrivilegedAction<?>)new GetPropertyAction(
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   209
                    ANIMATIONS_PROPERTY));
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   210
            return !"false".equals(sizeProperty); // should be true by default
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   211
        }
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
    private static boolean animationsEnabled() {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   214
        return enableAnimations.get();
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
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   217
    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
   218
    static void blinkMenu(final Selectable selectable) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   219
        if (!animationsEnabled()) return;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   220
        try {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   221
            selectable.paintSelected(false);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   222
            Thread.sleep(MENU_BLINK_DELAY);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   223
            selectable.paintSelected(true);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   224
            Thread.sleep(MENU_BLINK_DELAY);
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   225
        } catch (final InterruptedException ignored) { }
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   226
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   227
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   228
    interface Selectable {
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   229
        void paintSelected(boolean selected);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   230
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   231
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   232
    interface JComponentPainter {
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   233
        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
   234
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   235
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   236
    interface Painter {
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   237
        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
   238
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   239
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   240
    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
   241
        g.setFont(font);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   242
        g.setColor(shadowColor);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   243
        SwingUtilities2.drawString(c, g, text, x + offsetX, y + offsetY + metrics.getAscent());
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   244
        g.setColor(textColor);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   245
        SwingUtilities2.drawString(c, g, text, x, y + metrics.getAscent());
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   246
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   247
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   248
    static class ShadowBorder implements Border {
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   249
        private final Painter prePainter;
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   250
        private final Painter postPainter;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   251
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   252
        private final int offsetX;
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   253
        private final int offsetY;
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   254
        private final float distance;
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   255
        private final int blur;
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   256
        private final Insets insets;
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   257
        private final ConvolveOp blurOp;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   258
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   259
        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
   260
            this.prePainter = prePainter; this.postPainter = postPainter;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   261
            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
   262
            final int halfBlur = blur / 2;
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   263
            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
   264
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   265
            final float blurry = intensity / (blur * blur);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   266
            final float[] blurKernel = new float[blur * blur];
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   267
            for (int i = 0; i < blurKernel.length; i++) blurKernel[i] = blurry;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   268
            blurOp = new ConvolveOp(new Kernel(blur, blur, blurKernel));
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   269
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   270
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   271
        @Override
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   272
        public final boolean isBorderOpaque() {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   273
            return false;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   274
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   275
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   276
        @Override
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   277
        public final Insets getBorderInsets(final Component c) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   278
            return insets;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   279
        }
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
        @Override
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   282
        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
   283
            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
   284
            paintToImage(img, x, y, width, height);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   285
            g.drawImage(img, -blur, -blur, null);
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
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   288
        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
   289
            // clear the prior image
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   290
            Graphics2D imgG = (Graphics2D)img.getGraphics();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   291
            imgG.setComposite(AlphaComposite.Clear);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   292
            imgG.setColor(Color.black);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   293
            imgG.fillRect(0, 0, width + blur * 2, height + blur * 2);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   294
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   295
            final int adjX = (int)(x + blur + offsetX + (insets.left * distance));
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   296
            final int adjY = (int)(y + blur + offsetY + (insets.top * distance));
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   297
            final int adjW = (int)(width - (insets.left + insets.right) * distance);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   298
            final int adjH = (int)(height - (insets.top + insets.bottom) * distance);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   299
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   300
            // let the delegate paint whatever they want to be blurred
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   301
            imgG.setComposite(AlphaComposite.DstAtop);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   302
            if (prePainter != null) prePainter.paint(imgG, adjX, adjY, adjW, adjH);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   303
            imgG.dispose();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   304
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   305
            // blur the prior image back into the same pixels
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   306
            imgG = (Graphics2D)img.getGraphics();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   307
            imgG.setComposite(AlphaComposite.DstAtop);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   308
            imgG.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   309
            imgG.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   310
            imgG.drawImage(img, blurOp, 0, 0);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   311
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   312
            if (postPainter != null) postPainter.paint(imgG, adjX, adjY, adjW, adjH);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   313
            imgG.dispose();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   314
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   315
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   316
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   317
    static class SlicedShadowBorder extends ShadowBorder {
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   318
        private final SlicedImageControl slices;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   319
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   320
        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
   321
            super(prePainter, postPainter, offsetX, offsetY, distance, intensity, blur);
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
            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
   324
            super.paintBorder(null, i.getGraphics(), 0, 0, templateWidth, templateHeight);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   325
            slices = new SlicedImageControl(i, leftCut, topCut, rightCut, bottomCut, false);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   326
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   327
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   328
        @Override
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   329
        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
   330
            slices.paint(g, x, y, width, height);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   331
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   332
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   333
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   334
    private static final RecyclableSingleton<Method> getJComponentGetFlagMethod = new RecyclableSingleton<Method>() {
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   335
        @Override
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   336
        protected Method getInstance() {
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   337
            return AccessController.doPrivileged(
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   338
                new PrivilegedAction<Method>() {
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   339
                    @Override
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   340
                    public Method run() {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   341
                        try {
25762
c4a3548120c6 8044862: Fix raw and unchecked lint warnings in macosx specific code
henryjen
parents: 24532
diff changeset
   342
                            final Method method = JComponent.class.getDeclaredMethod(
c4a3548120c6 8044862: Fix raw and unchecked lint warnings in macosx specific code
henryjen
parents: 24532
diff changeset
   343
                                    "getFlag", new Class<?>[] { int.class });
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   344
                            method.setAccessible(true);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   345
                            return method;
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   346
                        } catch (final Throwable ignored) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   347
                            return null;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   348
                        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   349
                    }
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
            );
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   352
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   353
    };
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   354
40719
4ae72a69bd3b 8129854: Remove reflection from AWT/Swing classes
alexsch
parents: 40707
diff changeset
   355
    private static final int OPAQUE_SET_FLAG = 24; // private int JComponent.OPAQUE_SET
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   356
    static boolean hasOpaqueBeenExplicitlySet(final JComponent c) {
40719
4ae72a69bd3b 8129854: Remove reflection from AWT/Swing classes
alexsch
parents: 40707
diff changeset
   357
        return SwingAccessor.getJComponentAccessor().getFlag(c, OPAQUE_SET_FLAG);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   358
    }
13234
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   359
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   360
    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
   361
        if (!(c instanceof JComponent)) {
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   362
            return false;
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   363
        }
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   364
        final JRootPane pane = ((JComponent) c).getRootPane();
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   365
        if (pane == null) {
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   366
            return false;
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   367
        }
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   368
        Object prop = pane.getClientProperty(
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   369
                CPlatformWindow.WINDOW_BRUSH_METAL_LOOK);
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   370
        if (prop != null) {
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   371
            return Boolean.parseBoolean(prop.toString());
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   372
        }
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   373
        prop = pane.getClientProperty(CPlatformWindow.WINDOW_STYLE);
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   374
        return prop != null && "textured".equals(prop);
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   375
    }
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   376
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   377
    private static Color resetAlpha(final Color color) {
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   378
        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
   379
    }
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   380
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   381
    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
   382
        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
   383
    }
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   384
20835
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   385
    static void fillRect(final Graphics g, final Component c, final Color color,
56b90be9f6e2 8021282: Better recycling of object instances
serb
parents: 13234
diff changeset
   386
                         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
   387
        if (!(g instanceof Graphics2D)) {
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   388
            return;
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   389
        }
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   390
        final Graphics2D cg = (Graphics2D) g.create();
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   391
        try {
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   392
            if (color instanceof UIResource && isWindowTextured(c)
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   393
                    && color.equals(SystemColor.window)) {
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   394
                cg.setComposite(AlphaComposite.Src);
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   395
                cg.setColor(resetAlpha(color));
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   396
            } else {
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   397
                cg.setColor(color);
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   398
            }
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   399
            cg.fillRect(x, y, w, h);
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   400
        } finally {
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   401
            cg.dispose();
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   402
        }
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   403
    }
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   404
}
13234
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 12047
diff changeset
   405