API CHANGE (setIcon -> setImage) for API consistency with how

menus/entries get their image set. Class heirarchy is now consistent
with all menu types (system tray + sub-menus)
This commit is contained in:
nathan 2016-10-08 23:54:45 +02:00
parent 87222f8483
commit 1a35cb8726
11 changed files with 150 additions and 87 deletions

View File

@ -29,7 +29,7 @@ import dorkbox.systemTray.util.ImageUtils;
*/ */
@SuppressWarnings({"WeakerAccess", "unused"}) @SuppressWarnings({"WeakerAccess", "unused"})
public abstract public abstract
class Menu { class Menu implements MenuEntry {
public static final AtomicInteger MENU_ID_COUNTER = new AtomicInteger(); public static final AtomicInteger MENU_ID_COUNTER = new AtomicInteger();
private final int id = Menu.MENU_ID_COUNTER.getAndIncrement(); private final int id = Menu.MENU_ID_COUNTER.getAndIncrement();

View File

@ -746,72 +746,19 @@ class SystemTray extends Menu {
} }
protected protected
void setIcon_(File iconPath) { void setImage_(File iconPath) {
final Menu menu = systemTrayMenu; final Menu menu = systemTrayMenu;
if (menu instanceof AppIndicatorTray) { if (menu instanceof AppIndicatorTray) {
((AppIndicatorTray) menu).setIcon_(iconPath); ((AppIndicatorTray) menu).setImage_(iconPath);
} }
else if (menu instanceof GtkSystemTray) { else if (menu instanceof GtkSystemTray) {
((GtkSystemTray) menu).setIcon_(iconPath); ((GtkSystemTray) menu).setImage_(iconPath);
} else { } else {
// swing // swing (windows/mac)
((SwingSystemTray) menu).setIcon_(iconPath); ((SwingSystemTray) menu).setImage_(iconPath);
} }
} }
/**
* Changes the tray icon used.
*
* Because the cross-platform, underlying system uses a file path to load icons for the system tray,
* this will directly use the contents of the specified file.
*
* @param imagePath the path of the icon to use
*/
public
void setIcon(String imagePath) {
setIcon_(ImageUtils.resizeAndCache(ImageUtils.TRAY_SIZE, imagePath));
}
/**
* Changes the tray icon used.
*
* Because the cross-platform, underlying system uses a file path to load icons for the system tray, this will copy the contents of
* the URL to a temporary location on disk, based on the path specified by the URL.
*
* @param imageUrl the URL of the icon to use
*/
public
void setIcon(URL imageUrl) {
setIcon_(ImageUtils.resizeAndCache(ImageUtils.TRAY_SIZE, imageUrl));
}
/**
* Changes the tray icon used.
*
* Because the cross-platform, underlying system uses a file path to load icons for the system tray, this will copy the contents of
* the imageStream to a temporary location on disk, based on the `cacheName` specified.
*
* @param cacheName the name to use for lookup in the cache for the iconStream
* @param imageStream the InputStream of the icon to use
*/
public
void setIcon(String cacheName, InputStream imageStream) {
setIcon_(ImageUtils.resizeAndCache(ImageUtils.TRAY_SIZE, cacheName, imageStream));
}
/**
* Changes the tray icon used.
*
* Because the cross-platform, underlying system uses a file path to load icons for the system tray, this will copy the contents of
* the imageStream to a temporary location on disk.
*
* @param imageStream the InputStream of the icon to use
*/
public
void setIcon(InputStream imageStream) {
setIcon_(ImageUtils.resizeAndCache(ImageUtils.TRAY_SIZE, imageStream));
}
/** /**
* @return the parent menu (of this menu) or null if we are the root menu * @return the parent menu (of this menu) or null if we are the root menu
*/ */
@ -866,6 +813,110 @@ class SystemTray extends Menu {
void setEnabled(final boolean enabled) { void setEnabled(final boolean enabled) {
} }
@Override
public
String getText() {
return "";
}
// NO OP.
@Override
public
void setText(final String newText) {
}
/**
* Changes the tray image used.
*
* Because the cross-platform, underlying system uses a file path to load images for the system tray,
* this will directly use the contents of the specified file.
*
* @param imageFile the path of the image to use
*/
@Override
public
void setImage(final File imageFile) {
setImage_(ImageUtils.resizeAndCache(ImageUtils.TRAY_SIZE, imageFile));
}
/**
* Changes the tray image used.
*
* Because the cross-platform, underlying system uses a file path to load images for the system tray,
* this will directly use the contents of the specified file.
*
* @param imagePath the path of the image to use
*/
@Override
public
void setImage(final String imagePath) {
setImage_(ImageUtils.resizeAndCache(ImageUtils.TRAY_SIZE, imagePath));
}
/**
* Changes the tray image used.
*
* Because the cross-platform, underlying system uses a file path to load images for the system tray, this will copy the contents of
* the URL to a temporary location on disk, based on the path specified by the URL.
*
* @param imageUrl the URL of the image to use
*/
@Override
public
void setImage(URL imageUrl) {
setImage_(ImageUtils.resizeAndCache(ImageUtils.TRAY_SIZE, imageUrl));
}
/**
* Changes the tray image used.
*
* Because the cross-platform, underlying system uses a file path to load images for the system tray, this will copy the contents of
* the imageStream to a temporary location on disk, based on the `cacheName` specified.
*
* @param cacheName the name to use for lookup in the cache for the imageStream
* @param imageStream the InputStream of the image to use
*/
@Override
public
void setImage(String cacheName, InputStream imageStream) {
setImage_(ImageUtils.resizeAndCache(ImageUtils.TRAY_SIZE, cacheName, imageStream));
}
/**
* Changes the tray image used.
*
* Because the cross-platform, underlying system uses a file path to load images for the system tray, this will copy the contents of
* the imageStream to a temporary location on disk.
*
* @param imageStream the InputStream of the image to use
*/
@Override
public
void setImage(final InputStream imageStream) {
setImage_(ImageUtils.resizeAndCache(ImageUtils.TRAY_SIZE, imageStream));
}
/**
* By default, we always have an image for the system tray
*/
@Override
public
boolean hasImage() {
return true;
}
// NO OP.
@Override
public
void setCallback(final SystemTrayMenuAction callback) {
}
// NO OP.
@Override
public
void setShortcut(final char key) {
}
// NO OP. // NO OP.
@Override @Override
protected protected

View File

@ -105,7 +105,7 @@ class AppIndicatorTray extends GtkTypeSystemTray {
} }
public public
void setIcon_(final File iconFile) { void setImage_(final File iconFile) {
dispatch(new Runnable() { dispatch(new Runnable() {
@Override @Override
public public

View File

@ -34,7 +34,8 @@ import dorkbox.systemTray.SystemTrayMenuAction;
import dorkbox.systemTray.linux.jna.Gobject; import dorkbox.systemTray.linux.jna.Gobject;
import dorkbox.systemTray.linux.jna.Gtk; import dorkbox.systemTray.linux.jna.Gtk;
class GtkMenu extends Menu implements MenuEntry { @SuppressWarnings("ForLoopReplaceableByForEach")
class GtkMenu extends Menu {
// menu entry that this menu is attached to. Will be NULL when it's the system tray // menu entry that this menu is attached to. Will be NULL when it's the system tray
private final GtkEntryItem menuEntry; private final GtkEntryItem menuEntry;
@ -233,9 +234,8 @@ class GtkMenu extends Menu implements MenuEntry {
if (_native != null) { if (_native != null) {
// have to remove all other menu entries // have to remove all other menu entries
synchronized (menuEntries) { synchronized (menuEntries) {
for (int i = 0; i < menuEntries.size(); i++) { for (int i = 0, menuEntriesSize = menuEntries.size(); i < menuEntriesSize; i++) {
MenuEntry menuEntry__ = menuEntries.get(i); final MenuEntry menuEntry__ = menuEntries.get(i);
if (menuEntry__ instanceof GtkEntry) { if (menuEntry__ instanceof GtkEntry) {
GtkEntry entry = (GtkEntry) menuEntry__; GtkEntry entry = (GtkEntry) menuEntry__;
@ -282,11 +282,13 @@ class GtkMenu extends Menu implements MenuEntry {
// now add back other menu entries // now add back other menu entries
synchronized (menuEntries) { synchronized (menuEntries) {
for (MenuEntry menuEntry__ : menuEntries) { for (int i = 0, menuEntriesSize = menuEntries.size(); i < menuEntriesSize; i++) {
final MenuEntry menuEntry__ = menuEntries.get(i);
hasImages |= menuEntry__.hasImage(); hasImages |= menuEntry__.hasImage();
} }
for (MenuEntry menuEntry__ : menuEntries) { for (int i = 0, menuEntriesSize = menuEntries.size(); i < menuEntriesSize; i++) {
final MenuEntry menuEntry__ = menuEntries.get(i);
// the menu entry looks FUNKY when there are a mis-match of entries WITH and WITHOUT images // the menu entry looks FUNKY when there are a mis-match of entries WITH and WITHOUT images
if (menuEntry__ instanceof GtkEntry) { if (menuEntry__ instanceof GtkEntry) {
GtkEntry entry = (GtkEntry) menuEntry__; GtkEntry entry = (GtkEntry) menuEntry__;
@ -327,8 +329,8 @@ class GtkMenu extends Menu implements MenuEntry {
// have to remove all other menu entries // have to remove all other menu entries
synchronized (menuEntries) { synchronized (menuEntries) {
for (int i = 0; i < menuEntries.size(); i++) { for (int i = 0, menuEntriesSize = menuEntries.size(); i < menuEntriesSize; i++) {
MenuEntry menuEntry__ = menuEntries.get(i); final MenuEntry menuEntry__ = menuEntries.get(i);
menuEntry__.remove(); menuEntry__.remove();
} }
menuEntries.clear(); menuEntries.clear();

View File

@ -141,7 +141,7 @@ class GtkSystemTray extends GtkTypeSystemTray {
} }
public public
void setIcon_(final File iconFile) { void setImage_(final File iconFile) {
dispatch(new Runnable() { dispatch(new Runnable() {
@Override @Override
public public

View File

@ -70,7 +70,8 @@ class GtkTypeSystemTray extends GtkMenu {
menuEntry = new GtkEntryStatus(GtkTypeSystemTray.this, statusText); menuEntry = new GtkEntryStatus(GtkTypeSystemTray.this, statusText);
// status is ALWAYS at 0 index... // status is ALWAYS at 0 index...
menuEntries.add(0, menuEntry); menuEntries.add(0, menuEntry);
} else if (menuEntry instanceof GtkEntryStatus) { }
else if (menuEntry instanceof GtkEntryStatus) {
// change the text? // change the text?
if (statusText != null) { if (statusText != null) {
menuEntry = new GtkEntryStatus(GtkTypeSystemTray.this, statusText); menuEntry = new GtkEntryStatus(GtkTypeSystemTray.this, statusText);

View File

@ -35,7 +35,7 @@ import dorkbox.systemTray.util.ImageUtils;
import dorkbox.util.SwingUtil; 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 implements MenuEntry { class SwingMenu extends Menu {
volatile JComponent _native; volatile JComponent _native;

View File

@ -134,7 +134,7 @@ class SwingSystemTray extends SwingMenu {
} }
public public
void setIcon_(final File iconFile) { void setImage_(final File iconFile) {
dispatch(new Runnable() { dispatch(new Runnable() {
@Override @Override
public public

View File

@ -50,7 +50,7 @@ class TestTray {
throw new RuntimeException("Unable to load SystemTray!"); throw new RuntimeException("Unable to load SystemTray!");
} }
systemTray.setIcon(LT_GRAY_MAIL); systemTray.setImage(LT_GRAY_MAIL);
systemTray.setStatus("No Mail"); systemTray.setStatus("No Mail");
callbackGreen = new SystemTrayMenuAction() { callbackGreen = new SystemTrayMenuAction() {
@ -58,7 +58,7 @@ class TestTray {
public public
void onClick(final SystemTray systemTray, final Menu parent, final MenuEntry menuEntry) { void onClick(final SystemTray systemTray, final Menu parent, final MenuEntry menuEntry) {
systemTray.setStatus("Some Mail!"); systemTray.setStatus("Some Mail!");
systemTray.setIcon(GREEN_MAIL); systemTray.setImage(GREEN_MAIL);
menuEntry.setCallback(callbackGray); menuEntry.setCallback(callbackGray);
menuEntry.setImage(BLACK_MAIL); menuEntry.setImage(BLACK_MAIL);
@ -72,7 +72,7 @@ class TestTray {
public public
void onClick(final SystemTray systemTray, final Menu parent, final MenuEntry menuEntry) { void onClick(final SystemTray systemTray, final Menu parent, final MenuEntry menuEntry) {
systemTray.setStatus(null); systemTray.setStatus(null);
systemTray.setIcon(BLACK_MAIL); systemTray.setImage(BLACK_MAIL);
menuEntry.setCallback(null); menuEntry.setCallback(null);
// systemTray.setStatus("Mail Empty"); // systemTray.setStatus("Mail Empty");
@ -81,7 +81,10 @@ class TestTray {
} }
}; };
this.systemTray.addEntry("Green Mail", GREEN_MAIL, callbackGreen); MenuEntry menuEntry = this.systemTray.addEntry("Green Mail", GREEN_MAIL, callbackGreen);
// case does not matter
menuEntry.setShortcut('G');
this.systemTray.addSeparator(); this.systemTray.addSeparator();
final Menu submenu = this.systemTray.addMenu("Options", BLACK_MAIL); final Menu submenu = this.systemTray.addMenu("Options", BLACK_MAIL);
@ -108,6 +111,6 @@ class TestTray {
systemTray.shutdown(); systemTray.shutdown();
//System.exit(0); not necessary if all non-daemon threads have stopped. //System.exit(0); not necessary if all non-daemon threads have stopped.
} }
}); }).setShortcut('q'); // case does not matter
} }
} }

View File

@ -83,7 +83,7 @@ class TestTrayJavaFX extends Application {
throw new RuntimeException("Unable to load SystemTray!"); throw new RuntimeException("Unable to load SystemTray!");
} }
this.systemTray.setIcon(LT_GRAY_MAIL); this.systemTray.setImage(LT_GRAY_MAIL);
systemTray.setStatus("No Mail"); systemTray.setStatus("No Mail");
@ -91,7 +91,7 @@ class TestTrayJavaFX extends Application {
@Override @Override
public public
void onClick(final SystemTray systemTray, final Menu parent, final MenuEntry menuEntry) { void onClick(final SystemTray systemTray, final Menu parent, final MenuEntry menuEntry) {
systemTray.setIcon(GREEN_MAIL); systemTray.setImage(GREEN_MAIL);
systemTray.setStatus("Some Mail!"); systemTray.setStatus("Some Mail!");
menuEntry.setCallback(callbackGray); menuEntry.setCallback(callbackGray);
@ -106,7 +106,7 @@ class TestTrayJavaFX extends Application {
public public
void onClick(final SystemTray systemTray, final Menu parent, final MenuEntry menuEntry) { void onClick(final SystemTray systemTray, final Menu parent, final MenuEntry menuEntry) {
systemTray.setStatus(null); systemTray.setStatus(null);
systemTray.setIcon(BLACK_MAIL); systemTray.setImage(BLACK_MAIL);
menuEntry.setCallback(null); menuEntry.setCallback(null);
// systemTray.setStatus("Mail Empty"); // systemTray.setStatus("Mail Empty");
@ -115,7 +115,10 @@ class TestTrayJavaFX extends Application {
} }
}; };
this.systemTray.addEntry("Green Mail", GREEN_MAIL, callbackGreen); MenuEntry menuEntry = this.systemTray.addEntry("Green Mail", GREEN_MAIL, callbackGreen);
// case does not matter
menuEntry.setShortcut('G');
this.systemTray.addSeparator(); this.systemTray.addSeparator();
final Menu submenu = this.systemTray.addMenu("Options", BLACK_MAIL); final Menu submenu = this.systemTray.addMenu("Options", BLACK_MAIL);
@ -142,6 +145,6 @@ class TestTrayJavaFX extends Application {
Platform.exit(); // necessary to close javaFx Platform.exit(); // necessary to close javaFx
//System.exit(0); not necessary if all non-daemon threads have stopped. //System.exit(0); not necessary if all non-daemon threads have stopped.
} }
}); }).setShortcut('q'); // case does not matter
} }
} }

View File

@ -67,7 +67,7 @@ class TestTraySwt {
throw new RuntimeException("Unable to load SystemTray!"); throw new RuntimeException("Unable to load SystemTray!");
} }
this.systemTray.setIcon(LT_GRAY_MAIL); this.systemTray.setImage(LT_GRAY_MAIL);
systemTray.setStatus("No Mail"); systemTray.setStatus("No Mail");
@ -76,7 +76,7 @@ class TestTraySwt {
public public
void onClick(final SystemTray systemTray, final Menu parent, final MenuEntry menuEntry) { void onClick(final SystemTray systemTray, final Menu parent, final MenuEntry menuEntry) {
systemTray.setStatus("Some Mail!"); systemTray.setStatus("Some Mail!");
systemTray.setIcon(GREEN_MAIL); systemTray.setImage(GREEN_MAIL);
menuEntry.setCallback(callbackGray); menuEntry.setCallback(callbackGray);
menuEntry.setImage(BLACK_MAIL); menuEntry.setImage(BLACK_MAIL);
@ -90,7 +90,7 @@ class TestTraySwt {
public public
void onClick(final SystemTray systemTray, final Menu parent, final MenuEntry menuEntry) { void onClick(final SystemTray systemTray, final Menu parent, final MenuEntry menuEntry) {
systemTray.setStatus(null); systemTray.setStatus(null);
systemTray.setIcon(BLACK_MAIL); systemTray.setImage(BLACK_MAIL);
menuEntry.setCallback(null); menuEntry.setCallback(null);
// systemTray.setStatus("Mail Empty"); // systemTray.setStatus("Mail Empty");
@ -99,7 +99,10 @@ class TestTraySwt {
} }
}; };
this.systemTray.addEntry("Green Mail", GREEN_MAIL, callbackGreen); MenuEntry menuEntry = this.systemTray.addEntry("Green Mail", GREEN_MAIL, callbackGreen);
// case does not matter
menuEntry.setShortcut('G');
this.systemTray.addSeparator(); this.systemTray.addSeparator();
final Menu submenu = this.systemTray.addMenu("Options", BLACK_MAIL); final Menu submenu = this.systemTray.addMenu("Options", BLACK_MAIL);
@ -132,7 +135,7 @@ class TestTraySwt {
//System.exit(0); not necessary if all non-daemon threads have stopped. //System.exit(0); not necessary if all non-daemon threads have stopped.
} }
}); }).setShortcut('q'); // case does not matter