Initial commit of project
This commit is contained in:
parent
23646b0e65
commit
32ce0f5db9
|
@ -0,0 +1,9 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
|
||||
<classpathentry kind="lib" path="libs/jna/jna-4.1.0.jar" sourcepath="libs/jna/jna-4.1.0-sources.zip"/>
|
||||
<classpathentry kind="lib" path="libs/slf4j/slf4j-api-1.7.5.jar" sourcepath="libs/slf4j/slf4j-api-1.7.5-sources.zip"/>
|
||||
<classpathentry kind="lib" path="libs/dorkboxUtil.jar"/>
|
||||
<classpathentry kind="output" path="classes"/>
|
||||
</classpath>
|
|
@ -0,0 +1 @@
|
|||
/classes/
|
|
@ -0,0 +1,17 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>SystemTray</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
|
@ -0,0 +1,5 @@
|
|||
package dorkbox.util.tray;
|
||||
|
||||
public interface FailureCallback {
|
||||
public void createTrayFailed();
|
||||
}
|
|
@ -0,0 +1,234 @@
|
|||
package dorkbox.util.tray;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.math.BigInteger;
|
||||
import java.net.URL;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.Charset;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import dorkbox.util.NamedThreadFactory;
|
||||
import dorkbox.util.OS;
|
||||
import dorkbox.util.jna.linux.GtkSupport;
|
||||
import dorkbox.util.tray.linux.AppIndicatorTray;
|
||||
import dorkbox.util.tray.linux.GtkSystemTray;
|
||||
import dorkbox.util.tray.swing.SwingSystemTray;
|
||||
|
||||
|
||||
/**
|
||||
* Interface for system tray implementations.
|
||||
*/
|
||||
public abstract class SystemTray {
|
||||
private static final Charset UTF_8 = Charset.forName("UTF-8");
|
||||
private static MessageDigest digest;
|
||||
|
||||
protected static final Logger logger = LoggerFactory.getLogger(SystemTray.class);
|
||||
|
||||
public final static String LABEL_DISCONNECTED = "Not connected";
|
||||
public final static String LABEL_CONNECTING = "Connecting...";
|
||||
public final static String LABEL_CONNECTED = "Connected";
|
||||
public final static String LABEL_DISCONNECTING = "Disconnecting...";
|
||||
|
||||
// could be changed to red/yellow/green
|
||||
public final static String ICON_DISCONNECTED = "cubes_grey.png";
|
||||
public final static String ICON_CONNECTING = "cubes_green.png";
|
||||
public final static String ICON_CONNECTED = "cubes_blue.png";
|
||||
public final static String ICON_DISCONNECTING = "cubes_red.png";
|
||||
|
||||
|
||||
/** Size of the icon **/
|
||||
public static int ICON_SIZE = 22;
|
||||
|
||||
/** Location of the icon **/
|
||||
public static String ICON_PATH = "";
|
||||
|
||||
private static long runtimeRandom = new SecureRandom().nextLong();
|
||||
private static Class<? extends SystemTray> trayType;
|
||||
|
||||
static {
|
||||
if (OS.isLinux()) {
|
||||
GtkSupport.init();
|
||||
String getenv = System.getenv("XDG_CURRENT_DESKTOP");
|
||||
if (getenv != null && (getenv.equals("Unity") || getenv.equals("KDE"))) {
|
||||
if (GtkSupport.isSupported) {
|
||||
trayType = AppIndicatorTray.class;
|
||||
}
|
||||
} else {
|
||||
if (GtkSupport.isSupported) {
|
||||
trayType = GtkSystemTray.class;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// this is windows OR mac
|
||||
if (trayType == null && java.awt.SystemTray.isSupported()) {
|
||||
trayType = SwingSystemTray.class;
|
||||
}
|
||||
|
||||
if (trayType == null) {
|
||||
// unsupported tray
|
||||
logger.error("Unsupported tray type!");
|
||||
} else {
|
||||
try {
|
||||
digest = MessageDigest.getInstance("MD5");
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
logger.error("Unsupported hashing algoritm!");
|
||||
trayType = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected Executor callbackExecutor = Executors.newSingleThreadExecutor(new NamedThreadFactory("SysTrayExecutor", false));
|
||||
|
||||
public volatile FailureCallback failureCallback;
|
||||
protected volatile boolean active = false;
|
||||
protected String appName;
|
||||
|
||||
public static SystemTray create(String appName) {
|
||||
if (trayType != null) {
|
||||
try {
|
||||
SystemTray newInstance = trayType.newInstance();
|
||||
if (newInstance != null) {
|
||||
newInstance.setAppName(appName);
|
||||
}
|
||||
return newInstance;
|
||||
} catch (InstantiationException e) {
|
||||
e.printStackTrace();
|
||||
// log.error(e);
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
// log.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// unsupported
|
||||
return null;
|
||||
}
|
||||
|
||||
private void setAppName(String appName) {
|
||||
this.appName = appName;
|
||||
}
|
||||
|
||||
public abstract void createTray(String iconName);
|
||||
public abstract void removeTray();
|
||||
|
||||
public abstract void setStatus(String infoString, String iconName);
|
||||
|
||||
public abstract void addMenuEntry(String menuText, SystemTrayMenuAction callback);
|
||||
public abstract void updateMenuEntry(String origMenuText, String newMenuText, SystemTrayMenuAction newCallback);
|
||||
|
||||
|
||||
protected String iconPath(String fileName) {
|
||||
// is file sitting on drive
|
||||
File iconTest;
|
||||
if (ICON_PATH.isEmpty()) {
|
||||
iconTest = new File(fileName);
|
||||
} else {
|
||||
iconTest = new File(ICON_PATH, fileName);
|
||||
}
|
||||
if (iconTest.isFile() && iconTest.canRead()) {
|
||||
return iconTest.getAbsolutePath();
|
||||
} else {
|
||||
if (!ICON_PATH.isEmpty()) {
|
||||
fileName = ICON_PATH + "/" + fileName;
|
||||
}
|
||||
|
||||
String extension = "";
|
||||
int dot = fileName.lastIndexOf('.');
|
||||
if (dot > -1) {
|
||||
extension = fileName.substring(dot + 1);
|
||||
}
|
||||
|
||||
// maybe it's in somewhere else.
|
||||
URL systemResource = ClassLoader.getSystemResource(fileName);
|
||||
if (systemResource != null) {
|
||||
// copy out to a temp file, as a hash of the file
|
||||
String file = systemResource.getFile();
|
||||
byte[] bytes = file.getBytes(UTF_8);
|
||||
File newFile = null;
|
||||
String tempDir = System.getProperty("java.io.tmpdir");
|
||||
|
||||
// can be wimpy, only one at a time
|
||||
synchronized (SystemTray.this) {
|
||||
digest.reset();
|
||||
digest.update(bytes);
|
||||
|
||||
// For KDE4, it must also be unique across runs
|
||||
byte[] longBytes = new byte[8];
|
||||
ByteBuffer wrap = ByteBuffer.wrap(longBytes);
|
||||
wrap.putLong(runtimeRandom);
|
||||
digest.update(longBytes);
|
||||
|
||||
byte[] hashBytes = digest.digest();
|
||||
String hash = new BigInteger(1, hashBytes).toString(32);
|
||||
|
||||
newFile = new File(tempDir, hash + '.' + extension).getAbsoluteFile();
|
||||
newFile.deleteOnExit();
|
||||
}
|
||||
|
||||
|
||||
InputStream inStream = null;
|
||||
OutputStream outStream = null;
|
||||
|
||||
try {
|
||||
inStream = systemResource.openStream();
|
||||
outStream = new FileOutputStream(newFile);
|
||||
|
||||
byte[] buffer = new byte[2048];
|
||||
int read = 0;
|
||||
while ((read = inStream.read(buffer)) > 0) {
|
||||
outStream.write(buffer, 0, read);
|
||||
}
|
||||
|
||||
return newFile.getAbsolutePath();
|
||||
} catch (IOException e) {
|
||||
// Running from main line.
|
||||
String message = "Unable to copy icon '" + fileName + "' to location: '" + newFile.getAbsolutePath() + "'";
|
||||
logger.error(message, e);
|
||||
throw new RuntimeException(message);
|
||||
} finally {
|
||||
try {
|
||||
if (inStream != null) {
|
||||
inStream.close();
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
try {
|
||||
if (outStream != null) {
|
||||
outStream.close();
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
|
||||
// appIndicator/gtk require strings
|
||||
// swing version loads as an image
|
||||
}
|
||||
}
|
||||
|
||||
// Running from main line.
|
||||
String message = "Unable to find icon '" + fileName + "'";
|
||||
logger.error(message);
|
||||
throw new RuntimeException(message);
|
||||
}
|
||||
|
||||
public final void setFailureCallback(FailureCallback failureCallback) {
|
||||
this.failureCallback = failureCallback;
|
||||
}
|
||||
|
||||
public final boolean isActive() {
|
||||
return this.active;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
package dorkbox.util.tray;
|
||||
|
||||
|
||||
public interface SystemTrayMenuAction {
|
||||
void onClick(SystemTray systemTray);
|
||||
}
|
|
@ -0,0 +1,66 @@
|
|||
package dorkbox.util.tray;
|
||||
|
||||
import java.awt.Dimension;
|
||||
import java.awt.MouseInfo;
|
||||
import java.awt.Point;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
|
||||
import javax.swing.JPopupMenu;
|
||||
import javax.swing.SwingUtilities;
|
||||
|
||||
import dorkbox.util.DelayTimer;
|
||||
|
||||
public class SystemTrayMenuPopup extends JPopupMenu {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private DelayTimer timer;
|
||||
|
||||
public SystemTrayMenuPopup() {
|
||||
super();
|
||||
|
||||
this.timer = new DelayTimer("PopupMenuHider", true, new DelayTimer.Callback() {
|
||||
@Override
|
||||
public void execute() {
|
||||
SwingUtilities.invokeLater(new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
Point location = MouseInfo.getPointerInfo().getLocation();
|
||||
Point locationOnScreen = getLocationOnScreen();
|
||||
Dimension size = getSize();
|
||||
|
||||
|
||||
if (location.x >= locationOnScreen.x && location.x < locationOnScreen.x + size.width &&
|
||||
location.y >= locationOnScreen.y && location.y < locationOnScreen.y + size.height) {
|
||||
SystemTrayMenuPopup.this.timer.delay(SystemTrayMenuPopup.this.timer.getDelay());
|
||||
} else {
|
||||
setVisible(false);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
addMouseListener(new MouseAdapter() {
|
||||
@Override
|
||||
public void mouseExited(MouseEvent event) {
|
||||
// wait before checking if mouse is still on the menu
|
||||
SystemTrayMenuPopup.this.timer.delay(SystemTrayMenuPopup.this.timer.getDelay());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void setVisible(boolean b) {
|
||||
this.timer.cancel();
|
||||
|
||||
if (b) {
|
||||
// if the mouse isn't inside the popup in 5 seconds, close the popup
|
||||
this.timer.delay(5000L);
|
||||
}
|
||||
|
||||
super.setVisible(b);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,223 @@
|
|||
package dorkbox.util.tray.linux;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.sun.jna.Pointer;
|
||||
|
||||
import dorkbox.util.jna.linux.AppIndicator;
|
||||
import dorkbox.util.jna.linux.Gobject;
|
||||
import dorkbox.util.jna.linux.Gtk;
|
||||
import dorkbox.util.jna.linux.GtkSupport;
|
||||
import dorkbox.util.tray.SystemTray;
|
||||
import dorkbox.util.tray.SystemTrayMenuAction;
|
||||
|
||||
|
||||
/**
|
||||
* Class for handling all system tray interactions.
|
||||
*
|
||||
* specialization for using app indicators in ubuntu unity
|
||||
*
|
||||
* Heavily modified from
|
||||
*
|
||||
* Lantern: https://github.com/getlantern/lantern/
|
||||
* Apache 2.0 License
|
||||
* Copyright 2010 Brave New Software Project, Inc.
|
||||
*/
|
||||
public class AppIndicatorTray extends SystemTray {
|
||||
private static AppIndicator libappindicator = AppIndicator.INSTANCE;
|
||||
private static Gobject libgobject = Gobject.INSTANCE;
|
||||
private static Gtk libgtk = Gtk.INSTANCE;
|
||||
|
||||
|
||||
private final Map<String, MenuEntry> menuEntries = new HashMap<String, MenuEntry>(2);
|
||||
|
||||
private volatile AppIndicator.AppIndicatorInstanceStruct appIndicator;
|
||||
private volatile Pointer menu;
|
||||
|
||||
private volatile Pointer connectionStatusItem;
|
||||
|
||||
// need to hang on to these to prevent gc
|
||||
private List<Pointer> widgets = new ArrayList<Pointer>(4);
|
||||
|
||||
private Thread gtkUpdateThread;
|
||||
|
||||
|
||||
public AppIndicatorTray() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void createTray(String iconName) {
|
||||
this.appIndicator = libappindicator.app_indicator_new(this.appName, "indicator-messages-new", AppIndicator.CATEGORY_APPLICATION_STATUS);
|
||||
|
||||
/* basically a hack -- we should subclass the AppIndicator
|
||||
type and override the fallback entry in the 'vtable', instead we just
|
||||
hack the app indicator class itself. Not an issue unless we need other
|
||||
appindicators.
|
||||
*/
|
||||
AppIndicator.AppIndicatorClassStruct aiclass = new AppIndicator.AppIndicatorClassStruct(this.appIndicator.parent.g_type_instance.g_class);
|
||||
AppIndicator.Fallback replacementFallback = new AppIndicator.Fallback() {
|
||||
@Override
|
||||
public Pointer callback(final AppIndicator.AppIndicatorInstanceStruct self) {
|
||||
AppIndicatorTray.this.callbackExecutor.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
logger.warn("Failed to create appindicator system tray.");
|
||||
|
||||
if (AppIndicatorTray.this.failureCallback != null) {
|
||||
AppIndicatorTray.this.failureCallback.createTrayFailed();
|
||||
}
|
||||
}
|
||||
});
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
aiclass.fallback = replacementFallback;
|
||||
aiclass.write();
|
||||
|
||||
|
||||
this.menu = libgtk.gtk_menu_new();
|
||||
libappindicator.app_indicator_set_menu(this.appIndicator, this.menu);
|
||||
|
||||
libappindicator.app_indicator_set_icon_full(this.appIndicator, iconPath(iconName), this.appName);
|
||||
libappindicator.app_indicator_set_status(this.appIndicator, AppIndicator.STATUS_ACTIVE);
|
||||
|
||||
if (!GtkSupport.usesSwtMainLoop) {
|
||||
this.gtkUpdateThread = new Thread() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
libgtk.gtk_main();
|
||||
} catch (Throwable t) {
|
||||
logger.warn("Unable to run main loop", t);
|
||||
}
|
||||
}
|
||||
};
|
||||
this.gtkUpdateThread.setName("GTK event loop");
|
||||
this.gtkUpdateThread.setDaemon(true);
|
||||
this.gtkUpdateThread.start();
|
||||
}
|
||||
|
||||
this.active = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeTray() {
|
||||
for (Pointer widget : this.widgets) {
|
||||
libgtk.gtk_widget_destroy(widget);
|
||||
}
|
||||
|
||||
// this hides the indicator
|
||||
libappindicator.app_indicator_set_status(this.appIndicator, AppIndicator.STATUS_PASSIVE);
|
||||
this.appIndicator.write();
|
||||
Pointer p = this.appIndicator.getPointer();
|
||||
libgobject.g_object_unref(p);
|
||||
|
||||
this.active = false;
|
||||
|
||||
|
||||
// GC it
|
||||
this.appIndicator = null;
|
||||
this.widgets.clear();
|
||||
|
||||
// unrefs the children too
|
||||
libgobject.g_object_unref(this.menu);
|
||||
this.menu = null;
|
||||
|
||||
synchronized (this.menuEntries) {
|
||||
this.menuEntries.clear();
|
||||
}
|
||||
|
||||
this.connectionStatusItem = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setStatus(String infoString, String iconName) {
|
||||
if (this.connectionStatusItem == null) {
|
||||
this.connectionStatusItem = libgtk.gtk_menu_item_new_with_label(infoString);
|
||||
this.widgets.add(this.connectionStatusItem);
|
||||
libgtk.gtk_widget_set_sensitive(this.connectionStatusItem, Gtk.FALSE);
|
||||
libgtk.gtk_menu_shell_append(this.menu, this.connectionStatusItem);
|
||||
} else {
|
||||
libgtk.gtk_menu_item_set_label(this.connectionStatusItem, infoString);
|
||||
}
|
||||
|
||||
libgtk.gtk_widget_show_all(this.connectionStatusItem);
|
||||
|
||||
libappindicator.app_indicator_set_icon_full(this.appIndicator, iconPath(iconName), this.appName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Will add a new menu entry, or update one if it already exists
|
||||
*/
|
||||
@Override
|
||||
public void addMenuEntry(String menuText, final SystemTrayMenuAction callback) {
|
||||
synchronized (this.menuEntries) {
|
||||
MenuEntry menuEntry = this.menuEntries.get(menuText);
|
||||
|
||||
if (menuEntry == null) {
|
||||
Pointer dashboardItem = libgtk.gtk_menu_item_new_with_label(menuText);
|
||||
Gobject.GCallback gtkCallback = new Gobject.GCallback() {
|
||||
@Override
|
||||
public void callback(Pointer instance, Pointer data) {
|
||||
AppIndicatorTray.this.callbackExecutor.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
callback.onClick(AppIndicatorTray.this);
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
libgobject.g_signal_connect_data(dashboardItem, "activate", gtkCallback, null, null, 0);
|
||||
libgtk.gtk_menu_shell_append(this.menu, dashboardItem);
|
||||
libgtk.gtk_widget_show_all(dashboardItem);
|
||||
|
||||
menuEntry = new MenuEntry();
|
||||
menuEntry.dashboardItem = dashboardItem;
|
||||
menuEntry.gtkCallback = gtkCallback;
|
||||
|
||||
this.menuEntries.put(menuText, menuEntry);
|
||||
} else {
|
||||
updateMenuEntry(menuText, menuText, callback);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Will update an already existing menu entry (or add a new one, if it doesn't exist)
|
||||
*/
|
||||
@Override
|
||||
public void updateMenuEntry(String origMenuText, String newMenuText, final SystemTrayMenuAction newCallback) {
|
||||
synchronized (this.menuEntries) {
|
||||
MenuEntry menuEntry = this.menuEntries.get(origMenuText);
|
||||
|
||||
if (menuEntry != null) {
|
||||
libgtk.gtk_menu_item_set_label(menuEntry.dashboardItem, newMenuText);
|
||||
|
||||
Gobject.GCallback gtkCallback = new Gobject.GCallback() {
|
||||
@Override
|
||||
public void callback(Pointer instance, Pointer data) {
|
||||
AppIndicatorTray.this.callbackExecutor.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
newCallback.onClick(AppIndicatorTray.this);
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
libgobject.g_signal_connect_data(menuEntry.dashboardItem, "activate", gtkCallback, null, null, 0);
|
||||
|
||||
menuEntry.gtkCallback = gtkCallback;
|
||||
|
||||
libgtk.gtk_widget_show_all(menuEntry.dashboardItem);
|
||||
} else {
|
||||
addMenuEntry(origMenuText, newCallback);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,294 @@
|
|||
package dorkbox.util.tray.linux;
|
||||
|
||||
import java.awt.Dimension;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.swing.JMenuItem;
|
||||
import javax.swing.SwingUtilities;
|
||||
|
||||
import com.sun.jna.Pointer;
|
||||
|
||||
import dorkbox.util.jna.linux.Gobject;
|
||||
import dorkbox.util.jna.linux.Gtk;
|
||||
import dorkbox.util.jna.linux.Gtk.GdkEventButton;
|
||||
import dorkbox.util.jna.linux.GtkSupport;
|
||||
import dorkbox.util.tray.SystemTray;
|
||||
import dorkbox.util.tray.SystemTrayMenuAction;
|
||||
import dorkbox.util.tray.SystemTrayMenuPopup;
|
||||
|
||||
|
||||
/**
|
||||
* Class for handling all system tray interactions via GTK.
|
||||
*
|
||||
* This is the "old" way to do it, and does not work with some desktop environments.
|
||||
*/
|
||||
public class GtkSystemTray extends SystemTray {
|
||||
private static Gobject libgobject = Gobject.INSTANCE;
|
||||
private static Gtk libgtk = Gtk.INSTANCE;
|
||||
|
||||
private final Map<String, JMenuItem> menuEntries = new HashMap<String, JMenuItem>(2);
|
||||
|
||||
private volatile SystemTrayMenuPopup jmenu;
|
||||
private volatile JMenuItem connectionStatusItem;
|
||||
|
||||
private volatile Pointer trayIcon;
|
||||
|
||||
// need to hang on to these to prevent gc
|
||||
private List<Pointer> widgets = new ArrayList<Pointer>(4);
|
||||
private Thread gtkUpdateThread;
|
||||
|
||||
public GtkSystemTray() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void createTray(String iconName) {
|
||||
this.trayIcon = libgtk.gtk_status_icon_new();
|
||||
libgtk.gtk_status_icon_set_from_file(this.trayIcon, iconPath(iconName));
|
||||
libgtk.gtk_status_icon_set_tooltip(this.trayIcon, this.appName);
|
||||
libgtk.gtk_status_icon_set_visible(this.trayIcon, true);
|
||||
|
||||
Gobject.GEventCallback gtkCallback = new Gobject.GEventCallback() {
|
||||
@Override
|
||||
public void callback(Pointer instance, final GdkEventButton event) {
|
||||
// BUTTON_PRESS only
|
||||
if (event.type == 4) {
|
||||
SwingUtilities.invokeLater(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (GtkSystemTray.this.jmenu.isVisible()) {
|
||||
GtkSystemTray.this.jmenu.setVisible(false);
|
||||
} else {
|
||||
int iconX = (int) (event.x_root - event.x);
|
||||
int iconY = (int) (event.y_root - event.y);
|
||||
// System.err.println("x: " + iconX + " y: " + iconY);
|
||||
// System.err.println("x1: " + event.x_root + " y1: " + event.y_root); // relative to SCREEN
|
||||
// System.err.println("x2: " + event.x + " y2: " + event.y); // relative to WINDOW
|
||||
|
||||
Dimension size = GtkSystemTray.this.jmenu.getPreferredSize();
|
||||
|
||||
// do we open at top-right or top-left?
|
||||
// we ASSUME monitor size is greater than 640x480 AND that our tray icon is IN THE CORNER SOMEWHERE
|
||||
|
||||
// always put the menu in the middle
|
||||
iconX -= size.width/2;
|
||||
|
||||
// y = 2 -> top
|
||||
// y = 1068 -> bottom
|
||||
if (iconY > 240) {
|
||||
iconY -= size.height;
|
||||
} else {
|
||||
// have to account for the icon
|
||||
iconY += ICON_SIZE;
|
||||
}
|
||||
|
||||
GtkSystemTray.this.jmenu.setInvoker(GtkSystemTray.this.jmenu);
|
||||
GtkSystemTray.this.jmenu.setLocation(iconX, iconY);
|
||||
GtkSystemTray.this.jmenu.setVisible(true);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
// all the clicks. This is because native menu popups are a pain to figure out, so we cheat and use some java bits to do the popup
|
||||
libgobject.g_signal_connect_data(this.trayIcon, "button_press_event", gtkCallback, null, null, 0);
|
||||
|
||||
if (!GtkSupport.usesSwtMainLoop) {
|
||||
this.gtkUpdateThread = new Thread() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
libgtk.gtk_main();
|
||||
} catch (Throwable t) {
|
||||
logger.warn("Unable to run main loop", t);
|
||||
}
|
||||
}
|
||||
};
|
||||
this.gtkUpdateThread.setName("GTK event loop");
|
||||
this.gtkUpdateThread.setDaemon(true);
|
||||
this.gtkUpdateThread.start();
|
||||
}
|
||||
|
||||
try {
|
||||
SwingUtilities.invokeAndWait(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
GtkSystemTray.this.jmenu = new SystemTrayMenuPopup();
|
||||
}
|
||||
});
|
||||
} catch (InvocationTargetException e) {
|
||||
logger.error("Error creating tray menu", e);
|
||||
} catch (InterruptedException e) {
|
||||
logger.error("Error creating tray menu", e);
|
||||
}
|
||||
|
||||
this.active = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeTray() {
|
||||
for (Pointer widget : this.widgets) {
|
||||
libgtk.gtk_widget_destroy(widget);
|
||||
}
|
||||
|
||||
// this hides the indicator
|
||||
libgtk.gtk_status_icon_set_visible(this.trayIcon, false);
|
||||
libgobject.g_object_unref(this.trayIcon);
|
||||
|
||||
this.active = false;
|
||||
|
||||
|
||||
// GC it
|
||||
this.trayIcon = null;
|
||||
this.widgets.clear();
|
||||
|
||||
synchronized (this.menuEntries) {
|
||||
this.menuEntries.clear();
|
||||
}
|
||||
|
||||
this.jmenu.setVisible(false);
|
||||
this.jmenu.setEnabled(false);
|
||||
|
||||
this.jmenu = null;
|
||||
this.connectionStatusItem = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setStatus(final String infoString, String iconName) {
|
||||
Runnable doRun = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (GtkSystemTray.this.connectionStatusItem == null) {
|
||||
GtkSystemTray.this.connectionStatusItem = new JMenuItem(infoString);
|
||||
GtkSystemTray.this.connectionStatusItem.setEnabled(false);
|
||||
GtkSystemTray.this.jmenu.add(GtkSystemTray.this.connectionStatusItem);
|
||||
} else {
|
||||
GtkSystemTray.this.connectionStatusItem.setText(infoString);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
if (SwingUtilities.isEventDispatchThread()) {
|
||||
doRun.run();
|
||||
} else {
|
||||
try {
|
||||
SwingUtilities.invokeAndWait(doRun);
|
||||
} catch (InvocationTargetException e) {
|
||||
logger.error("Error updating tray menu", e);
|
||||
} catch (InterruptedException e) {
|
||||
logger.error("Error updating tray menu", e);
|
||||
}
|
||||
}
|
||||
|
||||
libgtk.gtk_status_icon_set_from_file(GtkSystemTray.this.trayIcon, iconPath(iconName));
|
||||
}
|
||||
|
||||
/**
|
||||
* Will add a new menu entry, or update one if it already exists
|
||||
*/
|
||||
@Override
|
||||
public void addMenuEntry(final String menuText, final SystemTrayMenuAction callback) {
|
||||
Runnable doRun = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
Map<String, JMenuItem> menuEntries2 = GtkSystemTray.this.menuEntries;
|
||||
|
||||
synchronized (menuEntries2) {
|
||||
JMenuItem menuEntry = menuEntries2.get(menuText);
|
||||
|
||||
if (menuEntry == null) {
|
||||
SystemTrayMenuPopup menu = GtkSystemTray.this.jmenu;
|
||||
|
||||
menuEntry = new JMenuItem(menuText);
|
||||
menuEntry.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
GtkSystemTray.this.callbackExecutor.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
callback.onClick(GtkSystemTray.this);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
menu.add(menuEntry);
|
||||
|
||||
menuEntries2.put(menuText, menuEntry);
|
||||
} else {
|
||||
updateMenuEntry(menuText, menuText, callback);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
if (SwingUtilities.isEventDispatchThread()) {
|
||||
doRun.run();
|
||||
} else {
|
||||
try {
|
||||
SwingUtilities.invokeAndWait(doRun);
|
||||
} catch (InvocationTargetException e) {
|
||||
logger.error("Error updating tray menu", e);
|
||||
} catch (InterruptedException e) {
|
||||
logger.error("Error updating tray menu", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Will update an already existing menu entry (or add a new one, if it doesn't exist)
|
||||
*/
|
||||
@Override
|
||||
public void updateMenuEntry(final String origMenuText, final String newMenuText, final SystemTrayMenuAction newCallback) {
|
||||
Runnable doRun = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
Map<String, JMenuItem> menuEntries2 = GtkSystemTray.this.menuEntries;
|
||||
|
||||
synchronized (menuEntries2) {
|
||||
JMenuItem menuEntry = menuEntries2.get(origMenuText);
|
||||
|
||||
if (menuEntry != null) {
|
||||
ActionListener[] actionListeners = menuEntry.getActionListeners();
|
||||
for (ActionListener l : actionListeners) {
|
||||
menuEntry.removeActionListener(l);
|
||||
}
|
||||
|
||||
menuEntry.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
GtkSystemTray.this.callbackExecutor.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
newCallback.onClick(GtkSystemTray.this);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
menuEntry.setText(newMenuText);
|
||||
menuEntry.revalidate();
|
||||
} else {
|
||||
addMenuEntry(origMenuText, newCallback);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
if (SwingUtilities.isEventDispatchThread()) {
|
||||
doRun.run();
|
||||
} else {
|
||||
try {
|
||||
SwingUtilities.invokeAndWait(doRun);
|
||||
} catch (InvocationTargetException e) {
|
||||
logger.error("Error updating tray menu", e);
|
||||
} catch (InterruptedException e) {
|
||||
logger.error("Error updating tray menu", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
package dorkbox.util.tray.linux;
|
||||
|
||||
import com.sun.jna.Pointer;
|
||||
|
||||
import dorkbox.util.jna.linux.Gobject;
|
||||
|
||||
/**
|
||||
* Can only access this from within a synchronized block!
|
||||
*/
|
||||
class MenuEntry {
|
||||
public final int hashCode;
|
||||
public Pointer dashboardItem;
|
||||
public Gobject.GCallback gtkCallback;
|
||||
|
||||
public MenuEntry() {
|
||||
long time = System.nanoTime();
|
||||
this.hashCode = (int)(time^time>>>32);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return this.hashCode;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (obj == null) {
|
||||
return false;
|
||||
}
|
||||
if (getClass() != obj.getClass()) {
|
||||
return false;
|
||||
}
|
||||
MenuEntry other = (MenuEntry) obj;
|
||||
if (this.hashCode != other.hashCode) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,357 @@
|
|||
package dorkbox.util.tray.swing;
|
||||
|
||||
|
||||
import java.awt.AWTException;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.GraphicsConfiguration;
|
||||
import java.awt.GraphicsDevice;
|
||||
import java.awt.GraphicsEnvironment;
|
||||
import java.awt.Image;
|
||||
import java.awt.Point;
|
||||
import java.awt.Rectangle;
|
||||
import java.awt.SystemTray;
|
||||
import java.awt.TrayIcon;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.swing.ImageIcon;
|
||||
import javax.swing.JMenuItem;
|
||||
import javax.swing.SwingUtilities;
|
||||
|
||||
import dorkbox.util.tray.SystemTrayMenuAction;
|
||||
import dorkbox.util.tray.SystemTrayMenuPopup;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Class for handling all system tray interaction, via SWING
|
||||
*/
|
||||
public class SwingSystemTray extends dorkbox.util.tray.SystemTray {
|
||||
private final Map<String, JMenuItem> menuEntries = new HashMap<String, JMenuItem>(2);
|
||||
|
||||
private volatile SystemTrayMenuPopup jmenu;
|
||||
private volatile JMenuItem connectionStatusItem;
|
||||
|
||||
private volatile SystemTray tray;
|
||||
private volatile TrayIcon trayIcon;
|
||||
|
||||
/**
|
||||
* Creates a new system tray handler class.
|
||||
*/
|
||||
public SwingSystemTray() {
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void removeTray() {
|
||||
Runnable doRun = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
SwingSystemTray.this.tray.remove(SwingSystemTray.this.trayIcon);
|
||||
SwingSystemTray.this.menuEntries.clear();
|
||||
}
|
||||
};
|
||||
|
||||
if (SwingUtilities.isEventDispatchThread()) {
|
||||
doRun.run();
|
||||
} else {
|
||||
try {
|
||||
SwingUtilities.invokeAndWait(doRun);
|
||||
} catch (InvocationTargetException e) {
|
||||
logger.error("Error updating tray menu", e);
|
||||
} catch (InterruptedException e) {
|
||||
logger.error("Error updating tray menu", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void createTray(final String iconName) {
|
||||
Runnable doRun = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
SwingSystemTray.this.tray = SystemTray.getSystemTray();
|
||||
if (SwingSystemTray.this.tray == null) {
|
||||
logger.warn("The system tray is not available");
|
||||
} else {
|
||||
SwingSystemTray.this.jmenu = new SystemTrayMenuPopup();
|
||||
|
||||
Image trayImage = newImage(iconName);
|
||||
SwingSystemTray.this.trayIcon = new TrayIcon(trayImage);
|
||||
SwingSystemTray.this.trayIcon.setToolTip(SwingSystemTray.this.appName);
|
||||
|
||||
SwingSystemTray.this.trayIcon.addMouseListener(new MouseAdapter() {
|
||||
@Override
|
||||
public void mousePressed(MouseEvent e) {
|
||||
Dimension size = SwingSystemTray.this.jmenu.getPreferredSize();
|
||||
|
||||
Point point = e.getPoint();
|
||||
Rectangle bounds = getScreenBoundsAt(point);
|
||||
|
||||
|
||||
int x = point.x;
|
||||
int y = point.y;
|
||||
|
||||
if (y < bounds.y) {
|
||||
y = bounds.y;
|
||||
} else if (y > bounds.y + bounds.height) {
|
||||
y = bounds.y + bounds.height + ICON_SIZE + 4; // 4 for padding
|
||||
}
|
||||
if (x < bounds.x) {
|
||||
x = bounds.x;
|
||||
} else if (x > bounds.x + bounds.width) {
|
||||
x = bounds.x + bounds.width;
|
||||
}
|
||||
|
||||
if (x + size.width > bounds.x + bounds.width) {
|
||||
// always put the menu in the middle
|
||||
x = bounds.x + bounds.width - size.width;
|
||||
}
|
||||
if (y + size.height > bounds.y + bounds.height) {
|
||||
y = bounds.y + bounds.height - size.height - ICON_SIZE - 4; // 4 for padding
|
||||
}
|
||||
|
||||
// do we open at top-right or top-left?
|
||||
// we ASSUME monitor size is greater than 640x480 AND that our tray icon is IN THE CORNER SOMEWHERE
|
||||
|
||||
// always put the menu in the middle
|
||||
x -= size.width/4;
|
||||
|
||||
|
||||
SwingSystemTray.this.jmenu.setInvoker(SwingSystemTray.this.jmenu);
|
||||
SwingSystemTray.this.jmenu.setLocation(x, y);
|
||||
SwingSystemTray.this.jmenu.setVisible(true);
|
||||
}
|
||||
});
|
||||
|
||||
try {
|
||||
SwingSystemTray.this.tray.add(SwingSystemTray.this.trayIcon);
|
||||
SwingSystemTray.this.active = true;
|
||||
} catch (AWTException e) {
|
||||
logger.error("TrayIcon could not be added.", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
if (SwingUtilities.isEventDispatchThread()) {
|
||||
doRun.run();
|
||||
} else {
|
||||
try {
|
||||
SwingUtilities.invokeAndWait(doRun);
|
||||
} catch (InvocationTargetException e) {
|
||||
logger.error("Error creating tray menu", e);
|
||||
} catch (InterruptedException e) {
|
||||
logger.error("Error creating tray menu", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected Image newImage(String name) {
|
||||
String iconPath = iconPath(name);
|
||||
|
||||
Image scaledInstance = new ImageIcon(iconPath).getImage().getScaledInstance(ICON_SIZE, ICON_SIZE, Image.SCALE_SMOOTH);
|
||||
return scaledInstance;
|
||||
}
|
||||
|
||||
|
||||
// public static Rectangle getSafeScreenBounds(Point pos) {
|
||||
// Rectangle bounds = getScreenBoundsAt(pos);
|
||||
// Insets insets = getScreenInsetsAt(pos);
|
||||
//
|
||||
// bounds.x += insets.left;
|
||||
// bounds.y += insets.top;
|
||||
// bounds.width -= insets.left + insets.right;
|
||||
// bounds.height -= insets.top + insets.bottom;
|
||||
//
|
||||
// return bounds;
|
||||
// }
|
||||
|
||||
// public static Insets getScreenInsetsAt(Point pos) {
|
||||
// GraphicsDevice gd = getGraphicsDeviceAt(pos);
|
||||
// Insets insets = null;
|
||||
// if (gd != null) {
|
||||
// insets = Toolkit.getDefaultToolkit().getScreenInsets(gd.getDefaultConfiguration());
|
||||
// }
|
||||
//
|
||||
// return insets;
|
||||
// }
|
||||
|
||||
public static Rectangle getScreenBoundsAt(Point pos) {
|
||||
GraphicsDevice gd = getGraphicsDeviceAt(pos);
|
||||
Rectangle bounds = null;
|
||||
if (gd != null) {
|
||||
bounds = gd.getDefaultConfiguration().getBounds();
|
||||
}
|
||||
|
||||
return bounds;
|
||||
}
|
||||
|
||||
public static GraphicsDevice getGraphicsDeviceAt(Point pos) {
|
||||
GraphicsDevice device = null;
|
||||
|
||||
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
|
||||
GraphicsDevice lstGDs[] = ge.getScreenDevices();
|
||||
|
||||
ArrayList<GraphicsDevice> lstDevices = new ArrayList<GraphicsDevice>(lstGDs.length);
|
||||
|
||||
for (GraphicsDevice gd : lstGDs) {
|
||||
|
||||
GraphicsConfiguration gc = gd.getDefaultConfiguration();
|
||||
Rectangle screenBounds = gc.getBounds();
|
||||
|
||||
if (screenBounds.contains(pos)) {
|
||||
lstDevices.add(gd);
|
||||
}
|
||||
}
|
||||
|
||||
if (lstDevices.size() > 0) {
|
||||
device = lstDevices.get(0);
|
||||
} else {
|
||||
device = ge.getDefaultScreenDevice();
|
||||
}
|
||||
|
||||
return device;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setStatus(final String infoString, final String iconName) {
|
||||
Runnable doRun = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (SwingSystemTray.this.connectionStatusItem == null) {
|
||||
SwingSystemTray.this.connectionStatusItem = new JMenuItem(infoString);
|
||||
SwingSystemTray.this.connectionStatusItem.setEnabled(false);
|
||||
SwingSystemTray.this.jmenu.add(SwingSystemTray.this.connectionStatusItem);
|
||||
} else {
|
||||
SwingSystemTray.this.connectionStatusItem.setText(infoString);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
if (SwingUtilities.isEventDispatchThread()) {
|
||||
doRun.run();
|
||||
} else {
|
||||
try {
|
||||
SwingUtilities.invokeAndWait(doRun);
|
||||
} catch (InvocationTargetException e) {
|
||||
logger.error("Error updating tray menu", e);
|
||||
} catch (InterruptedException e) {
|
||||
logger.error("Error updating tray menu", e);
|
||||
}
|
||||
}
|
||||
|
||||
Image trayImage = newImage(iconName);
|
||||
SwingSystemTray.this.trayIcon.setImage(trayImage);
|
||||
}
|
||||
|
||||
/**
|
||||
* Will add a new menu entry, or update one if it already exists
|
||||
*/
|
||||
@Override
|
||||
public void addMenuEntry(final String menuText, final SystemTrayMenuAction callback) {
|
||||
Runnable doRun = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
Map<String, JMenuItem> menuEntries2 = SwingSystemTray.this.menuEntries;
|
||||
|
||||
synchronized (menuEntries2) {
|
||||
JMenuItem menuEntry = menuEntries2.get(menuText);
|
||||
|
||||
if (menuEntry == null) {
|
||||
SystemTrayMenuPopup menu = SwingSystemTray.this.jmenu;
|
||||
|
||||
menuEntry = new JMenuItem(menuText);
|
||||
menuEntry.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
SwingSystemTray.this.callbackExecutor.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
callback.onClick(SwingSystemTray.this);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
menu.add(menuEntry);
|
||||
|
||||
menuEntries2.put(menuText, menuEntry);
|
||||
} else {
|
||||
updateMenuEntry(menuText, menuText, callback);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
if (SwingUtilities.isEventDispatchThread()) {
|
||||
doRun.run();
|
||||
} else {
|
||||
try {
|
||||
SwingUtilities.invokeAndWait(doRun);
|
||||
} catch (InvocationTargetException e) {
|
||||
logger.error("Error updating tray menu", e);
|
||||
} catch (InterruptedException e) {
|
||||
logger.error("Error updating tray menu", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Will update an already existing menu entry (or add a new one, if it doesn't exist)
|
||||
*/
|
||||
@Override
|
||||
public void updateMenuEntry(final String origMenuText, final String newMenuText, final SystemTrayMenuAction newCallback) {
|
||||
Runnable doRun = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
Map<String, JMenuItem> menuEntries2 = SwingSystemTray.this.menuEntries;
|
||||
|
||||
synchronized (menuEntries2) {
|
||||
JMenuItem menuEntry = menuEntries2.get(origMenuText);
|
||||
|
||||
if (menuEntry != null) {
|
||||
ActionListener[] actionListeners = menuEntry.getActionListeners();
|
||||
for (ActionListener l : actionListeners) {
|
||||
menuEntry.removeActionListener(l);
|
||||
}
|
||||
|
||||
menuEntry.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
SwingSystemTray.this.callbackExecutor.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
newCallback.onClick(SwingSystemTray.this);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
menuEntry.setText(newMenuText);
|
||||
menuEntry.revalidate();
|
||||
} else {
|
||||
addMenuEntry(origMenuText, newCallback);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
if (SwingUtilities.isEventDispatchThread()) {
|
||||
doRun.run();
|
||||
} else {
|
||||
try {
|
||||
SwingUtilities.invokeAndWait(doRun);
|
||||
} catch (InvocationTargetException e) {
|
||||
logger.error("Error updating tray menu", e);
|
||||
} catch (InterruptedException e) {
|
||||
logger.error("Error updating tray menu", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue