jdk/src/solaris/classes/sun/awt/X11/XSystemTrayPeer.java
author ant
Fri, 22 Feb 2013 15:13:13 +0400
changeset 15983 26a673dec5b2
parent 13604 31089af1a447
child 16839 d0f2e97b7359
permissions -rw-r--r--
8006406: lightweight embedding in other Java UI toolkits Reviewed-by: serb, anthony, art
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3938
diff changeset
     2
 * Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3938
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3938
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3938
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3938
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3938
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.awt.X11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.peer.SystemTrayPeer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import sun.awt.SunToolkit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import sun.awt.AppContext;
13604
31089af1a447 7163201: Simplify toolkit internals references
bagiras
parents: 5506
diff changeset
    32
import sun.awt.AWTAccessor;
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 439
diff changeset
    33
import sun.util.logging.PlatformLogger;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
public class XSystemTrayPeer implements SystemTrayPeer, XMSelectionListener {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 439
diff changeset
    36
    private static final PlatformLogger log = PlatformLogger.getLogger("sun.awt.X11.XSystemTrayPeer");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    SystemTray target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    static XSystemTrayPeer peerInstance; // there is only one SystemTray peer per application
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private volatile boolean available;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private final XMSelection selection = new XMSelection("_NET_SYSTEM_TRAY");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private static final int SCREEN = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private static final String SYSTEM_TRAY_PROPERTY_NAME = "systemTray";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private static final XAtom _NET_SYSTEM_TRAY = XAtom.get("_NET_SYSTEM_TRAY_S" + SCREEN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private static final XAtom _XEMBED_INFO = XAtom.get("_XEMBED_INFO");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private static final XAtom _NET_SYSTEM_TRAY_OPCODE = XAtom.get("_NET_SYSTEM_TRAY_OPCODE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private static final XAtom _NET_WM_ICON = XAtom.get("_NET_WM_ICON");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private static final long SYSTEM_TRAY_REQUEST_DOCK = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    XSystemTrayPeer(SystemTray target) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        this.target = target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        peerInstance = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        selection.addSelectionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        long selection_owner = selection.getOwner(SCREEN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        available = (selection_owner != XConstants.None);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        log.fine(" check if system tray is available. selection owner: " + selection_owner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    public void ownerChanged(int screen, XMSelection sel, long newOwner, long data, long timestamp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        if (screen != SCREEN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        if (!available) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            available = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            firePropertyChange(SYSTEM_TRAY_PROPERTY_NAME, null, target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            removeTrayPeers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        createTrayPeers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public void ownerDeath(int screen, XMSelection sel, long deadOwner) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        if (screen != SCREEN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        if (available) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            available = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            firePropertyChange(SYSTEM_TRAY_PROPERTY_NAME, target, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            removeTrayPeers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    public void selectionChanged(int screen, XMSelection sel, long owner, XPropertyEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public Dimension getTrayIconSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        return new Dimension(XTrayIconPeer.TRAY_ICON_HEIGHT, XTrayIconPeer.TRAY_ICON_WIDTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    boolean isAvailable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        return available;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    void dispose() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        selection.removeSelectionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    // ***********************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    // ***********************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    void addTrayIcon(XTrayIconPeer tiPeer) throws AWTException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        long selection_owner = selection.getOwner(SCREEN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        log.fine(" send SYSTEM_TRAY_REQUEST_DOCK message to owner: " + selection_owner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        if (selection_owner == XConstants.None) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            throw new AWTException("TrayIcon couldn't be displayed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        long tray_window = tiPeer.getWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        long data[] = new long[] {XEmbedHelper.XEMBED_VERSION, XEmbedHelper.XEMBED_MAPPED};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        long data_ptr = Native.card32ToData(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        _XEMBED_INFO.setAtomData(tray_window, data_ptr, data.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        sendMessage(selection_owner, SYSTEM_TRAY_REQUEST_DOCK, tray_window, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    void sendMessage(long win, long msg, long data1, long data2, long data3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        XClientMessageEvent xev = new XClientMessageEvent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        try {
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 2
diff changeset
   128
            xev.set_type(XConstants.ClientMessage);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            xev.set_window(win);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            xev.set_format(32);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            xev.set_message_type(_NET_SYSTEM_TRAY_OPCODE.getAtom());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            xev.set_data(0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            xev.set_data(1, msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            xev.set_data(2, data1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            xev.set_data(3, data2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            xev.set_data(4, data3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            XToolkit.awtLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                XlibWrapper.XSendEvent(XToolkit.getDisplay(), win, false,
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 2
diff changeset
   141
                                       XConstants.NoEventMask, xev.pData);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                XToolkit.awtUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            xev.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    static XSystemTrayPeer getPeerInstance() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        return peerInstance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
13604
31089af1a447 7163201: Simplify toolkit internals references
bagiras
parents: 5506
diff changeset
   154
    private void firePropertyChange(final String propertyName,
31089af1a447 7163201: Simplify toolkit internals references
bagiras
parents: 5506
diff changeset
   155
                                    final Object oldValue,
31089af1a447 7163201: Simplify toolkit internals references
bagiras
parents: 5506
diff changeset
   156
                                    final Object newValue) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        Runnable runnable = new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                public void run() {
13604
31089af1a447 7163201: Simplify toolkit internals references
bagiras
parents: 5506
diff changeset
   159
                    AWTAccessor.getSystemTrayAccessor()
31089af1a447 7163201: Simplify toolkit internals references
bagiras
parents: 5506
diff changeset
   160
                        .firePropertyChange(target, propertyName, oldValue, newValue);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        invokeOnEachAppContext(runnable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    private void createTrayPeers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        Runnable runnable = new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                    TrayIcon[] icons = target.getTrayIcons();
13604
31089af1a447 7163201: Simplify toolkit internals references
bagiras
parents: 5506
diff changeset
   170
                    try {
31089af1a447 7163201: Simplify toolkit internals references
bagiras
parents: 5506
diff changeset
   171
                        for (TrayIcon ti : icons) {
31089af1a447 7163201: Simplify toolkit internals references
bagiras
parents: 5506
diff changeset
   172
                            AWTAccessor.getTrayIconAccessor().addNotify(ti);
31089af1a447 7163201: Simplify toolkit internals references
bagiras
parents: 5506
diff changeset
   173
                        }
31089af1a447 7163201: Simplify toolkit internals references
bagiras
parents: 5506
diff changeset
   174
                    } catch (AWTException e) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        invokeOnEachAppContext(runnable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
13604
31089af1a447 7163201: Simplify toolkit internals references
bagiras
parents: 5506
diff changeset
   181
    private void removeTrayPeers() {
31089af1a447 7163201: Simplify toolkit internals references
bagiras
parents: 5506
diff changeset
   182
        Runnable runnable = new Runnable() {
31089af1a447 7163201: Simplify toolkit internals references
bagiras
parents: 5506
diff changeset
   183
                public void run() {
31089af1a447 7163201: Simplify toolkit internals references
bagiras
parents: 5506
diff changeset
   184
                    TrayIcon[] icons = target.getTrayIcons();
31089af1a447 7163201: Simplify toolkit internals references
bagiras
parents: 5506
diff changeset
   185
                    for (TrayIcon ti : icons) {
31089af1a447 7163201: Simplify toolkit internals references
bagiras
parents: 5506
diff changeset
   186
                        AWTAccessor.getTrayIconAccessor().removeNotify(ti);
31089af1a447 7163201: Simplify toolkit internals references
bagiras
parents: 5506
diff changeset
   187
                    }
31089af1a447 7163201: Simplify toolkit internals references
bagiras
parents: 5506
diff changeset
   188
                }
31089af1a447 7163201: Simplify toolkit internals references
bagiras
parents: 5506
diff changeset
   189
            };
31089af1a447 7163201: Simplify toolkit internals references
bagiras
parents: 5506
diff changeset
   190
        invokeOnEachAppContext(runnable);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    private void invokeOnEachAppContext(Runnable runnable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        for (AppContext appContext : AppContext.getAppContexts()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            SunToolkit.invokeLaterOnAppContext(appContext, runnable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
}