98 lines
2.7 KiB
Java
98 lines
2.7 KiB
Java
package jnet.client.gui;
|
|
|
|
import java.awt.*;
|
|
import java.awt.image.BufferedImage;
|
|
import java.io.IOException;
|
|
import javax.imageio.ImageIO;
|
|
import jnet.client.Client;
|
|
import jnet.lib.LogFile;
|
|
import jnet.lib.Status;
|
|
import jnet.lib.object.Map;
|
|
|
|
public class Tray {
|
|
|
|
private SystemTray tray;
|
|
private static TrayIcon trayIcon;
|
|
private static PopupMenu menu = new PopupMenu();
|
|
|
|
public void createAndShowTray() {
|
|
try {
|
|
// Ověření, zda je SystemTray podporován
|
|
if (!SystemTray.isSupported()) {
|
|
LogFile.printErr("SystemTray is not supported");
|
|
return;
|
|
}
|
|
|
|
// Menu pro tray ikonu
|
|
MenuItem exitItem = new MenuItem("Ukončit");
|
|
exitItem.addActionListener(e -> {
|
|
LogFile.printInfo("Exit application from tray icon");
|
|
System.exit(0);
|
|
});
|
|
menu.add(exitItem);
|
|
|
|
// Načtení výchozí ikony ze zdrojů
|
|
trayIcon = new TrayIcon(loadImage("/img/flag_gray.png"), Client.APP_NAME, menu);
|
|
trayIcon.setImageAutoSize(true);
|
|
trayIcon.addActionListener(e -> {
|
|
// Dvojklik - obnovit/minimalizovat okno
|
|
Client.okno.setVisible(!Client.okno.isVisible());
|
|
});
|
|
|
|
tray = SystemTray.getSystemTray();
|
|
tray.add(trayIcon);
|
|
|
|
} catch (IOException | AWTException ex) {
|
|
LogFile.printErr("TrayIcon error: " + ex.getMessage());
|
|
}
|
|
}
|
|
|
|
private static BufferedImage loadImage(String path) throws IOException {
|
|
try (var input = Tray.class.getResourceAsStream(path)) {
|
|
if (input == null) {
|
|
throw new IOException("Soubor nenalezen: " + path);
|
|
}
|
|
return ImageIO.read(input);
|
|
}
|
|
}
|
|
|
|
public static void setIcon(String icon) {
|
|
try {
|
|
trayIcon.setImage(loadImage("/img/" + icon));
|
|
} catch (IOException ex) {
|
|
LogFile.printErr("TrayIcon error: " + ex.getMessage());
|
|
}
|
|
}
|
|
|
|
public static void setStatusOffline() {
|
|
setIcon("flag_red.png");
|
|
}
|
|
|
|
public static void setStatusOnline() {
|
|
setIcon("flag_green.png");
|
|
}
|
|
|
|
public static void setStatusWarning() {
|
|
setIcon("flag_orange.png");
|
|
}
|
|
|
|
public static void setStatusDisconnected() {
|
|
setIcon("flag_gray.png");
|
|
}
|
|
|
|
public static void refresh() {
|
|
setStatusOnline(); // Výchozí stav
|
|
|
|
for (Map map : Client.maps) {
|
|
int status = map.getStatus();
|
|
if (status == Status.OFFLINE) {
|
|
setStatusOffline();
|
|
return;
|
|
}
|
|
if (status == Status.WARNING) {
|
|
setStatusWarning();
|
|
}
|
|
}
|
|
}
|
|
}
|