From b5badae77308a14860e200aa856c2e688e60b353 Mon Sep 17 00:00:00 2001 From: kske Date: Wed, 4 Dec 2019 18:52:48 +0100 Subject: [PATCH] Added StatusTrayIcon#displayMessageNotification method Using this method, a message object can be displayed as a OS-specific notification, which can be useful in the future to alert the user about an incoming message while the application is not in focus. --- .../java/envoy/client/ui/StatusTrayIcon.java | 26 +++++++++++++------ 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/src/main/java/envoy/client/ui/StatusTrayIcon.java b/src/main/java/envoy/client/ui/StatusTrayIcon.java index 7ef6877..869e678 100644 --- a/src/main/java/envoy/client/ui/StatusTrayIcon.java +++ b/src/main/java/envoy/client/ui/StatusTrayIcon.java @@ -7,8 +7,10 @@ import java.awt.PopupMenu; import java.awt.SystemTray; import java.awt.Toolkit; import java.awt.TrayIcon; +import java.awt.TrayIcon.MessageType; import envoy.exception.EnvoyException; +import envoy.schema.Message; /** * Project: envoy-client
@@ -25,22 +27,23 @@ public class StatusTrayIcon { public StatusTrayIcon() throws EnvoyException { if (!SystemTray.isSupported()) throw new EnvoyException("The Envoy tray icon is not supported."); - Image img = Toolkit.getDefaultToolkit().createImage(getClass().getResource("envoy_logo.png")); - trayIcon = new TrayIcon(img, "Envoy Client"); + Image img = Toolkit.getDefaultToolkit().createImage(getClass().getResource("envoy_logo.png")); + 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 { @@ -50,7 +53,14 @@ public class StatusTrayIcon { throw new EnvoyException("Could not attach Envoy tray icon to system tray.", e); } } - - // TODO: Add event listener - // trayIcon.displayMessage("Envoy Client", message, MessageType.INFO); + + /** + * Notifies the user of a message by displaying a popup. + * + * @param message the {@link Message} object to display in the popup + */ + public void displayMessageNotification(Message message) { + // TODO: Add event listener + trayIcon.displayMessage("New message received", message.getContent().get(0).getText(), MessageType.INFO); + } }