2011-07-15 09:46:24 +00:00
|
|
|
package net.i2p.itoopie.gui;
|
2011-06-20 12:38:52 +00:00
|
|
|
|
|
|
|
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-15 09:47:53 +00:00
|
|
|
import java.awt.event.MouseAdapter;
|
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-15 09:46:24 +00:00
|
|
|
import net.i2p.itoopie.Main;
|
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
|
|
|
|
|
|
|
///The tray area, or null if unsupported
|
2022-01-11 11:05:00 -05:00
|
|
|
protected SystemTray tray;
|
2011-06-20 12:38:52 +00:00
|
|
|
///Our tray icon, or null if unsupported
|
2022-01-11 11:05:00 -05:00
|
|
|
protected TrayIcon trayIcon;
|
|
|
|
private final net.i2p.itoopie.Main main;
|
2011-07-01 07:10:09 +00:00
|
|
|
|
2011-06-20 12:38:52 +00:00
|
|
|
/**
|
|
|
|
* Instantiate tray manager.
|
|
|
|
*/
|
2022-01-11 11:05:00 -05:00
|
|
|
public TrayManager(net.i2p.itoopie.Main m) {
|
|
|
|
main = m;
|
2011-06-20 12:38:52 +00:00
|
|
|
}
|
2022-01-11 11:05:00 -05:00
|
|
|
|
2011-06-20 12:38:52 +00:00
|
|
|
/**
|
|
|
|
* Add the tray icon to the system tray and start everything up.
|
|
|
|
*/
|
2022-01-09 12:12:58 -05:00
|
|
|
public synchronized void startManager() {
|
2022-01-11 11:05:00 -05:00
|
|
|
final WindowHandler windowHandler = new WindowHandler();
|
|
|
|
windowHandler.toggleFrames();
|
|
|
|
// so the tray icon works right on Gnome
|
|
|
|
try { Thread.sleep(500); } catch (InterruptedException ie) {}
|
2011-07-14 09:08:30 +00:00
|
|
|
SwingUtilities.invokeLater(new Runnable(){
|
|
|
|
public void run(){
|
|
|
|
if(SystemTray.isSupported()) {
|
2022-01-11 11:05:00 -05:00
|
|
|
final Image img = IconLoader.getTrayImage();
|
2011-07-14 09:08:30 +00:00
|
|
|
tray = SystemTray.getSystemTray();
|
2022-01-11 11:05:00 -05:00
|
|
|
trayIcon = new TrayIcon(img, "itoopie", getMainMenu());
|
2011-07-14 09:08:30 +00:00
|
|
|
trayIcon.setImageAutoSize(true); //Resize image to fit the system tray
|
2022-01-11 11:05:00 -05:00
|
|
|
try {
|
|
|
|
tray.add(trayIcon);
|
|
|
|
} catch (AWTException e) { e.printStackTrace(); }
|
2011-07-14 09:08:30 +00:00
|
|
|
|
2011-07-15 09:47:53 +00:00
|
|
|
trayIcon.addMouseListener(new MouseAdapter(){
|
2011-07-14 09:08:30 +00:00
|
|
|
@Override
|
|
|
|
public void mouseClicked(MouseEvent arg0) {
|
2022-01-11 11:05:00 -05:00
|
|
|
windowHandler.toggleFrames();
|
2011-07-14 09:08:30 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2011-06-20 12:38:52 +00:00
|
|
|
}
|
2022-01-09 12:12:58 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @since 0.0.5 for plugin only
|
|
|
|
*/
|
|
|
|
public synchronized void stopManager() {
|
|
|
|
try {
|
|
|
|
if (trayIcon != null && SystemTray.isSupported()) {
|
|
|
|
SystemTray.getSystemTray().remove(trayIcon);
|
|
|
|
// TODO stop it
|
|
|
|
trayIcon = null;
|
|
|
|
}
|
|
|
|
} catch (Exception 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();
|
2022-01-01 09:31:42 -05:00
|
|
|
MenuItem stopItem = new MenuItem(Transl._t("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 {
|
2022-01-11 11:05:00 -05:00
|
|
|
main.beginShutdown();
|
2011-06-20 12:38:52 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}.execute();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
popup.add(stopItem);
|
|
|
|
return popup;
|
|
|
|
}
|
|
|
|
}
|