Code polish

This commit is contained in:
nathan 2017-07-10 23:59:10 +02:00
parent 78b78b80b9
commit 876c5bb591
2 changed files with 15 additions and 28 deletions

View File

@ -28,7 +28,7 @@ class AwtMenuItem implements MenuItemPeer {
private final AwtMenu parent; private final AwtMenu parent;
private final java.awt.MenuItem _native = new java.awt.MenuItem(); private final java.awt.MenuItem _native = new java.awt.MenuItem();
private volatile ActionListener swingCallback; private volatile ActionListener callback;
// this is ALWAYS called on the EDT. // this is ALWAYS called on the EDT.
AwtMenuItem(final AwtMenu parent) { AwtMenuItem(final AwtMenu parent) {
@ -70,12 +70,12 @@ class AwtMenuItem implements MenuItemPeer {
@Override @Override
public public
void setCallback(final dorkbox.systemTray.MenuItem menuItem) { void setCallback(final dorkbox.systemTray.MenuItem menuItem) {
if (swingCallback != null) { if (callback != null) {
_native.removeActionListener(swingCallback); _native.removeActionListener(callback);
} }
if (menuItem.getCallback() != null) { if (menuItem.getCallback() != null) {
swingCallback = new ActionListener() { callback = new ActionListener() {
@Override @Override
public public
void actionPerformed(ActionEvent e) { void actionPerformed(ActionEvent e) {
@ -91,10 +91,10 @@ class AwtMenuItem implements MenuItemPeer {
} }
}; };
_native.addActionListener(swingCallback); _native.addActionListener(callback);
} }
else { else {
swingCallback = null; callback = null;
} }
} }
@ -125,9 +125,9 @@ class AwtMenuItem implements MenuItemPeer {
_native.deleteShortcut(); _native.deleteShortcut();
_native.setEnabled(false); _native.setEnabled(false);
if (swingCallback != null) { if (callback != null) {
_native.removeActionListener(swingCallback); _native.removeActionListener(callback);
swingCallback = null; callback = null;
} }
parent._native.remove(_native); parent._native.remove(_native);

View File

@ -16,7 +16,6 @@
package dorkbox.systemTray.nativeUI; package dorkbox.systemTray.nativeUI;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
@ -34,7 +33,7 @@ import dorkbox.systemTray.peer.MenuPeer;
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
class GtkMenu extends GtkBaseMenuItem implements MenuPeer { class GtkMenu extends GtkBaseMenuItem implements MenuPeer {
// this is a list (that mirrors the actual list) BECAUSE we have to create/delete the entire menu in GTK every time something is changed // this is a list (that mirrors the actual list) BECAUSE we have to create/delete the entire menu in GTK every time something is changed
private final List<GtkBaseMenuItem> menuEntries = new LinkedList<GtkBaseMenuItem>(); private final List<GtkBaseMenuItem> menuEntries = new ArrayList<GtkBaseMenuItem>();
private final GtkMenu parent; private final GtkMenu parent;
volatile Pointer _nativeMenu; // must ONLY be created at the end of delete! volatile Pointer _nativeMenu; // must ONLY be created at the end of delete!
@ -69,18 +68,6 @@ class GtkMenu extends GtkBaseMenuItem implements MenuPeer {
return parent; return parent;
} }
/**
* ALWAYS CALLED ON THE EDT
*/
private
void add(final GtkBaseMenuItem item, final int index) {
if (index > 0) {
menuEntries.add(index, item);
} else {
menuEntries.add(item);
}
}
/** /**
* Called inside the gdk_threads block * Called inside the gdk_threads block
* *
@ -221,27 +208,27 @@ class GtkMenu extends GtkBaseMenuItem implements MenuPeer {
// some implementations of appindicator, do NOT like having a menu added, which has no menu items yet. // some implementations of appindicator, do NOT like having a menu added, which has no menu items yet.
// see: https://bugs.launchpad.net/glipper/+bug/1203888 // see: https://bugs.launchpad.net/glipper/+bug/1203888
GtkMenu item = new GtkMenu(GtkMenu.this); GtkMenu item = new GtkMenu(GtkMenu.this);
add(item, index); menuEntries.add(index, item);
((Menu) entry).bind(item, parentMenu, parentMenu.getSystemTray()); ((Menu) entry).bind(item, parentMenu, parentMenu.getSystemTray());
} }
else if (entry instanceof Separator) { else if (entry instanceof Separator) {
GtkMenuItemSeparator item = new GtkMenuItemSeparator(GtkMenu.this); GtkMenuItemSeparator item = new GtkMenuItemSeparator(GtkMenu.this);
add(item, index); menuEntries.add(index, item);
entry.bind(item, parentMenu, parentMenu.getSystemTray()); entry.bind(item, parentMenu, parentMenu.getSystemTray());
} }
else if (entry instanceof Checkbox) { else if (entry instanceof Checkbox) {
GtkMenuItemCheckbox item = new GtkMenuItemCheckbox(GtkMenu.this); GtkMenuItemCheckbox item = new GtkMenuItemCheckbox(GtkMenu.this);
add(item, index); menuEntries.add(index, item);
((Checkbox) entry).bind(item, parentMenu, parentMenu.getSystemTray()); ((Checkbox) entry).bind(item, parentMenu, parentMenu.getSystemTray());
} }
else if (entry instanceof Status) { else if (entry instanceof Status) {
GtkMenuItemStatus item = new GtkMenuItemStatus(GtkMenu.this); GtkMenuItemStatus item = new GtkMenuItemStatus(GtkMenu.this);
add(item, index); menuEntries.add(index, item);
((Status) entry).bind(item, parentMenu, parentMenu.getSystemTray()); ((Status) entry).bind(item, parentMenu, parentMenu.getSystemTray());
} }
else if (entry instanceof MenuItem) { else if (entry instanceof MenuItem) {
GtkMenuItem item = new GtkMenuItem(GtkMenu.this); GtkMenuItem item = new GtkMenuItem(GtkMenu.this);
add(item, index); menuEntries.add(index, item);
((MenuItem) entry).bind(item, parentMenu, parentMenu.getSystemTray()); ((MenuItem) entry).bind(item, parentMenu, parentMenu.getSystemTray());
} }