X

Geertjan's Blog

  • February 27, 2006

Send a Word to the Browser (Part 3)

Geertjan Wielenga
Product Manager
I've worked out something really cool. Say you're new to the Java programming language. You see heaps of code in the editor, with identifiers such as "public" and "private" and so on. You don't know what these terms mean. So you use the plug-in module I blogged about yesterday (or you use Ramón's plug-in module, which is discussed in yesterday's blog too, and which does the same thing as mine). In other words, you select a word and then you click the "Word" button (in my plug-in module) or select the appropriate contextual menu item (in Ramón's plug-in module) and then the browser opens, the selected search engine is found, and a search is done for the selected word. However, a problem then arises—you get way too many responses from the search engine. Some of the responses are garbage and all you want is to know what "public" or "private" means. Wouldn't it be better if, instead of going to a search engine, you'd go to the IDE's internal help system instead?

So, that's what I've been working on today. Instead of going to the Internet, you go to the internal helpsystem (ultimately, once it is finished, if a word is not covered in the internal helpsystem, you will be informed about this, and then you will be asked whether you want to continue your search by going to the selected search engine).

This is the code that makes it all possible (the section I've highlighted is the part that I added to bring the internal helpsystem into the picture):

public static void sendWordToSearchEngine(JTextComponent textComponent) throws BadLocationException, MalformedURLException {
JTextComponent editor = Registry.getMostActiveComponent();
int pos = editor.getCaretPosition();
try {
int a = Utilities.getPreviousWord(editor,pos);
selection = Utilities.getIdentifier(Utilities.getDocument(editor),a);
if (selection == null || selection.equals("")) {
NotifyDescriptor.Message msg = new NotifyDescriptor.Message("Select a word!");
DialogDisplayer.getDefault().notify(msg);} else {
if (selection.equals("public")) {
HelpFileReferences.getHelpCtx("public");
} else if (selection.equals("private")) {
HelpFileReferences.getHelpCtx("private");
} else {
NotifyDescriptor.Message msg = new NotifyDescriptor.Message
("'" + selection + "' was not found in internal helpsystem.");
DialogDisplayer.getDefault().notify(msg);
//sendToSelectedSearchEngine();
}
}

} catch (BadLocationException ex) {
ErrorManager.getDefault().notify(ex);
}
}

Then, the string (in the above example, only "public" and "private" are catered for) is sent to this method in the HelpFileReferences class:

public static HelpCtx getHelpCtx(String receivedWord) {
String id = receivedWord;
Help help = (Help)Lookup.getDefault().lookup(Help.class);
if (help != null && help.isValidID(id, true).booleanValue()) {
help.showHelp(new HelpCtx(id));
} else {
Toolkit.getDefaultToolkit().beep();
}
return null;
}

Next, I used the JavaHelp Help Set wizard (which is in recent builds and will be available in the upcoming 5.0 Update 1 from the apisupport team) to create a helpset. In the map file, which is one of the files created for me by the JavaHelp Help Set wizard, I added these two map IDs:

<mapID target="public" url="public.html"/>
<mapID target="private" url="private.html"/>

And then I created the two HTML files. So now, when the action is invoked (currently via a button in the toolbar) on the words "public" or "private", a help topic dedicated to those specific words appears.

I think that's pretty cool. Does anyone know of content for these topics? Is there anywhere where I can go and just cut and paste explanatory text for typical Java identifiers (also "String", "int", etc). It would be good to cover as much of the standard Java syntax as possible... the only thing is I wouldn't want to write all of that from scratch...

By the way, if you're a NetBeans plug-in module developer (or a rich-client application developer on top of the NetBeans Platform), let me recommend a few brand new tutorials that you might find very interesting:

The latter two tutorials I came across by accident this morning—they're linked from the NetBeans Module & Platform Development Work-in-Progress Page. If you have anything to add to that page—please don't hesitate. You don't have to ask anyone's permission. Just go right ahead and educate the rest of the world about whatever cool new thing you've discovered while working on NetBeans plug-in modules (or rich-client applications built on the NetBeans Platform).

Postscript

Here's a much cleaner solution for the code that I outlined above:

public static void sendWordToSearchEngine(JTextComponent textComponent) throws BadLocationException, MalformedURLException {
JTextComponent editor = Registry.getMostActiveComponent();
int pos = editor.getCaretPosition();
try {
int a = Utilities.getPreviousWord(editor,pos);
selection = Utilities.getIdentifier(Utilities.getDocument(editor),a);
if (selection == null || selection.equals("")) {
NotifyDescriptor.Message msg = new NotifyDescriptor.Message("Select a word!");
DialogDisplayer.getDefault().notify(msg);
} else {
getHelpCtx(selection);
}
} catch (BadLocationException ex) {
ErrorManager.getDefault().notify(ex);
}
}
public static HelpCtx getHelpCtx(String receivedWord) {
String id = receivedWord;
String moreLikelyToBeUniqueId = "identifier_" + id;
Help help = (Help)Lookup.getDefault().lookup(Help.class);
if (help != null && help.isValidID(moreLikelyToBeUniqueId, true).booleanValue()) {
help.showHelp(new HelpCtx(moreLikelyToBeUniqueId));
} else {
NotifyDescriptor.Message msg = new NotifyDescriptor.Message
("'" + receivedWord + "' was not found in internal helpsystem.");
DialogDisplayer.getDefault().notify(msg);
}
return null;
}

Be the first to comment

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