X

Geertjan's Blog

  • February 26, 2006

Send a Word to the Browser (Part 2)

Geertjan Wielenga
Product Manager
In Send a Word to the Browser (Part 1), I blogged about a plug-in module by Ramón Ramos that lets you send the element under the caret to a search engine (note, though, that Ramón's plug-in module has been signficantly extended since I blogged about it—now you can not only select a search engine from a predefined list, but you can also add additional search engines, via a new category in the Options window).

I was particularly interested in Ramón's plug-in module because, sometime ago, I worked on something very similar, see Further Search Facilities for NetBeans IDE 5.0 for details. In that plug-in module, I was able to select a search egnine from a drop-down list and then send the current line to the selected search engine. At the time, I wanted to also be able to send a word—i.e., not just a line—but didn't know how to do that. In the meantime, I've learnt from Ramón's plug-in module and, as a result, have been able to extend my own (the screenshot below not only shows the user interface added by the plug-in module, but also the code that makes the word selected possible):

I basically used Ramón's code verbatim, just as I found it in his source code. However, the code used here seems pretty different to what is prescribed in the FAQ: How do I get the element under the caret?. I actually wanted to use that code, because it seemed 'prescribed' while I wondered about Ramón's code and didn't really know which was 'better', but the problem was that I would then have ended up with conflicting import statements. The code in the FAQ has this line:

Element el = resource.getElementByOffset(offset);

That line requires org.netbeans.jmi.javamodel.Element, while earlier in the code I had made use of javax.swing.text.Element. Since I couldn't use both of these packages in the same class (and didn't want to create a new one), I used Ramón's solution instead. And it works.

Next, I'm going to take a look at Ramón's addition to the Options window. It's pretty cool—if you don't like the selection of search engines that the plug-in module provides, you can simply add additional ones. Thanks a lot Ramón—your plug-in module provides exactly the kind of source code I've been needing to understand the NetBeans APIs better and to extend my own plug-in module further!

Join the discussion

Comments ( 4 )
  • Ramón Ramos Sunday, February 26, 2006
    Hi Geertjan,

    one difference that I see in the two implementation alternatives is that the one we used applies to every text editor. I think the other one is specially to work with the java code.

    BTW, my module is presented here:

    http://jroller.com/page/ramlog?entry=websearchengines_netbeans_module

    Regards,

    Ramón

  • Geertjan Monday, February 27, 2006
    Thanks Ramón. That makes sense. However, can you explain why "Utilities.getPreviousWord" is used? We're not using the previous word, but the current word. That's something I don't understand about this code.
  • Ramón Ramos Monday, February 27, 2006
    Hi Geertjan,
    I have looked at it again. And the statement is just unnecesary!
    Just throw it out and give the caret position to the Utilities.getIdentifier method. It works now accuratly.
    Regards
  • Geertjan Monday, February 27, 2006
    Good point. (But I wonder why it worked even though it was wrong.) This is how I have it now (and it works and it makes much more sense):

    public static void sendWordToSearchEngine(JTextComponent textComponent) throws BadLocationException, MalformedURLException {
    JTextComponent editor = Registry.getMostActiveComponent();
    int pos = editor.getCaretPosition();
    try {
    selection = Utilities.getIdentifier(Utilities.getDocument(editor),pos);
    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);
    }
    }
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.