Modified google code format
This commit is contained in:
parent
f48754838a
commit
462e15a291
@ -1,6 +1,20 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2014 dorkbox, llc
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
package dorkbox.util.tray;
|
package dorkbox.util.tray;
|
||||||
|
|
||||||
public interface FailureCallback {
|
public interface FailureCallback {
|
||||||
|
public void createTrayFailed();
|
||||||
public void createTrayFailed();
|
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,20 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2014 dorkbox, llc
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
package dorkbox.util.tray;
|
package dorkbox.util.tray;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -18,6 +30,9 @@ import java.security.SecureRandom;
|
|||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import dorkbox.util.NamedThreadFactory;
|
import dorkbox.util.NamedThreadFactory;
|
||||||
import dorkbox.util.OS;
|
import dorkbox.util.OS;
|
||||||
import dorkbox.util.jna.linux.GtkSupport;
|
import dorkbox.util.jna.linux.GtkSupport;
|
||||||
@ -31,197 +46,195 @@ import dorkbox.util.tray.swing.SwingSystemTray;
|
|||||||
*/
|
*/
|
||||||
public abstract class SystemTray {
|
public abstract class SystemTray {
|
||||||
|
|
||||||
private static final Charset UTF_8 = Charset.forName("UTF-8");
|
private static final Charset UTF_8 = Charset.forName("UTF-8");
|
||||||
private static MessageDigest digest;
|
private static MessageDigest digest;
|
||||||
|
|
||||||
protected static final Logger logger = LoggerFactory.getLogger(SystemTray.class);
|
protected static final Logger logger = LoggerFactory.getLogger(SystemTray.class);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Size of the icon *
|
* Size of the icon
|
||||||
*/
|
*/
|
||||||
public static int ICON_SIZE = 22;
|
public static int ICON_SIZE = 22;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Location of the icon *
|
* Location of the icon
|
||||||
*/
|
*/
|
||||||
public static String ICON_PATH = "";
|
public static String ICON_PATH = "";
|
||||||
|
|
||||||
private static final long runtimeRandom = new SecureRandom().nextLong();
|
private static final long runtimeRandom = new SecureRandom().nextLong();
|
||||||
private static Class<? extends SystemTray> trayType;
|
private static Class<? extends SystemTray> trayType;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
if (OS.isLinux()) {
|
if (OS.isLinux()) {
|
||||||
GtkSupport.init();
|
GtkSupport.init();
|
||||||
String getenv = System.getenv("XDG_CURRENT_DESKTOP");
|
String getenv = System.getenv("XDG_CURRENT_DESKTOP");
|
||||||
if (getenv != null && (getenv.equals("Unity") || getenv.equals("KDE"))) {
|
if (getenv != null && (getenv.equals("Unity") || getenv.equals("KDE"))) {
|
||||||
if (GtkSupport.isSupported) {
|
if (GtkSupport.isSupported) {
|
||||||
trayType = AppIndicatorTray.class;
|
trayType = AppIndicatorTray.class;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (GtkSupport.isSupported) {
|
if (GtkSupport.isSupported) {
|
||||||
trayType = GtkSystemTray.class;
|
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 algorithm!");
|
|
||||||
trayType = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected final ExecutorService
|
|
||||||
callbackExecutor =
|
|
||||||
Executors.newSingleThreadExecutor(new NamedThreadFactory("SysTrayExecutor", false));
|
|
||||||
|
|
||||||
protected 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 (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// unsupported
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void setAppName(String appName) {
|
|
||||||
this.appName = appName;
|
|
||||||
}
|
|
||||||
|
|
||||||
public abstract void createTray(String iconName);
|
|
||||||
|
|
||||||
public void removeTray() {
|
|
||||||
SystemTray.this.callbackExecutor.shutdown();
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
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;
|
|
||||||
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
|
// this is windows OR mac
|
||||||
// swing version loads as an image
|
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 algorithm!");
|
||||||
|
trayType = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Running from main line.
|
protected final ExecutorService callbackExecutor = Executors.newSingleThreadExecutor(new NamedThreadFactory("SysTrayExecutor", false));
|
||||||
String message = "Unable to find icon '" + fileName + "'";
|
|
||||||
logger.error(message);
|
|
||||||
throw new RuntimeException(message);
|
|
||||||
}
|
|
||||||
|
|
||||||
public final void setFailureCallback(FailureCallback failureCallback) {
|
protected volatile FailureCallback failureCallback;
|
||||||
this.failureCallback = failureCallback;
|
protected volatile boolean active = false;
|
||||||
}
|
protected String appName;
|
||||||
|
|
||||||
public final boolean isActive() {
|
public static SystemTray create(String appName) {
|
||||||
return this.active;
|
if (trayType != null) {
|
||||||
}
|
try {
|
||||||
|
SystemTray newInstance = trayType.newInstance();
|
||||||
|
if (newInstance != null) {
|
||||||
|
newInstance.setAppName(appName);
|
||||||
|
}
|
||||||
|
return newInstance;
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// unsupported
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setAppName(String appName) {
|
||||||
|
this.appName = appName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract void createTray(String iconName);
|
||||||
|
|
||||||
|
public void removeTray() {
|
||||||
|
SystemTray.this.callbackExecutor.shutdown();
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
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;
|
||||||
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,20 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2014 dorkbox, llc
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
package dorkbox.util.tray;
|
package dorkbox.util.tray;
|
||||||
|
|
||||||
|
|
||||||
public interface SystemTrayMenuAction {
|
public interface SystemTrayMenuAction {
|
||||||
|
void onClick(SystemTray systemTray);
|
||||||
void onClick(SystemTray systemTray);
|
|
||||||
}
|
}
|
||||||
|
@ -1,63 +1,66 @@
|
|||||||
package dorkbox.util.tray;
|
package dorkbox.util.tray;
|
||||||
|
|
||||||
import java.awt.*;
|
import java.awt.Dimension;
|
||||||
|
import java.awt.MouseInfo;
|
||||||
|
import java.awt.Point;
|
||||||
import java.awt.event.MouseAdapter;
|
import java.awt.event.MouseAdapter;
|
||||||
import java.awt.event.MouseEvent;
|
import java.awt.event.MouseEvent;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.JPopupMenu;
|
||||||
|
import javax.swing.SwingUtilities;
|
||||||
|
|
||||||
import dorkbox.util.DelayTimer;
|
import dorkbox.util.DelayTimer;
|
||||||
|
|
||||||
public class SystemTrayMenuPopup extends JPopupMenu {
|
public class SystemTrayMenuPopup extends JPopupMenu {
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
private static final long serialVersionUID = 1L;
|
private DelayTimer timer;
|
||||||
|
|
||||||
private DelayTimer timer;
|
public SystemTrayMenuPopup() {
|
||||||
|
super();
|
||||||
|
|
||||||
public SystemTrayMenuPopup() {
|
this.timer = new DelayTimer("PopupMenuHider", true, new DelayTimer.Callback() {
|
||||||
super();
|
@Override
|
||||||
|
public void execute() {
|
||||||
|
SwingUtilities.invokeLater(new Runnable() {
|
||||||
|
|
||||||
this.timer = new DelayTimer("PopupMenuHider", true, new DelayTimer.Callback() {
|
@Override
|
||||||
@Override
|
public void run() {
|
||||||
public void execute() {
|
Point location = MouseInfo.getPointerInfo().getLocation();
|
||||||
SwingUtilities.invokeLater(new Runnable() {
|
Point locationOnScreen = getLocationOnScreen();
|
||||||
|
Dimension size = getSize();
|
||||||
|
|
||||||
@Override
|
if (location.x >= locationOnScreen.x && location.x < locationOnScreen.x + size.width
|
||||||
public void run() {
|
&& location.y >= locationOnScreen.y && location.y < locationOnScreen.y + size.height) {
|
||||||
Point location = MouseInfo.getPointerInfo().getLocation();
|
|
||||||
Point locationOnScreen = getLocationOnScreen();
|
|
||||||
Dimension size = getSize();
|
|
||||||
|
|
||||||
if (location.x >= locationOnScreen.x && location.x < locationOnScreen.x + size.width &&
|
SystemTrayMenuPopup.this.timer.delay(SystemTrayMenuPopup.this.timer.getDelay());
|
||||||
location.y >= locationOnScreen.y && location.y < locationOnScreen.y + size.height) {
|
} else {
|
||||||
SystemTrayMenuPopup.this.timer.delay(SystemTrayMenuPopup.this.timer.getDelay());
|
setVisible(false);
|
||||||
} else {
|
}
|
||||||
setVisible(false);
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
addMouseListener(new MouseAdapter() {
|
addMouseListener(new MouseAdapter() {
|
||||||
@Override
|
@Override
|
||||||
public void mouseExited(MouseEvent event) {
|
public void mouseExited(MouseEvent event) {
|
||||||
// wait before checking if mouse is still on the menu
|
// wait before checking if mouse is still on the menu
|
||||||
SystemTrayMenuPopup.this.timer.delay(SystemTrayMenuPopup.this.timer.getDelay());
|
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);
|
|
||||||
}
|
@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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,28 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2014 dorkbox, llc
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
package dorkbox.util.tray.linux;
|
package dorkbox.util.tray.linux;
|
||||||
|
|
||||||
import com.sun.jna.Pointer;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.CountDownLatch;
|
import java.util.concurrent.CountDownLatch;
|
||||||
|
|
||||||
|
import com.sun.jna.Pointer;
|
||||||
|
|
||||||
import dorkbox.util.jna.linux.AppIndicator;
|
import dorkbox.util.jna.linux.AppIndicator;
|
||||||
import dorkbox.util.jna.linux.Gobject;
|
import dorkbox.util.jna.linux.Gobject;
|
||||||
import dorkbox.util.jna.linux.Gtk;
|
import dorkbox.util.jna.linux.Gtk;
|
||||||
@ -15,7 +30,6 @@ import dorkbox.util.jna.linux.GtkSupport;
|
|||||||
import dorkbox.util.tray.SystemTray;
|
import dorkbox.util.tray.SystemTray;
|
||||||
import dorkbox.util.tray.SystemTrayMenuAction;
|
import dorkbox.util.tray.SystemTrayMenuAction;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class for handling all system tray interactions.
|
* Class for handling all system tray interactions.
|
||||||
*
|
*
|
||||||
@ -26,204 +40,200 @@ import dorkbox.util.tray.SystemTrayMenuAction;
|
|||||||
* Lantern: https://github.com/getlantern/lantern/ Apache 2.0 License Copyright 2010 Brave New Software Project, Inc.
|
* Lantern: https://github.com/getlantern/lantern/ Apache 2.0 License Copyright 2010 Brave New Software Project, Inc.
|
||||||
*/
|
*/
|
||||||
public class AppIndicatorTray extends SystemTray {
|
public class AppIndicatorTray extends SystemTray {
|
||||||
|
private static final AppIndicator libappindicator = AppIndicator.INSTANCE;
|
||||||
|
private static final Gobject libgobject = Gobject.INSTANCE;
|
||||||
|
private static final Gtk libgtk = Gtk.INSTANCE;
|
||||||
|
|
||||||
private static final AppIndicator libappindicator = AppIndicator.INSTANCE;
|
private final CountDownLatch blockUntilStarted = new CountDownLatch(1);
|
||||||
private static final Gobject libgobject = Gobject.INSTANCE;
|
private final Map<String, MenuEntry> menuEntries = new HashMap<String, MenuEntry>(2);
|
||||||
private static final Gtk libgtk = Gtk.INSTANCE;
|
|
||||||
|
|
||||||
private final CountDownLatch blockUntilStarted = new CountDownLatch(1);
|
private volatile AppIndicator.AppIndicatorInstanceStruct appIndicator;
|
||||||
private final Map<String, MenuEntry> menuEntries = new HashMap<String, MenuEntry>(2);
|
private volatile Pointer menu;
|
||||||
|
|
||||||
private volatile AppIndicator.AppIndicatorInstanceStruct appIndicator;
|
private volatile Pointer connectionStatusItem;
|
||||||
private volatile Pointer menu;
|
|
||||||
|
|
||||||
private volatile Pointer connectionStatusItem;
|
// need to hang on to these to prevent gc
|
||||||
|
private final List<Pointer> widgets = new ArrayList<Pointer>(4);
|
||||||
// need to hang on to these to prevent gc
|
|
||||||
private final List<Pointer> widgets = new ArrayList<Pointer>(4);
|
|
||||||
|
|
||||||
|
|
||||||
public AppIndicatorTray() {
|
public AppIndicatorTray() {}
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void createTray(String iconName) {
|
public void createTray(String iconName) {
|
||||||
this.appIndicator =
|
this.appIndicator =
|
||||||
libappindicator
|
libappindicator.app_indicator_new(this.appName, "indicator-messages-new", AppIndicator.CATEGORY_APPLICATION_STATUS);
|
||||||
.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
|
* 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
|
* hack the app indicator class itself. Not an issue unless we need other appindicators.
|
||||||
appindicators.
|
*/
|
||||||
*/
|
AppIndicator.AppIndicatorClassStruct aiclass =
|
||||||
AppIndicator.AppIndicatorClassStruct
|
new AppIndicator.AppIndicatorClassStruct(this.appIndicator.parent.g_type_instance.g_class);
|
||||||
aiclass =
|
|
||||||
new AppIndicator.AppIndicatorClassStruct(this.appIndicator.parent.g_type_instance.g_class);
|
|
||||||
|
|
||||||
aiclass.fallback = 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) {
|
aiclass.fallback = new AppIndicator.Fallback() {
|
||||||
AppIndicatorTray.this.failureCallback.createTrayFailed();
|
@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.write();
|
||||||
return null;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
aiclass.write();
|
|
||||||
|
|
||||||
this.menu = libgtk.gtk_menu_new();
|
this.menu = libgtk.gtk_menu_new();
|
||||||
libappindicator.app_indicator_set_menu(this.appIndicator, this.menu);
|
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_icon_full(this.appIndicator, iconPath(iconName), this.appName);
|
||||||
libappindicator.app_indicator_set_status(this.appIndicator, AppIndicator.STATUS_ACTIVE);
|
libappindicator.app_indicator_set_status(this.appIndicator, AppIndicator.STATUS_ACTIVE);
|
||||||
|
|
||||||
if (!GtkSupport.usesSwtMainLoop) {
|
if (!GtkSupport.usesSwtMainLoop) {
|
||||||
Thread gtkUpdateThread = new Thread() {
|
Thread gtkUpdateThread = new Thread() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
// notify our main thread to continue
|
// notify our main thread to continue
|
||||||
AppIndicatorTray.this.blockUntilStarted.countDown();
|
AppIndicatorTray.this.blockUntilStarted.countDown();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
libgtk.gtk_main();
|
libgtk.gtk_main();
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
logger.warn("Unable to run main loop", t);
|
logger.warn("Unable to run main loop", t);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
gtkUpdateThread.setName("GTK event loop");
|
||||||
|
gtkUpdateThread.setDaemon(true);
|
||||||
|
gtkUpdateThread.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
// we CANNOT continue until the GTK thread has started! (ignored if SWT is used)
|
||||||
|
try {
|
||||||
|
this.blockUntilStarted.await();
|
||||||
|
this.active = true;
|
||||||
|
} catch (InterruptedException ignored) {
|
||||||
}
|
}
|
||||||
};
|
|
||||||
gtkUpdateThread.setName("GTK event loop");
|
|
||||||
gtkUpdateThread.setDaemon(true);
|
|
||||||
gtkUpdateThread.start();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// we CANNOT continue until the GTK thread has started! (ignored if SWT is used)
|
@Override
|
||||||
try {
|
public void removeTray() {
|
||||||
this.blockUntilStarted.await();
|
for (Pointer widget : this.widgets) {
|
||||||
this.active = true;
|
libgtk.gtk_widget_destroy(widget);
|
||||||
} catch (InterruptedException ignored) {
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
// this hides the indicator
|
||||||
public void removeTray() {
|
libappindicator.app_indicator_set_status(this.appIndicator, AppIndicator.STATUS_PASSIVE);
|
||||||
for (Pointer widget : this.widgets) {
|
this.appIndicator.write();
|
||||||
libgtk.gtk_widget_destroy(widget);
|
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;
|
||||||
|
|
||||||
|
super.removeTray();
|
||||||
}
|
}
|
||||||
|
|
||||||
// this hides the indicator
|
@Override
|
||||||
libappindicator.app_indicator_set_status(this.appIndicator, AppIndicator.STATUS_PASSIVE);
|
public void setStatus(String infoString, String iconName) {
|
||||||
this.appIndicator.write();
|
if (this.connectionStatusItem == null) {
|
||||||
Pointer p = this.appIndicator.getPointer();
|
this.connectionStatusItem = libgtk.gtk_menu_item_new_with_label(infoString);
|
||||||
libgobject.g_object_unref(p);
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
this.active = false;
|
libgtk.gtk_widget_show_all(this.connectionStatusItem);
|
||||||
|
|
||||||
// GC it
|
libappindicator.app_indicator_set_icon_full(this.appIndicator, iconPath(iconName), this.appName);
|
||||||
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;
|
/**
|
||||||
|
* 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);
|
||||||
|
|
||||||
super.removeTray();
|
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);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
@Override
|
libgobject.g_signal_connect_data(dashboardItem, "activate", gtkCallback, null, null, 0);
|
||||||
public void setStatus(String infoString, String iconName) {
|
libgtk.gtk_menu_shell_append(this.menu, dashboardItem);
|
||||||
if (this.connectionStatusItem == null) {
|
libgtk.gtk_widget_show_all(dashboardItem);
|
||||||
this.connectionStatusItem = libgtk.gtk_menu_item_new_with_label(infoString);
|
|
||||||
this.widgets.add(this.connectionStatusItem);
|
menuEntry = new MenuEntry();
|
||||||
libgtk.gtk_widget_set_sensitive(this.connectionStatusItem, Gtk.FALSE);
|
menuEntry.dashboardItem = dashboardItem;
|
||||||
libgtk.gtk_menu_shell_append(this.menu, this.connectionStatusItem);
|
|
||||||
} else {
|
this.menuEntries.put(menuText, menuEntry);
|
||||||
libgtk.gtk_menu_item_set_label(this.connectionStatusItem, infoString);
|
} else {
|
||||||
|
updateMenuEntry(menuText, menuText, callback);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
libgtk.gtk_widget_show_all(this.connectionStatusItem);
|
/**
|
||||||
|
* 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);
|
||||||
|
|
||||||
libappindicator.app_indicator_set_icon_full(this.appIndicator, iconPath(iconName), this.appName);
|
if (menuEntry != null) {
|
||||||
}
|
libgtk.gtk_menu_item_set_label(menuEntry.dashboardItem, newMenuText);
|
||||||
|
|
||||||
/**
|
Gobject.GCallback gtkCallback = new Gobject.GCallback() {
|
||||||
* Will add a new menu entry, or update one if it already exists
|
@Override
|
||||||
*/
|
public void callback(Pointer instance, Pointer data) {
|
||||||
@Override
|
AppIndicatorTray.this.callbackExecutor.execute(new Runnable() {
|
||||||
public void addMenuEntry(String menuText, final SystemTrayMenuAction callback) {
|
@Override
|
||||||
synchronized (this.menuEntries) {
|
public void run() {
|
||||||
MenuEntry menuEntry = this.menuEntries.get(menuText);
|
newCallback.onClick(AppIndicatorTray.this);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
if (menuEntry == null) {
|
libgobject.g_signal_connect_data(menuEntry.dashboardItem, "activate", gtkCallback, null, null, 0);
|
||||||
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_widget_show_all(menuEntry.dashboardItem);
|
||||||
libgtk.gtk_menu_shell_append(this.menu, dashboardItem);
|
} else {
|
||||||
libgtk.gtk_widget_show_all(dashboardItem);
|
addMenuEntry(origMenuText, newCallback);
|
||||||
|
}
|
||||||
menuEntry = new MenuEntry();
|
}
|
||||||
menuEntry.dashboardItem = dashboardItem;
|
|
||||||
|
|
||||||
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);
|
|
||||||
|
|
||||||
libgtk.gtk_widget_show_all(menuEntry.dashboardItem);
|
|
||||||
} else {
|
|
||||||
addMenuEntry(origMenuText, newCallback);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,21 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2014 dorkbox, llc
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
package dorkbox.util.tray.linux;
|
package dorkbox.util.tray.linux;
|
||||||
|
|
||||||
import com.sun.jna.Pointer;
|
import java.awt.Dimension;
|
||||||
|
|
||||||
import java.awt.*;
|
|
||||||
import java.awt.event.ActionEvent;
|
import java.awt.event.ActionEvent;
|
||||||
import java.awt.event.ActionListener;
|
import java.awt.event.ActionListener;
|
||||||
import java.lang.reflect.InvocationTargetException;
|
import java.lang.reflect.InvocationTargetException;
|
||||||
@ -12,7 +25,10 @@ import java.util.List;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.CountDownLatch;
|
import java.util.concurrent.CountDownLatch;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.JMenuItem;
|
||||||
|
import javax.swing.SwingUtilities;
|
||||||
|
|
||||||
|
import com.sun.jna.Pointer;
|
||||||
|
|
||||||
import dorkbox.util.jna.linux.Gobject;
|
import dorkbox.util.jna.linux.Gobject;
|
||||||
import dorkbox.util.jna.linux.Gtk;
|
import dorkbox.util.jna.linux.Gtk;
|
||||||
@ -30,276 +46,275 @@ import dorkbox.util.tray.SystemTrayMenuPopup;
|
|||||||
*/
|
*/
|
||||||
public class GtkSystemTray extends SystemTray {
|
public class GtkSystemTray extends SystemTray {
|
||||||
|
|
||||||
private static final Gobject libgobject = Gobject.INSTANCE;
|
private static final Gobject libgobject = Gobject.INSTANCE;
|
||||||
private static final Gtk libgtk = Gtk.INSTANCE;
|
private static final Gtk libgtk = Gtk.INSTANCE;
|
||||||
|
|
||||||
private final CountDownLatch blockUntilStarted = new CountDownLatch(1);
|
private final CountDownLatch blockUntilStarted = new CountDownLatch(1);
|
||||||
private final Map<String, JMenuItem> menuEntries = new HashMap<String, JMenuItem>(2);
|
private final Map<String, JMenuItem> menuEntries = new HashMap<String, JMenuItem>(2);
|
||||||
|
|
||||||
private volatile SystemTrayMenuPopup jmenu;
|
private volatile SystemTrayMenuPopup jmenu;
|
||||||
private volatile JMenuItem connectionStatusItem;
|
private volatile JMenuItem connectionStatusItem;
|
||||||
|
|
||||||
private volatile Pointer trayIcon;
|
private volatile Pointer trayIcon;
|
||||||
|
|
||||||
// need to hang on to these to prevent gc
|
// need to hang on to these to prevent gc
|
||||||
private final List<Pointer> widgets = new ArrayList<Pointer>(4);
|
private final List<Pointer> widgets = new ArrayList<Pointer>(4);
|
||||||
|
|
||||||
public GtkSystemTray() {
|
public GtkSystemTray() {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void createTray(String iconName) {
|
public void createTray(String iconName) {
|
||||||
this.trayIcon = libgtk.gtk_status_icon_new();
|
this.trayIcon = libgtk.gtk_status_icon_new();
|
||||||
libgtk.gtk_status_icon_set_from_file(this.trayIcon, iconPath(iconName));
|
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_tooltip(this.trayIcon, this.appName);
|
||||||
libgtk.gtk_status_icon_set_visible(this.trayIcon, true);
|
libgtk.gtk_status_icon_set_visible(this.trayIcon, true);
|
||||||
|
|
||||||
Gobject.GEventCallback gtkCallback = new Gobject.GEventCallback() {
|
Gobject.GEventCallback gtkCallback = new Gobject.GEventCallback() {
|
||||||
@Override
|
@Override
|
||||||
public void callback(Pointer instance, final GdkEventButton event) {
|
public void callback(Pointer instance, final GdkEventButton event) {
|
||||||
// BUTTON_PRESS only
|
// BUTTON_PRESS only
|
||||||
if (event.type == 4) {
|
if (event.type == 4) {
|
||||||
SwingUtilities.invokeLater(new Runnable() {
|
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) {
|
||||||
|
Thread gtkUpdateThread = new Thread() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
// notify our main thread to continue
|
||||||
|
GtkSystemTray.this.blockUntilStarted.countDown();
|
||||||
|
|
||||||
|
try {
|
||||||
|
libgtk.gtk_main();
|
||||||
|
} catch (Throwable t) {
|
||||||
|
logger.warn("Unable to run main loop", t);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
gtkUpdateThread.setName("GTK event loop");
|
||||||
|
gtkUpdateThread.setDaemon(true);
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
// we CANNOT continue until the GTK thread has started! (ignored if SWT is used)
|
||||||
|
try {
|
||||||
|
this.blockUntilStarted.await();
|
||||||
|
this.active = true;
|
||||||
|
} catch (InterruptedException ignored) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@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;
|
||||||
|
|
||||||
|
super.removeTray();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setStatus(final String infoString, String iconName) {
|
||||||
|
Runnable doRun = new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
if (GtkSystemTray.this.jmenu.isVisible()) {
|
if (GtkSystemTray.this.connectionStatusItem == null) {
|
||||||
GtkSystemTray.this.jmenu.setVisible(false);
|
GtkSystemTray.this.connectionStatusItem = new JMenuItem(infoString);
|
||||||
} else {
|
GtkSystemTray.this.connectionStatusItem.setEnabled(false);
|
||||||
int iconX = (int) (event.x_root - event.x);
|
GtkSystemTray.this.jmenu.add(GtkSystemTray.this.connectionStatusItem);
|
||||||
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 {
|
} else {
|
||||||
// have to account for the icon
|
GtkSystemTray.this.connectionStatusItem.setText(infoString);
|
||||||
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) {
|
if (SwingUtilities.isEventDispatchThread()) {
|
||||||
Thread gtkUpdateThread = new Thread() {
|
doRun.run();
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
// notify our main thread to continue
|
|
||||||
GtkSystemTray.this.blockUntilStarted.countDown();
|
|
||||||
|
|
||||||
try {
|
|
||||||
libgtk.gtk_main();
|
|
||||||
} catch (Throwable t) {
|
|
||||||
logger.warn("Unable to run main loop", t);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
gtkUpdateThread.setName("GTK event loop");
|
|
||||||
gtkUpdateThread.setDaemon(true);
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
// we CANNOT continue until the GTK thread has started! (ignored if SWT is used)
|
|
||||||
try {
|
|
||||||
this.blockUntilStarted.await();
|
|
||||||
this.active = true;
|
|
||||||
} catch (InterruptedException ignored) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@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;
|
|
||||||
|
|
||||||
super.removeTray();
|
|
||||||
}
|
|
||||||
|
|
||||||
@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 {
|
} else {
|
||||||
GtkSystemTray.this.connectionStatusItem.setText(infoString);
|
try {
|
||||||
}
|
SwingUtilities.invokeAndWait(doRun);
|
||||||
}
|
} catch (InvocationTargetException e) {
|
||||||
};
|
logger.error("Error updating tray menu", e);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
if (SwingUtilities.isEventDispatchThread()) {
|
logger.error("Error updating tray menu", e);
|
||||||
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()) {
|
libgtk.gtk_status_icon_set_from_file(GtkSystemTray.this.trayIcon, iconPath(iconName));
|
||||||
doRun.run();
|
}
|
||||||
} else {
|
|
||||||
try {
|
/**
|
||||||
SwingUtilities.invokeAndWait(doRun);
|
* Will add a new menu entry, or update one if it already exists
|
||||||
} catch (InvocationTargetException e) {
|
*/
|
||||||
logger.error("Error updating tray menu", e);
|
@Override
|
||||||
} catch (InterruptedException e) {
|
public void addMenuEntry(final String menuText, final SystemTrayMenuAction callback) {
|
||||||
logger.error("Error updating tray menu", e);
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,18 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2014 dorkbox, llc
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
package dorkbox.util.tray.linux;
|
package dorkbox.util.tray.linux;
|
||||||
|
|
||||||
import com.sun.jna.Pointer;
|
import com.sun.jna.Pointer;
|
||||||
@ -7,31 +22,31 @@ import com.sun.jna.Pointer;
|
|||||||
*/
|
*/
|
||||||
class MenuEntry {
|
class MenuEntry {
|
||||||
|
|
||||||
private final int hashCode;
|
private final int hashCode;
|
||||||
public Pointer dashboardItem;
|
public Pointer dashboardItem;
|
||||||
|
|
||||||
public MenuEntry() {
|
public MenuEntry() {
|
||||||
long time = System.nanoTime();
|
long time = System.nanoTime();
|
||||||
this.hashCode = (int) (time ^ time >>> 32);
|
this.hashCode = (int) (time ^ time >>> 32);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
return this.hashCode;
|
return this.hashCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object obj) {
|
public boolean equals(Object obj) {
|
||||||
if (this == obj) {
|
if (this == obj) {
|
||||||
return true;
|
return true;
|
||||||
|
}
|
||||||
|
if (obj == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (getClass() != obj.getClass()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
MenuEntry other = (MenuEntry) obj;
|
||||||
|
return this.hashCode == other.hashCode;
|
||||||
}
|
}
|
||||||
if (obj == null) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (getClass() != obj.getClass()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
MenuEntry other = (MenuEntry) obj;
|
|
||||||
return this.hashCode == other.hashCode;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,30 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2014 dorkbox, llc
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
package dorkbox.util.tray.swing;
|
package dorkbox.util.tray.swing;
|
||||||
|
|
||||||
|
import java.awt.AWTException;
|
||||||
import java.awt.*;
|
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.ActionEvent;
|
||||||
import java.awt.event.ActionListener;
|
import java.awt.event.ActionListener;
|
||||||
import java.awt.event.MouseAdapter;
|
import java.awt.event.MouseAdapter;
|
||||||
@ -11,142 +34,142 @@ import java.util.ArrayList;
|
|||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.ImageIcon;
|
||||||
|
import javax.swing.JMenuItem;
|
||||||
|
import javax.swing.SwingUtilities;
|
||||||
|
|
||||||
import dorkbox.util.tray.SystemTrayMenuAction;
|
import dorkbox.util.tray.SystemTrayMenuAction;
|
||||||
import dorkbox.util.tray.SystemTrayMenuPopup;
|
import dorkbox.util.tray.SystemTrayMenuPopup;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class for handling all system tray interaction, via SWING
|
* Class for handling all system tray interaction, via SWING
|
||||||
*/
|
*/
|
||||||
public class SwingSystemTray extends dorkbox.util.tray.SystemTray {
|
public class SwingSystemTray extends dorkbox.util.tray.SystemTray {
|
||||||
|
|
||||||
private final Map<String, JMenuItem> menuEntries = new HashMap<String, JMenuItem>(2);
|
private final Map<String, JMenuItem> menuEntries = new HashMap<String, JMenuItem>(2);
|
||||||
|
|
||||||
private volatile SystemTrayMenuPopup jmenu;
|
private volatile SystemTrayMenuPopup jmenu;
|
||||||
private volatile JMenuItem connectionStatusItem;
|
private volatile JMenuItem connectionStatusItem;
|
||||||
|
|
||||||
private volatile SystemTray tray;
|
private volatile SystemTray tray;
|
||||||
private volatile TrayIcon trayIcon;
|
private volatile TrayIcon trayIcon;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new system tray handler class.
|
* Creates a new system tray handler class.
|
||||||
*/
|
*/
|
||||||
public SwingSystemTray() {
|
public SwingSystemTray() {}
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void removeTray() {
|
public void removeTray() {
|
||||||
Runnable doRun = new Runnable() {
|
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
super.removeTray();
|
|
||||||
}
|
|
||||||
|
|
||||||
@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
|
@Override
|
||||||
public void mousePressed(MouseEvent e) {
|
public void run() {
|
||||||
Dimension size = SwingSystemTray.this.jmenu.getPreferredSize();
|
SwingSystemTray.this.tray.remove(SwingSystemTray.this.trayIcon);
|
||||||
|
SwingSystemTray.this.menuEntries.clear();
|
||||||
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 {
|
if (SwingUtilities.isEventDispatchThread()) {
|
||||||
SwingSystemTray.this.tray.add(SwingSystemTray.this.trayIcon);
|
doRun.run();
|
||||||
SwingSystemTray.this.active = true;
|
} else {
|
||||||
} catch (AWTException e) {
|
try {
|
||||||
logger.error("TrayIcon could not be added.", e);
|
SwingUtilities.invokeAndWait(doRun);
|
||||||
}
|
} catch (InvocationTargetException e) {
|
||||||
|
logger.error("Error updating tray menu", e);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
logger.error("Error updating tray menu", e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
if (SwingUtilities.isEventDispatchThread()) {
|
super.removeTray();
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
Image newImage(String name) {
|
@Override
|
||||||
String iconPath = iconPath(name);
|
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();
|
||||||
|
|
||||||
return new ImageIcon(iconPath).getImage().getScaledInstance(ICON_SIZE, ICON_SIZE, Image.SCALE_SMOOTH);
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Image newImage(String name) {
|
||||||
|
String iconPath = iconPath(name);
|
||||||
|
|
||||||
|
return new ImageIcon(iconPath).getImage().getScaledInstance(ICON_SIZE, ICON_SIZE, Image.SCALE_SMOOTH);
|
||||||
|
}
|
||||||
|
|
||||||
// public static Rectangle getSafeScreenBounds(Point pos) {
|
// public static Rectangle getSafeScreenBounds(Point pos) {
|
||||||
// Rectangle bounds = getScreenBoundsAt(pos);
|
// Rectangle bounds = getScreenBoundsAt(pos);
|
||||||
@ -170,175 +193,174 @@ public class SwingSystemTray extends dorkbox.util.tray.SystemTray {
|
|||||||
// return insets;
|
// return insets;
|
||||||
// }
|
// }
|
||||||
|
|
||||||
private static Rectangle getScreenBoundsAt(Point pos) {
|
private static Rectangle getScreenBoundsAt(Point pos) {
|
||||||
GraphicsDevice gd = getGraphicsDeviceAt(pos);
|
GraphicsDevice gd = getGraphicsDeviceAt(pos);
|
||||||
Rectangle bounds = null;
|
Rectangle bounds = null;
|
||||||
if (gd != null) {
|
if (gd != null) {
|
||||||
bounds = gd.getDefaultConfiguration().getBounds();
|
bounds = gd.getDefaultConfiguration().getBounds();
|
||||||
}
|
|
||||||
|
|
||||||
return bounds;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static GraphicsDevice getGraphicsDeviceAt(Point pos) {
|
|
||||||
GraphicsDevice device;
|
|
||||||
|
|
||||||
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()) {
|
return bounds;
|
||||||
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);
|
private static GraphicsDevice getGraphicsDeviceAt(Point pos) {
|
||||||
SwingSystemTray.this.trayIcon.setImage(trayImage);
|
GraphicsDevice device;
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
|
||||||
* Will add a new menu entry, or update one if it already exists
|
GraphicsDevice lstGDs[] = ge.getScreenDevices();
|
||||||
*/
|
|
||||||
@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) {
|
ArrayList<GraphicsDevice> lstDevices = new ArrayList<GraphicsDevice>(lstGDs.length);
|
||||||
JMenuItem menuEntry = menuEntries2.get(menuText);
|
|
||||||
|
|
||||||
if (menuEntry == null) {
|
for (GraphicsDevice gd : lstGDs) {
|
||||||
SystemTrayMenuPopup menu = SwingSystemTray.this.jmenu;
|
|
||||||
|
|
||||||
menuEntry = new JMenuItem(menuText);
|
GraphicsConfiguration gc = gd.getDefaultConfiguration();
|
||||||
menuEntry.addActionListener(new ActionListener() {
|
Rectangle screenBounds = gc.getBounds();
|
||||||
@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);
|
if (screenBounds.contains(pos)) {
|
||||||
} else {
|
lstDevices.add(gd);
|
||||||
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()) {
|
if (lstDevices.size() > 0) {
|
||||||
doRun.run();
|
device = lstDevices.get(0);
|
||||||
} else {
|
} else {
|
||||||
try {
|
device = ge.getDefaultScreenDevice();
|
||||||
SwingUtilities.invokeAndWait(doRun);
|
}
|
||||||
} catch (InvocationTargetException e) {
|
|
||||||
logger.error("Error updating tray menu", e);
|
return device;
|
||||||
} catch (InterruptedException e) {
|
}
|
||||||
logger.error("Error updating tray menu", e);
|
|
||||||
}
|
@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
Block a user