jdk/src/java.desktop/unix/classes/sun/awt/X11/XTaskbarPeer.java
changeset 36903 addc2e9d4bd4
child 37550 c8252b8fea3d
equal deleted inserted replaced
36902:bb30d89aa00e 36903:addc2e9d4bd4
       
     1 /*
       
     2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 package sun.awt.X11;
       
    26 
       
    27 import java.awt.MenuItem;
       
    28 import java.awt.PopupMenu;
       
    29 import java.awt.Taskbar.Feature;
       
    30 import java.awt.peer.TaskbarPeer;
       
    31 import java.awt.event.ActionEvent;
       
    32 import sun.misc.ManagedLocalsThread;
       
    33 import java.security.AccessController;
       
    34 import sun.security.action.GetPropertyAction;
       
    35 
       
    36 final class XTaskbarPeer implements TaskbarPeer {
       
    37 
       
    38     private static boolean nativeLibraryLoaded = false;
       
    39     private static boolean initExecuted = false;
       
    40 
       
    41     private PopupMenu menu = null;
       
    42 
       
    43     private static void initWithLock() {
       
    44         XToolkit.awtLock();
       
    45         try {
       
    46             if (!initExecuted) {
       
    47                 String dname = AccessController.doPrivileged(
       
    48                                 new GetPropertyAction("java.desktop.appName", ""));
       
    49                 nativeLibraryLoaded = init(dname);
       
    50                 if (nativeLibraryLoaded) {
       
    51                     ManagedLocalsThread t
       
    52                             = new ManagedLocalsThread(() -> {
       
    53                                 runloop();
       
    54                             });
       
    55                     t.setDaemon(true);
       
    56                     t.start();
       
    57                 }
       
    58             }
       
    59         } finally {
       
    60             initExecuted = true;
       
    61             XToolkit.awtUnlock();
       
    62         }
       
    63     }
       
    64 
       
    65     XTaskbarPeer() {
       
    66         initWithLock();
       
    67     }
       
    68 
       
    69     static boolean isTaskbarSupported() {
       
    70         initWithLock();
       
    71         return nativeLibraryLoaded;
       
    72     }
       
    73 
       
    74     @Override
       
    75     public boolean isSupported(Feature feature) {
       
    76         switch (feature) {
       
    77             case ICON_BADGE_NUMBER:
       
    78             case MENU:
       
    79             case PROGRESS_VALUE:
       
    80             case USER_ATTENTION:
       
    81                 return true;
       
    82             default:
       
    83                 return false;
       
    84         }
       
    85     }
       
    86 
       
    87     @Override
       
    88     public void setProgressValue(int value) {
       
    89         boolean visible
       
    90                 = value >= 0
       
    91                 && value <= 100;
       
    92 
       
    93         double v = visible
       
    94                 ? (double) value / 100
       
    95                 : 0d;
       
    96 
       
    97         updateProgress(v, visible);
       
    98     }
       
    99 
       
   100     @Override
       
   101     public void setIconBadge(String badge) {
       
   102         boolean visible = false;
       
   103         long val = 0;
       
   104         if (badge != null) {
       
   105             try {
       
   106                 val = Long.parseLong(badge);
       
   107                 visible = true;
       
   108             } catch (NumberFormatException e) {
       
   109             }
       
   110         }
       
   111         setBadge(val, visible);
       
   112     }
       
   113 
       
   114     @Override
       
   115     public PopupMenu getMenu() {
       
   116         return menu;
       
   117     }
       
   118 
       
   119     @Override
       
   120     public synchronized void setMenu(PopupMenu m) {
       
   121         this.menu = m;
       
   122 
       
   123         if (menu != null && menu.getItemCount() > 0) {
       
   124             int msize = menu.getItemCount();
       
   125             MenuItem[] items = new MenuItem[msize];
       
   126             for (int i = 0; i < msize; i++) {
       
   127                 items[i] = menu.getItem(i);
       
   128             }
       
   129             setNativeMenu(items);
       
   130         } else {
       
   131             setNativeMenu(null);
       
   132         }
       
   133     }
       
   134 
       
   135     @Override
       
   136     public void requestUserAttention(boolean enabled, boolean critical) {
       
   137         setUrgent(enabled);
       
   138     }
       
   139 
       
   140     private static void menuItemCallback(MenuItem mi) {
       
   141         if (mi != null) {
       
   142             ActionEvent ae = new ActionEvent(mi, ActionEvent.ACTION_PERFORMED,
       
   143                     mi.getActionCommand());
       
   144             try {
       
   145                 XToolkit.awtLock();
       
   146                 XToolkit.postEvent(XToolkit.targetToAppContext(ae.getSource()), ae);
       
   147             } finally {
       
   148                 XToolkit.awtUnlock();
       
   149             }
       
   150         }
       
   151     }
       
   152 
       
   153     private static native boolean init(String name);
       
   154 
       
   155     private static native void runloop();
       
   156 
       
   157     private native void setBadge(long value, boolean visible);
       
   158 
       
   159     private native void updateProgress(double value, boolean visible);
       
   160 
       
   161     private native void setUrgent(boolean urgent);
       
   162 
       
   163     private native void setNativeMenu(MenuItem[] items);
       
   164 }