API cleanup, WIP adding widgets to the menu (on pause because of
problems with the enter key not responding after this). Updated test examples.
This commit is contained in:
parent
84a7fb77af
commit
09143f7630
@ -16,9 +16,9 @@
|
|||||||
package dorkbox.systemTray;
|
package dorkbox.systemTray;
|
||||||
|
|
||||||
public
|
public
|
||||||
interface SystemTrayMenuAction {
|
interface Action {
|
||||||
/**
|
/**
|
||||||
* This method will ALWAYS be called in the swing EDT
|
* This method will ALWAYS be called in the swing EDT. If there is work conducted in this method, it will slow-down the GUI.
|
||||||
*
|
*
|
||||||
* @param systemTray this is the parent, system tray object
|
* @param systemTray this is the parent, system tray object
|
||||||
* @param parent this is the parent menu of this menu entry
|
* @param parent this is the parent menu of this menu entry
|
@ -97,7 +97,7 @@ interface Entry {
|
|||||||
*
|
*
|
||||||
* @param callback the callback to set. If null, the callback is safely removed.
|
* @param callback the callback to set. If null, the callback is safely removed.
|
||||||
*/
|
*/
|
||||||
void setCallback(SystemTrayMenuAction callback);
|
void setCallback(Action callback);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets a menu entry shortcut key (Mnemonic) so that menu entry can be "selected" via the keyboard while the menu is displayed.
|
* Sets a menu entry shortcut key (Mnemonic) so that menu entry can be "selected" via the keyboard while the menu is displayed.
|
||||||
|
@ -52,7 +52,7 @@ interface Menu extends Entry {
|
|||||||
/**
|
/**
|
||||||
* This removes al menu entries from this menu
|
* This removes al menu entries from this menu
|
||||||
*/
|
*/
|
||||||
void clear();
|
void removeAll();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the menu entry for a specified text
|
* Gets the menu entry for a specified text
|
||||||
@ -62,17 +62,17 @@ interface Menu extends Entry {
|
|||||||
Entry get(final String menuText);
|
Entry get(final String menuText);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the first menu entry or sub-menu, ignoring status and spacers
|
* Gets the first menu entry or sub-menu, ignoring status and separators
|
||||||
*/
|
*/
|
||||||
Entry getFirst();
|
Entry getFirst();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the last menu entry or sub-menu, ignoring status and spacers
|
* Gets the last menu entry or sub-menu, ignoring status and separators
|
||||||
*/
|
*/
|
||||||
Entry getLast();
|
Entry getLast();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the menu entry or sub-menu for a specified index (zero-index), ignoring status and spacers
|
* Gets the menu entry or sub-menu for a specified index (zero-index), ignoring status and separators
|
||||||
*
|
*
|
||||||
* @param menuIndex the menu entry index to use to retrieve the menu entry.
|
* @param menuIndex the menu entry index to use to retrieve the menu entry.
|
||||||
*/
|
*/
|
||||||
@ -86,7 +86,7 @@ interface Menu extends Entry {
|
|||||||
* @param menuText string of the text you want to appear
|
* @param menuText string of the text you want to appear
|
||||||
* @param callback callback that will be executed when this menu entry is clicked
|
* @param callback callback that will be executed when this menu entry is clicked
|
||||||
*/
|
*/
|
||||||
Entry addEntry(String menuText, SystemTrayMenuAction callback);
|
Entry addEntry(String menuText, Action callback);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds a menu entry with text + image
|
* Adds a menu entry with text + image
|
||||||
@ -95,7 +95,7 @@ interface Menu extends Entry {
|
|||||||
* @param imagePath the image (full path required) to use. If null, no image will be used
|
* @param imagePath the image (full path required) to use. If null, no image will be used
|
||||||
* @param callback callback that will be executed when this menu entry is clicked
|
* @param callback callback that will be executed when this menu entry is clicked
|
||||||
*/
|
*/
|
||||||
Entry addEntry(String menuText, String imagePath, SystemTrayMenuAction callback);
|
Entry addEntry(String menuText, String imagePath, Action callback);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds a menu entry with text + image
|
* Adds a menu entry with text + image
|
||||||
@ -104,7 +104,7 @@ interface Menu extends Entry {
|
|||||||
* @param imageUrl the URL of the image to use. If null, no image will be used
|
* @param imageUrl the URL of the image to use. If null, no image will be used
|
||||||
* @param callback callback that will be executed when this menu entry is clicked
|
* @param callback callback that will be executed when this menu entry is clicked
|
||||||
*/
|
*/
|
||||||
Entry addEntry(String menuText, URL imageUrl, SystemTrayMenuAction callback);
|
Entry addEntry(String menuText, URL imageUrl, Action callback);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds a menu entry with text + image
|
* Adds a menu entry with text + image
|
||||||
@ -114,7 +114,7 @@ interface Menu extends Entry {
|
|||||||
* @param imageStream the InputStream of the image to use. If null, no image will be used
|
* @param imageStream the InputStream of the image to use. If null, no image will be used
|
||||||
* @param callback callback that will be executed when this menu entry is clicked
|
* @param callback callback that will be executed when this menu entry is clicked
|
||||||
*/
|
*/
|
||||||
Entry addEntry(String menuText, String cacheName, InputStream imageStream, SystemTrayMenuAction callback);
|
Entry addEntry(String menuText, String cacheName, InputStream imageStream, Action callback);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds a menu entry with text + image
|
* Adds a menu entry with text + image
|
||||||
@ -123,7 +123,7 @@ interface Menu extends Entry {
|
|||||||
* @param imageStream the InputStream of the image to use. If null, no image will be used
|
* @param imageStream the InputStream of the image to use. If null, no image will be used
|
||||||
* @param callback callback that will be executed when this menu entry is clicked
|
* @param callback callback that will be executed when this menu entry is clicked
|
||||||
*/
|
*/
|
||||||
Entry addEntry(String menuText, InputStream imageStream, SystemTrayMenuAction callback);
|
Entry addEntry(String menuText, InputStream imageStream, Action callback);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -168,6 +168,14 @@ interface Menu extends Entry {
|
|||||||
*/
|
*/
|
||||||
Menu addMenu(String menuText, InputStream imageStream);
|
Menu addMenu(String menuText, InputStream imageStream);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a swing widget as a menu entry.
|
||||||
|
*
|
||||||
|
* @param widget the JComponent that is to be added as an entry
|
||||||
|
*/
|
||||||
|
// TODO: buggy. The menu will **sometimes** stop responding to the "enter" key after this. Mnemonics still work however.
|
||||||
|
// Entry addWidget(JComponent widget);
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This removes a menu entry from the dropdown menu.
|
* This removes a menu entry from the dropdown menu.
|
||||||
|
@ -562,10 +562,7 @@ class SystemTray implements Menu {
|
|||||||
/*
|
/*
|
||||||
* appIndicator/gtk require strings (which is the path)
|
* appIndicator/gtk require strings (which is the path)
|
||||||
* swing version loads as an image (which can be stream or path, we use path)
|
* swing version loads as an image (which can be stream or path, we use path)
|
||||||
*
|
|
||||||
* For KDE4, it must also be unique across runs
|
|
||||||
*/
|
*/
|
||||||
CacheUtil.setUniqueCachePerRun = isKDE;
|
|
||||||
CacheUtil.tempDir = "SysTray";
|
CacheUtil.tempDir = "SysTray";
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -883,7 +880,7 @@ class SystemTray implements Menu {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public
|
public
|
||||||
void setCallback(final SystemTrayMenuAction callback) {
|
void setCallback(final Action callback) {
|
||||||
// NO OP.
|
// NO OP.
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -951,7 +948,7 @@ class SystemTray implements Menu {
|
|||||||
* @param callback callback that will be executed when this menu entry is clicked
|
* @param callback callback that will be executed when this menu entry is clicked
|
||||||
*/
|
*/
|
||||||
public final
|
public final
|
||||||
Entry addEntry(String menuText, SystemTrayMenuAction callback) {
|
Entry addEntry(String menuText, Action callback) {
|
||||||
return addEntry(menuText, (String) null, callback);
|
return addEntry(menuText, (String) null, callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -963,7 +960,7 @@ class SystemTray implements Menu {
|
|||||||
* @param callback callback that will be executed when this menu entry is clicked
|
* @param callback callback that will be executed when this menu entry is clicked
|
||||||
*/
|
*/
|
||||||
public final
|
public final
|
||||||
Entry addEntry(String menuText, String imagePath, SystemTrayMenuAction callback) {
|
Entry addEntry(String menuText, String imagePath, Action callback) {
|
||||||
return systemTrayMenu.addEntry(menuText, imagePath, callback);
|
return systemTrayMenu.addEntry(menuText, imagePath, callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -975,7 +972,7 @@ class SystemTray implements Menu {
|
|||||||
* @param callback callback that will be executed when this menu entry is clicked
|
* @param callback callback that will be executed when this menu entry is clicked
|
||||||
*/
|
*/
|
||||||
public final
|
public final
|
||||||
Entry addEntry(String menuText, URL imageUrl, SystemTrayMenuAction callback) {
|
Entry addEntry(String menuText, URL imageUrl, Action callback) {
|
||||||
return systemTrayMenu.addEntry(menuText, imageUrl, callback);
|
return systemTrayMenu.addEntry(menuText, imageUrl, callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -988,7 +985,7 @@ class SystemTray implements Menu {
|
|||||||
* @param callback callback that will be executed when this menu entry is clicked
|
* @param callback callback that will be executed when this menu entry is clicked
|
||||||
*/
|
*/
|
||||||
public
|
public
|
||||||
Entry addEntry(String menuText, String cacheName, InputStream imageStream, SystemTrayMenuAction callback) {
|
Entry addEntry(String menuText, String cacheName, InputStream imageStream, Action callback) {
|
||||||
return systemTrayMenu.addEntry(menuText, cacheName, imageStream, callback);
|
return systemTrayMenu.addEntry(menuText, cacheName, imageStream, callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1000,7 +997,7 @@ class SystemTray implements Menu {
|
|||||||
* @param callback callback that will be executed when this menu entry is clicked
|
* @param callback callback that will be executed when this menu entry is clicked
|
||||||
*/
|
*/
|
||||||
public final
|
public final
|
||||||
Entry addEntry(String menuText, InputStream imageStream, SystemTrayMenuAction callback) {
|
Entry addEntry(String menuText, InputStream imageStream, Action callback) {
|
||||||
return systemTrayMenu.addEntry(menuText, imageStream, callback);
|
return systemTrayMenu.addEntry(menuText, imageStream, callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1063,6 +1060,17 @@ class SystemTray implements Menu {
|
|||||||
return systemTrayMenu.addMenu(menuText, imageStream);
|
return systemTrayMenu.addMenu(menuText, imageStream);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a swing widget as a menu entry.
|
||||||
|
*
|
||||||
|
* @param widget the JComponent that is to be added as an entry
|
||||||
|
*/
|
||||||
|
// TODO: buggy. The menu will **sometimes** stop responding to the "enter" key after this. Mnemonics still work however.
|
||||||
|
// @Override
|
||||||
|
// public
|
||||||
|
// Entry addWidget(final JComponent widget) {
|
||||||
|
// return systemTrayMenu.addWidget(widget);
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -1092,8 +1100,8 @@ class SystemTray implements Menu {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public final
|
public final
|
||||||
void clear() {
|
void removeAll() {
|
||||||
systemTrayMenu.clear();
|
systemTrayMenu.removeAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -25,9 +25,7 @@ import javax.swing.JMenuItem;
|
|||||||
|
|
||||||
import dorkbox.systemTray.Entry;
|
import dorkbox.systemTray.Entry;
|
||||||
import dorkbox.systemTray.Menu;
|
import dorkbox.systemTray.Menu;
|
||||||
import dorkbox.systemTray.SystemTray;
|
|
||||||
import dorkbox.systemTray.util.ImageUtils;
|
import dorkbox.systemTray.util.ImageUtils;
|
||||||
import dorkbox.util.SwingUtil;
|
|
||||||
|
|
||||||
abstract
|
abstract
|
||||||
class EntryImpl implements Entry {
|
class EntryImpl implements Entry {
|
||||||
@ -102,7 +100,7 @@ class EntryImpl implements Entry {
|
|||||||
void setText(final String newText) {
|
void setText(final String newText) {
|
||||||
this.text = newText;
|
this.text = newText;
|
||||||
|
|
||||||
SwingUtil.invokeLater(new Runnable() {
|
parent.dispatch(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public
|
public
|
||||||
void run() {
|
void run() {
|
||||||
@ -169,8 +167,7 @@ class EntryImpl implements Entry {
|
|||||||
@Override
|
@Override
|
||||||
public final
|
public final
|
||||||
void remove() {
|
void remove() {
|
||||||
try {
|
parent.dispatchAndWait(new Runnable() {
|
||||||
SwingUtil.invokeAndWait(new Runnable() {
|
|
||||||
@Override
|
@Override
|
||||||
public
|
public
|
||||||
void run() {
|
void run() {
|
||||||
@ -178,9 +175,6 @@ class EntryImpl implements Entry {
|
|||||||
parent._native.remove(_native);
|
parent._native.remove(_native);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} catch (Exception e) {
|
|
||||||
SystemTray.logger.error("Error processing event on the dispatch thread.", e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// called when this item is removed. Necessary to cleanup/remove itself
|
// called when this item is removed. Necessary to cleanup/remove itself
|
||||||
|
@ -22,7 +22,7 @@ import java.io.File;
|
|||||||
import javax.swing.ImageIcon;
|
import javax.swing.ImageIcon;
|
||||||
import javax.swing.JMenuItem;
|
import javax.swing.JMenuItem;
|
||||||
|
|
||||||
import dorkbox.systemTray.SystemTrayMenuAction;
|
import dorkbox.systemTray.Action;
|
||||||
import dorkbox.util.SwingUtil;
|
import dorkbox.util.SwingUtil;
|
||||||
|
|
||||||
class EntryItem extends EntryImpl {
|
class EntryItem extends EntryImpl {
|
||||||
@ -30,10 +30,10 @@ class EntryItem extends EntryImpl {
|
|||||||
private final ActionListener swingCallback;
|
private final ActionListener swingCallback;
|
||||||
|
|
||||||
private volatile boolean hasLegitIcon = false;
|
private volatile boolean hasLegitIcon = false;
|
||||||
private volatile SystemTrayMenuAction callback;
|
private volatile Action callback;
|
||||||
|
|
||||||
// this is ALWAYS called on the EDT.
|
// this is ALWAYS called on the EDT.
|
||||||
EntryItem(final MenuImpl parent, final SystemTrayMenuAction callback) {
|
EntryItem(final MenuImpl parent, final Action callback) {
|
||||||
super(parent, new AdjustedJMenuItem());
|
super(parent, new AdjustedJMenuItem());
|
||||||
this.callback = callback;
|
this.callback = callback;
|
||||||
|
|
||||||
@ -58,7 +58,7 @@ class EntryItem extends EntryImpl {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public
|
public
|
||||||
void setCallback(final SystemTrayMenuAction callback) {
|
void setCallback(final Action callback) {
|
||||||
this.callback = callback;
|
this.callback = callback;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,7 +19,7 @@ import java.io.File;
|
|||||||
|
|
||||||
import javax.swing.JSeparator;
|
import javax.swing.JSeparator;
|
||||||
|
|
||||||
import dorkbox.systemTray.SystemTrayMenuAction;
|
import dorkbox.systemTray.Action;
|
||||||
|
|
||||||
class EntrySeparator extends EntryImpl implements dorkbox.systemTray.Separator {
|
class EntrySeparator extends EntryImpl implements dorkbox.systemTray.Separator {
|
||||||
|
|
||||||
@ -54,6 +54,6 @@ class EntrySeparator extends EntryImpl implements dorkbox.systemTray.Separator {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public
|
public
|
||||||
void setCallback(final SystemTrayMenuAction callback) {
|
void setCallback(final Action callback) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,8 +20,8 @@ import java.io.File;
|
|||||||
|
|
||||||
import javax.swing.JMenuItem;
|
import javax.swing.JMenuItem;
|
||||||
|
|
||||||
|
import dorkbox.systemTray.Action;
|
||||||
import dorkbox.systemTray.Status;
|
import dorkbox.systemTray.Status;
|
||||||
import dorkbox.systemTray.SystemTrayMenuAction;
|
|
||||||
|
|
||||||
class EntryStatus extends EntryImpl implements Status {
|
class EntryStatus extends EntryImpl implements Status {
|
||||||
|
|
||||||
@ -64,7 +64,7 @@ class EntryStatus extends EntryImpl implements Status {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public
|
public
|
||||||
void setCallback(final SystemTrayMenuAction callback) {
|
void setCallback(final Action callback) {
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
62
src/dorkbox/systemTray/swing/EntryWidget.java
Normal file
62
src/dorkbox/systemTray/swing/EntryWidget.java
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2016 dorkbox, llc
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package dorkbox.systemTray.swing;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
import javax.swing.JComponent;
|
||||||
|
|
||||||
|
import dorkbox.systemTray.Action;
|
||||||
|
|
||||||
|
// TODO: buggy. The menu will **sometimes** stop responding to the "enter" key after this. Mnemonics still work however.
|
||||||
|
class EntryWidget extends EntryImpl implements dorkbox.systemTray.Separator {
|
||||||
|
|
||||||
|
// this is ALWAYS called on the EDT.
|
||||||
|
EntryWidget(final MenuImpl parent, JComponent widget) {
|
||||||
|
super(parent, widget);
|
||||||
|
|
||||||
|
_native.setEnabled(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
// called in the EDT thread
|
||||||
|
@Override
|
||||||
|
void renderText(final String text) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
void setImage_(final File imageFile) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
void removePrivate() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public
|
||||||
|
void setShortcut(final char key) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public
|
||||||
|
boolean hasImage() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public
|
||||||
|
void setCallback(final Action callback) {
|
||||||
|
}
|
||||||
|
}
|
@ -30,20 +30,21 @@ import javax.swing.ImageIcon;
|
|||||||
import javax.swing.JComponent;
|
import javax.swing.JComponent;
|
||||||
import javax.swing.JMenuItem;
|
import javax.swing.JMenuItem;
|
||||||
|
|
||||||
|
import dorkbox.systemTray.Action;
|
||||||
import dorkbox.systemTray.Entry;
|
import dorkbox.systemTray.Entry;
|
||||||
import dorkbox.systemTray.Menu;
|
import dorkbox.systemTray.Menu;
|
||||||
import dorkbox.systemTray.Status;
|
import dorkbox.systemTray.Status;
|
||||||
import dorkbox.systemTray.SystemTray;
|
import dorkbox.systemTray.SystemTray;
|
||||||
import dorkbox.systemTray.SystemTrayMenuAction;
|
|
||||||
import dorkbox.systemTray.util.ImageUtils;
|
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 Entry -- so it has both
|
// this is a weird composite class, because it must be a Menu, but ALSO a Entry -- so it has both
|
||||||
|
@SuppressWarnings("ForLoopReplaceableByForEach")
|
||||||
class MenuImpl implements Menu {
|
class MenuImpl implements Menu {
|
||||||
public static final AtomicInteger MENU_ID_COUNTER = new AtomicInteger();
|
static final AtomicInteger MENU_ID_COUNTER = new AtomicInteger();
|
||||||
private final int id = MenuImpl.MENU_ID_COUNTER.getAndIncrement();
|
private final int id = MenuImpl.MENU_ID_COUNTER.getAndIncrement();
|
||||||
|
|
||||||
protected final java.util.List<Entry> menuEntries = new ArrayList<Entry>();
|
private final java.util.List<Entry> menuEntries = new ArrayList<Entry>();
|
||||||
|
|
||||||
private final SystemTray systemTray;
|
private final SystemTray systemTray;
|
||||||
private final Menu parent;
|
private final Menu parent;
|
||||||
@ -69,13 +70,11 @@ class MenuImpl implements Menu {
|
|||||||
this._native = _native;
|
this._native = _native;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected
|
|
||||||
void dispatch(final Runnable runnable) {
|
void dispatch(final Runnable runnable) {
|
||||||
// this will properly check if we are running on the EDT
|
// this will properly check if we are running on the EDT
|
||||||
SwingUtil.invokeLater(runnable);
|
SwingUtil.invokeLater(runnable);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected
|
|
||||||
void dispatchAndWait(final Runnable runnable) {
|
void dispatchAndWait(final Runnable runnable) {
|
||||||
// this will properly check if we are running on the EDT
|
// this will properly check if we are running on the EDT
|
||||||
try {
|
try {
|
||||||
@ -98,7 +97,7 @@ class MenuImpl implements Menu {
|
|||||||
* NOT ALWAYS CALLED ON EDT
|
* NOT ALWAYS CALLED ON EDT
|
||||||
*/
|
*/
|
||||||
private
|
private
|
||||||
Entry addEntry_(final String menuText, final File imagePath, final SystemTrayMenuAction callback) {
|
Entry addEntry_(final String menuText, final File imagePath, final Action callback) {
|
||||||
if (menuText == null) {
|
if (menuText == null) {
|
||||||
throw new NullPointerException("Menu text cannot be null");
|
throw new NullPointerException("Menu text cannot be null");
|
||||||
}
|
}
|
||||||
@ -154,7 +153,7 @@ class MenuImpl implements Menu {
|
|||||||
if (entry == null) {
|
if (entry == null) {
|
||||||
// must always be called on the EDT
|
// must always be called on the EDT
|
||||||
entry = new MenuImpl(getSystemTray(), MenuImpl.this, new AdjustedJMenu());
|
entry = new MenuImpl(getSystemTray(), MenuImpl.this, new AdjustedJMenu());
|
||||||
_native.add(((MenuImpl) entry)._native);
|
_native.add(((MenuImpl) entry)._native); // have to add it separately
|
||||||
|
|
||||||
entry.setText(menuText);
|
entry.setText(menuText);
|
||||||
entry.setImage(imagePath);
|
entry.setImage(imagePath);
|
||||||
@ -256,6 +255,31 @@ class MenuImpl implements Menu {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: buggy. The menu will **sometimes** stop responding to the "enter" key after this. Mnemonics still work however.
|
||||||
|
// public
|
||||||
|
// Entry addWidget(final JComponent widget) {
|
||||||
|
// if (widget == null) {
|
||||||
|
// throw new NullPointerException("Widget cannot be null");
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// final AtomicReference<Entry> value = new AtomicReference<Entry>();
|
||||||
|
//
|
||||||
|
// dispatchAndWait(new Runnable() {
|
||||||
|
// @Override
|
||||||
|
// public
|
||||||
|
// void run() {
|
||||||
|
// synchronized (menuEntries) {
|
||||||
|
// // must always be called on the EDT
|
||||||
|
// Entry entry = new EntryWidget(MenuImpl.this, widget);
|
||||||
|
// value.set(entry);
|
||||||
|
// menuEntries.add(entry);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// });
|
||||||
|
//
|
||||||
|
// return value.get();
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
public
|
public
|
||||||
Entry get(final String menuText) {
|
Entry get(final String menuText) {
|
||||||
@ -265,7 +289,8 @@ class MenuImpl implements Menu {
|
|||||||
|
|
||||||
// Must be wrapped in a synchronized block for object visibility
|
// Must be wrapped in a synchronized block for object visibility
|
||||||
synchronized (menuEntries) {
|
synchronized (menuEntries) {
|
||||||
for (Entry entry : menuEntries) {
|
for (int i = 0, menuEntriesSize = menuEntries.size(); i < menuEntriesSize; i++) {
|
||||||
|
final Entry entry = menuEntries.get(i);
|
||||||
String text = entry.getText();
|
String text = entry.getText();
|
||||||
|
|
||||||
// text can be null
|
// text can be null
|
||||||
@ -278,30 +303,33 @@ class MenuImpl implements Menu {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ignores status + separators
|
||||||
public
|
public
|
||||||
Entry getFirst() {
|
Entry getFirst() {
|
||||||
return get(0);
|
return get(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ignores status + separators
|
||||||
public
|
public
|
||||||
Entry getLast() {
|
Entry getLast() {
|
||||||
// Must be wrapped in a synchronized block for object visibility
|
// Must be wrapped in a synchronized block for object visibility
|
||||||
synchronized (menuEntries) {
|
synchronized (menuEntries) {
|
||||||
if (!menuEntries.isEmpty()) {
|
if (!menuEntries.isEmpty()) {
|
||||||
Entry entry = null;
|
Entry entry;
|
||||||
for (int i = 0, menuEntriesSize = menuEntries.size(); i < menuEntriesSize; i++) {
|
for (int i = menuEntries.size()-1; i >= 0; i--) {
|
||||||
entry = menuEntries.get(i);
|
entry = menuEntries.get(i);
|
||||||
}
|
|
||||||
|
|
||||||
if (!(entry instanceof dorkbox.systemTray.Separator || entry instanceof Status)) {
|
if (!(entry instanceof dorkbox.systemTray.Separator || entry instanceof Status)) {
|
||||||
return entry;
|
return entry;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ignores status + separators
|
||||||
public
|
public
|
||||||
Entry get(final int menuIndex) {
|
Entry get(final int menuIndex) {
|
||||||
if (menuIndex < 0) {
|
if (menuIndex < 0) {
|
||||||
@ -331,12 +359,12 @@ class MenuImpl implements Menu {
|
|||||||
|
|
||||||
|
|
||||||
public
|
public
|
||||||
Entry addEntry(String menuText, SystemTrayMenuAction callback) {
|
Entry addEntry(String menuText, Action callback) {
|
||||||
return addEntry(menuText, (String) null, callback);
|
return addEntry(menuText, (String) null, callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
public
|
public
|
||||||
Entry addEntry(String menuText, String imagePath, SystemTrayMenuAction callback) {
|
Entry addEntry(String menuText, String imagePath, Action callback) {
|
||||||
if (imagePath == null) {
|
if (imagePath == null) {
|
||||||
return addEntry_(menuText, null, callback);
|
return addEntry_(menuText, null, callback);
|
||||||
}
|
}
|
||||||
@ -346,7 +374,7 @@ class MenuImpl implements Menu {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public
|
public
|
||||||
Entry addEntry(String menuText, URL imageUrl, SystemTrayMenuAction callback) {
|
Entry addEntry(String menuText, URL imageUrl, Action callback) {
|
||||||
if (imageUrl == null) {
|
if (imageUrl == null) {
|
||||||
return addEntry_(menuText, null, callback);
|
return addEntry_(menuText, null, callback);
|
||||||
}
|
}
|
||||||
@ -356,7 +384,7 @@ class MenuImpl implements Menu {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public
|
public
|
||||||
Entry addEntry(String menuText, String cacheName, InputStream imageStream, SystemTrayMenuAction callback) {
|
Entry addEntry(String menuText, String cacheName, InputStream imageStream, Action callback) {
|
||||||
if (imageStream == null) {
|
if (imageStream == null) {
|
||||||
return addEntry_(menuText, null, callback);
|
return addEntry_(menuText, null, callback);
|
||||||
}
|
}
|
||||||
@ -366,7 +394,7 @@ class MenuImpl implements Menu {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public
|
public
|
||||||
Entry addEntry(String menuText, InputStream imageStream, SystemTrayMenuAction callback) {
|
Entry addEntry(String menuText, InputStream imageStream, Action callback) {
|
||||||
if (imageStream == null) {
|
if (imageStream == null) {
|
||||||
return addEntry_(menuText, null, callback);
|
return addEntry_(menuText, null, callback);
|
||||||
}
|
}
|
||||||
@ -483,29 +511,54 @@ class MenuImpl implements Menu {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public
|
||||||
|
String getStatus() {
|
||||||
|
synchronized (menuEntries) {
|
||||||
|
Entry entry = menuEntries.get(0);
|
||||||
|
if (entry instanceof EntryStatus) {
|
||||||
|
return entry.getText();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public
|
||||||
|
void setStatus(final String statusText) {
|
||||||
|
final MenuImpl _this = this;
|
||||||
|
dispatchAndWait(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public
|
||||||
|
void run() {
|
||||||
|
synchronized (menuEntries) {
|
||||||
|
// status is ALWAYS at 0 index...
|
||||||
|
EntryImpl menuEntry = null;
|
||||||
|
if (!menuEntries.isEmpty()) {
|
||||||
|
menuEntry = (EntryImpl) menuEntries.get(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (menuEntry instanceof EntryStatus) {
|
||||||
|
// set the text or delete...
|
||||||
|
|
||||||
|
if (statusText == null) {
|
||||||
|
// delete
|
||||||
|
remove(menuEntry);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// set text
|
||||||
|
menuEntry.setText(statusText);
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
// create a new one
|
||||||
|
menuEntry = new EntryStatus(_this, statusText);
|
||||||
|
// status is ALWAYS at 0 index...
|
||||||
|
menuEntries.add(0, menuEntry);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -531,7 +584,7 @@ class MenuImpl implements Menu {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public
|
public
|
||||||
void setCallback(final SystemTrayMenuAction callback) {
|
void setCallback(final Action callback) {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -702,7 +755,7 @@ class MenuImpl implements Menu {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public
|
public
|
||||||
void clear() {
|
void removeAll() {
|
||||||
dispatch(new Runnable() {
|
dispatch(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public
|
public
|
||||||
@ -713,8 +766,33 @@ class MenuImpl implements Menu {
|
|||||||
for (Entry entry : menuEntriesCopy) {
|
for (Entry entry : menuEntriesCopy) {
|
||||||
entry.remove();
|
entry.remove();
|
||||||
}
|
}
|
||||||
|
menuEntries.clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public final
|
||||||
|
int hashCode() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public final
|
||||||
|
boolean equals(Object obj) {
|
||||||
|
if (this == obj) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (obj == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (getClass() != obj.getClass()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
MenuImpl other = (MenuImpl) obj;
|
||||||
|
return this.id == other.id;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -140,12 +140,7 @@ class TrayPopup extends JPopupMenu {
|
|||||||
hiddenDialog.dispatchEvent(new WindowEvent(hiddenDialog, WindowEvent.WINDOW_CLOSING));
|
hiddenDialog.dispatchEvent(new WindowEvent(hiddenDialog, WindowEvent.WINDOW_CLOSING));
|
||||||
}
|
}
|
||||||
|
|
||||||
void doShow(final Point point, int offset) {
|
void doShow(final MenuImpl systemTray, final Point point, int offset) {
|
||||||
// when the menu entries are changed, this makes sure to correctly show them
|
|
||||||
invalidate();
|
|
||||||
revalidate();
|
|
||||||
doLayout();
|
|
||||||
|
|
||||||
Dimension size = getPreferredSize();
|
Dimension size = getPreferredSize();
|
||||||
Rectangle bounds = ScreenUtil.getScreenBoundsAt(point);
|
Rectangle bounds = ScreenUtil.getScreenBoundsAt(point);
|
||||||
|
|
||||||
@ -185,6 +180,13 @@ class TrayPopup extends JPopupMenu {
|
|||||||
setLocation(x, y);
|
setLocation(x, y);
|
||||||
setVisible(true);
|
setVisible(true);
|
||||||
|
|
||||||
|
// when the menu entries are changed, this makes sure to correctly show them
|
||||||
|
invalidate();
|
||||||
|
revalidate();
|
||||||
|
doLayout();
|
||||||
|
|
||||||
requestFocusInWindow();
|
requestFocusInWindow();
|
||||||
|
|
||||||
|
((EntryImpl) systemTray.getFirst())._native.requestFocusInWindow();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -145,7 +145,7 @@ class _AppIndicatorTray extends MenuImpl {
|
|||||||
.getLocation();
|
.getLocation();
|
||||||
|
|
||||||
TrayPopup popupMenu = (TrayPopup) _native;
|
TrayPopup popupMenu = (TrayPopup) _native;
|
||||||
popupMenu.doShow(point, SystemTray.DEFAULT_TRAY_SIZE);
|
popupMenu.doShow(_AppIndicatorTray.this, point, SystemTray.DEFAULT_TRAY_SIZE);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -219,8 +219,8 @@ class _AppIndicatorTray extends MenuImpl {
|
|||||||
Gtk.shutdownGui();
|
Gtk.shutdownGui();
|
||||||
|
|
||||||
// uses EDT
|
// uses EDT
|
||||||
clear();
|
removeAll();
|
||||||
remove();
|
remove(); // remove ourselves from our parent
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -81,7 +81,7 @@ class _GtkStatusIconTray extends MenuImpl {
|
|||||||
.getLocation();
|
.getLocation();
|
||||||
|
|
||||||
TrayPopup popupMenu = (TrayPopup) _native;
|
TrayPopup popupMenu = (TrayPopup) _native;
|
||||||
popupMenu.doShow(point, 0);
|
popupMenu.doShow(_GtkStatusIconTray.this, point, 0);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -171,8 +171,8 @@ class _GtkStatusIconTray extends MenuImpl {
|
|||||||
Gtk.shutdownGui();
|
Gtk.shutdownGui();
|
||||||
|
|
||||||
// uses EDT
|
// uses EDT
|
||||||
clear();
|
removeAll();
|
||||||
remove();
|
remove(); // remove ourselves from our parent
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -67,7 +67,7 @@ class _SwingTray extends MenuImpl {
|
|||||||
void run() {
|
void run() {
|
||||||
tray.remove(trayIcon);
|
tray.remove(trayIcon);
|
||||||
|
|
||||||
clear();
|
removeAll();
|
||||||
remove();
|
remove();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -100,7 +100,7 @@ class _SwingTray extends MenuImpl {
|
|||||||
public
|
public
|
||||||
void mousePressed(MouseEvent e) {
|
void mousePressed(MouseEvent e) {
|
||||||
TrayPopup popupMenu = (TrayPopup) _native;
|
TrayPopup popupMenu = (TrayPopup) _native;
|
||||||
popupMenu.doShow(e.getPoint(), 0);
|
popupMenu.doShow(_SwingTray.this, e.getPoint(), 0);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -18,10 +18,10 @@ package dorkbox;
|
|||||||
|
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
|
||||||
|
import dorkbox.systemTray.Action;
|
||||||
import dorkbox.systemTray.Entry;
|
import dorkbox.systemTray.Entry;
|
||||||
import dorkbox.systemTray.Menu;
|
import dorkbox.systemTray.Menu;
|
||||||
import dorkbox.systemTray.SystemTray;
|
import dorkbox.systemTray.SystemTray;
|
||||||
import dorkbox.systemTray.SystemTrayMenuAction;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Icons from 'SJJB Icons', public domain/CC0 icon set
|
* Icons from 'SJJB Icons', public domain/CC0 icon set
|
||||||
@ -29,9 +29,18 @@ import dorkbox.systemTray.SystemTrayMenuAction;
|
|||||||
public
|
public
|
||||||
class TestTray {
|
class TestTray {
|
||||||
|
|
||||||
public static final URL BLACK_MAIL = TestTray.class.getResource("transport_bus_station.p.000000.32.png");
|
public static final URL BLUE_CAMPING = TestTray.class.getResource("accommodation_camping.glow.0092DA.32.png");
|
||||||
public static final URL GREEN_MAIL = TestTray.class.getResource("transport_bus_station.p.39AC39.32.png");
|
public static final URL BLACK_FIRE = TestTray.class.getResource("amenity_firestation.p.000000.32.png");
|
||||||
public static final URL LT_GRAY_MAIL = TestTray.class.getResource("transport_bus_station.p.999999.32.png");
|
|
||||||
|
public static final URL BLACK_MAIL = TestTray.class.getResource("amenity_post_box.p.000000.32.png");
|
||||||
|
public static final URL GREEN_MAIL = TestTray.class.getResource("amenity_post_box.p.39AC39.32.png");
|
||||||
|
|
||||||
|
public static final URL BLACK_BUS = TestTray.class.getResource("transport_bus_station.p.000000.32.png");
|
||||||
|
public static final URL LT_GRAY_BUS = TestTray.class.getResource("transport_bus_station.p.999999.32.png");
|
||||||
|
|
||||||
|
public static final URL BLACK_TRAIN = TestTray.class.getResource("transport_train_station.p.000000.32.png");
|
||||||
|
public static final URL GREEN_TRAIN = TestTray.class.getResource("transport_train_station.p.39AC39.32.png");
|
||||||
|
public static final URL LT_GRAY_TRAIN = TestTray.class.getResource("transport_train_station.p.666666.32.png");
|
||||||
|
|
||||||
public static
|
public static
|
||||||
void main(String[] args) {
|
void main(String[] args) {
|
||||||
@ -40,8 +49,8 @@ class TestTray {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private SystemTray systemTray;
|
private SystemTray systemTray;
|
||||||
private SystemTrayMenuAction callbackGreen;
|
private Action callbackGreen;
|
||||||
private SystemTrayMenuAction callbackGray;
|
private Action callbackGray;
|
||||||
|
|
||||||
public
|
public
|
||||||
TestTray() {
|
TestTray() {
|
||||||
@ -50,15 +59,15 @@ class TestTray {
|
|||||||
throw new RuntimeException("Unable to load SystemTray!");
|
throw new RuntimeException("Unable to load SystemTray!");
|
||||||
}
|
}
|
||||||
|
|
||||||
systemTray.setImage(LT_GRAY_MAIL);
|
systemTray.setImage(LT_GRAY_TRAIN);
|
||||||
systemTray.setStatus("No Mail");
|
systemTray.setStatus("No Mail");
|
||||||
|
|
||||||
callbackGreen = new SystemTrayMenuAction() {
|
callbackGreen = new Action() {
|
||||||
@Override
|
@Override
|
||||||
public
|
public
|
||||||
void onClick(final SystemTray systemTray, final Menu parent, final Entry entry) {
|
void onClick(final SystemTray systemTray, final Menu parent, final Entry entry) {
|
||||||
systemTray.setStatus("Some Mail!");
|
systemTray.setStatus("Some Mail!");
|
||||||
systemTray.setImage(GREEN_MAIL);
|
systemTray.setImage(GREEN_TRAIN);
|
||||||
|
|
||||||
entry.setCallback(callbackGray);
|
entry.setCallback(callbackGray);
|
||||||
entry.setImage(BLACK_MAIL);
|
entry.setImage(BLACK_MAIL);
|
||||||
@ -67,12 +76,12 @@ class TestTray {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
callbackGray = new SystemTrayMenuAction() {
|
callbackGray = new Action() {
|
||||||
@Override
|
@Override
|
||||||
public
|
public
|
||||||
void onClick(final SystemTray systemTray, final Menu parent, final Entry entry) {
|
void onClick(final SystemTray systemTray, final Menu parent, final Entry entry) {
|
||||||
systemTray.setStatus(null);
|
systemTray.setStatus(null);
|
||||||
systemTray.setImage(BLACK_MAIL);
|
systemTray.setImage(BLACK_TRAIN);
|
||||||
|
|
||||||
entry.setCallback(null);
|
entry.setCallback(null);
|
||||||
// systemTray.setStatus("Mail Empty");
|
// systemTray.setStatus("Mail Empty");
|
||||||
@ -87,23 +96,34 @@ class TestTray {
|
|||||||
|
|
||||||
this.systemTray.addSeparator();
|
this.systemTray.addSeparator();
|
||||||
|
|
||||||
final Menu submenu = this.systemTray.addMenu("Options", BLACK_MAIL);
|
final Menu submenu = this.systemTray.addMenu("Options", BLUE_CAMPING);
|
||||||
submenu.addEntry("Disable menu", LT_GRAY_MAIL, new SystemTrayMenuAction() {
|
submenu.setShortcut('t');
|
||||||
|
submenu.addEntry("Disable menu", BLACK_BUS, new Action() {
|
||||||
@Override
|
@Override
|
||||||
public
|
public
|
||||||
void onClick(final SystemTray systemTray, final Menu parent, final Entry entry) {
|
void onClick(final SystemTray systemTray, final Menu parent, final Entry entry) {
|
||||||
submenu.setEnabled(false);
|
submenu.setEnabled(false);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
submenu.addEntry("Hide tray", BLACK_MAIL, new SystemTrayMenuAction() {
|
// TODO: buggy. The menu will **sometimes** stop responding to the "enter" key after this. Mnemonics still work however.
|
||||||
|
// submenu.addEntry("Add widget", GREEN_BUS, new Action() {
|
||||||
|
// @Override
|
||||||
|
// public
|
||||||
|
// void onClick(final SystemTray systemTray, final Menu parent, final Entry entry) {
|
||||||
|
// JProgressBar progressBar = new JProgressBar(0, 100);
|
||||||
|
// progressBar.setValue(new Random().nextInt(101));
|
||||||
|
// progressBar.setStringPainted(true);
|
||||||
|
// systemTray.addWidget(progressBar);
|
||||||
|
// }
|
||||||
|
// });
|
||||||
|
submenu.addEntry("Hide tray", LT_GRAY_BUS, new Action() {
|
||||||
@Override
|
@Override
|
||||||
public
|
public
|
||||||
void onClick(final SystemTray systemTray, final Menu parent, final Entry entry) {
|
void onClick(final SystemTray systemTray, final Menu parent, final Entry entry) {
|
||||||
systemTray.setEnabled(false);
|
systemTray.setEnabled(false);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
submenu.addEntry("Remove menu", BLACK_FIRE, new Action() {
|
||||||
submenu.addEntry("Remove menu", GREEN_MAIL, new SystemTrayMenuAction() {
|
|
||||||
@Override
|
@Override
|
||||||
public
|
public
|
||||||
void onClick(final SystemTray systemTray, final Menu parent, final Entry entry) {
|
void onClick(final SystemTray systemTray, final Menu parent, final Entry entry) {
|
||||||
@ -112,7 +132,7 @@ class TestTray {
|
|||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
systemTray.addEntry("Quit", new SystemTrayMenuAction() {
|
systemTray.addEntry("Quit", new Action() {
|
||||||
@Override
|
@Override
|
||||||
public
|
public
|
||||||
void onClick(final SystemTray systemTray, final Menu parent, final Entry entry) {
|
void onClick(final SystemTray systemTray, final Menu parent, final Entry entry) {
|
||||||
|
@ -18,10 +18,10 @@ package dorkbox;
|
|||||||
|
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
|
||||||
|
import dorkbox.systemTray.Action;
|
||||||
import dorkbox.systemTray.Entry;
|
import dorkbox.systemTray.Entry;
|
||||||
import dorkbox.systemTray.Menu;
|
import dorkbox.systemTray.Menu;
|
||||||
import dorkbox.systemTray.SystemTray;
|
import dorkbox.systemTray.SystemTray;
|
||||||
import dorkbox.systemTray.SystemTrayMenuAction;
|
|
||||||
import javafx.application.Application;
|
import javafx.application.Application;
|
||||||
import javafx.application.Platform;
|
import javafx.application.Platform;
|
||||||
import javafx.event.ActionEvent;
|
import javafx.event.ActionEvent;
|
||||||
@ -39,9 +39,18 @@ import javafx.stage.Stage;
|
|||||||
public
|
public
|
||||||
class TestTrayJavaFX extends Application {
|
class TestTrayJavaFX extends Application {
|
||||||
|
|
||||||
public static final URL BLACK_MAIL = TestTray.class.getResource("transport_bus_station.p.000000.32.png");
|
public static final URL BLUE_CAMPING = TestTray.class.getResource("accommodation_camping.glow.0092DA.32.png");
|
||||||
public static final URL GREEN_MAIL = TestTray.class.getResource("transport_bus_station.p.39AC39.32.png");
|
public static final URL BLACK_FIRE = TestTray.class.getResource("amenity_firestation.p.000000.32.png");
|
||||||
public static final URL LT_GRAY_MAIL = TestTray.class.getResource("transport_bus_station.p.999999.32.png");
|
|
||||||
|
public static final URL BLACK_MAIL = TestTray.class.getResource("amenity_post_box.p.000000.32.png");
|
||||||
|
public static final URL GREEN_MAIL = TestTray.class.getResource("amenity_post_box.p.39AC39.32.png");
|
||||||
|
|
||||||
|
public static final URL BLACK_BUS = TestTray.class.getResource("transport_bus_station.p.000000.32.png");
|
||||||
|
public static final URL LT_GRAY_BUS = TestTray.class.getResource("transport_bus_station.p.999999.32.png");
|
||||||
|
|
||||||
|
public static final URL BLACK_TRAIN = TestTray.class.getResource("transport_train_station.p.000000.32.png");
|
||||||
|
public static final URL GREEN_TRAIN = TestTray.class.getResource("transport_train_station.p.39AC39.32.png");
|
||||||
|
public static final URL LT_GRAY_TRAIN = TestTray.class.getResource("transport_train_station.p.666666.32.png");
|
||||||
|
|
||||||
public static
|
public static
|
||||||
void main(String[] args) {
|
void main(String[] args) {
|
||||||
@ -50,8 +59,8 @@ class TestTrayJavaFX extends Application {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private SystemTray systemTray;
|
private SystemTray systemTray;
|
||||||
private SystemTrayMenuAction callbackGreen;
|
private Action callbackGreen;
|
||||||
private SystemTrayMenuAction callbackGray;
|
private Action callbackGray;
|
||||||
|
|
||||||
public
|
public
|
||||||
TestTrayJavaFX() {
|
TestTrayJavaFX() {
|
||||||
@ -83,16 +92,15 @@ class TestTrayJavaFX extends Application {
|
|||||||
throw new RuntimeException("Unable to load SystemTray!");
|
throw new RuntimeException("Unable to load SystemTray!");
|
||||||
}
|
}
|
||||||
|
|
||||||
this.systemTray.setImage(LT_GRAY_MAIL);
|
systemTray.setImage(LT_GRAY_TRAIN);
|
||||||
|
|
||||||
systemTray.setStatus("No Mail");
|
systemTray.setStatus("No Mail");
|
||||||
|
|
||||||
callbackGreen = new SystemTrayMenuAction() {
|
callbackGreen = new Action() {
|
||||||
@Override
|
@Override
|
||||||
public
|
public
|
||||||
void onClick(final SystemTray systemTray, final Menu parent, final Entry entry) {
|
void onClick(final SystemTray systemTray, final Menu parent, final Entry entry) {
|
||||||
systemTray.setImage(GREEN_MAIL);
|
|
||||||
systemTray.setStatus("Some Mail!");
|
systemTray.setStatus("Some Mail!");
|
||||||
|
systemTray.setImage(GREEN_TRAIN);
|
||||||
|
|
||||||
entry.setCallback(callbackGray);
|
entry.setCallback(callbackGray);
|
||||||
entry.setImage(BLACK_MAIL);
|
entry.setImage(BLACK_MAIL);
|
||||||
@ -101,12 +109,12 @@ class TestTrayJavaFX extends Application {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
callbackGray = new SystemTrayMenuAction() {
|
callbackGray = new Action() {
|
||||||
@Override
|
@Override
|
||||||
public
|
public
|
||||||
void onClick(final SystemTray systemTray, final Menu parent, final Entry entry) {
|
void onClick(final SystemTray systemTray, final Menu parent, final Entry entry) {
|
||||||
systemTray.setStatus(null);
|
systemTray.setStatus(null);
|
||||||
systemTray.setImage(BLACK_MAIL);
|
systemTray.setImage(BLACK_TRAIN);
|
||||||
|
|
||||||
entry.setCallback(null);
|
entry.setCallback(null);
|
||||||
// systemTray.setStatus("Mail Empty");
|
// systemTray.setStatus("Mail Empty");
|
||||||
@ -121,22 +129,34 @@ class TestTrayJavaFX extends Application {
|
|||||||
|
|
||||||
this.systemTray.addSeparator();
|
this.systemTray.addSeparator();
|
||||||
|
|
||||||
final Menu submenu = this.systemTray.addMenu("Options", BLACK_MAIL);
|
final Menu submenu = this.systemTray.addMenu("Options", BLUE_CAMPING);
|
||||||
submenu.addEntry("Disable menu", LT_GRAY_MAIL, new SystemTrayMenuAction() {
|
submenu.setShortcut('t');
|
||||||
|
submenu.addEntry("Disable menu", BLACK_BUS, new Action() {
|
||||||
@Override
|
@Override
|
||||||
public
|
public
|
||||||
void onClick(final SystemTray systemTray, final Menu parent, final Entry entry) {
|
void onClick(final SystemTray systemTray, final Menu parent, final Entry entry) {
|
||||||
submenu.setEnabled(false);
|
submenu.setEnabled(false);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
submenu.addEntry("Hide tray", BLACK_MAIL, new SystemTrayMenuAction() {
|
// TODO: buggy. The menu will **sometimes** stop responding to the "enter" key after this. Mnemonics still work however.
|
||||||
|
// submenu.addEntry("Add widget", GREEN_BUS, new Action() {
|
||||||
|
// @Override
|
||||||
|
// public
|
||||||
|
// void onClick(final SystemTray systemTray, final Menu parent, final Entry entry) {
|
||||||
|
// JProgressBar progressBar = new JProgressBar(0, 100);
|
||||||
|
// progressBar.setValue(new Random().nextInt(101));
|
||||||
|
// progressBar.setStringPainted(true);
|
||||||
|
// systemTray.addWidget(progressBar);
|
||||||
|
// }
|
||||||
|
// });
|
||||||
|
submenu.addEntry("Hide tray", LT_GRAY_BUS, new Action() {
|
||||||
@Override
|
@Override
|
||||||
public
|
public
|
||||||
void onClick(final SystemTray systemTray, final Menu parent, final Entry entry) {
|
void onClick(final SystemTray systemTray, final Menu parent, final Entry entry) {
|
||||||
systemTray.setEnabled(false);
|
systemTray.setEnabled(false);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
submenu.addEntry("Remove menu", GREEN_MAIL, new SystemTrayMenuAction() {
|
submenu.addEntry("Remove menu", BLACK_FIRE, new Action() {
|
||||||
@Override
|
@Override
|
||||||
public
|
public
|
||||||
void onClick(final SystemTray systemTray, final Menu parent, final Entry entry) {
|
void onClick(final SystemTray systemTray, final Menu parent, final Entry entry) {
|
||||||
@ -144,7 +164,7 @@ class TestTrayJavaFX extends Application {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
systemTray.addEntry("Quit", new SystemTrayMenuAction() {
|
systemTray.addEntry("Quit", new Action() {
|
||||||
@Override
|
@Override
|
||||||
public
|
public
|
||||||
void onClick(final SystemTray systemTray, final Menu parent, final Entry entry) {
|
void onClick(final SystemTray systemTray, final Menu parent, final Entry entry) {
|
||||||
|
@ -23,10 +23,10 @@ import org.eclipse.swt.widgets.Display;
|
|||||||
import org.eclipse.swt.widgets.Shell;
|
import org.eclipse.swt.widgets.Shell;
|
||||||
import org.eclipse.swt.widgets.Text;
|
import org.eclipse.swt.widgets.Text;
|
||||||
|
|
||||||
|
import dorkbox.systemTray.Action;
|
||||||
import dorkbox.systemTray.Entry;
|
import dorkbox.systemTray.Entry;
|
||||||
import dorkbox.systemTray.Menu;
|
import dorkbox.systemTray.Menu;
|
||||||
import dorkbox.systemTray.SystemTray;
|
import dorkbox.systemTray.SystemTray;
|
||||||
import dorkbox.systemTray.SystemTrayMenuAction;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Icons from 'SJJB Icons', public domain/CC0 icon set
|
* Icons from 'SJJB Icons', public domain/CC0 icon set
|
||||||
@ -36,9 +36,18 @@ import dorkbox.systemTray.SystemTrayMenuAction;
|
|||||||
public
|
public
|
||||||
class TestTraySwt {
|
class TestTraySwt {
|
||||||
|
|
||||||
public static final URL BLACK_MAIL = TestTray.class.getResource("transport_bus_station.p.000000.32.png");
|
public static final URL BLUE_CAMPING = TestTray.class.getResource("accommodation_camping.glow.0092DA.32.png");
|
||||||
public static final URL GREEN_MAIL = TestTray.class.getResource("transport_bus_station.p.39AC39.32.png");
|
public static final URL BLACK_FIRE = TestTray.class.getResource("amenity_firestation.p.000000.32.png");
|
||||||
public static final URL LT_GRAY_MAIL = TestTray.class.getResource("transport_bus_station.p.999999.32.png");
|
|
||||||
|
public static final URL BLACK_MAIL = TestTray.class.getResource("amenity_post_box.p.000000.32.png");
|
||||||
|
public static final URL GREEN_MAIL = TestTray.class.getResource("amenity_post_box.p.39AC39.32.png");
|
||||||
|
|
||||||
|
public static final URL BLACK_BUS = TestTray.class.getResource("transport_bus_station.p.000000.32.png");
|
||||||
|
public static final URL LT_GRAY_BUS = TestTray.class.getResource("transport_bus_station.p.999999.32.png");
|
||||||
|
|
||||||
|
public static final URL BLACK_TRAIN = TestTray.class.getResource("transport_train_station.p.000000.32.png");
|
||||||
|
public static final URL GREEN_TRAIN = TestTray.class.getResource("transport_train_station.p.39AC39.32.png");
|
||||||
|
public static final URL LT_GRAY_TRAIN = TestTray.class.getResource("transport_train_station.p.666666.32.png");
|
||||||
|
|
||||||
public static
|
public static
|
||||||
void main(String[] args) {
|
void main(String[] args) {
|
||||||
@ -49,8 +58,8 @@ class TestTraySwt {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private SystemTray systemTray;
|
private SystemTray systemTray;
|
||||||
private SystemTrayMenuAction callbackGreen;
|
private Action callbackGreen;
|
||||||
private SystemTrayMenuAction callbackGray;
|
private Action callbackGray;
|
||||||
|
|
||||||
public
|
public
|
||||||
TestTraySwt() {
|
TestTraySwt() {
|
||||||
@ -67,16 +76,15 @@ class TestTraySwt {
|
|||||||
throw new RuntimeException("Unable to load SystemTray!");
|
throw new RuntimeException("Unable to load SystemTray!");
|
||||||
}
|
}
|
||||||
|
|
||||||
this.systemTray.setImage(LT_GRAY_MAIL);
|
systemTray.setImage(LT_GRAY_TRAIN);
|
||||||
|
|
||||||
systemTray.setStatus("No Mail");
|
systemTray.setStatus("No Mail");
|
||||||
|
|
||||||
callbackGreen = new SystemTrayMenuAction() {
|
callbackGreen = new Action() {
|
||||||
@Override
|
@Override
|
||||||
public
|
public
|
||||||
void onClick(final SystemTray systemTray, final Menu parent, final Entry entry) {
|
void onClick(final SystemTray systemTray, final Menu parent, final Entry entry) {
|
||||||
systemTray.setStatus("Some Mail!");
|
systemTray.setStatus("Some Mail!");
|
||||||
systemTray.setImage(GREEN_MAIL);
|
systemTray.setImage(GREEN_TRAIN);
|
||||||
|
|
||||||
entry.setCallback(callbackGray);
|
entry.setCallback(callbackGray);
|
||||||
entry.setImage(BLACK_MAIL);
|
entry.setImage(BLACK_MAIL);
|
||||||
@ -85,12 +93,12 @@ class TestTraySwt {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
callbackGray = new SystemTrayMenuAction() {
|
callbackGray = new Action() {
|
||||||
@Override
|
@Override
|
||||||
public
|
public
|
||||||
void onClick(final SystemTray systemTray, final Menu parent, final Entry entry) {
|
void onClick(final SystemTray systemTray, final Menu parent, final Entry entry) {
|
||||||
systemTray.setStatus(null);
|
systemTray.setStatus(null);
|
||||||
systemTray.setImage(BLACK_MAIL);
|
systemTray.setImage(BLACK_TRAIN);
|
||||||
|
|
||||||
entry.setCallback(null);
|
entry.setCallback(null);
|
||||||
// systemTray.setStatus("Mail Empty");
|
// systemTray.setStatus("Mail Empty");
|
||||||
@ -105,22 +113,34 @@ class TestTraySwt {
|
|||||||
|
|
||||||
this.systemTray.addSeparator();
|
this.systemTray.addSeparator();
|
||||||
|
|
||||||
final Menu submenu = this.systemTray.addMenu("Options", BLACK_MAIL);
|
final Menu submenu = this.systemTray.addMenu("Options", BLUE_CAMPING);
|
||||||
submenu.addEntry("Disable menu", LT_GRAY_MAIL, new SystemTrayMenuAction() {
|
submenu.setShortcut('t');
|
||||||
|
submenu.addEntry("Disable menu", BLACK_BUS, new Action() {
|
||||||
@Override
|
@Override
|
||||||
public
|
public
|
||||||
void onClick(final SystemTray systemTray, final Menu parent, final Entry entry) {
|
void onClick(final SystemTray systemTray, final Menu parent, final Entry entry) {
|
||||||
submenu.setEnabled(false);
|
submenu.setEnabled(false);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
submenu.addEntry("Hide tray", BLACK_MAIL, new SystemTrayMenuAction() {
|
// TODO: buggy. The menu will **sometimes** stop responding to the "enter" key after this. Mnemonics still work however.
|
||||||
|
// submenu.addEntry("Add widget", GREEN_BUS, new Action() {
|
||||||
|
// @Override
|
||||||
|
// public
|
||||||
|
// void onClick(final SystemTray systemTray, final Menu parent, final Entry entry) {
|
||||||
|
// JProgressBar progressBar = new JProgressBar(0, 100);
|
||||||
|
// progressBar.setValue(new Random().nextInt(101));
|
||||||
|
// progressBar.setStringPainted(true);
|
||||||
|
// systemTray.addWidget(progressBar);
|
||||||
|
// }
|
||||||
|
// });
|
||||||
|
submenu.addEntry("Hide tray", LT_GRAY_BUS, new Action() {
|
||||||
@Override
|
@Override
|
||||||
public
|
public
|
||||||
void onClick(final SystemTray systemTray, final Menu parent, final Entry entry) {
|
void onClick(final SystemTray systemTray, final Menu parent, final Entry entry) {
|
||||||
systemTray.setEnabled(false);
|
systemTray.setEnabled(false);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
submenu.addEntry("Remove menu", GREEN_MAIL, new SystemTrayMenuAction() {
|
submenu.addEntry("Remove menu", BLACK_FIRE, new Action() {
|
||||||
@Override
|
@Override
|
||||||
public
|
public
|
||||||
void onClick(final SystemTray systemTray, final Menu parent, final Entry entry) {
|
void onClick(final SystemTray systemTray, final Menu parent, final Entry entry) {
|
||||||
@ -128,7 +148,7 @@ class TestTraySwt {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
systemTray.addEntry("Quit", new SystemTrayMenuAction() {
|
systemTray.addEntry("Quit", new Action() {
|
||||||
@Override
|
@Override
|
||||||
public
|
public
|
||||||
void onClick(final SystemTray systemTray, final Menu parent, final Entry entry) {
|
void onClick(final SystemTray systemTray, final Menu parent, final Entry entry) {
|
||||||
|
BIN
test/dorkbox/accommodation_camping.glow.0092DA.32.png
Normal file
BIN
test/dorkbox/accommodation_camping.glow.0092DA.32.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.7 KiB |
BIN
test/dorkbox/amenity_firestation.p.000000.32.png
Normal file
BIN
test/dorkbox/amenity_firestation.p.000000.32.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 636 B |
BIN
test/dorkbox/amenity_post_box.p.000000.32.png
Normal file
BIN
test/dorkbox/amenity_post_box.p.000000.32.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 474 B |
BIN
test/dorkbox/amenity_post_box.p.39AC39.32.png
Normal file
BIN
test/dorkbox/amenity_post_box.p.39AC39.32.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 669 B |
BIN
test/dorkbox/transport_train_station.p.000000.32.png
Normal file
BIN
test/dorkbox/transport_train_station.p.000000.32.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 623 B |
BIN
test/dorkbox/transport_train_station.p.666666.32.png
Normal file
BIN
test/dorkbox/transport_train_station.p.666666.32.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 937 B |
Loading…
Reference in New Issue
Block a user