Getting Started Plugging into the "Find in Projects" Dialog

Posted by Geertjan on Oracle Blogs See other posts from Oracle Blogs or by Geertjan
Published on Wed, 30 May 2012 11:33:20 +0000 Indexed on 2012/05/30 16:52 UTC
Read the original article Hit count: 256

Filed under:

In case you missed it amidst all the code in yesterday's blog entry, the "Find in Projects" dialog is now pluggable. I think that's really cool. The code yesterday gives you a complete example, but let's break it down a bit and deconstruct down to a very simple hello world scenario.

We'll end up with as many extra tabs in the "Find in Projects" dialog as we need, for example, three in this case: 

And clicking on any of those extra tabs will, in this simple example, simply show us this:

Once we have that, we'll be able to continue adding small bits of code over the next few blog entries until we have something more useful. So, in this blog entry, you'll literally be able to display "Hello World" within a new tab in the "Find in Projects" dialog:

import javax.swing.JComponent;
import javax.swing.JLabel;
import org.netbeans.spi.search.provider.SearchComposition;
import org.netbeans.spi.search.provider.SearchProvider;
import org.netbeans.spi.search.provider.SearchProvider.Presenter;
import org.openide.NotificationLineSupport;
import org.openide.util.lookup.ServiceProvider;

@ServiceProvider(service = SearchProvider.class)
public class ExampleSearchProvider1 extends SearchProvider {

    @Override
    public Presenter createPresenter(boolean replaceMode) {
        return new ExampleSearchPresenter(this);
    }

    @Override
    public boolean isReplaceSupported() {
        return false;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }

    @Override
    public String getTitle() {
        return "Demo Extension 1";
    }
    
    public class ExampleSearchPresenter extends SearchProvider.Presenter {

        private ExampleSearchPresenter(ExampleSearchProvider1 sp) {
            super(sp, true);
        }
        
        @Override
        public JComponent getForm() {
            return new JLabel("Hello World");
        }

        @Override
        public SearchComposition composeSearch() {
            return null;
        }

        @Override
        public boolean isUsable(NotificationLineSupport nls) {
            return true;
        }

    }

}

That's it, not much code, works fine in NetBeans IDE 7.2 Beta, and is easier to digest than the big chunk from yesterday. If you make three classes like the above in a NetBeans module, and you install it, you'll have three new tabs in the "Find in Projects" dialog. The only required dependencies are Dialogs API, Lookup API, and Search in Projects API. Read the javadoc linked above and then in next blog entries we'll continue to build out something like the sample you saw in yesterday's blog entry.

© Oracle Blogs or respective owner

Related posts about /NetBeans IDE