package ui.tray;
import java.awt.AWTException;
import java.awt.Frame;
import java.awt.Image;
import java.awt.SplashScreen;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.ImageIcon;
import ui.MainFrame;
import main.settings.Settings;
public class TrayManager extends MouseAdapter
{
private MainFrame mainFrame;
private TrayIcon trayIcon;
private boolean trayed;
public TrayManager(MainFrame mainFrame)
{
this.mainFrame = mainFrame;
Image iconImage = new ImageIcon("gfx/icons/16x16.png").getImage();
trayIcon = new TrayIcon(iconImage, Settings.title, new TrayMenu(this));
trayed = false;
mainFrame.addWindowListener(new MainFrameListener());
trayIcon.addMouseListener(this);
}
private final class MainFrameListener extends WindowAdapter
{
public void windowIconified(WindowEvent event)
{
if (SystemTray.isSupported() && Settings.application.minimize_to_tray)
{
try
{
SystemTray.getSystemTray().add(trayIcon);
}
catch (AWTException e)
{
return;
}
trayed = true;
mainFrame.setVisible(false);
}
}
}
public void startToTray()
{
try
{
SystemTray.getSystemTray().add(trayIcon);
}
catch (AWTException e)
{
mainFrame.setVisible(true);
}
trayed = true;
closeSplashScreen();
}
private void closeSplashScreen()
{
SplashScreen splashScreen = SplashScreen.getSplashScreen();
if (splashScreen != null)
{
SplashScreen.getSplashScreen().close();
}
}
public void showTrayMessage(String title, String text,
TrayIcon.MessageType messageType)
{
trayIcon.displayMessage(title, text, messageType);
}
public boolean isTrayed()
{
return trayed;
}
public void mouseClicked(MouseEvent event)
{
if (event.getButton() == MouseEvent.BUTTON1)
{
manageLeftMouseButton(event);
}
}
private void manageLeftMouseButton(MouseEvent event)
{
if (event.getClickCount() == 1)
{
// TODO: show quick status window
}
else if (event.getClickCount() >= 2)
{
restoreMainWindow();
}
}
public void restoreMainWindow()
{
mainFrame.setVisible(true);
SystemTray.getSystemTray().remove(trayIcon);
trayed = false;
mainFrame.setExtendedState(Frame.NORMAL);
}
}