X

Geertjan's Blog

  • July 4, 2010

NetBeans Groovy Editor in a JEditorPane

Geertjan Wielenga
Product Manager
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.

Join the discussion

Comments ( 6 )
  • Eduardo Costa Tuesday, July 6, 2010

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


  • Mathieu Wednesday, July 28, 2010

    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


  • Lo Wednesday, March 9, 2011

    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


  • philip Thursday, June 30, 2011

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


  • Geertjan Thursday, June 30, 2011

    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


  • projo Friday, August 12, 2011

    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.


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.