Most operating systems have the concept of a tray as a set of icons visible from the main window, which can provide quick access components. On OS X, these are represented as icons across the top menu bar; on Windows, as icons on the bottom-right near the clock. Linux systems have various approaches which do similar, 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 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(new Runnable() { public void run() { 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(new Image(trayItem.getDisplay(), Activator.class.getResourceAsStream("/icons/sample.gif"))); } } });
sample.gif
icon should appear in the task area (top right of OS X, bottom-right of 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
class's stop()
method:public void stop(BundleContext context) throws Exception { if (trayItem != null && !trayItem.isDisposed()) { Display.getDefault().asyncExec(new Runnable() { public void run() { if (trayItem != null && !trayItem.isDisposed()) trayItem.dispose(); } }); } if (image != null && !image.isDisposed()) { Display.getDefault().asyncExec(new Runnable() { public void run() { if (image != null && !image.isDisposed()) image.dispose(); } }); }
An SWT TrayItem
is added to the system's Tray
when the bundle started, and removed it when the bundle 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 a menu item. If a View is opened, the bundle that class is loaded from is automatically started. They 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.