src/java.desktop/macosx/classes/sun/lwawt/macosx/CTrayIcon.java
author serb
Tue, 01 Oct 2019 10:28:12 -0700
changeset 58597 4ff14948c6a1
parent 53794 c2b366b9c360
permissions -rw-r--r--
7124404: [macosx] no awt.multiClickInterval desktop property Reviewed-by: dmarkov
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
/*
47508
33da1153954c 8187639: TrayIcon is not properly supported on macOS in multi-screen environment
serb
parents: 47216
diff changeset
     2
 * Copyright (c) 2011, 2017, 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 sun.lwawt.macosx;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    27
47508
33da1153954c 8187639: TrayIcon is not properly supported on macOS in multi-screen environment
serb
parents: 47216
diff changeset
    28
import java.awt.AWTEvent;
33da1153954c 8187639: TrayIcon is not properly supported on macOS in multi-screen environment
serb
parents: 47216
diff changeset
    29
import java.awt.Button;
33da1153954c 8187639: TrayIcon is not properly supported on macOS in multi-screen environment
serb
parents: 47216
diff changeset
    30
import java.awt.Frame;
33da1153954c 8187639: TrayIcon is not properly supported on macOS in multi-screen environment
serb
parents: 47216
diff changeset
    31
import java.awt.Graphics2D;
33da1153954c 8187639: TrayIcon is not properly supported on macOS in multi-screen environment
serb
parents: 47216
diff changeset
    32
import java.awt.GraphicsConfiguration;
33da1153954c 8187639: TrayIcon is not properly supported on macOS in multi-screen environment
serb
parents: 47216
diff changeset
    33
import java.awt.GraphicsDevice;
33da1153954c 8187639: TrayIcon is not properly supported on macOS in multi-screen environment
serb
parents: 47216
diff changeset
    34
import java.awt.GraphicsEnvironment;
33da1153954c 8187639: TrayIcon is not properly supported on macOS in multi-screen environment
serb
parents: 47216
diff changeset
    35
import java.awt.Image;
33da1153954c 8187639: TrayIcon is not properly supported on macOS in multi-screen environment
serb
parents: 47216
diff changeset
    36
import java.awt.MediaTracker;
33da1153954c 8187639: TrayIcon is not properly supported on macOS in multi-screen environment
serb
parents: 47216
diff changeset
    37
import java.awt.PopupMenu;
33da1153954c 8187639: TrayIcon is not properly supported on macOS in multi-screen environment
serb
parents: 47216
diff changeset
    38
import java.awt.RenderingHints;
33da1153954c 8187639: TrayIcon is not properly supported on macOS in multi-screen environment
serb
parents: 47216
diff changeset
    39
import java.awt.Toolkit;
33da1153954c 8187639: TrayIcon is not properly supported on macOS in multi-screen environment
serb
parents: 47216
diff changeset
    40
import java.awt.Transparency;
33da1153954c 8187639: TrayIcon is not properly supported on macOS in multi-screen environment
serb
parents: 47216
diff changeset
    41
import java.awt.TrayIcon;
33da1153954c 8187639: TrayIcon is not properly supported on macOS in multi-screen environment
serb
parents: 47216
diff changeset
    42
import java.awt.event.ActionEvent;
33da1153954c 8187639: TrayIcon is not properly supported on macOS in multi-screen environment
serb
parents: 47216
diff changeset
    43
import java.awt.event.MouseEvent;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    44
import java.awt.geom.Point2D;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    45
import java.awt.image.BufferedImage;
50649
9cf90ac8dbf7 8029250: [macosx] There is no tray icon shown in the system tray area when case starts
mhalder
parents: 47508
diff changeset
    46
import java.awt.image.ImageObserver;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    47
import java.awt.peer.TrayIconPeer;
47508
33da1153954c 8187639: TrayIcon is not properly supported on macOS in multi-screen environment
serb
parents: 47216
diff changeset
    48
33da1153954c 8187639: TrayIcon is not properly supported on macOS in multi-screen environment
serb
parents: 47216
diff changeset
    49
import javax.swing.Icon;
33da1153954c 8187639: TrayIcon is not properly supported on macOS in multi-screen environment
serb
parents: 47216
diff changeset
    50
import javax.swing.UIManager;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    51
47508
33da1153954c 8187639: TrayIcon is not properly supported on macOS in multi-screen environment
serb
parents: 47216
diff changeset
    52
import sun.awt.SunToolkit;
33da1153954c 8187639: TrayIcon is not properly supported on macOS in multi-screen environment
serb
parents: 47216
diff changeset
    53
33da1153954c 8187639: TrayIcon is not properly supported on macOS in multi-screen environment
serb
parents: 47216
diff changeset
    54
import static sun.awt.AWTAccessor.MenuComponentAccessor;
33da1153954c 8187639: TrayIcon is not properly supported on macOS in multi-screen environment
serb
parents: 47216
diff changeset
    55
import static sun.awt.AWTAccessor.getMenuComponentAccessor;
30469
bac0a7ff7e1e 8074028: Remove API references to java.awt.peer
serb
parents: 28235
diff changeset
    56
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    57
public class CTrayIcon extends CFRetainedResource implements TrayIconPeer {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    58
    private TrayIcon target;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    59
    private PopupMenu popup;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    60
14157
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
    61
    // In order to construct MouseEvent object, we need to specify a
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
    62
    // Component target. Because TrayIcon isn't Component's subclass,
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
    63
    // we use this dummy frame instead
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
    64
    private final Frame dummyFrame;
50649
9cf90ac8dbf7 8029250: [macosx] There is no tray icon shown in the system tray area when case starts
mhalder
parents: 47508
diff changeset
    65
    IconObserver observer = new IconObserver();
14157
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
    66
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
    67
    // A bitmask that indicates what mouse buttons produce MOUSE_CLICKED events
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
    68
    // on MOUSE_RELEASE. Click events are only generated if there were no drag
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
    69
    // events between MOUSE_PRESSED and MOUSE_RELEASED for particular button
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
    70
    private static int mouseClickButtons = 0;
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
    71
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    72
    CTrayIcon(TrayIcon target) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    73
        super(0, true);
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
        this.target = target;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    76
        this.popup = target.getPopupMenu();
14157
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
    77
        this.dummyFrame = new Frame();
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    78
        setPtr(createModel());
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
        //if no one else is creating the peer.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    81
        checkAndCreatePopupPeer();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    82
        updateImage();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    83
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    84
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    85
    private CPopupMenu checkAndCreatePopupPeer() {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    86
        CPopupMenu menuPeer = null;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    87
        if (popup != null) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    88
            try {
30469
bac0a7ff7e1e 8074028: Remove API references to java.awt.peer
serb
parents: 28235
diff changeset
    89
                final MenuComponentAccessor acc = getMenuComponentAccessor();
bac0a7ff7e1e 8074028: Remove API references to java.awt.peer
serb
parents: 28235
diff changeset
    90
                menuPeer = acc.getPeer(popup);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    91
                if (menuPeer == null) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    92
                    popup.addNotify();
30469
bac0a7ff7e1e 8074028: Remove API references to java.awt.peer
serb
parents: 28235
diff changeset
    93
                    menuPeer = acc.getPeer(popup);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    94
                }
23247
dd6c58eeb05c 8007220: [macosx] Setting popupmenu on TrayIcon do not work if done *after* adding TrayIcon
pchelko
parents: 18241
diff changeset
    95
            } catch (Exception e) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    96
                e.printStackTrace();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    97
            }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    98
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    99
        return menuPeer;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   100
    }
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
    private long createModel() {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   103
        return nativeCreate();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   104
    }
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
    private native long nativeCreate();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   107
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   108
    //invocation from the AWTTrayIcon.m
37686
1ad88c2dfacd 8147841: [macosx] Updating TrayIcons popup menu does not work on Mac OS X
psadhukhan
parents: 35657
diff changeset
   109
    public long getPopupMenuModel() {
1ad88c2dfacd 8147841: [macosx] Updating TrayIcons popup menu does not work on Mac OS X
psadhukhan
parents: 35657
diff changeset
   110
        PopupMenu newPopup = target.getPopupMenu();
1ad88c2dfacd 8147841: [macosx] Updating TrayIcons popup menu does not work on Mac OS X
psadhukhan
parents: 35657
diff changeset
   111
1ad88c2dfacd 8147841: [macosx] Updating TrayIcons popup menu does not work on Mac OS X
psadhukhan
parents: 35657
diff changeset
   112
        if (popup == newPopup) {
1ad88c2dfacd 8147841: [macosx] Updating TrayIcons popup menu does not work on Mac OS X
psadhukhan
parents: 35657
diff changeset
   113
            if (popup == null) {
1ad88c2dfacd 8147841: [macosx] Updating TrayIcons popup menu does not work on Mac OS X
psadhukhan
parents: 35657
diff changeset
   114
                return 0L;
1ad88c2dfacd 8147841: [macosx] Updating TrayIcons popup menu does not work on Mac OS X
psadhukhan
parents: 35657
diff changeset
   115
            }
1ad88c2dfacd 8147841: [macosx] Updating TrayIcons popup menu does not work on Mac OS X
psadhukhan
parents: 35657
diff changeset
   116
        } else {
1ad88c2dfacd 8147841: [macosx] Updating TrayIcons popup menu does not work on Mac OS X
psadhukhan
parents: 35657
diff changeset
   117
            if (newPopup != null) {
1ad88c2dfacd 8147841: [macosx] Updating TrayIcons popup menu does not work on Mac OS X
psadhukhan
parents: 35657
diff changeset
   118
                if (popup != null) {
1ad88c2dfacd 8147841: [macosx] Updating TrayIcons popup menu does not work on Mac OS X
psadhukhan
parents: 35657
diff changeset
   119
                    popup.removeNotify();
1ad88c2dfacd 8147841: [macosx] Updating TrayIcons popup menu does not work on Mac OS X
psadhukhan
parents: 35657
diff changeset
   120
                    popup = newPopup;
1ad88c2dfacd 8147841: [macosx] Updating TrayIcons popup menu does not work on Mac OS X
psadhukhan
parents: 35657
diff changeset
   121
                } else {
1ad88c2dfacd 8147841: [macosx] Updating TrayIcons popup menu does not work on Mac OS X
psadhukhan
parents: 35657
diff changeset
   122
                    popup = newPopup;
1ad88c2dfacd 8147841: [macosx] Updating TrayIcons popup menu does not work on Mac OS X
psadhukhan
parents: 35657
diff changeset
   123
                }
23247
dd6c58eeb05c 8007220: [macosx] Setting popupmenu on TrayIcon do not work if done *after* adding TrayIcon
pchelko
parents: 18241
diff changeset
   124
            } else {
dd6c58eeb05c 8007220: [macosx] Setting popupmenu on TrayIcon do not work if done *after* adding TrayIcon
pchelko
parents: 18241
diff changeset
   125
                return 0L;
dd6c58eeb05c 8007220: [macosx] Setting popupmenu on TrayIcon do not work if done *after* adding TrayIcon
pchelko
parents: 18241
diff changeset
   126
            }
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   127
        }
37686
1ad88c2dfacd 8147841: [macosx] Updating TrayIcons popup menu does not work on Mac OS X
psadhukhan
parents: 35657
diff changeset
   128
43204
4358b67f2e5a 8164143: Improve components for menu items
serb
parents: 37686
diff changeset
   129
        // This method is executed on Appkit, so if ptr is not zero means that,
4358b67f2e5a 8164143: Improve components for menu items
serb
parents: 37686
diff changeset
   130
        // it is still not deallocated(even if we call NSApp postRunnableEvent)
4358b67f2e5a 8164143: Improve components for menu items
serb
parents: 37686
diff changeset
   131
        // and sent CFRelease to the native queue
4358b67f2e5a 8164143: Improve components for menu items
serb
parents: 37686
diff changeset
   132
        return checkAndCreatePopupPeer().ptr;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   133
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   134
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   135
    /**
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   136
     * We display tray icon message as a small dialog with OK button.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   137
     * This is lame, but JDK 1.6 does basically the same. There is a new
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   138
     * kind of window in Lion, NSPopover, so perhaps it could be used it
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   139
     * to implement better looking notifications.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   140
     */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   141
    public void displayMessage(final String caption, final String text,
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   142
                               final String messageType) {
47508
33da1153954c 8187639: TrayIcon is not properly supported on macOS in multi-screen environment
serb
parents: 47216
diff changeset
   143
        // obtain icon to show along the message
33da1153954c 8187639: TrayIcon is not properly supported on macOS in multi-screen environment
serb
parents: 47216
diff changeset
   144
        Icon icon = getIconForMessageType(messageType);
33da1153954c 8187639: TrayIcon is not properly supported on macOS in multi-screen environment
serb
parents: 47216
diff changeset
   145
        CImage cimage = null;
33da1153954c 8187639: TrayIcon is not properly supported on macOS in multi-screen environment
serb
parents: 47216
diff changeset
   146
        if (icon != null) {
33da1153954c 8187639: TrayIcon is not properly supported on macOS in multi-screen environment
serb
parents: 47216
diff changeset
   147
            BufferedImage image = scaleIcon(icon, 0.75);
50649
9cf90ac8dbf7 8029250: [macosx] There is no tray icon shown in the system tray area when case starts
mhalder
parents: 47508
diff changeset
   148
            cimage = CImage.getCreator().createFromImage(image, null);
47508
33da1153954c 8187639: TrayIcon is not properly supported on macOS in multi-screen environment
serb
parents: 47216
diff changeset
   149
        }
33da1153954c 8187639: TrayIcon is not properly supported on macOS in multi-screen environment
serb
parents: 47216
diff changeset
   150
        if (cimage != null) {
33da1153954c 8187639: TrayIcon is not properly supported on macOS in multi-screen environment
serb
parents: 47216
diff changeset
   151
            cimage.execute(imagePtr -> {
33da1153954c 8187639: TrayIcon is not properly supported on macOS in multi-screen environment
serb
parents: 47216
diff changeset
   152
                execute(ptr -> nativeShowNotification(ptr, caption, text,
33da1153954c 8187639: TrayIcon is not properly supported on macOS in multi-screen environment
serb
parents: 47216
diff changeset
   153
                                                      imagePtr));
33da1153954c 8187639: TrayIcon is not properly supported on macOS in multi-screen environment
serb
parents: 47216
diff changeset
   154
            });
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   155
        } else {
47508
33da1153954c 8187639: TrayIcon is not properly supported on macOS in multi-screen environment
serb
parents: 47216
diff changeset
   156
            execute(ptr -> nativeShowNotification(ptr, caption, text, 0));
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   157
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   158
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   159
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   160
    @Override
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   161
    public void dispose() {
14157
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
   162
        dummyFrame.dispose();
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
   163
23247
dd6c58eeb05c 8007220: [macosx] Setting popupmenu on TrayIcon do not work if done *after* adding TrayIcon
pchelko
parents: 18241
diff changeset
   164
        if (popup != null) {
dd6c58eeb05c 8007220: [macosx] Setting popupmenu on TrayIcon do not work if done *after* adding TrayIcon
pchelko
parents: 18241
diff changeset
   165
            popup.removeNotify();
dd6c58eeb05c 8007220: [macosx] Setting popupmenu on TrayIcon do not work if done *after* adding TrayIcon
pchelko
parents: 18241
diff changeset
   166
        }
dd6c58eeb05c 8007220: [macosx] Setting popupmenu on TrayIcon do not work if done *after* adding TrayIcon
pchelko
parents: 18241
diff changeset
   167
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   168
        LWCToolkit.targetDisposedPeer(target, this);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   169
        target = null;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   170
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   171
        super.dispose();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   172
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   173
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   174
    @Override
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   175
    public void setToolTip(String tooltip) {
44752
97a2817b5a9b 8165626: Improved window framing
serb
parents: 43204
diff changeset
   176
        execute(ptr -> nativeSetToolTip(ptr, tooltip));
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   177
    }
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
    //adds tooltip to the NSStatusBar's NSButton.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   180
    private native void nativeSetToolTip(long trayIconModel, String tooltip);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   181
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   182
    @Override
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   183
    public void showPopupMenu(int x, int y) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   184
        //Not used. The popupmenu is shown from the native code.
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
    @Override
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   188
    public void updateImage() {
50649
9cf90ac8dbf7 8029250: [macosx] There is no tray icon shown in the system tray area when case starts
mhalder
parents: 47508
diff changeset
   189
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   190
        Image image = target.getImage();
50649
9cf90ac8dbf7 8029250: [macosx] There is no tray icon shown in the system tray area when case starts
mhalder
parents: 47508
diff changeset
   191
        if (image != null) {
9cf90ac8dbf7 8029250: [macosx] There is no tray icon shown in the system tray area when case starts
mhalder
parents: 47508
diff changeset
   192
            updateNativeImage(image);
9cf90ac8dbf7 8029250: [macosx] There is no tray icon shown in the system tray area when case starts
mhalder
parents: 47508
diff changeset
   193
        }
9cf90ac8dbf7 8029250: [macosx] There is no tray icon shown in the system tray area when case starts
mhalder
parents: 47508
diff changeset
   194
    }
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   195
50649
9cf90ac8dbf7 8029250: [macosx] There is no tray icon shown in the system tray area when case starts
mhalder
parents: 47508
diff changeset
   196
    void updateNativeImage(Image image) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   197
        MediaTracker tracker = new MediaTracker(new Button(""));
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   198
        tracker.addImage(image, 0);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   199
        try {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   200
            tracker.waitForAll();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   201
        } catch (InterruptedException ignore) { }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   202
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   203
        if (image.getWidth(null) <= 0 ||
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   204
            image.getHeight(null) <= 0)
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;
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
50649
9cf90ac8dbf7 8029250: [macosx] There is no tray icon shown in the system tray area when case starts
mhalder
parents: 47508
diff changeset
   209
        CImage cimage = CImage.getCreator().createFromImage(image, observer);
44752
97a2817b5a9b 8165626: Improved window framing
serb
parents: 43204
diff changeset
   210
        boolean imageAutoSize = target.isImageAutoSize();
53794
c2b366b9c360 8207938: At step6,Click Add button,case failed automatically.
mhalder
parents: 50649
diff changeset
   211
        if (cimage != null) {
c2b366b9c360 8207938: At step6,Click Add button,case failed automatically.
mhalder
parents: 50649
diff changeset
   212
            cimage.execute(imagePtr -> {
c2b366b9c360 8207938: At step6,Click Add button,case failed automatically.
mhalder
parents: 50649
diff changeset
   213
                execute(ptr -> {
c2b366b9c360 8207938: At step6,Click Add button,case failed automatically.
mhalder
parents: 50649
diff changeset
   214
                    setNativeImage(ptr, imagePtr, imageAutoSize);
c2b366b9c360 8207938: At step6,Click Add button,case failed automatically.
mhalder
parents: 50649
diff changeset
   215
                });
44752
97a2817b5a9b 8165626: Improved window framing
serb
parents: 43204
diff changeset
   216
            });
53794
c2b366b9c360 8207938: At step6,Click Add button,case failed automatically.
mhalder
parents: 50649
diff changeset
   217
        }
12047
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
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   220
    private native void setNativeImage(final long model, final long nsimage, final boolean autosize);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   221
14157
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
   222
    private void postEvent(final AWTEvent event) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   223
        SunToolkit.executeOnEventHandlerThread(target, new Runnable() {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   224
            public void run() {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   225
                SunToolkit.postEvent(SunToolkit.targetToAppContext(target), event);
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
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   229
14157
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
   230
    //invocation from the AWTTrayIcon.m
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
   231
    private void handleMouseEvent(NSEvent nsEvent) {
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
   232
        int buttonNumber = nsEvent.getButtonNumber();
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
   233
        final SunToolkit tk = (SunToolkit)Toolkit.getDefaultToolkit();
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
   234
        if ((buttonNumber > 2 && !tk.areExtraMouseButtonsEnabled())
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
   235
                || buttonNumber > tk.getNumberOfButtons() - 1) {
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
   236
            return;
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
   237
        }
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
   238
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
   239
        int jeventType = NSEvent.nsToJavaEventType(nsEvent.getType());
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
   240
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
   241
        int jbuttonNumber = MouseEvent.NOBUTTON;
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
   242
        int jclickCount = 0;
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
   243
        if (jeventType != MouseEvent.MOUSE_MOVED) {
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
   244
            jbuttonNumber = NSEvent.nsToJavaButton(buttonNumber);
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
   245
            jclickCount = nsEvent.getClickCount();
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
   246
        }
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
   247
35657
5ee7a31d7923 8143054: [macosx] KeyEvent modifiers do not contain information about mouse buttons
serb
parents: 30469
diff changeset
   248
        int jmodifiers = NSEvent.nsToJavaModifiers(
14157
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
   249
                nsEvent.getModifierFlags());
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
   250
        boolean isPopupTrigger = NSEvent.isPopupTrigger(jmodifiers);
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
   251
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
   252
        int eventButtonMask = (jbuttonNumber > 0)?
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
   253
                MouseEvent.getMaskForButton(jbuttonNumber) : 0;
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
   254
        long when = System.currentTimeMillis();
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
   255
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
   256
        if (jeventType == MouseEvent.MOUSE_PRESSED) {
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
   257
            mouseClickButtons |= eventButtonMask;
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
   258
        } else if (jeventType == MouseEvent.MOUSE_DRAGGED) {
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
   259
            mouseClickButtons = 0;
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
   260
        }
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
   261
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
   262
        // The MouseEvent's coordinates are relative to screen
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
   263
        int absX = nsEvent.getAbsX();
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
   264
        int absY = nsEvent.getAbsY();
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
   265
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
   266
        MouseEvent mouseEvent = new MouseEvent(dummyFrame, jeventType, when,
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
   267
                jmodifiers, absX, absY, absX, absY, jclickCount, isPopupTrigger,
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
   268
                jbuttonNumber);
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
   269
        mouseEvent.setSource(target);
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
   270
        postEvent(mouseEvent);
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
   271
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
   272
        // fire ACTION event
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
   273
        if (jeventType == MouseEvent.MOUSE_PRESSED && isPopupTrigger) {
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
   274
            final String cmd = target.getActionCommand();
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
   275
            final ActionEvent event = new ActionEvent(target,
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
   276
                    ActionEvent.ACTION_PERFORMED, cmd);
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
   277
            postEvent(event);
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
   278
        }
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
   279
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
   280
        // synthesize CLICKED event
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
   281
        if (jeventType == MouseEvent.MOUSE_RELEASED) {
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
   282
            if ((mouseClickButtons & eventButtonMask) != 0) {
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
   283
                MouseEvent clickEvent = new MouseEvent(dummyFrame,
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
   284
                        MouseEvent.MOUSE_CLICKED, when, jmodifiers, absX, absY,
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
   285
                        absX, absY, jclickCount, isPopupTrigger, jbuttonNumber);
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
   286
                clickEvent.setSource(target);
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
   287
                postEvent(clickEvent);
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
   288
            }
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
   289
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
   290
            mouseClickButtons &= ~eventButtonMask;
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
   291
        }
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
   292
    }
5d50d32dd770 7124321: [macosx] TrayIcon MouseListener is never triggered
leonidr
parents: 12047
diff changeset
   293
47508
33da1153954c 8187639: TrayIcon is not properly supported on macOS in multi-screen environment
serb
parents: 47216
diff changeset
   294
    private native void nativeShowNotification(long trayIconModel,
33da1153954c 8187639: TrayIcon is not properly supported on macOS in multi-screen environment
serb
parents: 47216
diff changeset
   295
                                               String caption, String text,
33da1153954c 8187639: TrayIcon is not properly supported on macOS in multi-screen environment
serb
parents: 47216
diff changeset
   296
                                               long nsimage);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   297
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   298
    /**
47508
33da1153954c 8187639: TrayIcon is not properly supported on macOS in multi-screen environment
serb
parents: 47216
diff changeset
   299
     * Used by the automated tests.
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   300
     */
47508
33da1153954c 8187639: TrayIcon is not properly supported on macOS in multi-screen environment
serb
parents: 47216
diff changeset
   301
    private native Point2D nativeGetIconLocation(long trayIconModel);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   302
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   303
    /**
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   304
     * Scales an icon using specified scale factor
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   305
     *
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   306
     * @param icon        icon to scale
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   307
     * @param scaleFactor scale factor to use
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   308
     * @return scaled icon as BuffedredImage
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
    private static BufferedImage scaleIcon(Icon icon, double scaleFactor) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   311
        if (icon == null) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   312
            return null;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   313
        }
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
        int w = icon.getIconWidth();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   316
        int h = icon.getIconHeight();
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
        GraphicsEnvironment ge =
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   319
                GraphicsEnvironment.getLocalGraphicsEnvironment();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   320
        GraphicsDevice gd = ge.getDefaultScreenDevice();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   321
        GraphicsConfiguration gc = gd.getDefaultConfiguration();
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
        // convert icon into image
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   324
        BufferedImage iconImage = gc.createCompatibleImage(w, h,
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   325
                Transparency.TRANSLUCENT);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   326
        Graphics2D g = iconImage.createGraphics();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   327
        icon.paintIcon(null, g, 0, 0);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   328
        g.dispose();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   329
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   330
        // and scale it nicely
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   331
        int scaledW = (int) (w * scaleFactor);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   332
        int scaledH = (int) (h * scaleFactor);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   333
        BufferedImage scaledImage = gc.createCompatibleImage(scaledW, scaledH,
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   334
                Transparency.TRANSLUCENT);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   335
        g = scaledImage.createGraphics();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   336
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   337
                RenderingHints.VALUE_INTERPOLATION_BILINEAR);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   338
        g.drawImage(iconImage, 0, 0, scaledW, scaledH, null);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   339
        g.dispose();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   340
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   341
        return scaledImage;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   342
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   343
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   344
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
     * Gets Aqua icon used in message dialog.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   347
     */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   348
    private static Icon getIconForMessageType(String messageType) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   349
        if (messageType.equals("ERROR")) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   350
            return UIManager.getIcon("OptionPane.errorIcon");
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   351
        } else if (messageType.equals("WARNING")) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   352
            return UIManager.getIcon("OptionPane.warningIcon");
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   353
        } else {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   354
            // this is just an application icon
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   355
            return UIManager.getIcon("OptionPane.informationIcon");
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
    }
50649
9cf90ac8dbf7 8029250: [macosx] There is no tray icon shown in the system tray area when case starts
mhalder
parents: 47508
diff changeset
   358
9cf90ac8dbf7 8029250: [macosx] There is no tray icon shown in the system tray area when case starts
mhalder
parents: 47508
diff changeset
   359
    class IconObserver implements ImageObserver {
9cf90ac8dbf7 8029250: [macosx] There is no tray icon shown in the system tray area when case starts
mhalder
parents: 47508
diff changeset
   360
        @Override
9cf90ac8dbf7 8029250: [macosx] There is no tray icon shown in the system tray area when case starts
mhalder
parents: 47508
diff changeset
   361
        public boolean imageUpdate(Image image, int flags, int x, int y, int width, int height) {
53794
c2b366b9c360 8207938: At step6,Click Add button,case failed automatically.
mhalder
parents: 50649
diff changeset
   362
            if (target == null || image != target.getImage()) //if the image has been changed
50649
9cf90ac8dbf7 8029250: [macosx] There is no tray icon shown in the system tray area when case starts
mhalder
parents: 47508
diff changeset
   363
            {
9cf90ac8dbf7 8029250: [macosx] There is no tray icon shown in the system tray area when case starts
mhalder
parents: 47508
diff changeset
   364
                return false;
9cf90ac8dbf7 8029250: [macosx] There is no tray icon shown in the system tray area when case starts
mhalder
parents: 47508
diff changeset
   365
            }
9cf90ac8dbf7 8029250: [macosx] There is no tray icon shown in the system tray area when case starts
mhalder
parents: 47508
diff changeset
   366
            if ((flags & (ImageObserver.FRAMEBITS | ImageObserver.ALLBITS |
9cf90ac8dbf7 8029250: [macosx] There is no tray icon shown in the system tray area when case starts
mhalder
parents: 47508
diff changeset
   367
                          ImageObserver.WIDTH | ImageObserver.HEIGHT)) != 0)
9cf90ac8dbf7 8029250: [macosx] There is no tray icon shown in the system tray area when case starts
mhalder
parents: 47508
diff changeset
   368
            {
9cf90ac8dbf7 8029250: [macosx] There is no tray icon shown in the system tray area when case starts
mhalder
parents: 47508
diff changeset
   369
                SunToolkit.executeOnEventHandlerThread(target, new Runnable() {
9cf90ac8dbf7 8029250: [macosx] There is no tray icon shown in the system tray area when case starts
mhalder
parents: 47508
diff changeset
   370
                            public void run() {
9cf90ac8dbf7 8029250: [macosx] There is no tray icon shown in the system tray area when case starts
mhalder
parents: 47508
diff changeset
   371
                                updateNativeImage(image);
9cf90ac8dbf7 8029250: [macosx] There is no tray icon shown in the system tray area when case starts
mhalder
parents: 47508
diff changeset
   372
                            }
9cf90ac8dbf7 8029250: [macosx] There is no tray icon shown in the system tray area when case starts
mhalder
parents: 47508
diff changeset
   373
                        });
9cf90ac8dbf7 8029250: [macosx] There is no tray icon shown in the system tray area when case starts
mhalder
parents: 47508
diff changeset
   374
            }
9cf90ac8dbf7 8029250: [macosx] There is no tray icon shown in the system tray area when case starts
mhalder
parents: 47508
diff changeset
   375
            return (flags & ImageObserver.ALLBITS) == 0;
9cf90ac8dbf7 8029250: [macosx] There is no tray icon shown in the system tray area when case starts
mhalder
parents: 47508
diff changeset
   376
        }
9cf90ac8dbf7 8029250: [macosx] There is no tray icon shown in the system tray area when case starts
mhalder
parents: 47508
diff changeset
   377
    }
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   378
}
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   379