X

Geertjan's Blog

  • October 5, 2006

3 Lines of Code to Integrate Google Code Search in NetBeans IDE

Geertjan Wielenga
Product Manager
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.

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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.

Join the discussion

Comments ( 11 )
  • Miso Hlavac Thursday, October 5, 2006
    Hello Geertjan,
    this feture is also useful with jarfinder.com
  • Thomas Zillinger Friday, October 6, 2006
    Or you could use Ramons Websearch plugin (follow the white rabbit)
    \*gg\*
    Yeah, Iiii know you wanted to demonstrate the ease of netbeans development :) And - it IS great :)
  • Geertjan Friday, October 6, 2006
    Hi Thomas, yes that's what I meant when I said "some community members have added functionality allowing the user to select alternative search engines". And hello Miso: you should take a look at that plugin, you can register search engines there, for example, also jarfinder.com. Thomas, you're right -- this is really a demo of the ease of NB module development. The combination of the power of the NetBeans APIs and all the tools in the user interface create a really low threshold (i.e., you don't need to be a Java guru!) to create plugins to make your life better/easier in NetBeans (or in any other app based on the NetBeans Platform, of course).
  • Dan Friday, October 6, 2006
    It doesn't work for me. The menu item shows up but it's not enabled, so I can't select it and click it. Idea?
  • Geertjan Friday, October 6, 2006
    Do you have this somewhere in the code:

    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!

  • some community members Friday, October 6, 2006
    Here is the direct link to the nbm file of the Web Search Module: nbm
  • Geertjan Friday, October 6, 2006
    :-) Ramon, you're in Germany I think -- will you be at the NetBeans User Group event in Munich on the 17th?
  • Dan Friday, October 6, 2006
    Yes, it did work, seems like my mistake was I chose EditCookie instead of EditorCookie. Thanks
  • Geertjan Friday, October 6, 2006
    Hi Dan -- that's what I thought. Happy Google code searching. They've done a great job there.
  • Ramon Friday, October 6, 2006
    I still don't know for sure if I will be there. Maybe.
  • Angad's Blog Tuesday, July 8, 2008
    [Trackback] Since yesterday I've been looking for a simple and effective way to search the Netbeans source code as the need arises every now and then as I need to check for an example of how a particular API (like the HyperlinkProviderExt or the GSF API) is implem...
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.