From 378a83638a3ff2fc498a6cfd2fba5e7b0305b368 Mon Sep 17 00:00:00 2001 From: kske Date: Wed, 4 Dec 2019 07:50:59 +0100 Subject: [PATCH] Added a small popup menu to StatusTrayIcon and loading it in Startup --- src/main/java/envoy/client/ui/Startup.java | 1 + .../java/envoy/client/ui/StatusTrayIcon.java | 21 +++++++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/main/java/envoy/client/ui/Startup.java b/src/main/java/envoy/client/ui/Startup.java index c3a3baa..7b35158 100644 --- a/src/main/java/envoy/client/ui/Startup.java +++ b/src/main/java/envoy/client/ui/Startup.java @@ -64,6 +64,7 @@ public class Startup { EventQueue.invokeLater(() -> { try { ChatWindow frame = new ChatWindow(client, localDB); + new StatusTrayIcon().show(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); diff --git a/src/main/java/envoy/client/ui/StatusTrayIcon.java b/src/main/java/envoy/client/ui/StatusTrayIcon.java index 54bc3a3..7ef6877 100644 --- a/src/main/java/envoy/client/ui/StatusTrayIcon.java +++ b/src/main/java/envoy/client/ui/StatusTrayIcon.java @@ -2,6 +2,8 @@ package envoy.client.ui; import java.awt.AWTException; import java.awt.Image; +import java.awt.MenuItem; +import java.awt.PopupMenu; import java.awt.SystemTray; import java.awt.Toolkit; import java.awt.TrayIcon; @@ -24,16 +26,31 @@ public class StatusTrayIcon { if (!SystemTray.isSupported()) throw new EnvoyException("The Envoy tray icon is not supported."); Image img = Toolkit.getDefaultToolkit().createImage(getClass().getResource("envoy_logo.png")); - TrayIcon trayIcon = new TrayIcon(img, "Envoy Client"); + trayIcon = new TrayIcon(img, "Envoy Client"); trayIcon.setImageAutoSize(true); trayIcon.setToolTip("You are notified if you have unread messages."); + + PopupMenu popup = new PopupMenu(); + + MenuItem exitMenuItem = new MenuItem("Exit"); + exitMenuItem.addActionListener((evt) -> System.exit(0)); + popup.add(exitMenuItem); + + trayIcon.setPopupMenu(popup); + } + + /** + * Makes this {@link StatusTrayIcon} appear in the system tray. + * @throws EnvoyException + */ + public void show() throws EnvoyException { try { SystemTray.getSystemTray().add(trayIcon); } catch (AWTException e) { throw new EnvoyException("Could not attach Envoy tray icon to system tray.", e); } } - + // TODO: Add event listener // trayIcon.displayMessage("Envoy Client", message, MessageType.INFO); }