Time for action – modal and other effects

There are a number of style bits that are applicable to windows, and some useful methods to affect how the window appears. For example, it might be desirable to make the clock appear semi-transparently, which allows the clock to float above other windows. SWT's Shell has a number of these options that can be set.

  1. Modify the instantiation of the Shell inside the widgetSelected() method in the Activator class's inner class to add SWT.NO_TRIM (no close/minimize/maximize widgets) and SWT.ON_TOP (floating on top of other windows):
    shell = new Shell(trayItem.getDisplay(),SWT.NO_TRIM|SWT.ON_TOP);
  2. Set the alpha value as 128, which is semi-transparent:
    shell.setAlpha(128);
  3. Run the Eclipse instance, and click on the tray item to see what kind of window is created.
  4. To create a modal window (and thus, prevent interaction on the main window), change the flag to use SWT.APPLICATION_MODAL:
    shell = new Shell(trayItem.getDisplay(),SWT.APPLICATION_MODAL);
  5. To make the application full-screen, call either setFullScreen() or setMaximized() depending on the platform:
    shell.setFullScreen(true);
    shell.setMaximized(true);
  6. Note that without trims, it may be necessary to add controls such as detecting selection events to close the window.
  7. Run the Eclipse application and see the effect these flags have on the window.
  8. Change the Shell back to use SWT.NO_TRIM and SWT.ON_TOP.
  9. To calculate a circular shape for the floating clock window, add the circle() method to the Activator class, which has been taken from Snippet134.java (taken from the SWT snippets page at http://www.eclipse.org/swt/snippets/):
    private static int[] circle(int r, int offsetX, int offsetY) {
      int[] polygon = new int[8 * r + 4];
      //x^2 + y^2 = r^2
      for (int i = 0; i < 2 * r + 1; i++) {
        int x = i – r;
        int y = (int)Math.sqrt(r*r – x*x);
        polygon[2*i] = offsetX + x;
        polygon[2*i+1] = offsetY + y;
        polygon[8*r - 2*i - 2] = offsetX + x;
        polygon[8*r - 2*i - 1] = offsetY – y;
      }
      return polygon;
    }
  10. Finally, change the shape of the window to be circular by setting a Region on the Shell. This will have the effect of making it look like the clock itself is floating. Add the following code after the Shell is created in the widgetSelected() method:
    final Region region = new Region();
    region.add(circle(25, 25, 25));
    shell.setRegion(region);
  11. When run, the clock will look something like this:
    Time for action – modal and other effects
  12. For completeness, register a dispose listener on the shell to ensure that Region is cleaned up:
    shell.addDisposeListener(new DisposeListener() {
      public void widgetDisposed(DisposeEvent e) {
        if (region != null && !region.isDisposed())
          region.dispose();
      }
    });

What just happened?

Varying the flags used to create the shell affects how that window is displayed and interacts with the user. Other calls on the shell can programmatically drive transitions to full-screen and maximized or minimized status, which can be useful in specific circumstances. Some windowing systems differentiate maximized and full-screen; others have distinct characteristics.

The SWT.NO_TRIM flag is used to display a window without the normal window furniture. This can be combined with setting a region via setRegion(), which allows creation of a non-rectangular shape.

Often, windows without trim are floating, that is, they should stay on top of the application window even when it hasn't got the focus. To achieve this, set the SWT.ON_TOP flag as well, and adjust the alpha (transparency) value with setAlpha(). The alpha value is between 0 (fully transparent) and 255 (fully opaque).

A Region can be defined from a set of connected points, or set on a pixel-by-pixel basis. It's important to note that a Region is also a Resource, and thus must be disposed of after use (which is typically when the Shell is closed). The clean-up operation is similar to that of the others discussed earlier, via the addDisposeListener() on the Shell.

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

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