OpenJUMP and Groovy

From OpenJUMP Wiki
Revision as of 10:04, 8 October 2016 by Michaudm (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

The following was posted by Benjamin on the jpp-devel list (3rd June 2010):


I use Groovy the same way as you use Java (importing the OJ classes).

I put groovy-all-1.7.0.jar (ca. 5 MB) into OpenJUMP's /lib directory and develop OJ plugins with (using the amazing Groovy-Eclipse Plugin). NetBeans and IntelliJ IDEA also support Groovy. You can use Java classes in Groovy classes and vice versa (Groovy files are compiled into .class files). Thus you can even use Groovy classes from the BeanShell or Jython.

Since my two classes (mentioned previously) require Groovy Closures as method parameters, it is necessary to call their methods within a groovy file (these two classes are special cases).

Here is a HelloWorldPlugIn in Groovy:

import com.vividsolutions.jump.workbench.plugin.AbstractPlugIn
import com.vividsolutions.jump.workbench.plugin.PlugInContext
import com.vividsolutions.jump.workbench.ui.plugin.FeatureInstaller

class HelloWorldPlugIn extends AbstractPlugIn {

    void initialize(PlugInContext pluginContext) {}

    boolean execute(PlugInContext pluginContext) {
        def pluginPath = pluginContext.getWorkbenchContext().getWorkbench().
            getPlugInManager().getPlugInDirectory().getAbsolutePath()
           
        def htmlFrame = pluginContext.getWorkbenchFrame().getOutputFrame()
        htmlFrame.setTitle("Hello, World!")
        htmlFrame.createNewDocument()
        htmlFrame.addText("Hello, World!")
        htmlFrame.addText(pluginPath.toString())
        htmlFrame.surface()
       
        return true
    }

}

Another funny thing is, you can copy your Java code into a Groovy file and it compiles.

--Benjamin