X

Geertjan's Blog

  • January 20, 2011

Quick Search Aligned Right in Menu Bar

Geertjan Wielenga
Product Manager
Thanks to Ernest Lotter's investigations, this is how to put the Quick Search bar in the menu bar, aligned right:
<folder name="Menu">
<file name="Spacer.instance">
<attr name="instanceCreate" methodvalue="javax.swing.Box.createHorizontalGlue"/>
<attr name="position" intvalue="9005"/>
</file>
<file name="org-netbeans-modules-quicksearch-QuickSearchAction.shadow">
<attr name="originalFile" stringvalue="Actions/Edit/org-netbeans-modules-quicksearch-QuickSearchAction.instance"/>
<attr name="position" intvalue="9010"/>
</file>
</folder>

Here you see the Quick Search bar, in the menu bar, aligned right:

Now that the Quick Search bar is visible, let's use it.

<folder name="QuickSearch">
<folder name="Car">
<attr name="position" intvalue="0"/>
<file name="org-car-viewer-CarQuickSearchProvider.instance">
<attr name="displayName" stringvalue="Cars"/>
</file>
</folder>
</folder>

And here's the class referred to above:

public class CarQuickSearchProvider implements SearchProvider {
@Override
public void evaluate(final SearchRequest request, final SearchResponse response) {
Runnable r = new Runnable() {
@Override
public void run() {//Tightly coupled with the CarViewerTopComponent:
TopComponent tc = WindowManager.getDefault().findTopComponent("CarViewerTopComponent");//Get the ExplorerManager of the CarViewerTopComponent:
final ExplorerManager em = ((ExplorerManager.Provider) tc).getExplorerManager();//Get the root context:
Node root = em.getRootContext();//Iterate through the nodes of the children:
for (final Node node : root.getChildren().getNodes()) {//Get the Car object from each Node:
Car car = node.getLookup().lookup(Car.class);//Perform check to see whether the Car contains the entered text:
if (accept(car, request)) {//If the car type contains the entered text,
//add the type to the result object,
//and select the related Node:

if (!response.addResult(new Runnable() {
@Override
public void run() {
try {
em.setSelectedNodes(new Node[]{node});
} catch (PropertyVetoException ex) {
Exceptions.printStackTrace(ex);
}
}
}, car.getType())) {
return;
}
}
}
}
};
SwingUtilities.invokeLater(r);
}//Check that the entered text is contained in the type of the car:
private boolean accept(Car car, SearchRequest request) {
if (car.getType() != null && car.getType().contains(request.getText())) {
return true;
}
return false;
}
}

Join the discussion

Comments ( 4 )
  • Jesse Glick Thursday, January 20, 2011

    Quick Search is on the right side of the menu bar in 7.0 already - you don't have to do anything. Or are you talking about backporting http://hg.netbeans.org/main/rev/0889e2f4e981 to 6.9? If so, the simplified version of the patch in this blog entry will (I guess) not work on Macs, and will cause a duplicated QS field on other platforms.

    By the way, iterating the children of a node like this is a sign of poor style. (org.openide.nodes should be treated as an SPI; you should be suspicious of any code other than an explorer view calling it as an API.) The QS provider should work directly on the underlying data model:

    for (Car car : Lookups.forPath("Cars").lookupAll(Car.class)) {

    if (accept(car, request)) {...}

    }

    to show all matches regardless of what windows currently happen to be open. The action callback for a hit can then look for the Cars window, opening it if necessary, and trying to find and select the corresponding car node if possible. (The API in https://netbeans.org/bugzilla/show_bug.cgi?id=7551 might be helpful in this regard.) Or more realistically, it would open a detail view of a single car.


  • Geertjan Wielenga Thursday, January 20, 2011

    The quick search isn't available when you run a bare NetBeans Platform application. You need to manually add the elements shown in this blog entry to the layer of one of the modules in your application. Otherwise, no quick search.


  • Peter Harman Sunday, January 23, 2011

    What about if you don't have a menubar (i.e. with ribbon instead), is the Quick Search component available to be put anywhere?

    I reported a bug (/feature!) a while back on Quick Search - it doesn't report to the SearchProvider if the user has moved focus away and therefore if you have started a long process searching for something I don't believe you have the opportunity to stop if the user no longer wants it.


  • Brian Jenkins Thursday, August 22, 2013

    I know I'm a little late to the game, but I'm just starting to incorporate the information from this blog entry into our platform application. For the most part the code works well. The problem I'm having is that after the result is selected from the drop down list, focus remains in the quick search control. This behavior is different from how the quick search works in the context of the IDE. In this case, after a result item is selected, the focus moves away from the control. This is what I would like to have happen in my application. Do you have any insight into how this could be accomplished?


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