Clickable Links in Balloon Notifications – IntelliJ Plugin

The other day I took another look at my IntelliJ IDEA Plugin project, ‘Pastebin Unofficial‘ and it’s notifications. At the time of writing this project is in a very early, basic stage, it is rather an idea that hasn’t been worked properly (yet).

Clickable links in notifications

So what the project does at this time, is create a Pastebin paste from the opened document in the IDE. You right click anywhere on the opened file and you select ‘New Paste’. A popup will appear for you to specify a paste title (the filename will be picked by default). When dismissing the dialog, a paste will be sent to Pastebin and a balloon notification will be displayed for a while in the bottom right corner of the IDE.

A balloon notification with link in IntelliJ IDEA.
A balloon notification with link in IntelliJ IDEA.

The goal of the notification was to notify the user that the paste was successfully posted (a different notification would be shown when an error occurred). Only that would not be enough, no, a clickable link to this new paste would be appropriate as well.

Unfortunately, the original attempt didn’t satisfy the requirements:

final Response<String> postResult = Constants.PASTEBIN.post(paste);
NotificationGroup balloonNotifications = new NotificationGroup("Notification group", NotificationDisplayType.BALLOON, true);
    if (postResult.hasError()) {
    //Display error notification
} else {
    //Display success notification
    Notification success = balloonNotifications.createNotification("Successful Paste", "<a href=\"" + postResult.get() + "\">Paste</a> successfully posted!", NotificationType.INFORMATION, null);
    Notifications.Bus.notify(success, project);
}

In fact, it was rather naive to have used a simple html <a href="..."> tag. Even more impulsive was leaving out the required surrounding <html></html> tags.

As if those absolute failures weren’t enough, I also managed to completely ignore the latest parameter of Type NotificationListener. Passing null isn’t really going to do anything there, is it?

After a couple of Google searches I managed to find the solution. When instantiating the notification, I now include the proper surrounding tags and I provide a bit of code handling a click on the hyperlink.

This did the trick in the end:

Notification success = balloonNotifications.createNotification("<html>Successful Paste", "<a href=\"" + postResult.get() + "\" target=\"blank\">Paste</a> successfully posted!</html>", NotificationType.INFORMATION, (notification, hyperlinkEvent) -> {
    if (hyperlinkEvent.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
        BrowserUtil.browse(hyperlinkEvent.getURL());
    }
});

Now links are opening in the default browser. Note that links in the event log are now clickable as well.

However this being a valid solution, another, much cleaner one, is available as well. The final and thus preferable solution looks like this:

Notification success = balloonNotifications.createNotification(
                "<html>Successful Paste", "<a href=\"" + postResult.get() + "\" target=\"blank\">Paste</a> successfully posted!</html>",
                NotificationType.INFORMATION, new NotificationListener.UrlOpeningListener(true));

Find the original question on StackOverflow.

Author: Thibault Helsmoortel

Belgian IT enthusiast with a wide range of interests. Enjoys table tennis and meeting friends at the local bar.

Leave a Reply