6737722: api/java_awt/TrayIcon/index.html#TrayIconHeadlessMode
authordcherepanov
Mon, 25 Aug 2008 19:15:37 +0400
changeset 1191 f142c1da78c2
parent 1190 f27065d0d9f0
child 1192 715cf9378c53
child 1954 b93b85df3211
6737722: api/java_awt/TrayIcon/index.html#TrayIconHeadlessMode Summary: isSupported() should skip tray initialization in case of headless Reviewed-by: art, ant
jdk/src/share/classes/java/awt/SystemTray.java
jdk/src/share/classes/java/awt/TrayIcon.java
jdk/test/java/awt/Toolkit/HeadlessTray/HeadlessTray.java
--- a/jdk/src/share/classes/java/awt/SystemTray.java	Thu Aug 14 12:58:51 2008 +0400
+++ b/jdk/src/share/classes/java/awt/SystemTray.java	Mon Aug 25 19:15:37 2008 +0400
@@ -1,5 +1,5 @@
 /*
- * Copyright 2005-2007 Sun Microsystems, Inc.  All Rights Reserved.
+ * Copyright 2005-2008 Sun Microsystems, Inc.  All Rights Reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -203,16 +203,18 @@
      * functionality is supported for the current platform
      */
     public static boolean isSupported() {
-        initializeSystemTrayIfNeeded();
-
         Toolkit toolkit = Toolkit.getDefaultToolkit();
-
         if (toolkit instanceof SunToolkit) {
+            // connecting tray to native resource
+            initializeSystemTrayIfNeeded();
             return ((SunToolkit)toolkit).isTraySupported();
         } else if (toolkit instanceof HeadlessToolkit) {
+            // skip initialization as the init routine
+            // throws HeadlessException
             return ((HeadlessToolkit)toolkit).isTraySupported();
+        } else {
+            return false;
         }
-        return false;
     }
 
     /**
@@ -476,7 +478,12 @@
 
     synchronized void addNotify() {
         if (peer == null) {
-            peer = ((SunToolkit)Toolkit.getDefaultToolkit()).createSystemTray(this);
+            Toolkit toolkit = Toolkit.getDefaultToolkit();
+            if (toolkit instanceof SunToolkit) {
+                peer = ((SunToolkit)Toolkit.getDefaultToolkit()).createSystemTray(this);
+            } else if (toolkit instanceof HeadlessToolkit) {
+                peer = ((HeadlessToolkit)Toolkit.getDefaultToolkit()).createSystemTray(this);
+            }
         }
     }
 
--- a/jdk/src/share/classes/java/awt/TrayIcon.java	Thu Aug 14 12:58:51 2008 +0400
+++ b/jdk/src/share/classes/java/awt/TrayIcon.java	Mon Aug 25 19:15:37 2008 +0400
@@ -1,5 +1,5 @@
 /*
- * Copyright 2005-2006 Sun Microsystems, Inc.  All Rights Reserved.
+ * Copyright 2005-2008 Sun Microsystems, Inc.  All Rights Reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -38,6 +38,7 @@
 import java.awt.peer.TrayIconPeer;
 import sun.awt.AppContext;
 import sun.awt.SunToolkit;
+import sun.awt.HeadlessToolkit;
 import java.util.EventObject;
 
 /**
@@ -659,7 +660,12 @@
     {
         synchronized (this) {
             if (peer == null) {
-                peer = ((SunToolkit)Toolkit.getDefaultToolkit()).createTrayIcon(this);
+                Toolkit toolkit = Toolkit.getDefaultToolkit();
+                if (toolkit instanceof SunToolkit) {
+                    peer = ((SunToolkit)Toolkit.getDefaultToolkit()).createTrayIcon(this);
+                } else if (toolkit instanceof HeadlessToolkit) {
+                    peer = ((HeadlessToolkit)Toolkit.getDefaultToolkit()).createTrayIcon(this);
+                }
             }
         }
         peer.setToolTip(tooltip);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/awt/Toolkit/HeadlessTray/HeadlessTray.java	Mon Aug 25 19:15:37 2008 +0400
@@ -0,0 +1,49 @@
+/*
+ * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+  @test
+  @bug 6737722
+  @summary no tray support in headless mode
+  @author dmitry.cherepanov: area=awt.headless
+  @run main HeadlessTray
+*/
+
+import java.awt.*;
+
+public class HeadlessTray
+{
+    public static void main (String args[]) {
+
+        System.setProperty("java.awt.headless", "true");
+
+        // We expect the method returns false and no exception thrown
+        boolean isSupported = SystemTray.isSupported();
+
+        if (isSupported) {
+            throw new RuntimeException("Tray shouldn't be supported in headless mode ");
+        }
+
+    }
+
+}