RCP应用程序中的通知小部件

时间:2019-06-05 14:14:34

标签: java swt rcp

我正在寻找一个小部件或组件(SWT),每当后台作业完成时,我都可以通知用户。我知道MyLyn之类的东西提供了创建系统通知的方法。但是,我希望使用在窗口中显示通知的小部件。有没有我找不到的现有小部件?

谢谢。

[编辑] 我已经看到一个组件或多或少地实现了我想要的功能。如以下所述,它用于eclipse错误报告中:https://eclipsesource.com/blogs/2015/06/23/error-reporting-top-eclipse-mars-feature-2/但是,我似乎找不到在此使用的基础小部件。

2 个答案:

答案 0 :(得分:0)

您可以使用工具提示作为通知,该通知将出现在任务栏托盘项目中。我在下面提供了您可以尝试使用的代码段。在窗口中,它的弹出窗口类似于右下角的黑色小弹出窗口。我提供了按钮只是为了模拟。后台长时间运行的任务结束后,您可以用自己的方式实现。

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolTip;
import org.eclipse.swt.widgets.Tray;
import org.eclipse.swt.widgets.TrayItem;

public class ToolTipBalloon {

  public static void showNotificationPopup(Shell shell) {
    ToolTip tip = new ToolTip(shell, SWT.BALLOON | SWT.ICON_INFORMATION);
    tip.setMessage("Your Notification Message");
    Display display = shell.getDisplay();
    Tray tray = display.getSystemTray();
    if (tray != null) {
      TrayItem item = new TrayItem(tray, SWT.NONE);
      //      Image image = new Image(display, "yourFile.gif");
      //      item.setImage(image);
      tip.setText("Notification from a Windows Tray");
      item.setToolTip(tip);
    } else {
      tip.setText("Notification from anywhere");
      tip.setLocation(400, 400);
    }
    tip.setVisible(true);
  }

  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);

    // To simulate notification
    Button notifyBtn = new Button(shell, SWT.PUSH);
    notifyBtn.setText("Press for Notification");
    notifyBtn.addListener(
        SWT.Selection,
        new Listener() {
          public void handleEvent(Event event) {
            showNotificationPopup(shell);
          }
        });
    notifyBtn.pack();
    shell.setBounds(50, 50, 200, 100);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) display.sleep();
    }
    display.dispose();
  }
}

答案 1 :(得分:0)

我建议您可以探索Nebula Notifier。您可能必须对其进行自定义,以便在应用程序内部进行通知

请看下面的代码snippet

public class NotifierSnippet {
    /**
     * @param args
     */
    public static void main(final String[] args) {
        final Display display = new Display();
        final Shell shell = new Shell(display);
        shell.setText("Notifier Snippet");
        shell.setSize(200, 200);
        shell.setLayout(new FillLayout(SWT.VERTICAL));

        final int[] counter = new int[1];
        counter[0] = 0;

        // Yellow theme (default)
        final Button testerYellow = new Button(shell, SWT.PUSH);
        testerYellow.setText("Push me [Yellow theme]!");
        testerYellow.addListener(SWT.Selection, event -> {
            Notifier.notify("New Mail message", "Laurent CARON (lcaron@...)<br/><br/>Test message #" + counter[0] + "...");
            counter[0]++;
        });

        // Blue theme
        final Button testerBlue = new Button(shell, SWT.PUSH);
        testerBlue.setText("Push me [Blue theme]!");
        testerBlue.addListener(SWT.Selection, event -> {
            Notifier.notify("New Mail message", "Laurent CARON (lcaron@...)<br/><br/>Test message #" + counter[0] + "...", NotifierTheme.BLUE_THEME);
            counter[0]++;
        });

        // Grey theme
        final Button testerGrey = new Button(shell, SWT.PUSH);
        testerGrey.setText("Push me [Gray theme]!");
        testerGrey.addListener(SWT.Selection, event -> {
            Notifier.notify("New Mail message", "Laurent CARON (lcaron@...)<br/><br/>Test message #" + counter[0] + "...", NotifierTheme.GRAY_THEME);
            counter[0]++;
        });

        shell.open();

        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }
}