How to make a URL in a balloon notification clickable?

1k views Asked by At

I'm creating a simple IntelliJ Plugin that allows for creating new Pastebin pastes straight from the IDE.

When a paste has been successfully posted to Pastebin, I'd like to display a balloon notification.

Currently the notification is displayed as follows:

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

Now, this balloon notification contains the URL to the newly created paste, but unfortunately clicking it does not open the link in a browser. How can that be achieved?

The balloon notification with the URL that should become clickable: Balloon notification

2

There are 2 answers

1
Argb32 On BEST ANSWER

There is NotificationListener which opens urls in notifications: com.intellij.notification.UrlOpeningListener

So you can write:

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

After some searching I found the answer myself. It wasn't too difficult actually.

If I instantiate the notification as follows, the link is clickable as was required.

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

Note: It is also rather useful that the link in the event log is also clickable now.