【问题标题】:Notification widget within an RCP applicationRCP 应用程序中的通知小部件
【发布时间】:2019-06-05 14:14:34
【问题描述】:

我正在寻找一个小部件或组件 (SWT),它允许我在后台作业完成时通知用户。我知道像MyLyn 这样的东西,它们提供了创建系统通知的方法。但是,我更喜欢在我的窗口中显示通知的小部件。有没有我找不到的现有小部件?

谢谢。

[编辑] 我已经看到一个组件或多或少地做了我想要的。它用于 Eclipse 错误报告,如下所述:https://eclipsesource.com/blogs/2015/06/23/error-reporting-top-eclipse-mars-feature-2/ 但是我似乎找不到这里使用的底层小部件。

【问题讨论】:

    标签: java swt rcp


    【解决方案1】:

    您可以使用 ToolTip 作为通知,它会出现在任务栏托盘项中。我在下面提供了代码 sn -p 你可以试试。在windows中,它会像右下角的黑色小弹出窗口一样弹出。我提供了按钮只是为了模拟。后台长时间运行的任务结束后,您可以按照自己的方式实现。

    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();
      }
    }
    

    【讨论】:

    • 感谢您的努力,但有足够的解决方案来处理系统托盘通知,例如 MyLyn 或 Nebula Notifier。
    【解决方案2】:

    我建议你可以探索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();
        }
    }
    

    【讨论】:

    • 谢谢。尽管这是一种可能性,但为了适应 Nebula 小部件(或 MyLyn 小部件),我宁愿不必这样做,因为这会增加另一个要维护的组件。但我可能不得不求助于这个。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-10
    相关资源
    最近更新 更多