Quick Search Aligned Right in Menu Bar

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;
    }
    
}

Comments:

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.

Posted by Jesse Glick on January 20, 2011 at 12:10 AM PST #

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.

Posted by Geertjan Wielenga on January 20, 2011 at 10:00 AM PST #

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.

Posted by Peter Harman on January 23, 2011 at 03:34 AM PST #

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?

Posted by Brian Jenkins on August 22, 2013 at 12:30 PM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
12
13
14
24
25
26
27
28
29
30
   
       
Today