NetBeans Groovy Editor in a JEditorPane

Even when you're not creating some kind of IDE, you might still have technical end users in mind. For this kind of user, it might be handy if you'd integrate a small scripting editor into your application, as I've done here for Groovy:

The editor area above is a JEditorPane. In the constructor of the TopComponent is this code:

EditorKit kit = CloneableEditorSupport.getEditorKit("text/x-groovy");
editorPane.setEditorKit(kit);
FileObject fob;
try {
    //Create a file in memory:
    fob = FileUtil.createMemoryFileSystem().getRoot().createData("tmp", "groovy");
    DataObject dob = DataObject.find(fob);
    editorPane.getDocument().putProperty(
            Document.StreamDescriptionProperty,
            dob);
    DialogBinding.bindComponentToFile(fob, 0, 0, editorPane);
    //Provide some default content:
    editorPane.setText("def scores = [80, 90, 70]\\n\\n\\"\\"\\"Maximum: ${scores.max()}\\n"
            + "Minimum: ${scores.min()}\\"\\"\\"");
} catch (IOException ex) {
    Exceptions.printStackTrace(ex);
}

When the "Evaluate" button is clicked, the following happens:

private void evaluateButtonActionPerformed(java.awt.event.ActionEvent evt) {
    ScriptEngineManager factory = new ScriptEngineManager();
    ScriptEngine engine = factory.getEngineByName("groovy");
    try {
        InputOutput inout = IOProvider.getDefault().getIO("Result", true);
        inout.select();
        inout.getOut().print(engine.eval(editorPane.getText()));
    } catch (ScriptException ex) {
        Exceptions.printStackTrace(ex);
    }
}

Right now, though syntax coloring works, as well as keyboard shortcuts (such as Ctrl-E to delete a line), the code completion does not work yet. The code comes from here, but somehow ClassPathProvider needs to be hooked into the code so that the source files on the Groovy classpath are found in order to build up the code completion box. However, if I embed a Java editor (i.e., instead of creating "tmp.groovy", I create "tmp.java"), there's no problem with code completion at all. I believe the DialogBinding statement may be the cause of the problem.

Comments:

Nice tip, but I have a silly question: what's the difference of using NB's editor from IDE cluster?

Posted by Eduardo Costa on July 05, 2010 at 10:45 PM PDT #

Hi Geertjan,

Thanks for this post, it is very helpfull for me.
Actually I really need to get a Groovy editor in my project and this example sounds like exactly what I need; so that I tried to reproduce your code and I get a very basic error when loading the Document class from the JDK 1.6.
I really have no idea of what is wrong:

INFO [org.netbeans.core.windows.persistence]: [PersistenceManager.getTopComponentForID] Problem when deserializing TopComponent for tcID:'GroovyEditorTopComponent'. Reason: Error reading MultiFileObject@5b187658[Windows2Local/Components/GroovyEditorTopComponent.settings]: java.lang.reflect.InvocationTargetException
java.lang.ClassNotFoundException: javax.swing.text.Document
at org.apache.felix.framework.ModuleImpl.findClassOrResourceByDelegation(ModuleImpl.java:779)
at org.apache.felix.framework.ModuleImpl.access$100(ModuleImpl.java:61)
at org.apache.felix.framework.ModuleImpl$ModuleClassLoader.loadClass(ModuleImpl.java:1698)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Caused: java.lang.NoClassDefFoundError: javax/swing/text/Document
at org.openmole.editor.GroovyEditorTopComponent.<init>(GroovyEditorTopComponent.java:58)
at org.openmole.editor.GroovyEditorTopComponent.getDefault(GroovyEditorTopComponent.java:101)
Caused: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.netbeans.modules.settings.convertors.XMLSettingsSupport$SettingsRecognizer.createFromMethod(XMLSettingsSupport.java:674)
Caused: java.io.IOException: Error reading MultiFileObject@5b187658[Windows2Local/Components/GroovyEditorTopComponent.settings]
at org.netbeans.modules.settings.convertors.XMLSettingsSupport$SettingsRecognizer.createFromMethod(XMLSettingsSupport.java:682)
at org.netbeans.modules.settings.convertors.XMLSettingsSupport$SettingsRecognizer.instanceCreate(XMLSettingsSupport.java:585)
at org.netbeans.modules.settings.convertors.SerialDataConvertor$SettingsInstance.instanceCreate(SerialDataConvertor.java:424)
at org.netbeans.core.windows.persistence.PersistenceManager.getTopComponentPersistentForID(PersistenceManager.java:545)
at org.netbeans.core.windows.persistence.PersistenceManager.getTopComponentForID(PersistenceManager.java:655)
at org.netbeans.core.windows.PersistenceHandler.getTopComponentForID(PersistenceHandler.java:460)
at org.netbeans.core.windows.PersistenceHandler.initModeFromConfig(PersistenceHandler.java:421)
at org.netbeans.core.windows.PersistenceHandler.load(PersistenceHandler.java:206)
at org.netbeans.core.windows.WindowSystemImpl.load(WindowSystemImpl.java:73)
at org.netbeans.core.GuiRunLevel$2.run(GuiRunLevel.java:181)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
at org.netbeans.core.TimableEventQueue.dispatchEvent(TimableEventQueue.java:137)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
[catch] at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

Thanks for your help!
Mathieu

Posted by Mathieu on July 27, 2010 at 07:13 PM PDT #

Hello !

I've got a problem with this :

DialogBinding.bindComponentToFile(fob, 0, 0, editorPane);

I've try to put :
import org.netbeans.api.java.source.ui.DialogBinding;

But the application doesn't find the package, I don't know wich dependencies I have to add.

It woul'd be a pleasure if you could help me.

Peter

Posted by Lo on March 08, 2011 at 04:48 PM PST #

I have the same problem as the above user, that package seems to have disappeared.

Posted by philip on June 30, 2011 at 03:31 AM PDT #

Tried it a few seconds ago in 7.0. Make sure the Groovy & Grails plugin is installed. Uploading the completed sample to the versions/7.0/misc folder here now:

http://java.net/projects/nb-api-samples

Posted by Geertjan on June 30, 2011 at 07:40 AM PDT #

hi Geertjan,
thank you for your great blog. Your posts helps me much to code a prototyp for my master thesis.
I have checked out the sources from java.net and always get a nullpointer with same stacktrace mentioned here http://netbeans.org/bugzilla/show_bug.cgi?id=198415
respectively
http://netbeans.org/bugzilla/show_bug.cgi?id=198719
I have already 7.01... the bug seems not to be solved. Maybe some readers can give me a piece of advice.

Posted by projo on August 12, 2011 at 01:50 AM 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
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today