Skip Navigation

Creating a Notification with a Progress Bar

In a Java editor, add the following code to create a Composite that will hold all widgets that the notification needs.

        Composite requires a parent, therefore, the shell in the active Workbench window is used.

        Composite changes parent and will not be visible on the shell.

        The background is set to white because notifications must have a white background.

        The mode for child widgets is set to inherit that background.

Shell shell = Workbench.getInstance().getActiveWorkbenchWindow().getShell();  Composite content = new Composite(shell, SWT.NONE);
content.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_WHITE));
content.setBackgroundMode(SWT.INHERIT_FORCE);
content.setLayout(new FillLayout(SWT.VERTICAL));
  1. Add the following code to place the widgets on Composite.

        This sample code places the following two widgets:

  •         A Label object that prints static text

  •         An indeterminate progress bar

Label label = new Label(content, SWT.TRANSPARENT);
  label.setText("Custom Composite");
new ProgressBar(content, SWT.INDETERMINATE  SWT.SMOOTH);
  1. Create the Notification entity object that will contain all information for the notification.

        The entity object for a custom notification has the additional setCompositeContent field. This field sets the composite to be the main content of the notification.

        The following figure shows a notification with an indeterminate a progress bar.

image036
      final Notification notification = new Notification();
      notification.setWindowTitle("Title");
      notification.setPriority(Priority.LOW);
      notification.setCompositeContent(content);
      notification.setLeftSideBarColor(SideBarColor.RED);
      notification.setNotificationTime(30000);
  1. Add the following code to display the notification in a UI thread. If the method that calls createNotification() is already in the SWT UI thread, the special UiThreadInvoker is not needed.

      final INotificationService service = Activator.getDefault().getNotificationService();

      UiThreadInvoker <Object> invoker = new UiThreadInvoker <Object>() {
          protected Object invoke() {
              service.createNotification(notification);
              return null;
          }
      };

      invoker.asyncExec();