Search code examples
javaintellij-ideaintellij-plugin

How to make a URL in a balloon notification clickable?


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


Solution

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