X

Geertjan's Blog

  • March 1, 2006

Communicating with the User from a NetBeans Plug-in Module

Geertjan Wielenga
Product Manager
I didn't realize, until yesterday, that the NetBeans APIs provide a wide range of descriptions for simple communication with the user. For example, in the code below, a small dialog box with an input line (thanks to the NotifyDescriptor.InputLine class) appears and, depending on the user's response, your code can branch off in an appropriate direction. In this case, the getInputText() method is used to test whether the correct answer is given.

So, when the action is invoked (on a node), this dialog box is displayed:

And the lines in bold below were all that was needed to create the above dialog box:

protected void performAction(Node[] activatedNodes) {NotifyDescriptor.InputLine msg = new NotifyDescriptor.InputLine("Answer:",
"Is this a java, properties, or xml file?"
,NotifyDescriptor.OK_CANCEL_OPTION,NotifyDescriptor.QUESTION_MESSAGE);
DialogDisplayer.getDefault().notify(msg);

DataObject dataObject = (DataObject) activatedNodes[0].getLookup().lookup(DataObject.class);
if (dataObject instanceof JavaDataObject && msg.getInputText().equals("java")) {
NotifyDescriptor.Message msg1 = new NotifyDescriptor.Message("Well done! That's a Java source file");
DialogDisplayer.getDefault().notify(msg1);
} else if (dataObject instanceof PropertiesDataObject && msg.getInputText().equals("properties")) {
NotifyDescriptor.Message msg1 = new NotifyDescriptor.Message("Well done! That's a properties file.");
DialogDisplayer.getDefault().notify(msg1);
} else if (dataObject instanceof XMLDataObject && msg.getInputText().equals("xml")) {
NotifyDescriptor.Message msg1 = new NotifyDescriptor.Message("Well done! That's an XML file.");
DialogDisplayer.getDefault().notify(msg1);
} else {
NotifyDescriptor.Message d = new NotifyDescriptor.Message("Wrong!");
DialogDisplayer.getDefault().notify(d);
}
}

Then the test is done, by checking to see whether the DataObject matches the user's input. And then, if correct, the NotifyDescriptor.Message class is used to display whether the answer is correct or not.

In addition to these two classes, you can also use NotifyDescriptor.Confirmation (as I did in yesterday's blog entry, to check whether the user wants to continue their search on-line) and the NotifyDescriptor.Exception class.

All of these classes, together with related classes that also handle user notifications, can be found here. Another interesting link: APIs related to user interface.

By the way, does anyone know where I can find the JSPDataObject?

Join the discussion

Comments ( 2 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.