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 
  26 package java.awt;
  27 
  28 import java.awt.peer.TaskbarPeer;
  29 import sun.awt.SunToolkit;
  30 
  31 /**
  32  * The {@code Taskbar} class allows a Java application to interact with
  33  * the system task area (taskbar, Dock, etc.).
  34  *
  35  * <p>
  36  * There are a variety of interactions depending on the current platform such as
  37  * displaying progress of some task, appending user-specified menu to the application
  38  * icon context menu, etc.
  39  *
  40  * @implNote Linux support is currently limited to Unity. However to make these
  41  * features work on Unity, the app should be run from a .desktop file with
  42  * specified {@code java.desktop.appName} system property set to this .desktop
  43  * file name:
  44  * {@code Exec=java -Djava.desktop.appName=MyApp.desktop -jar /path/to/myapp.jar}
  45  * see <a href="https://help.ubuntu.com/community/UnityLaunchersAndDesktopFiles">
  46  * https://help.ubuntu.com/community/UnityLaunchersAndDesktopFiles</a>
  47  *
  48  * @since 9
  49  */
  50 
  51 public class Taskbar {
  52 
  53     /**
  54      * List of provided features. Each platform supports a different
  55      * set of features.  You may use the {@link Taskbar#isSupported}
  56      * method to determine if the given feature is supported by the
  57      * current platform.
  58      */
  59     public static enum Feature {
  60 
  61         /**
  62          * Represents a textual icon badge feature.
  63          * @see #setIconBadge(java.lang.String)
  64          */
  65         ICON_BADGE_TEXT,
  66 
  67         /**
  68          * Represents a numerical icon badge feature.
  69          * @see #setIconBadge(java.lang.String)
  70          */
  71         ICON_BADGE_NUMBER,
  72 
  73         /**
  74          * Represents a graphical icon badge feature for a window.
  75          * @see #setWindowIconBadge(java.awt.Window, java.awt.Image)
  76          */
  77         ICON_BADGE_IMAGE_WINDOW,
  78 
  79         /**
  80          * Represents an icon feature.
  81          * @see #setIconImage(java.awt.Image)
  82          */
  83         ICON_IMAGE,
  84 
  85         /**
  86          * Represents a menu feature.
  87          * @see #setMenu(java.awt.PopupMenu)
  88          * @see #getMenu()
  89          */
  90         MENU,
  91 
  92         /**
  93          * Represents a progress state feature for a specified window.
  94          * @see #setWindowProgressState(java.awt.Window, State)
  95          */
  96         PROGRESS_STATE_WINDOW,
  97 
  98         /**
  99          * Represents a progress value feature.
 100          * @see #setProgressValue(int)
 101          */
 102         PROGRESS_VALUE,
 103 
 104         /**
 105          * Represents a progress value feature for a specified window.
 106          * @see #setWindowProgressValue(java.awt.Window, int)
 107          */
 108         PROGRESS_VALUE_WINDOW,
 109 
 110         /**
 111          * Represents a user attention request feature.
 112          * @see #requestUserAttention(boolean, boolean)
 113          */
 114         USER_ATTENTION,
 115 
 116         /**
 117          * Represents a user attention request feature for a specified window.
 118          * @see #requestWindowUserAttention(java.awt.Window)
 119          */
 120         USER_ATTENTION_WINDOW
 121     }
 122 
 123     /**
 124      * Kinds of available window progress states.
 125      *
 126      * @see #setWindowProgressState(java.awt.Window, java.awt.Taskbar.State)
 127      */
 128     public static enum State {
 129         /**
 130          * Stops displaying the progress.
 131          */
 132         OFF,
 133         /**
 134          * The progress indicator displays with normal color and determinate
 135          * mode.
 136          */
 137         NORMAL,
 138         /**
 139          * Shows progress as paused, progress can be resumed by the user.
 140          * Switches to the determinate display.
 141          */
 142         PAUSED,
 143         /**
 144          * The progress indicator displays activity without specifying what
 145          * proportion of the progress is complete.
 146          */
 147         INDETERMINATE,
 148         /**
 149          * Shows that an error has occurred. Switches to the determinate
 150          * display.
 151          */
 152         ERROR
 153     }
 154 
 155     private TaskbarPeer peer;
 156 
 157     /**
 158      * Tests whether a {@code Feature} is supported on the current platform.
 159      * @param feature the specified {@link Feature}
 160      * @return true if the specified feature is supported on the current platform
 161      */
 162     public boolean isSupported(Feature feature) {
 163         return peer.isSupported(feature);
 164     }
 165 
 166     /**
 167      * Checks if the feature type is supported.
 168      *
 169      * @param featureType the action type in question
 170      * @throws UnsupportedOperationException if the specified action type is not
 171      *         supported on the current platform
 172      */
 173     private void checkFeatureSupport(Feature featureType){
 174         if (!isSupported(featureType)) {
 175             throw new UnsupportedOperationException("The " + featureType.name()
 176                     + " feature is not supported on the current platform!");
 177         }
 178     }
 179 
 180     /**
 181      *  Calls to the security manager's {@code checkPermission} method with
 182      *  an {@code RuntimePermission("canProcessApplicationEvents")} permissions.
 183      */
 184     private void checkEventsProcessingPermission(){
 185         SecurityManager sm = System.getSecurityManager();
 186         if (sm != null) {
 187             sm.checkPermission(new RuntimePermission(
 188                     "canProcessApplicationEvents"));
 189         }
 190     }
 191 
 192     private Taskbar() {
 193         Toolkit defaultToolkit = Toolkit.getDefaultToolkit();
 194         if (defaultToolkit instanceof SunToolkit) {
 195             peer = ((SunToolkit) defaultToolkit).createTaskbarPeer(this);
 196         }
 197     }
 198 
 199     private static Taskbar taskbar;
 200 
 201     /**
 202      * Returns the {@code Taskbar} instance of the current
 203      * taskbar context.  On some platforms the Taskbar API may not be
 204      * supported; use the {@link #isTaskbarSupported} method to
 205      * determine if the current taskbar is supported.
 206      * @return the Taskbar instance
 207      * @throws HeadlessException if {@link
 208      * GraphicsEnvironment#isHeadless()} returns {@code true}
 209      * @throws UnsupportedOperationException if this class is not
 210      * supported on the current platform
 211      * @see #isTaskbarSupported()
 212      * @see java.awt.GraphicsEnvironment#isHeadless
 213      */
 214     public static synchronized Taskbar getTaskbar(){
 215         if (GraphicsEnvironment.isHeadless()) throw new HeadlessException();
 216 
 217         if (!Taskbar.isTaskbarSupported()) {
 218             throw new UnsupportedOperationException("Taskbar API is not " +
 219                                                     "supported on the current platform");
 220         }
 221 
 222         Taskbar taskbar = null;
 223         sun.awt.AppContext context = sun.awt.AppContext.getAppContext();
 224 
 225         taskbar = (context == null)
 226                 ? Taskbar.taskbar
 227                 : (Taskbar)context.get(Taskbar.class);
 228 
 229         if (taskbar == null) {
 230             taskbar = new Taskbar();
 231             if (context != null) {
 232                 context.put(Taskbar.class, taskbar);
 233             } else {
 234                 Taskbar.taskbar = taskbar;
 235             }
 236         }
 237 
 238         return taskbar;
 239     }
 240 
 241     /**
 242      * Tests whether this class is supported on the current platform.
 243      * If it's supported, use {@link #getTaskbar()} to retrieve an
 244      * instance.
 245      *
 246      * @return {@code true} if this class is supported on the
 247      *         current platform; {@code false} otherwise
 248      * @see #getTaskbar()
 249      */
 250     public static boolean isTaskbarSupported(){
 251         Toolkit defaultToolkit = Toolkit.getDefaultToolkit();
 252         if (defaultToolkit instanceof SunToolkit) {
 253             return ((SunToolkit)defaultToolkit).isTaskbarSupported();
 254         }
 255         return false;
 256     }
 257 
 258     /**
 259      * Requests user attention to this application.
 260      *
 261      * Depending on the platform, this may be visually indicated by a bouncing
 262      * or flashing icon in the task area. It may have no effect on an already active
 263      * application.
 264      *
 265      * On some platforms (e.g. Mac OS) this effect may disappear upon app activation
 266      * and cannot be dismissed by setting {@code enabled} to false.
 267      * Other platforms may require an additional call
 268      * {@link #requestUserAttention} to dismiss this request
 269      * with {@code enabled} parameter set to false.
 270      *
 271      * @param enabled disables this request if false
 272      * @param critical if this is an important request
 273      * @throws SecurityException if a security manager exists and it denies the
 274      * {@code RuntimePermission("canProcessApplicationEvents")} permission.
 275      * @throws UnsupportedOperationException if the current platform
 276      * does not support the {@link Taskbar.Feature#USER_ATTENTION} feature
 277      */
 278     public void requestUserAttention(final boolean enabled, final boolean critical) {
 279         checkEventsProcessingPermission();
 280         checkFeatureSupport(Feature.USER_ATTENTION);
 281         peer.requestUserAttention(enabled, critical);
 282     }
 283 
 284     /**
 285      * Requests user attention to the specified window.
 286      *
 287      * @param w window
 288      * @throws SecurityException if a security manager exists and it denies the
 289      * {@code RuntimePermission("canProcessApplicationEvents")} permission.
 290      * @throws UnsupportedOperationException if the current platform
 291      * does not support the {@link Taskbar.Feature#USER_ATTENTION_WINDOW} feature
 292      */
 293     public void requestWindowUserAttention(Window w) {
 294         checkEventsProcessingPermission();
 295         checkFeatureSupport(Feature.USER_ATTENTION_WINDOW);
 296         peer.requestWindowUserAttention(w);
 297     }
 298 
 299     /**
 300      * Attaches the contents of the provided PopupMenu to the application icon
 301      * in the task area.
 302      *
 303      * @param menu the PopupMenu to attach to this application
 304      * @throws SecurityException if a security manager exists and it denies the
 305      * {@code RuntimePermission("canProcessApplicationEvents")} permission.
 306      * @throws UnsupportedOperationException if the current platform
 307      * does not support the {@link Taskbar.Feature#MENU} feature
 308      */
 309     public void setMenu(final PopupMenu menu) {
 310         checkEventsProcessingPermission();
 311         checkFeatureSupport(Feature.MENU);
 312         peer.setMenu(menu);
 313     }
 314 
 315     /**
 316      * Gets PopupMenu used to add items to this application's icon in system task area.
 317      *
 318      * @return the PopupMenu
 319      * @throws SecurityException if a security manager exists and it denies the
 320      * {@code RuntimePermission("canProcessApplicationEvents")} permission.
 321      * @throws UnsupportedOperationException if the current platform
 322      * does not support the {@link Taskbar.Feature#MENU} feature
 323      */
 324     public PopupMenu getMenu() {
 325         checkEventsProcessingPermission();
 326         checkFeatureSupport(Feature.MENU);
 327         return peer.getMenu();
 328     }
 329 
 330     /**
 331      * Changes this application's icon to the provided image.
 332      *
 333      * @param image to change
 334      * @throws SecurityException if a security manager exists and it denies the
 335      * {@code RuntimePermission("canProcessApplicationEvents")} permission.
 336      * @throws UnsupportedOperationException if the current platform
 337      * does not support the {@link Taskbar.Feature#ICON_IMAGE} feature
 338      */
 339     public void setIconImage(final Image image) {
 340         checkEventsProcessingPermission();
 341         checkFeatureSupport(Feature.ICON_IMAGE);
 342         peer.setIconImage(image);
 343     }
 344 
 345     /**
 346      * Obtains an image of this application's icon.
 347      *
 348      * @return an image of this application's icon
 349      * @throws SecurityException if a security manager exists and it denies the
 350      * {@code RuntimePermission("canProcessApplicationEvents")} permission.
 351      * @throws UnsupportedOperationException if the current platform
 352      * does not support the {@link Taskbar.Feature#ICON_IMAGE} feature
 353      */
 354     public Image getIconImage() {
 355         checkEventsProcessingPermission();
 356         checkFeatureSupport(Feature.ICON_IMAGE);
 357         return peer.getIconImage();
 358     }
 359 
 360     /**
 361      * Affixes a small system-provided badge to this application's icon.
 362      * Usually a number.
 363      *
 364      * Some platforms do not support string values and accept only integer
 365      * values. In this case, pass an integer represented as a string as parameter.
 366      * This can be tested by {@code Feature.ICON_BADGE_TEXT} and
 367      * {@code Feature.ICON_BADGE_NUMBER}.
 368      *
 369      * Passing {@code null} as parameter hides the badge.
 370      * @param badge label to affix to the icon
 371      * @throws SecurityException if a security manager exists and it denies the
 372      * {@code RuntimePermission("canProcessApplicationEvents")} permission.
 373      * @throws UnsupportedOperationException if the current platform
 374      * does not support the {@link Taskbar.Feature#ICON_BADGE_NUMBER}
 375      * or {@link Taskbar.Feature#ICON_BADGE_TEXT} feature
 376      */
 377     public void setIconBadge(final String badge) {
 378         checkEventsProcessingPermission();
 379         checkFeatureSupport(Feature.ICON_BADGE_NUMBER);
 380         peer.setIconBadge(badge);
 381     }
 382 
 383     /**
 384      * Affixes a small badge to this application's icon in the task area
 385      * for the specified window.
 386      * It may be disabled by system settings.
 387      *
 388      * @param w window to update
 389      * @param badge image to affix to the icon
 390      * @throws SecurityException if a security manager exists and it denies the
 391      * {@code RuntimePermission("canProcessApplicationEvents")} permission.
 392      * @throws UnsupportedOperationException if the current platform
 393      * does not support the {@link Taskbar.Feature#ICON_BADGE_IMAGE_WINDOW} feature
 394      */
 395     public void setWindowIconBadge(Window w, final Image badge) {
 396         checkEventsProcessingPermission();
 397         checkFeatureSupport(Feature.ICON_BADGE_IMAGE_WINDOW);
 398         if (w != null) {
 399             peer.setWindowIconBadge(w, badge);
 400         }
 401     }
 402 
 403 
 404     /**
 405      * Affixes a small system-provided progress bar to this application's icon.
 406      *
 407      * @param value from 0 to 100, other to disable progress indication
 408      * @throws SecurityException if a security manager exists and it denies the
 409      * {@code RuntimePermission("canProcessApplicationEvents")} permission.
 410      * @throws UnsupportedOperationException if the current platform
 411      * does not support the {@link Taskbar.Feature#PROGRESS_VALUE} feature
 412      */
 413     public void setProgressValue(int value) {
 414         checkEventsProcessingPermission();
 415         checkFeatureSupport(Feature.PROGRESS_VALUE);
 416         peer.setProgressValue(value);
 417     }
 418 
 419     /**
 420      * Displays a determinate progress bar in the task area for the specified
 421      * window.
 422      * <br>
 423      * The visual behavior is platform and {@link State} dependent.
 424      * <br>
 425      * This call cancels the {@link State#INDETERMINATE INDETERMINATE} state
 426      * of the window.
 427      * <br>
 428      * Note that when multiple windows is grouped in the task area
 429      * the behavior is platform specific.
 430      *
 431      * @param w window to update
 432      * @param value from 0 to 100, other to switch to {@link State#OFF} state
 433      *              and disable progress indication
 434      * @see #setWindowProgressState(java.awt.Window, State)
 435      * @throws SecurityException if a security manager exists and it denies the
 436      * {@code RuntimePermission("canProcessApplicationEvents")} permission.
 437      * @throws UnsupportedOperationException if the current platform
 438      * does not support the {@link Taskbar.Feature#PROGRESS_VALUE_WINDOW} feature
 439      */
 440     public void setWindowProgressValue(Window w, int value) {
 441         checkEventsProcessingPermission();
 442         checkFeatureSupport(Feature.PROGRESS_VALUE_WINDOW);
 443         if (w != null) {
 444             peer.setWindowProgressValue(w, value);
 445         }
 446     }
 447 
 448     /**
 449      * Sets a progress state for a specified window.
 450      * <br>
 451      * Each state displays a progress in a platform-dependent way.
 452      * <br>
 453      * Note than switching from {@link State#INDETERMINATE INDETERMINATE} state
 454      * to any of determinate states may reset value set by
 455      * {@link #setWindowProgressValue(java.awt.Window, int) setWindowProgressValue}
 456      *
 457      * @param w window
 458      * @param state to change to
 459      * @see State#OFF
 460      * @see State#NORMAL
 461      * @see State#PAUSED
 462      * @see State#ERROR
 463      * @see State#INDETERMINATE
 464      * @see #setWindowProgressValue(java.awt.Window, int)
 465      * @throws SecurityException if a security manager exists and it denies the
 466      * {@code RuntimePermission("canProcessApplicationEvents")} permission.
 467      * @throws UnsupportedOperationException if the current platform
 468      * does not support the {@link Taskbar.Feature#PROGRESS_STATE_WINDOW} feature
 469      */
 470     public void setWindowProgressState(Window w, State state) {
 471         checkEventsProcessingPermission();
 472         checkFeatureSupport(Feature.PROGRESS_STATE_WINDOW);
 473         if (w != null) {
 474             peer.setWindowProgressState(w, state);
 475         }
 476     }
 477 }