JEditorPane Code Completion (Part 2)
- by Geertjan
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.