2019-12-03 21:48:16 +01:00
|
|
|
package envoy.client.ui;
|
|
|
|
|
|
|
|
import java.awt.AWTException;
|
|
|
|
import java.awt.Image;
|
2019-12-04 07:50:59 +01:00
|
|
|
import java.awt.MenuItem;
|
|
|
|
import java.awt.PopupMenu;
|
2019-12-03 21:48:16 +01:00
|
|
|
import java.awt.SystemTray;
|
|
|
|
import java.awt.Toolkit;
|
|
|
|
import java.awt.TrayIcon;
|
2019-12-04 18:52:48 +01:00
|
|
|
import java.awt.TrayIcon.MessageType;
|
2019-12-05 16:17:33 +01:00
|
|
|
import java.awt.Window;
|
2019-12-05 15:42:20 +01:00
|
|
|
import java.awt.event.WindowAdapter;
|
|
|
|
import java.awt.event.WindowEvent;
|
2019-12-04 22:26:24 +01:00
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.Set;
|
2019-12-03 21:48:16 +01:00
|
|
|
|
2019-12-04 22:26:24 +01:00
|
|
|
import envoy.client.event.Event;
|
|
|
|
import envoy.client.event.EventBus;
|
|
|
|
import envoy.client.event.EventHandler;
|
|
|
|
import envoy.client.event.MessageCreationEvent;
|
2019-12-03 21:48:16 +01:00
|
|
|
import envoy.exception.EnvoyException;
|
2019-12-05 15:42:20 +01:00
|
|
|
import envoy.schema.Message;
|
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
|
|
|
|
* @since Envoy v0.2-alpha
|
|
|
|
*/
|
2019-12-04 22:26:24 +01:00
|
|
|
public class StatusTrayIcon implements EventHandler {
|
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.
|
|
|
|
*/
|
2019-12-03 21:48:16 +01:00
|
|
|
private TrayIcon trayIcon;
|
|
|
|
|
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
|
2019-12-05 16:17:33 +01:00
|
|
|
* @since Envoy 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.");
|
|
|
|
|
2019-12-05 15:42:20 +01:00
|
|
|
ClassLoader loader = Thread.currentThread().getContextClassLoader();
|
|
|
|
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
|
|
|
|
2019-12-04 07:50:59 +01:00
|
|
|
PopupMenu popup = new PopupMenu();
|
2019-12-04 18:52:48 +01:00
|
|
|
|
2019-12-04 07:50:59 +01:00
|
|
|
MenuItem exitMenuItem = new MenuItem("Exit");
|
|
|
|
exitMenuItem.addActionListener((evt) -> System.exit(0));
|
|
|
|
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
|
|
|
|
public void windowGainedFocus(WindowEvent e) {
|
|
|
|
displayMessages = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void windowLostFocus(WindowEvent e) {
|
|
|
|
displayMessages = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Start processing message events
|
2019-12-04 22:26:24 +01:00
|
|
|
EventBus.getInstance().register(this);
|
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
|
|
|
|
* @since Envoy 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);
|
|
|
|
} catch (AWTException e) {
|
|
|
|
throw new EnvoyException("Could not attach Envoy tray icon to system tray.", e);
|
|
|
|
}
|
|
|
|
}
|
2019-12-04 18:52:48 +01:00
|
|
|
|
|
|
|
/**
|
2019-12-04 23:27:17 +01:00
|
|
|
* Notifies the user of a message by displaying a pop-up every time a new
|
|
|
|
* message is received.
|
2019-12-05 16:17:33 +01:00
|
|
|
*
|
2019-12-04 23:27:17 +01:00
|
|
|
* @since Envoy v0.2-alpha
|
2019-12-04 18:52:48 +01:00
|
|
|
*/
|
2019-12-04 22:26:24 +01:00
|
|
|
@Override
|
|
|
|
public void handle(Event<?> event) {
|
2019-12-05 15:42:20 +01:00
|
|
|
System.out.println("Message received. Displaying message: " + displayMessages);
|
|
|
|
if (displayMessages)
|
|
|
|
trayIcon.displayMessage("New message received", ((MessageCreationEvent) event).get().getContent().get(0).getText(), MessageType.INFO);
|
2019-12-04 22:26:24 +01:00
|
|
|
}
|
|
|
|
|
2019-12-04 23:27:17 +01:00
|
|
|
/**
|
|
|
|
* The {@link StatusTrayIcon} only reacts to {@link MessageCreationEvent}
|
|
|
|
* instances which signify newly received messages.
|
2019-12-05 16:17:33 +01:00
|
|
|
*
|
2019-12-04 23:27:17 +01:00
|
|
|
* @return A set with the single element {@code MessageCreationEvent.class}
|
|
|
|
* @since Envoy v0.2-alpha
|
|
|
|
*/
|
2019-12-04 22:26:24 +01:00
|
|
|
@Override
|
|
|
|
public Set<Class<? extends Event<?>>> supports() {
|
|
|
|
Set<Class<? extends Event<?>>> supportedEvents = new HashSet<>();
|
|
|
|
supportedEvents.add(MessageCreationEvent.class);
|
|
|
|
return supportedEvents;
|
2019-12-04 18:52:48 +01:00
|
|
|
}
|
2019-12-03 21:48:16 +01:00
|
|
|
}
|