Most operating systems have a concept of tray, as a set of icons visible from the main window that can provide quick access components. On macOS, these are represented as icons across the top menu bar, and on Windows, as icons on the bottom-right near the clock. Linux systems have various approaches that do similar things, and some operating systems have none. Since there is only one tray, it is necessary to add the item only once. The Activator
class can be used to ensure that the TrayItem
is created at startup and removed at shutdown.
Activator
class, and add two private fields:private TrayItem trayItem; private Image image;
start
method:final Display display = Display.getDefault(); display.asyncExec(() -> { image = new Image(display, Activator.class.getResourceAsStream("/icons/sample.gif")); Tray tray = display.getSystemTray(); if (tray != null && image != null) { trayItem = new TrayItem(tray, SWT.NONE); trayItem.setToolTipText("Hello World"); trayItem.setVisible(true); trayItem.setText("Hello World"); trayItem.setImage(image); } });
sample.gif
icon should appear in the task area (top right in macOS, bottom right in Windows).WARNING: This console is connected to the current running instance of Eclipse! osgi> "Framework is launched."
ss clock
at the osgi> prompt and it will show a bundle ID, which can be used to start/stop:osgi> ss clock id State Bundle 4 RESOLVED com.packtpub.e4.clock.ui_1.0.0.qualifier
start
and stop
into the console with the ID
given the preceding output:osgi> stop 4 osgi> start 4 osgi> stop 4 osgi> start 4
TrayItem
appears in the Tray
each time it is started. The clean routine needs to be added in the Activator
method stop
:public void stop(BundleContext context) throws Exception { if (trayItem != null) { Display.getDefault().asyncExec(trayItem::dispose); } if (image != null) { Display.getDefault().asyncExec(image::dispose); }
An SWT TrayItem
is added to the system's Tray
when the bundle is started and removed when the bundle is stopped. The icon that came with the sample project was used. To use a different one, don't forget to update the build.properties
file.
Since the tray is a graphical component, if there's no image, then the item isn't shown. The tooltips are optional. Note also that not every system has the concept of a tray, so null
is a legitimate return value for display.getSystemTray()
.
A bundle is started automatically when it is loaded, and the loading is triggered by showing a view or selecting a menu item. If a view is opened, the bundle that class is loaded from is automatically started. Bundles can also be started and stopped programmatically, or through the Host OSGi Console, which is useful to test whether the Activator
class's start
and stop
methods are working correctly.
3.135.194.251