Time for action – adding items to the tray

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.

  1. Open the Activator class and add two private fields:
    private TrayItem trayItem;
    private Image image;
  2. Add the following to the 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")));
        }
      }
    });
  3. Run the test Eclipse instance, and show the Clock View. The small sample.gif icon should appear in the task area (top right of OS X, bottom-right of Windows).
  4. To test the effect of stopping and restarting the bundle, open the Console View in the test Eclipse instance. Click on the drop-down list on the top-right of the View to create a Host OSGi Console.
    WARNING: This console is connected to the current running instance of Eclipse!osgi> 
    "Framework is launched."
  5. Type 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
  6. Start and stop the bundle by typing 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
  7. Notice that a new 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();
          }
      });
    }
  8. Re-run the application and start and stop the bundle (probably the same bundle ID as before, but you should check rather than assume); the SWT tray icon should go and come back each time the bundle is stopped and started.

What just happened?

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset