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-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;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
|
|
|
private TrayIcon trayIcon;
|
|
|
|
|
|
|
|
public StatusTrayIcon() throws EnvoyException {
|
|
|
|
if (!SystemTray.isSupported()) throw new EnvoyException("The Envoy tray icon is not supported.");
|
|
|
|
|
2019-12-04 18:52:48 +01:00
|
|
|
Image img = Toolkit.getDefaultToolkit().createImage(getClass().getResource("envoy_logo.png"));
|
|
|
|
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-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 07:50:59 +01:00
|
|
|
* @throws EnvoyException
|
|
|
|
*/
|
|
|
|
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 22:26:24 +01:00
|
|
|
* Notifies the user of a message by displaying a pop-up.
|
2019-12-04 18:52:48 +01:00
|
|
|
*/
|
2019-12-04 22:26:24 +01:00
|
|
|
@Override
|
|
|
|
public void handle(Event<?> event) {
|
|
|
|
trayIcon.displayMessage("New message received", ((MessageCreationEvent) event).get().getContent().get(0).getText(), MessageType.INFO);
|
|
|
|
}
|
|
|
|
|
|
|
|
@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
|
|
|
}
|