forked from dorkbox/SystemTray
Organized swing menu logic
This commit is contained in:
parent
b976338b46
commit
0e56ca199a
@ -37,6 +37,8 @@ import dorkbox.util.SwingUtil;
|
|||||||
// this is a weird composite class, because it must be a Menu, but ALSO a MenuEntry -- so it has both
|
// this is a weird composite class, because it must be a Menu, but ALSO a MenuEntry -- so it has both
|
||||||
class SwingMenu extends Menu {
|
class SwingMenu extends Menu {
|
||||||
|
|
||||||
|
// sub-menu = AdjustedJMenu
|
||||||
|
// systemtray = SwingSystemTrayMenuPopup
|
||||||
volatile JComponent _native;
|
volatile JComponent _native;
|
||||||
|
|
||||||
// this have to be volatile, because they can be changed from any thread
|
// this have to be volatile, because they can be changed from any thread
|
||||||
@ -44,32 +46,16 @@ class SwingMenu extends Menu {
|
|||||||
private volatile boolean hasLegitIcon = false;
|
private volatile boolean hasLegitIcon = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Called in the EDT
|
||||||
|
*
|
||||||
* @param systemTray
|
* @param systemTray
|
||||||
* the system tray (which is the object that sits in the system tray)
|
* the system tray (which is the object that sits in the system tray)
|
||||||
* @param parent
|
* @param parent
|
||||||
* the parent of this menu, null if the parent is the system tray
|
* the parent of this menu, null if the parent is the system tray
|
||||||
*/
|
*/
|
||||||
SwingMenu(final SystemTray systemTray, final Menu parent) {
|
SwingMenu(final SystemTray systemTray, final Menu parent, final JComponent _native) {
|
||||||
super(systemTray, parent);
|
super(systemTray, parent);
|
||||||
|
this._native = _native;
|
||||||
try {
|
|
||||||
SwingUtil.invokeAndWait(new Runnable() {
|
|
||||||
@Override
|
|
||||||
public
|
|
||||||
void run() {
|
|
||||||
if (parent != null) {
|
|
||||||
// when we are a sub-menu
|
|
||||||
_native = new AdjustedJMenu();
|
|
||||||
((SwingMenu) parent)._native.add(_native);
|
|
||||||
} else {
|
|
||||||
// when we are the system tray
|
|
||||||
_native = new SwingSystemTrayMenuPopup();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} catch (Exception e) {
|
|
||||||
SystemTray.logger.error("Error processing event on the dispatch thread.", e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected
|
protected
|
||||||
@ -173,7 +159,9 @@ class SwingMenu extends Menu {
|
|||||||
|
|
||||||
if (menuEntry == null) {
|
if (menuEntry == null) {
|
||||||
// must always be called on the EDT
|
// must always be called on the EDT
|
||||||
menuEntry = new SwingMenu(getSystemTray(), SwingMenu.this);
|
menuEntry = new SwingMenu(getSystemTray(), SwingMenu.this, new AdjustedJMenu());
|
||||||
|
_native.add(((SwingMenu) menuEntry)._native);
|
||||||
|
|
||||||
menuEntry.setText(menuText);
|
menuEntry.setText(menuText);
|
||||||
menuEntry.setImage(imagePath);
|
menuEntry.setImage(imagePath);
|
||||||
value.set((Menu)menuEntry);
|
value.set((Menu)menuEntry);
|
||||||
|
@ -48,22 +48,16 @@ class SwingSystemTray extends SwingMenu {
|
|||||||
volatile TrayIcon trayIcon;
|
volatile TrayIcon trayIcon;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new system tray handler class.
|
* Creates a new system tray handler class. Called in the EDT
|
||||||
*/
|
*/
|
||||||
public
|
public
|
||||||
SwingSystemTray(final dorkbox.systemTray.SystemTray systemTray) {
|
SwingSystemTray(final dorkbox.systemTray.SystemTray systemTray) {
|
||||||
super(systemTray, null);
|
super(systemTray, null, new SwingSystemTrayMenuPopup());
|
||||||
|
|
||||||
ImageUtils.determineIconSize();
|
ImageUtils.determineIconSize();
|
||||||
|
|
||||||
dispatchAndWait(new Runnable() {
|
|
||||||
@Override
|
|
||||||
public
|
|
||||||
void run() {
|
|
||||||
SwingSystemTray.this.tray = SystemTray.getSystemTray();
|
SwingSystemTray.this.tray = SystemTray.getSystemTray();
|
||||||
}
|
}
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public
|
public
|
||||||
void shutdown() {
|
void shutdown() {
|
||||||
|
Loading…
Reference in New Issue
Block a user