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-06-28 07:20:04 +00:00
|
|
|
import java.net.URL;
|
|
|
|
|
2011-07-01 06:34:21 +00:00
|
|
|
import javax.swing.JOptionPane;
|
2011-06-20 12:38:52 +00:00
|
|
|
import javax.swing.SwingWorker;
|
|
|
|
|
2011-07-01 06:34:21 +00:00
|
|
|
import net.i2p.itoopie.i18n.Transl;
|
2011-06-20 12:38:52 +00:00
|
|
|
import net.i2p.itoopie.util.BrowseException;
|
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() {
|
|
|
|
if(SystemTray.isSupported()) {
|
|
|
|
tray = SystemTray.getSystemTray();
|
2011-06-28 10:33:50 +00:00
|
|
|
trayIcon = new TrayIcon(getTrayImage(), "itoopie", getMainMenu());
|
2011-06-20 12:38:52 +00:00
|
|
|
trayIcon.setImageAutoSize(true); //Resize image to fit the system tray
|
|
|
|
try {
|
|
|
|
tray.add(trayIcon);
|
|
|
|
} catch (AWTException e) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void languageChanged() {
|
|
|
|
trayIcon.setPopupMenu(getMainMenu());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get tray icon image from the itoopie resources in the jar file.
|
|
|
|
* @return image used for the tray icon
|
|
|
|
*/
|
|
|
|
private Image getTrayImage() {
|
2011-06-28 10:33:50 +00:00
|
|
|
|
|
|
|
// Assume square icons.
|
|
|
|
int icoHeight = (int) SystemTray.getSystemTray().getTrayIconSize().getHeight();
|
|
|
|
int desiredHeight;
|
|
|
|
if (icoHeight == 16 ||
|
|
|
|
icoHeight == 24 ||
|
|
|
|
icoHeight == 32 ||
|
|
|
|
icoHeight == 48 ||
|
|
|
|
icoHeight == 64 ||
|
|
|
|
icoHeight == 128 ||
|
|
|
|
icoHeight == 256 ||
|
|
|
|
icoHeight == 512){
|
|
|
|
desiredHeight = icoHeight;
|
|
|
|
} else {
|
|
|
|
desiredHeight = 512;
|
|
|
|
}
|
|
|
|
|
2011-07-01 06:34:21 +00:00
|
|
|
if (IsJar.isRunningJar()){
|
|
|
|
URL url = getClass().getResource("/resources/images/itoopie-"+desiredHeight+".png");
|
|
|
|
return Toolkit.getDefaultToolkit().getImage(url);
|
|
|
|
} else {
|
|
|
|
return Toolkit.getDefaultToolkit().getImage("resources/images/itoopie-"+desiredHeight+".png");
|
|
|
|
}
|
2011-06-20 12:38:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 browserLauncher = new MenuItem(Transl._("Launch I2P Browser"));
|
2011-06-20 12:38:52 +00:00
|
|
|
browserLauncher.addActionListener(new ActionListener() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void actionPerformed(ActionEvent arg0) {
|
|
|
|
new SwingWorker<Object, Object>() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected Object doInBackground() throws Exception {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void done() {
|
2011-06-28 13:19:09 +00:00
|
|
|
System.out.println("Tried to open a browser");
|
2011-06-20 12:38:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}.execute();
|
|
|
|
}
|
|
|
|
});
|
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-01 06:34:21 +00:00
|
|
|
System.exit(0);
|
2011-06-20 12:38:52 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}.execute();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
popup.add(stopItem);
|
|
|
|
|
|
|
|
return popup;
|
|
|
|
}
|
|
|
|
}
|