Friday Oct 12, 2012

JEditorPane Code Completion (Part 2)

Figured it out! No need to create a fake Java file, unlike what I said in part 1, no need to depend on all the Java Editor modules, if you use DialogBinding.bindComponentToDocument, instead of DialogBinding.bindComponentToFile:

public final class CountryEditorTopComponent extends TopComponent {

    public CountryEditorTopComponent() {

        initComponents();
        setName(Bundle.CTL_CountryEditorTopComponent());
        setToolTipText(Bundle.HINT_CountryEditorTopComponent());

        EditorKit kit = CloneableEditorSupport.getEditorKit("text/plain");
        jEditorPane1.setEditorKit(kit);
        DialogBinding.bindComponentToDocument(jEditorPane1.getDocument(), 0, 0, jEditorPane1);
        jEditorPane1.setText("Egypt");

    }

The above requires a dependency on Editor Library 2, which is where DialogBinding is found. Aside from that, you need all the dependencies required by the Code Completion API, as described in the Code Completion tutorial on the NetBeans Platform Learning Trail.

Once you've done that, go to the Project Properties dialog of the application and then in the "ide" cluster, include "Plain Editor" and "Plain Editor Library". I.e., two additional JARs only. These two are needed because you've set the MIME type to "text/plain", which is needed because DialogBinding expects the JEditorPane to have a MIME type.

And now everything works. Press Ctrl-Space in your JEditorPane and, because your CompletionProvider is registered in "text/x-dialog-binding" (via the annotation on CompletionProvider), your completion items are displayed. (The only MIME type for binding a document to a component, by default, is "text/x-dialog-binding", which means the next step is for someone to figure out how to support multiple different of such MIME types, since each JEditorPane in your application is likely to require its own specific code completion support.)

I think this is a really workable solution for real scenarios where JEditorPanes in NetBeans Platform applications require code completion.

Update. Part 3 shows how to register multiple CompletionProviders in text/x-dialog-binding, while ensuring that only the appropriate items appear in the code completion.

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
« October 2012 »
SunMonTueWedThuFriSat
 
1
2
3
21
   
       
Today