Our Blog

Let us find tech solutions together

Apr 22

IDEA Development: Adding notifications

By kinabalu | Comments

 

Our initial plugin for IDEA that talked to MysticPaste.com was fairly simple, offered no feedback to the user that anything happened. I spent some time trying to find out a method of achieving unobtrusive notification that the paste was successful. On initial glance at the API, nothing jumped out except for Messages, and StatusBar.

First crack was using Messages:

1
    Messages.showInfoMessage("Paste successful!","URL automatically copied to your clipboard");

It meets the standard of showing feedback to the end-user, but its definitely obtrusive. The user is forced to click on the OK button, its less of a status and more of a confirmation.

Second crack was using StatusBar. Only thing found in Google is for version 5.0 of the plugin API, and it only has one method, setInfo(String s). Doesn’t look very promising. Introspection though, proves that nothing is as it seems. Through some sleuth googling, downloading some plugins that showed the notifications we like, and googling more, a solution was found.

Adding a Project Component

There’s only one way to get at a StatusBar instance, and that’s through the WindowManager. And there isn’t a global StatusBar, there’s one per Project. So where we got by without a ProjectComponent previously, we need one now. The following snippet gives us a StatusBar object:

1
    StatusBar statusBar = WindowManager.getInstance().getStatusBar(project);

And from that, we can call fireNotificationPopup with the proper parameters, to show our message. If you happen to find the JavaDoc, congratulations, initial searches came up empty on this method, but it works great. Here’s the entire method for the notification, which we add to the event thread so Swing can fire and show it whenever it makes sense:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
    private Project project;

    public MysticPasteIndicationComponent(Project inProject) {
        project = inProject;
    }

    ...

    protected void updateWithStatus(final String statusMessage) {

        /**
         * Don't make Swing angry.  You won't like it when its angry
         *
         */
        ApplicationManager.getApplication().invokeLater(
                new Runnable() {
                    public void run() {
                        JTextArea area = new JTextArea();
                        area.setOpaque(false);
                        area.setEditable(false);
                        StringBuffer notification = new StringBuffer(statusMessage);
                        area.setText(notification.toString());
                        StatusBar statusBar = WindowManager.getInstance().getStatusBar(project);
                        if (statusBar != null)

                        {
                            statusBar.fireNotificationPopup(area, LightColors.GREEN);
                        }
                    }

                }
        );

    }

And now all we need is to call this after the paste was successful. So similar to how we grabbed the Editor, we’ll again pull out our DataContext object bag of tricks, and find our ProjectComponent instance, and fire off our status update as you can see here:

1
2
3
    Project project = DataKeys.PROJECT.getData(context);
    MysticPasteIndicationComponent mpiComponent = project.getComponent(MysticPasteIndicationComponent.class);
    mpiComponent.updateWithStatus("Paste successful!nnURL automatically copied to your clipboard");

That’s it. And we promise, no animals were hurt during this exercise. And DO try this at home.