Geertjan is a DZone Zone Leader and has posted 453 posts at DZone. You can read more from them at their website. View Full User Profile

Searching NetBeans Zone Inside NetBeans IDE

06.27.2008
| 6484 views |
  • submit to reddit

The new NetBeans Quick Search API gives you an entry point into NetBeans IDE's new Quick Search feature, which will be part of 6.5 from M1 onwards. By default, the Quick Search feature lets you search for actions (i.e., the things that are invoked via menu items, toolbar buttons, and keyboard shortcuts) and types (i.e., interfaces and classes) which can then be invoked/opened when selected.

As a result, in the case of actions, the user does not need to know the location in the menu bar or toolbar of the action in question. For example, don't know where to go to invoke the "Help" action? Type "help" in the Quick Search and then you'll get a list of actions and types that match the search string:

Then the Help pops up when you select Help above, without requiring you to go to the Help menu. Even if you know where that menu item is found, being able to invoke all your actions from the same place saves a lot of time. Imagine if you want to, for example, open the Projects window after you've opened the Help. In the past you'd need to move from one menu item (or toolbar button) to another, whereas now you'll simply type a new search string and then click the relevant result. A real time saver. After making use of it, the invoked action (or type) is added to the Recent Search list, making it even easier to find next time round:

However, thanks to the API, the Quick Search feature isn't simply a feature. It is a framework, providing you with a user interface, a filtering mechanism, and the related search algorithms, so that all you need to provide is the content.

Simple example. Let's integrate the tutorials from platform.netbeans.org/tutorials into the Quick Search feature:

That's pretty handy. Now, you can see the tutorials that relate to the actions and types that you're searching for. And vice versa. In the same way, let's incorporate the titles of NetBeans Zone articles into the Quick Search feature:

When clicked, the related article opens in the browser. In the code below (which is ALL the code you will need for the integration shown above) you can see that JTidy is used for parsing purposes, but you could use any approach you like instead:

public class NetBeansZoneSearchProvider implements SearchProvider {

public void evaluate(SearchRequest request, SearchResponse response) {
try {

//The URL that we are providing a search for:
URL url = new URL("http://netbeans.dzone.com");

//Stuff needed by Tidy:
Tidy tidy = new Tidy();
tidy.setXHTML(true);
tidy.setTidyMark(false);
tidy.setShowWarnings(false);
tidy.setQuiet(true);

//Get the org.w3c.dom.Document from Tidy:
Document doc = tidy.parseDOM(url.openStream(), null);

//Get all "a" elements:
NodeList list = doc.getElementsByTagName("a");

//Get the number of elements:
int length = list.getLength();

//Loop through all the "a" elements:
for (int i = 0; i < length; i++) {

String href = null;
if (null != list.item(i).getAttributes().getNamedItem("href")) {
//Get the "href" attribute from the current "a" element:
href = list.item(i).getAttributes().getNamedItem("href").getNodeValue();
}

//Get the "title" attribute from the current "a" element:
if (null != list.item(i).getAttributes().getNamedItem("title")) {
String title = list.item(i).getAttributes().getNamedItem("title").getNodeValue();

//If the title matches the requested text:
if (title.toLowerCase().indexOf(request.getText().toLowerCase()) != -1) {

//Add the runnable and the title to the response
//and return if nothing is added:
if (!response.addResult(new OpenFoundArticle(href), title)) {
return;
}

}

}

}

} catch (IOException ex) {
Exceptions.printStackTrace(ex);
}
}

private static class OpenFoundArticle implements Runnable {

private String article;

public OpenFoundArticle(String article {
this.article = article;
}

public void run() {
try {
URLDisplayer.getDefault().showURL(new URL("http://netbeans.dzone.com" + article));
} catch (MalformedURLException ex) {
Logger.getLogger(NetBeansZoneSearchProvider.class.getName()).log(Level.SEVERE, null, ex);
}
}

}

}

Here's another example, via the code above, pointing to netbeans.dzone.com, that shows the usefulness of this extension of the Quick Search feature:

Here's how to register it in the layer.xml file:

<folder name="QuickSearch">
<folder name="NetBeansZone">
<attr name="SystemFileSystem.localizingBundle"
stringvalue="org.nb.nbzonesearch.Bundle"/>
<attr name="command" stringvalue="G"/>
<attr name="position" intvalue="1"/>
<file name="org-nb-nbzonesearch-NetBeansZoneSearchProvider.instance"/>
</folder>
</folder>


There's also a template (from 6.5 Milestone 2 onwards), which generates the layer entries, the relevant dependency, and a stub Java class, as explained in my blog.

Finally, imagine how cool it would be if a Quick Search extension were to be created for a site like the NetBeans User FAQ! The user would be able to find the solutions to their problems right inside the IDE, together with all the related actions and types. But these scenarios are just the tip of the iceberg. And even if you were to have a dozen or so different search categories, it would be fairly trivial to include customization settings so that the user would be able to determine which/how many searches are actually performed. Plus, maybe instead of searching titles (as done above), one could search the META tags in the HTML file, for example. That's completely up to the implementor. Also, bear in mind that here the Quick Search feature and its API are discussed in the context of NetBeans IDE, though everything said here applies equally to ANY application created on top of the NetBeans Platform, where this functionality might be equally useful. In short, the Quick Search API opens the IDE (and any other NetBeans Platform application) up to a vast integration of cross-referenced searches for as many related items as your users want. That's a pretty cool development.

AttachmentSize
tutorials-quick-search.png38.2 KB
netbeans-dzone-titles2.png47.99 KB
netbeans-dzone-titles4.png44.37 KB
help1.png13.53 KB
help2.png16.05 KB
Published at DZone with permission of its author, Geertjan Wielenga.

Comments

Varun Nischal replied on Fri, 2008/06/27 - 3:05am

That's really cool :-) Awesome post!

Michael Bien replied on Fri, 2008/06/27 - 11:21am

I really like it. NetBeans needs more of this usefull little gimmics! (but of course never forget an option to disable them)

Kirill Grouchnikov also implemented a similar feature in the substance l&f. It expanded the menues and showed all items which matched. Maybe this could be also implemented. e.g triggered when selecting a suggested item which is also found in the menue (after a delay or so). (hint substance is BSD and pretty cool)

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.