tezem

AWT SystemTray

Discussion created by tezem on May 8, 2007
Latest reply on May 9, 2007 by tezem

I wanted to test the AWT SystemTray class in Java 1.6, so I changed Notifications.java. Works nice for me and it looks nicer in my opinion at least in Linux. In case someone is interested here is the diff:

 

Index: Notifications.java

===================================================================

--- Notifications.java     (revision 8210)

+++ Notifications.java     (working copy)

@@ -10,8 +10,10 @@

 

 

package org.jivesoftware.spark.component;

 

 

 

-import org.jdesktop.jdic.tray.SystemTray;

 

-import org.jdesktop.jdic.tray.TrayIcon;

 

+import java.awt.AWTException;

 

+import java.awt.SystemTray;

 

+import java.awt.TrayIcon;

 

+import java.awt.event.MouseListener;

 

import org.jivesoftware.MainWindow;

 

import org.jivesoftware.MainWindowListener;

 

import org.jivesoftware.Spark;

 

@@ -56,7 +58,7 @@

   

  • Handles tray icon operations inside of Spark. Use to display incoming chat requests, incoming messages

 

   

  • and general notifications.

 

  */

 

-public final class Notifications implements ActionListener, MainWindowListener {

 

+public final class Notifications implements MouseListener, ActionListener, MainWindowListener {

 

     private ImageIcon availableIcon;

 

     private ImageIcon unavaliableIcon;

 

     private ImageIcon busyIcon;

 

@@ -78,6 +80,8 @@

     private JFrame hideWindow = null;

 

 

 

     private long madeVisibleTime = 0;

 

+   

 

+    private JPopupMenu popupMenu = new JPopupMenu(Res.getString("title.tray.information"));

 

 

 

     /**

 

       

  • Creates a new instance of notifications.

 

@@ -90,7 +94,7 @@

 

 

         SystemTray tray = null;

 

         try {

 

-            tray = SystemTray.getDefaultSystemTray();

 

+            tray = SystemTray.getSystemTray();

 

         }

 

         catch (Throwable e) {

 

             Log.error(e);

 

@@ -101,11 +105,9 @@

         availableIcon = SparkRes.getImageIcon(SparkRes.TRAY_IMAGE);

 

         unavaliableIcon = SparkRes.getImageIcon(SparkRes.MESSAGE_AWAY);

 

         busyIcon = SparkRes.getImageIcon(SparkRes.MESSAGE_DND);

 

-        trayIcon = new TrayIcon(availableIcon);

 

+        trayIcon = new TrayIcon(availableIcon.getImage());

 

         trayIcon.setToolTip("Spark Client"); // NORES

 

 

 

-        JPopupMenu popupMenu = new JPopupMenu(Res.getString("title.tray.information"));

 

-

 

         // Add DND Menus

 

         addStatusMenuItems();

 

 

 

@@ -142,12 +144,17 @@

 

 

         SparkManager.getMainWindow().addMainWindowListener(this);

 

 

 

-        trayIcon.setPopupMenu(popupMenu);

 

+        trayIcon.addMouseListener(this);

 

         trayIcon.addActionListener(this);

 

 

 

         if (tray != null) {

 

-            tray.addTrayIcon(trayIcon);

 

+            try {

 

+                tray.add(trayIcon);

 

+            } catch (AWTException ex) {

 

+                ex.printStackTrace();

 

+            }

 

         }

 

+       

 

         SparkManager.getSessionManager().addPresenceListener(new PresenceListener() {

 

             public void presenceChanged(Presence presence) {

 

                 changePresence(presence);

 

@@ -166,13 +173,13 @@

         }

 

 

 

         if (presence.getMode() == Presence.Mode.available || presence.getMode() == Presence.Mode.chat) {

 

-            trayIcon.setIcon(availableIcon);

 

+            trayIcon.setImage(availableIcon.getImage());

 

         }

 

         else if (presence.getMode() == Presence.Mode.away || presence.getMode() == Presence.Mode.xa) {

 

-            trayIcon.setIcon(unavaliableIcon);

 

+            trayIcon.setImage(unavaliableIcon.getImage());

 

         }

 

         else {

 

-            trayIcon.setIcon(busyIcon);

 

+            trayIcon.setImage(busyIcon.getImage());

 

         }

 

 

 

         // Get Status Text

 

@@ -364,5 +371,30 @@

         //     trayIcon.displayMessage(title, body, TrayIcon.INFO_MESSAGE_TYPE);

 

     }

 

 

 

+    public void mouseClicked(MouseEvent e) {

 

+    }

 

 

 

+    public void mousePressed(MouseEvent e) {

 

+        if (e.isPopupTrigger()) {

 

+                popupMenu.setLocation(e.getX(), e.getY());

 

+                popupMenu.setInvoker(popupMenu);

 

+                popupMenu.setVisible(true);

 

+            }

 

+    }

 

+

 

+    public void mouseReleased(MouseEvent e) {

 

+        if (e.isPopupTrigger()) {

 

+                popupMenu.setLocation(e.getX(), e.getY());

 

+                popupMenu.setInvoker(popupMenu);

 

+                popupMenu.setVisible(true);

 

+            }

 

+    }

 

+

 

+    public void mouseEntered(MouseEvent e) {

 

+    }

 

+

 

+    public void mouseExited(MouseEvent e) {

 

+    }

 

+

 

+

 

}

Outcomes