10

Figured out how to show one of those little notification bubble messages in the top right of the screen, answer below.

centic
  • 15,565
  • 9
  • 68
  • 125
Verdagon
  • 2,456
  • 3
  • 22
  • 36

2 Answers2

18

Turns out, you have to make a NotificationGroup instance, and then use that to make a Notification, and pass the notification and the Project to Notifications.Bus.notify().

public class VoiceApplicationComponentImpl implements ApplicationComponent, VoiceApplicationComponent {
    ...
    public static final NotificationGroup GROUP_DISPLAY_ID_INFO =
        new NotificationGroup("My notification group",
            NotificationDisplayType.BALLOON, true);
    ...
    void showMyMessage(String message) {
        ApplicationManager.getApplication().invokeLater(new Runnable() {
            @Override
            public void run() {
                Notification notification = GROUP_DISPLAY_ID_INFO.createNotification(message, NotificationType.ERROR);
                Project[] projects = ProjectManager.getInstance().getOpenProjects();
                Notifications.Bus.notify(notification, projects[0]);
            }
        });
    }

Note: you'll probably have a better way to get the current Project, right now I just assume there's one open project. This means my method doesn't work on startup (projects array is empty).

Another note: you'll probably not need to wrap with the invokeLater but I did, because I was calling showMyMessage in a different thread.

Verdagon
  • 2,456
  • 3
  • 22
  • 36
  • Does someone know why `ApplicationManager.getApplication().invokeLater` is necessary?The [doc](http://www.jetbrains.org/intellij/sdk/docs/user_interface_components/notifications.html) doesn't mention it – Robin Thoni Nov 18 '16 at 20:55
  • 1
    @robin.thoni invokeLater() is a swing method, it allows to stack up ui tasks under the event dispatch thread which is the only thread allowed to execute ui tasks... this is more related to swing than intellij api though... shouldn't be in the docs. – ALTN Mar 06 '17 at 15:02
  • 2
    @Verdagon you should use Project project = e.getData(LangDataKeys.PROJECT) to get your current project reference, otherwise the notification risks to show up under the wrong project when multiple projects are open. e is the AnActionEvent you get from within your action class. Also make sure you call e.getData(LangDataKeys.PROJECT) out of the invokeLater runnable, otherwise an exception will be thrown at runtime. For some reason intellij does not allow the call to getData under the event dispatch thread. – ALTN Mar 06 '17 at 15:08
0

this will be better!

StatusBar statusBar = WindowManager.getInstance()
    .getStatusBar(DataKeys.PROJECT.getData(actionEvent.getDataContext()));

JBPopupFactory.getInstance()
    .createHtmlTextBalloonBuilder(htmlText, messageType, null)
    .setFadeoutTime(7500)
    .createBalloon()
    .show(RelativePoint.getCenterOf(statusBar.getComponent()),
                                             Balloon.Position.atRight);

reference link:
1. original link
2. enter link description here

scruel
  • 426
  • 6
  • 14