Added setImage from a file

This commit is contained in:
nathan 2016-09-28 17:00:31 +02:00
parent ddcec4cb68
commit 83c865a51c
4 changed files with 39 additions and 5 deletions

View File

@ -16,6 +16,7 @@
package dorkbox.systemTray; package dorkbox.systemTray;
import java.io.File;
import java.io.InputStream; import java.io.InputStream;
import java.net.URL; import java.net.URL;
@ -36,6 +37,13 @@ interface MenuEntry {
*/ */
void setText(String newText); void setText(String newText);
/**
* Specifies the new image to set for a menu entry, NULL to delete the image
*
* @param imageFile the file of the image to use or null
*/
void setImage(File imageFile);
/** /**
* Specifies the new image to set for a menu entry, NULL to delete the image * Specifies the new image to set for a menu entry, NULL to delete the image
* *

View File

@ -82,6 +82,17 @@ class GtkMenuEntry implements MenuEntry {
}); });
} }
@Override
public
void setImage(final File imageFile) {
if (imageFile == null) {
setImage_(null);
}
else {
setImage_(ImageUtils.resizeAndCache(ImageUtils.ENTRY_SIZE, imageFile));
}
}
@Override @Override
public final public final
void setImage(final String imagePath) { void setImage(final String imagePath) {

View File

@ -30,18 +30,18 @@ abstract
class SwingMenuEntry implements MenuEntry { class SwingMenuEntry implements MenuEntry {
private final int id = SwingSystemTray.MENU_ID_COUNTER.getAndIncrement(); private final int id = SwingSystemTray.MENU_ID_COUNTER.getAndIncrement();
final SwingSystemTray systemTray; private final SwingSystemTrayMenuPopup menu;
final JComponent menuItem; final JComponent menuItem;
// 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
private volatile String text; private volatile String text;
// this is ALWAYS called on the EDT. // this is ALWAYS called on the EDT.
SwingMenuEntry(JComponent menuItem, final SwingSystemTray systemTray) { SwingMenuEntry(JComponent menuItem, final SwingSystemTrayMenuPopup menu) {
this.menuItem = menuItem; this.menuItem = menuItem;
this.systemTray = systemTray; this.menu = menu;
systemTray.getMenu().add(menuItem); menu.add(menuItem);
} }
/** /**
@ -74,6 +74,17 @@ class SwingMenuEntry implements MenuEntry {
}); });
} }
@Override
public
void setImage(final File imageFile) {
if (imageFile == null) {
setImage_(null);
}
else {
setImage_(ImageUtils.resizeAndCache(ImageUtils.ENTRY_SIZE, imageFile));
}
}
@Override @Override
public final public final
void setImage(final String imagePath) { void setImage(final String imagePath) {
@ -126,7 +137,7 @@ class SwingMenuEntry implements MenuEntry {
public public
void run() { void run() {
removePrivate(); removePrivate();
systemTray.getMenu().remove(menuItem); menu.remove(menuItem);
} }
}); });
} }

View File

@ -258,6 +258,10 @@ class ImageUtils {
return null; return null;
} }
public static synchronized
File resizeAndCache(final int size, final File file) {
return resizeAndCache(size, file.getAbsolutePath());
}
public static synchronized public static synchronized
File resizeAndCache(final int size, final String fileName) { File resizeAndCache(final int size, final String fileName) {