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.
This commit is contained in:
Kai S. K. Engelbart 2019-12-04 18:52:48 +01:00
parent c70416df28
commit 761db20f5f

View File

@ -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: <strong>envoy-client</strong><br>
@ -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);
}
}