changeset 563:e8301d67f7fa

6737722: api/java_awt/TrayIcon/index.html#TrayIconHeadlessMode Summary: isSupported() should skip tray initialization in case of headless Reviewed-by: art, ant
author dcherepanov
date Mon, 25 Aug 2008 19:15:37 +0400
parents 99658a8d42fa
children 9d5e52760d53 75755e92430c
files src/share/classes/java/awt/SystemTray.java src/share/classes/java/awt/TrayIcon.java test/java/awt/Toolkit/HeadlessTray/HeadlessTray.java
diffstat 3 files changed, 70 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/awt/SystemTray.java	Thu Aug 14 12:58:51 2008 +0400
+++ b/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/src/share/classes/java/awt/TrayIcon.java	Thu Aug 14 12:58:51 2008 +0400
+++ b/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/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 ");
+        }
+
+    }
+
+}