Tuesday Nov 27, 2012

Embedded Nashorn in JEditorPane

Here's a prototype for some kind of backoffice content management system. Several interesting goodies are included, such as an embedded JavaScript editor, as can be seen in the screenshot:

Key items of interest in the above are as follows:

  • Embedded JavaScript editor (i.e., the latest and greatest Nashorn technology, look it up, if you're not aware of what that is.) The way that's done is to include the relevant JavaScript modules in your NetBeans Platform application. Make very sure to include "Lexer to NetBeans Bridge", which does a bunch of critical stuff under the hood. The JEditorPane is defined as follows, along the lines that I blogged about recently thanks to Steven Yi:
    javaScriptPane.setContentType("text/javascript");
    EditorKit kit = CloneableEditorSupport.getEditorKit("text/javascript");
    javaScriptPane.setEditorKit(kit);
    javaScriptPane.getDocument().putProperty("mimeType", "text/javascript");

    Note that "javaScriptPane" above is simply a JEditorPane.

  • Timon Veenstra's excellent solution for integrating Nodes with MultiViewElements, which is described here by Timon, and nowhere else in the world. The tab you see above is within a pluggable container, so anyone else could create a new module and register their own MultiViewElement such that it will be incorporated into the editor.

  • A small trick to ensure that only one window opens per news item:
    @NbBundle.Messages("OpenNews=Open")
    private class OpenNewsAction extends AbstractAction {
    
        public OpenNewsAction() {
            super(Bundle.OpenNews());
        }
    
        @Override
        public void actionPerformed(ActionEvent e) {
            News news = getLookup().lookup(News.class);
            Mode editorMode = WindowManager.getDefault().findMode("editor");
            for (TopComponent tc : WindowManager.getDefault().getOpenedTopComponents(editorMode)) {
                if (tc.getDisplayName().equals(news.getTitle())) {
                    tc.requestActive();
                    return;
                }
            }
            TopComponent tc = MultiViews.createMultiView("application/x-newsnode", NewsNode.this);
            tc.open();
            tc.requestActive();
        }
    
    }

The rest of what you see above is all standard NetBeans Platform stuff. The sources of everything you see above is here:

http://java.net/projects/nb-api-samples/sources/api-samples/show/versions/7.3/misc/CMSBackOffice

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
« November 2012 »
SunMonTueWedThuFriSat
    
11
13
14
15
24
25
 
       
Today