Thursday October 05, 2006
3 Lines of Code to Integrate Google Code Search in NetBeans IDE
The unofficial relationship between Google and NetBeans IDE started when Ludo Champenois created on October 5, 2005, a Google Search Toolbar for NetBeans IDE. This Google integration was so popular, and Ludo's implementation was so cool, that it formed the basis of the very frequently used NetBeans Google Toolbar Module Tutorial. Since then, several enhancements have been built on top of this toolbar. For example, some community members have added functionality allowing the user to select alternative search engines, or to (instead of a textfield) provide a right-click popup action on the selected line or word.
Now that the new Google Code Search has been announced (and to celebrate the one year anniversary of Ludo's Google toolbar), its time for something else that's quite special. And here it is... you can integrate Google Code Search in NetBeans IDE in only three lines of code. That's all that you need to do. The NetBeans APIs and NetBeans IDE module development functionality (wizards and tools in the editor) do everything else. Proof? Here it is, follow along and you will be able to send a selection of code to Google Code Search, from a Java file open in the IDE's Source Editor. Here, I've written the steps for the complete beginner, so if you've never written a NetBeans module before, here's your chance! This one could not possibly be simpler.
- Choose File > New Project. In the New Project wizard, choose "Module Project" in the "NetBeans Plug-in Modules" category. Click Next. Name the project "GoogleCodeSearch", browse to an appropriate location, and select "Standalone Module" and "Set as Main Project", if these are not already selected by default. Click Next. Click Finish.
- Right-click the project, choose New, and then choose Action. Select "Conditionally Enabled". In the Cookie Classes drop-down, choose "EditorCookie". Click Next. In Category, choose Tools. Unselect "Global Menu Item". Select "Editor Context Menu Item". Choose "text/x-java" from the drop-down list (this sets the MIME type, which determines the type of file your new action is applicable to). Click Next. Type "GoogleCodeSearch" in class name. Type "Google Code Search" in display name. Click Finish.
- Right-click the project, choose Properties. In the Libraries category, click Add and scroll to "Editor Library". Click OK. And then click OK again. Time for your first two lines of code! Stick these two lines in the action's performAction event:
JTextComponent editor = Registry.getMostActiveComponent(); String selection = editor.getSelectedText();
The first line becomes underlined in red, because you need import statements. Thanks to the dependency on "Editor Library", you can choose org.netbeans.editor.Registry. Next, choose javax.swing.text.JTextComponent.
- Go back to the Project Properties dialog box. This time add a dependency on "UI Utilities API". Time for the final line of code! Here it is, at the end of the performAction event:
URLDisplayer.getDefault().showURL(new URL("http://www.google.com/codesearch?hl=en&q="+selection+"&btnG=Google+Search"));When prompted for import statements, you will need java.net.URL and org.openide.awt.HtmlBrowser.URLDisplayer.
A red underline will remain. Click on the lightbulb and let the IDE surround the code with a try/catch block.
- Hurray, you're done. Right-click the module and choose "Install/Reload in Development IDE". Now select something in a Java file and choose the new menu item:
Selecting the menu item sends the selection to the IDE's default browser, opening it in the Google Code Search page.
Google Code Search rocks. I'm going to be making use of this module a lot. Thanks a lot Google people.
In other news. Read Antonio's brand new and ever-growing Cooking with the NetBeans Platform. If you're a publisher interested in a hot new technical book, written in a fun and non-threatening style, I'd grab Antonio and get him to write it on the NetBeans Platform... Especially his layer sandwich image, and its explanation, is excellent.
Oct 05 2006, 11:18:13 PM PDT Permalink
Posted by Miso Hlavac on October 05, 2006 at 11:44 PM PDT #
Posted by Thomas Zillinger on October 06, 2006 at 12:10 AM PDT #
Posted by Geertjan on October 06, 2006 at 12:14 AM PDT #
Posted by Dan on October 06, 2006 at 12:53 AM PDT #
protected Class[] cookieClasses() {
return new Class[] {
EditorCookie.class
};
}
In step 2, you should have chosen "EditorCookie". If you didn't, modify the code as above and install again. Please leave a message if it worked and also if it didn't!
Posted by Geertjan on October 06, 2006 at 12:57 AM PDT #
Posted by some community members on October 06, 2006 at 02:38 AM PDT #
Posted by Geertjan on October 06, 2006 at 03:10 AM PDT #
Posted by Dan on October 06, 2006 at 05:25 AM PDT #
Posted by Geertjan on October 06, 2006 at 05:30 AM PDT #
Posted by Angad's Blog on July 08, 2008 at 07:11 AM PDT #


