From 0e56ca199aaecf00bcdc0c57f5fa797245400698 Mon Sep 17 00:00:00 2001 From: nathan Date: Sun, 9 Oct 2016 15:56:58 +0200 Subject: [PATCH] Organized swing menu logic --- src/dorkbox/systemTray/swing/SwingMenu.java | 30 ++++++------------- .../systemTray/swing/SwingSystemTray.java | 12 ++------ 2 files changed, 12 insertions(+), 30 deletions(-) diff --git a/src/dorkbox/systemTray/swing/SwingMenu.java b/src/dorkbox/systemTray/swing/SwingMenu.java index 45c6b235..d28485ea 100644 --- a/src/dorkbox/systemTray/swing/SwingMenu.java +++ b/src/dorkbox/systemTray/swing/SwingMenu.java @@ -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 class SwingMenu extends Menu { + // sub-menu = AdjustedJMenu + // systemtray = SwingSystemTrayMenuPopup volatile JComponent _native; // 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; /** + * Called in the EDT + * * @param systemTray * the system tray (which is the object that sits in the system tray) * @param parent * 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); - - 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); - } + this._native = _native; } protected @@ -173,7 +159,9 @@ class SwingMenu extends Menu { if (menuEntry == null) { // 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.setImage(imagePath); value.set((Menu)menuEntry); diff --git a/src/dorkbox/systemTray/swing/SwingSystemTray.java b/src/dorkbox/systemTray/swing/SwingSystemTray.java index 146ef7e6..197aee4b 100644 --- a/src/dorkbox/systemTray/swing/SwingSystemTray.java +++ b/src/dorkbox/systemTray/swing/SwingSystemTray.java @@ -48,21 +48,15 @@ class SwingSystemTray extends SwingMenu { volatile TrayIcon trayIcon; /** - * Creates a new system tray handler class. + * Creates a new system tray handler class. Called in the EDT */ public SwingSystemTray(final dorkbox.systemTray.SystemTray systemTray) { - super(systemTray, null); + super(systemTray, null, new SwingSystemTrayMenuPopup()); ImageUtils.determineIconSize(); - dispatchAndWait(new Runnable() { - @Override - public - void run() { - SwingSystemTray.this.tray = SystemTray.getSystemTray(); - } - }); + SwingSystemTray.this.tray = SystemTray.getSystemTray(); } public