2011-06-20 12:38:52 +00:00
|
|
|
package net.i2p.itoopie;
|
|
|
|
|
|
|
|
import java.awt.AWTException;
|
|
|
|
import java.awt.Image;
|
|
|
|
import java.awt.MenuItem;
|
|
|
|
import java.awt.PopupMenu;
|
|
|
|
import java.awt.SystemTray;
|
|
|
|
import java.awt.Toolkit;
|
|
|
|
import java.awt.TrayIcon;
|
|
|
|
import java.awt.event.ActionEvent;
|
|
|
|
import java.awt.event.ActionListener;
|
2011-07-12 18:56:49 +00:00
|
|
|
import java.awt.event.MouseEvent;
|
|
|
|
import java.awt.event.MouseListener;
|
2011-06-28 07:20:04 +00:00
|
|
|
import java.net.URL;
|
|
|
|
|
2011-07-01 06:34:21 +00:00
|
|
|
import javax.swing.JOptionPane;
|
2011-07-14 09:08:30 +00:00
|
|
|
import javax.swing.SwingUtilities;
|
2011-06-20 12:38:52 +00:00
|
|
|
import javax.swing.SwingWorker;
|
|
|
|
|
2011-07-12 18:56:49 +00:00
|
|
|
import net.i2p.itoopie.gui.WindowHandler;
|
2011-07-01 06:34:21 +00:00
|
|
|
import net.i2p.itoopie.i18n.Transl;
|
2011-07-12 18:56:49 +00:00
|
|
|
import net.i2p.itoopie.util.IconLoader;
|
2011-07-01 06:34:21 +00:00
|
|
|
import net.i2p.itoopie.util.IsJar;
|
2011-06-20 12:38:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Manages the tray icon life.
|
|
|
|
*/
|
2011-06-27 11:56:29 +00:00
|
|
|
public class TrayManager {
|
2011-06-20 12:38:52 +00:00
|
|
|
|
|
|
|
private static TrayManager instance = null;
|
|
|
|
///The tray area, or null if unsupported
|
|
|
|
protected SystemTray tray = null;
|
|
|
|
///Our tray icon, or null if unsupported
|
|
|
|
protected TrayIcon trayIcon = null;
|
|
|
|
|
2011-07-01 07:10:09 +00:00
|
|
|
|
2011-06-20 12:38:52 +00:00
|
|
|
/**
|
|
|
|
* Instantiate tray manager.
|
|
|
|
*/
|
|
|
|
protected TrayManager() {}
|
|
|
|
|
|
|
|
protected static synchronized TrayManager getInstance() {
|
|
|
|
if(instance == null) {
|
2011-06-27 11:56:29 +00:00
|
|
|
instance = new TrayManager();
|
2011-06-20 12:38:52 +00:00
|
|
|
}
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add the tray icon to the system tray and start everything up.
|
|
|
|
*/
|
|
|
|
protected void startManager() {
|
2011-07-14 09:08:30 +00:00
|
|
|
SwingUtilities.invokeLater(new Runnable(){
|
|
|
|
public void run(){
|
|
|
|
if(SystemTray.isSupported()) {
|
|
|
|
tray = SystemTray.getSystemTray();
|
|
|
|
trayIcon = new TrayIcon(IconLoader.getTrayImage(), "itoopie", getMainMenu());
|
|
|
|
trayIcon.setImageAutoSize(true); //Resize image to fit the system tray
|
|
|
|
|
|
|
|
trayIcon.addMouseListener(new MouseListener(){
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void mouseClicked(MouseEvent arg0) {
|
|
|
|
WindowHandler.toggleFrames();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void mouseEntered(MouseEvent arg0) {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void mouseExited(MouseEvent arg0) {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void mousePressed(MouseEvent arg0) {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void mouseReleased(MouseEvent arg0) {
|
|
|
|
}
|
|
|
|
});
|
|
|
|
try {
|
|
|
|
tray.add(trayIcon);
|
|
|
|
} catch (AWTException e) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2011-06-20 12:38:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected void languageChanged() {
|
|
|
|
trayIcon.setPopupMenu(getMainMenu());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build a popup menu, adding callbacks to the different items.
|
|
|
|
* @return popup menu
|
|
|
|
*/
|
|
|
|
public PopupMenu getMainMenu() {
|
|
|
|
PopupMenu popup = new PopupMenu();
|
2011-07-01 06:34:21 +00:00
|
|
|
MenuItem stopItem = new MenuItem(Transl._("Exit itoopie"));
|
2011-06-20 12:38:52 +00:00
|
|
|
stopItem.addActionListener(new ActionListener() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void actionPerformed(ActionEvent arg0) {
|
|
|
|
new SwingWorker<Object, Object>() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected Object doInBackground() throws Exception {
|
2011-07-05 14:10:19 +00:00
|
|
|
Main.beginShutdown();
|
2011-06-20 12:38:52 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}.execute();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
popup.add(stopItem);
|
|
|
|
return popup;
|
|
|
|
}
|
|
|
|
}
|