2014-11-24 17:40:06 +01:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2014-11-03 02:11:03 +01:00
|
|
|
package dorkbox.util.tray.swing;
|
|
|
|
|
2015-08-04 21:10:01 +02:00
|
|
|
import dorkbox.util.ScreenUtil;
|
2015-06-28 01:47:02 +02:00
|
|
|
import dorkbox.util.SwingUtil;
|
|
|
|
import dorkbox.util.tray.SystemTrayMenuAction;
|
|
|
|
import dorkbox.util.tray.SystemTrayMenuPopup;
|
|
|
|
|
|
|
|
import javax.swing.*;
|
|
|
|
import java.awt.*;
|
2014-11-03 02:11:03 +01:00
|
|
|
import java.awt.event.ActionEvent;
|
|
|
|
import java.awt.event.ActionListener;
|
|
|
|
import java.awt.event.MouseAdapter;
|
|
|
|
import java.awt.event.MouseEvent;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class for handling all system tray interaction, via SWING
|
|
|
|
*/
|
2015-06-28 01:47:02 +02:00
|
|
|
public
|
|
|
|
class SwingSystemTray extends dorkbox.util.tray.SystemTray {
|
2014-11-03 02:11:03 +01:00
|
|
|
|
2015-08-04 21:10:01 +02:00
|
|
|
final Map<String, JMenuItem> menuEntries = new HashMap<String, JMenuItem>(2);
|
2014-11-24 17:40:06 +01:00
|
|
|
|
2015-08-04 21:10:01 +02:00
|
|
|
volatile SystemTrayMenuPopup jmenu;
|
|
|
|
volatile JMenuItem connectionStatusItem;
|
2014-11-24 17:40:06 +01:00
|
|
|
|
2015-08-04 21:10:01 +02:00
|
|
|
volatile SystemTray tray;
|
|
|
|
volatile TrayIcon trayIcon;
|
2014-11-24 17:40:06 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new system tray handler class.
|
|
|
|
*/
|
2015-06-28 01:47:02 +02:00
|
|
|
public
|
|
|
|
SwingSystemTray() {
|
|
|
|
}
|
2014-11-24 17:40:06 +01:00
|
|
|
|
|
|
|
|
|
|
|
@Override
|
2015-06-28 01:47:02 +02:00
|
|
|
public
|
|
|
|
void removeTray() {
|
2014-12-12 02:25:50 +01:00
|
|
|
SwingUtil.invokeAndWait(new Runnable() {
|
2014-11-24 17:40:06 +01:00
|
|
|
@Override
|
2015-06-28 01:47:02 +02:00
|
|
|
public
|
|
|
|
void run() {
|
2014-11-24 17:40:06 +01:00
|
|
|
SwingSystemTray.this.tray.remove(SwingSystemTray.this.trayIcon);
|
|
|
|
SwingSystemTray.this.menuEntries.clear();
|
|
|
|
}
|
2014-12-12 02:25:50 +01:00
|
|
|
});
|
2014-11-16 15:27:54 +01:00
|
|
|
|
2014-11-24 17:40:06 +01:00
|
|
|
super.removeTray();
|
|
|
|
}
|
2014-11-03 02:11:03 +01:00
|
|
|
|
2015-08-04 21:10:01 +02:00
|
|
|
@SuppressWarnings("FieldRepeatedlyAccessedInMethod")
|
2014-11-24 17:40:06 +01:00
|
|
|
@Override
|
2015-06-28 01:47:02 +02:00
|
|
|
public
|
|
|
|
void createTray(final String iconName) {
|
2014-12-12 02:25:50 +01:00
|
|
|
SwingUtil.invokeAndWait(new Runnable() {
|
2014-11-03 02:11:03 +01:00
|
|
|
@Override
|
2015-06-28 01:47:02 +02:00
|
|
|
public
|
|
|
|
void run() {
|
2014-11-24 17:40:06 +01:00
|
|
|
SwingSystemTray.this.tray = SystemTray.getSystemTray();
|
|
|
|
if (SwingSystemTray.this.tray == null) {
|
2015-01-23 02:52:09 +01:00
|
|
|
logger.error("The system tray is not available");
|
2015-06-28 01:47:02 +02:00
|
|
|
}
|
|
|
|
else {
|
2014-11-24 17:40:06 +01:00
|
|
|
SwingSystemTray.this.jmenu = new SystemTrayMenuPopup();
|
|
|
|
|
2015-08-04 21:10:01 +02:00
|
|
|
final Image trayImage = newImage(iconName);
|
|
|
|
final TrayIcon trayIcon = new TrayIcon(trayImage);
|
|
|
|
SwingSystemTray.this.trayIcon = trayIcon;
|
|
|
|
trayIcon.setToolTip(SwingSystemTray.this.appName);
|
2014-11-24 17:40:06 +01:00
|
|
|
|
2015-08-04 21:10:01 +02:00
|
|
|
trayIcon.addMouseListener(new MouseAdapter() {
|
2014-11-24 17:40:06 +01:00
|
|
|
@Override
|
2015-06-28 01:47:02 +02:00
|
|
|
public
|
|
|
|
void mousePressed(MouseEvent e) {
|
2015-08-04 21:10:01 +02:00
|
|
|
final SystemTrayMenuPopup jmenu = SwingSystemTray.this.jmenu;
|
|
|
|
Dimension size = jmenu.getPreferredSize();
|
2014-11-24 17:40:06 +01:00
|
|
|
|
|
|
|
Point point = e.getPoint();
|
2015-08-04 21:10:01 +02:00
|
|
|
Rectangle bounds = ScreenUtil.getScreenBoundsAt(point);
|
2014-11-24 17:40:06 +01:00
|
|
|
|
|
|
|
int x = point.x;
|
|
|
|
int y = point.y;
|
|
|
|
|
|
|
|
if (y < bounds.y) {
|
|
|
|
y = bounds.y;
|
2015-06-28 01:47:02 +02:00
|
|
|
}
|
|
|
|
else if (y + size.height > bounds.y + bounds.height) {
|
2015-01-23 00:25:35 +01:00
|
|
|
// our menu cannot have the top-edge snap to the mouse
|
|
|
|
// so we make the bottom-edge snap to the mouse
|
|
|
|
y -= size.height; // snap to edge of mouse
|
2014-11-24 17:40:06 +01:00
|
|
|
}
|
2015-01-23 00:25:35 +01:00
|
|
|
|
2014-11-24 17:40:06 +01:00
|
|
|
if (x < bounds.x) {
|
|
|
|
x = bounds.x;
|
2015-06-28 01:47:02 +02:00
|
|
|
}
|
|
|
|
else if (x + size.width > bounds.x + bounds.width) {
|
2015-01-23 00:25:35 +01:00
|
|
|
// our menu cannot have the left-edge snap to the mouse
|
|
|
|
// so we make the right-edge snap to the mouse
|
|
|
|
x -= size.width; // snap to edge of mouse
|
2014-11-24 17:40:06 +01:00
|
|
|
}
|
|
|
|
|
2015-08-04 21:10:01 +02:00
|
|
|
jmenu.setInvoker(jmenu);
|
|
|
|
jmenu.setLocation(x, y);
|
|
|
|
jmenu.setVisible(true);
|
|
|
|
jmenu.requestFocus();
|
2014-11-24 17:40:06 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
2015-08-04 21:10:01 +02:00
|
|
|
SwingSystemTray.this.tray.add(trayIcon);
|
2014-11-24 17:40:06 +01:00
|
|
|
SwingSystemTray.this.active = true;
|
|
|
|
} catch (AWTException e) {
|
|
|
|
logger.error("TrayIcon could not be added.", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-12-12 02:25:50 +01:00
|
|
|
});
|
2014-11-03 02:11:03 +01:00
|
|
|
}
|
|
|
|
|
2014-11-24 17:40:06 +01:00
|
|
|
Image newImage(String name) {
|
|
|
|
String iconPath = iconPath(name);
|
2014-11-03 02:11:03 +01:00
|
|
|
|
2015-01-23 02:52:09 +01:00
|
|
|
return new ImageIcon(iconPath).getImage().getScaledInstance(TRAY_SIZE, TRAY_SIZE, Image.SCALE_SMOOTH);
|
2014-11-24 17:40:06 +01:00
|
|
|
}
|
2014-11-03 02:11:03 +01:00
|
|
|
|
2015-08-04 21:10:01 +02:00
|
|
|
@SuppressWarnings("FieldRepeatedlyAccessedInMethod")
|
2014-11-24 17:40:06 +01:00
|
|
|
@Override
|
2015-06-28 01:47:02 +02:00
|
|
|
public
|
|
|
|
void setStatus(final String infoString, final String iconName) {
|
2014-12-12 02:25:50 +01:00
|
|
|
SwingUtil.invokeAndWait(new Runnable() {
|
2014-11-24 17:40:06 +01:00
|
|
|
@Override
|
2015-06-28 01:47:02 +02:00
|
|
|
public
|
|
|
|
void run() {
|
2014-11-24 17:40:06 +01:00
|
|
|
if (SwingSystemTray.this.connectionStatusItem == null) {
|
2015-08-04 21:10:01 +02:00
|
|
|
final JMenuItem connectionStatusItem = new JMenuItem(infoString);
|
|
|
|
connectionStatusItem.setEnabled(false);
|
|
|
|
SwingSystemTray.this.jmenu.add(connectionStatusItem);
|
|
|
|
|
|
|
|
SwingSystemTray.this.connectionStatusItem = connectionStatusItem;
|
2015-06-28 01:47:02 +02:00
|
|
|
}
|
|
|
|
else {
|
2014-11-24 17:40:06 +01:00
|
|
|
SwingSystemTray.this.connectionStatusItem.setText(infoString);
|
|
|
|
}
|
|
|
|
}
|
2014-12-12 02:25:50 +01:00
|
|
|
});
|
2014-11-24 17:40:06 +01:00
|
|
|
|
|
|
|
Image trayImage = newImage(iconName);
|
|
|
|
SwingSystemTray.this.trayIcon.setImage(trayImage);
|
2014-11-03 02:11:03 +01:00
|
|
|
}
|
|
|
|
|
2014-11-24 17:40:06 +01:00
|
|
|
/**
|
|
|
|
* Will add a new menu entry, or update one if it already exists
|
|
|
|
*/
|
|
|
|
@Override
|
2015-06-28 01:47:02 +02:00
|
|
|
public
|
|
|
|
void addMenuEntry(final String menuText, final SystemTrayMenuAction callback) {
|
2014-12-12 02:25:50 +01:00
|
|
|
SwingUtil.invokeAndWait(new Runnable() {
|
2015-08-04 21:10:01 +02:00
|
|
|
@SuppressWarnings("SynchronizationOnLocalVariableOrMethodParameter")
|
2014-11-24 17:40:06 +01:00
|
|
|
@Override
|
2015-06-28 01:47:02 +02:00
|
|
|
public
|
|
|
|
void run() {
|
2015-08-04 21:10:01 +02:00
|
|
|
final Map<String, JMenuItem> menuEntries2 = SwingSystemTray.this.menuEntries;
|
2014-11-24 17:40:06 +01:00
|
|
|
|
|
|
|
synchronized (menuEntries2) {
|
|
|
|
JMenuItem menuEntry = menuEntries2.get(menuText);
|
|
|
|
|
|
|
|
if (menuEntry == null) {
|
|
|
|
SystemTrayMenuPopup menu = SwingSystemTray.this.jmenu;
|
|
|
|
|
|
|
|
menuEntry = new JMenuItem(menuText);
|
|
|
|
menuEntry.addActionListener(new ActionListener() {
|
|
|
|
@Override
|
2015-06-28 01:47:02 +02:00
|
|
|
public
|
|
|
|
void actionPerformed(ActionEvent e) {
|
2014-11-24 17:40:06 +01:00
|
|
|
SwingSystemTray.this.callbackExecutor.execute(new Runnable() {
|
|
|
|
@Override
|
2015-06-28 01:47:02 +02:00
|
|
|
public
|
|
|
|
void run() {
|
2014-11-24 17:40:06 +01:00
|
|
|
callback.onClick(SwingSystemTray.this);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
menu.add(menuEntry);
|
|
|
|
|
|
|
|
menuEntries2.put(menuText, menuEntry);
|
2015-06-28 01:47:02 +02:00
|
|
|
}
|
|
|
|
else {
|
2014-11-24 17:40:06 +01:00
|
|
|
updateMenuEntry(menuText, menuText, callback);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-12-12 02:25:50 +01:00
|
|
|
});
|
2014-11-03 02:11:03 +01:00
|
|
|
}
|
2014-11-24 17:40:06 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Will update an already existing menu entry (or add a new one, if it doesn't exist)
|
|
|
|
*/
|
|
|
|
@Override
|
2015-06-28 01:47:02 +02:00
|
|
|
public
|
|
|
|
void updateMenuEntry(final String origMenuText, final String newMenuText, final SystemTrayMenuAction newCallback) {
|
2014-12-12 02:25:50 +01:00
|
|
|
SwingUtil.invokeAndWait(new Runnable() {
|
2015-08-04 21:10:01 +02:00
|
|
|
@SuppressWarnings("SynchronizationOnLocalVariableOrMethodParameter")
|
2014-11-24 17:40:06 +01:00
|
|
|
@Override
|
2015-06-28 01:47:02 +02:00
|
|
|
public
|
|
|
|
void run() {
|
2015-08-04 21:10:01 +02:00
|
|
|
final Map<String, JMenuItem> menuEntries2 = SwingSystemTray.this.menuEntries;
|
2014-11-24 17:40:06 +01:00
|
|
|
|
|
|
|
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
|
2015-06-28 01:47:02 +02:00
|
|
|
public
|
|
|
|
void actionPerformed(ActionEvent e) {
|
2014-11-24 17:40:06 +01:00
|
|
|
SwingSystemTray.this.callbackExecutor.execute(new Runnable() {
|
|
|
|
@Override
|
2015-06-28 01:47:02 +02:00
|
|
|
public
|
|
|
|
void run() {
|
2014-11-24 17:40:06 +01:00
|
|
|
newCallback.onClick(SwingSystemTray.this);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
menuEntry.setText(newMenuText);
|
|
|
|
menuEntry.revalidate();
|
2015-06-28 01:47:02 +02:00
|
|
|
}
|
|
|
|
else {
|
2014-11-24 17:40:06 +01:00
|
|
|
addMenuEntry(origMenuText, newCallback);
|
|
|
|
}
|
|
|
|
}
|
2014-11-03 02:11:03 +01:00
|
|
|
}
|
2014-12-12 02:25:50 +01:00
|
|
|
});
|
2014-11-03 02:11:03 +01:00
|
|
|
}
|
|
|
|
}
|