2019-12-03 21:48:16 +01:00
|
|
|
package envoy.client.ui;
|
|
|
|
|
2019-12-28 21:20:43 +01:00
|
|
|
import java.awt.*;
|
2019-12-04 18:52:48 +01:00
|
|
|
import java.awt.TrayIcon.MessageType;
|
2019-12-05 15:42:20 +01:00
|
|
|
import java.awt.event.WindowAdapter;
|
|
|
|
import java.awt.event.WindowEvent;
|
2020-06-13 22:36:52 +02:00
|
|
|
import java.util.logging.Level;
|
2019-12-03 21:48:16 +01:00
|
|
|
|
2019-12-04 22:26:24 +01:00
|
|
|
import envoy.client.event.MessageCreationEvent;
|
2019-12-28 21:20:43 +01:00
|
|
|
import envoy.data.Message;
|
2019-12-29 11:54:05 +01:00
|
|
|
import envoy.event.EventBus;
|
2019-12-03 21:48:16 +01:00
|
|
|
import envoy.exception.EnvoyException;
|
2020-06-13 22:36:52 +02:00
|
|
|
import envoy.util.EnvoyLog;
|
2019-12-03 21:48:16 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Project: <strong>envoy-client</strong><br>
|
|
|
|
* File: <strong>StatusTrayIcon.java</strong><br>
|
|
|
|
* Created: <strong>3 Dec 2019</strong><br>
|
|
|
|
*
|
|
|
|
* @author Kai S. K. Engelbart
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.2-alpha
|
2019-12-03 21:48:16 +01:00
|
|
|
*/
|
2019-12-16 09:41:21 +01:00
|
|
|
public class StatusTrayIcon {
|
2019-12-03 21:48:16 +01:00
|
|
|
|
2019-12-04 23:27:17 +01:00
|
|
|
/**
|
|
|
|
* The {@link TrayIcon} provided by the System Tray API for controlling the
|
|
|
|
* system tray. This includes displaying the icon, but also creating
|
|
|
|
* notifications when new messages are received.
|
|
|
|
*/
|
2020-06-13 22:36:52 +02:00
|
|
|
private final TrayIcon trayIcon;
|
2019-12-03 21:48:16 +01:00
|
|
|
|
2019-12-05 15:42:20 +01:00
|
|
|
/**
|
|
|
|
* A received {@link Message} is only displayed as a system tray notification if
|
|
|
|
* this variable is set to {@code true}.
|
|
|
|
*/
|
|
|
|
private boolean displayMessages = false;
|
|
|
|
|
2019-12-04 23:27:17 +01:00
|
|
|
/**
|
|
|
|
* Creates a {@link StatusTrayIcon} with the Envoy logo, a tool tip and a pop-up
|
|
|
|
* menu.
|
2019-12-05 16:17:33 +01:00
|
|
|
*
|
|
|
|
* @param focusTarget the {@link Window} which focus determines if message
|
|
|
|
* notifications are displayed
|
2019-12-04 23:27:17 +01:00
|
|
|
* @throws EnvoyException if the currently used OS does not support the System
|
|
|
|
* Tray API
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.2-alpha
|
2019-12-04 23:27:17 +01:00
|
|
|
*/
|
2019-12-05 16:17:33 +01:00
|
|
|
public StatusTrayIcon(Window focusTarget) throws EnvoyException {
|
2019-12-03 21:48:16 +01:00
|
|
|
if (!SystemTray.isSupported()) throw new EnvoyException("The Envoy tray icon is not supported.");
|
|
|
|
|
2020-06-13 22:36:52 +02:00
|
|
|
final ClassLoader loader = Thread.currentThread().getContextClassLoader();
|
|
|
|
final Image img = Toolkit.getDefaultToolkit().createImage(loader.getResource("envoy_logo.png"));
|
2019-12-04 18:52:48 +01:00
|
|
|
trayIcon = new TrayIcon(img, "Envoy Client");
|
2019-12-03 21:48:16 +01:00
|
|
|
trayIcon.setImageAutoSize(true);
|
|
|
|
trayIcon.setToolTip("You are notified if you have unread messages.");
|
2019-12-04 18:52:48 +01:00
|
|
|
|
2020-06-13 22:36:52 +02:00
|
|
|
final PopupMenu popup = new PopupMenu();
|
2019-12-04 18:52:48 +01:00
|
|
|
|
2020-06-13 22:36:52 +02:00
|
|
|
final MenuItem exitMenuItem = new MenuItem("Exit");
|
|
|
|
exitMenuItem.addActionListener(evt -> System.exit(0));
|
2019-12-04 07:50:59 +01:00
|
|
|
popup.add(exitMenuItem);
|
2019-12-04 18:52:48 +01:00
|
|
|
|
2019-12-04 07:50:59 +01:00
|
|
|
trayIcon.setPopupMenu(popup);
|
2019-12-05 15:42:20 +01:00
|
|
|
|
|
|
|
// Only display messages if the chat window is not focused
|
2019-12-05 16:17:33 +01:00
|
|
|
focusTarget.addWindowFocusListener(new WindowAdapter() {
|
2019-12-05 15:42:20 +01:00
|
|
|
|
|
|
|
@Override
|
2019-12-14 14:17:24 +01:00
|
|
|
public void windowGainedFocus(WindowEvent e) { displayMessages = false; }
|
2019-12-05 15:42:20 +01:00
|
|
|
|
|
|
|
@Override
|
2019-12-14 14:17:24 +01:00
|
|
|
public void windowLostFocus(WindowEvent e) { displayMessages = true; }
|
2019-12-05 15:42:20 +01:00
|
|
|
});
|
|
|
|
|
2019-12-14 14:17:24 +01:00
|
|
|
// Show the window if the user clicks on the icon
|
2020-06-13 22:36:52 +02:00
|
|
|
trayIcon.addActionListener(evt -> { focusTarget.setVisible(true); focusTarget.requestFocus(); });
|
2019-12-14 14:17:24 +01:00
|
|
|
|
2019-12-05 15:42:20 +01:00
|
|
|
// Start processing message events
|
2020-02-12 06:12:04 +01:00
|
|
|
// TODO: Handle other message types
|
2019-12-28 21:20:43 +01:00
|
|
|
EventBus.getInstance()
|
|
|
|
.register(MessageCreationEvent.class,
|
2020-02-12 06:12:04 +01:00
|
|
|
evt -> { if (displayMessages) trayIcon.displayMessage("New message received", evt.get().getText(), MessageType.INFO); });
|
2019-12-04 07:50:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Makes this {@link StatusTrayIcon} appear in the system tray.
|
2019-12-04 22:26:24 +01:00
|
|
|
*
|
2019-12-04 23:27:17 +01:00
|
|
|
* @throws EnvoyException if the status icon could not be attaches to the system
|
|
|
|
* tray for system-internal reasons
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.2-alpha
|
2019-12-04 07:50:59 +01:00
|
|
|
*/
|
|
|
|
public void show() throws EnvoyException {
|
2019-12-03 21:48:16 +01:00
|
|
|
try {
|
|
|
|
SystemTray.getSystemTray().add(trayIcon);
|
2020-06-13 22:36:52 +02:00
|
|
|
} catch (final AWTException e) {
|
|
|
|
EnvoyLog.getLogger(StatusTrayIcon.class).log(Level.INFO, "Could not display StatusTrayIcon: ", e);
|
2019-12-03 21:48:16 +01:00
|
|
|
throw new EnvoyException("Could not attach Envoy tray icon to system tray.", e);
|
|
|
|
}
|
|
|
|
}
|
2020-03-14 19:59:37 +01:00
|
|
|
}
|