What to use instead of `notification.setListener()`?

Answered

In my current code I have the following:

Notification notification = new Notification("CustomGroupId",
                            "No Test Resources folder found!",
                            "Please mark at least one directory as <strong>Test Resources</strong> folder.\n <a href=\"projectSettings\">Click here</a> to open project settings.",
                            NotificationType.WARNING);
                            
notification.setListener(new NotificationListener() {
    @Override
    public void hyperlinkUpdate(@NotNull Notification notification, @NotNull HyperlinkEvent event) {
        if (event.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
            if ("projectSettings".equals(event.getDescription())) {
                ProjectSettingsService.getInstance(project).openModuleSettings(module);
            }
        }
    }
});

This opens project settings when the user clicks on the hyperlink in the notification text. 

But apparently the setListener is deprecated, so what do I use instead? In the source code it says use addAction(AnAction) but is there any example of an action that is activated by clicking on the hyperlink? 

1
3 comments

Hi Eugene,

There are hundreds of examples in the intellij-community repository:
https://github.com/JetBrains/intellij-community

I suggest checking out the sources and finding usages of Notification.addAction().

Please also see other techniques for exploring the API:
https://plugins.jetbrains.com/docs/intellij/explore-api.html

-2

Thanks, Karol, after some searching I was able to find what I was looking for. 

0

addAction() only let's listen to the whole notification.

Many people need specific parts to be clickable, also with different links, e.g.:

“For more information click here or contact us.”

1

Please sign in to leave a comment.