简体   繁体   中英

Using the Groovy editor from the Groovy-Eclipse plugin in an RCP application

I am developing an RCP application that uses Groovy as a scripting language. I included the Groovy-Eclipse plugin to let the user of the application to write Groovy code within the application using the Groovy editor.

The Groovy scripts have to use some Java classes from the application, how can I add visibility to these classes in the Groovy editor to provide the user with features like code completion as transparently as possible.

To execute the Groovy scripts I am using GroovyShell and I add a property to the Binding that is passed to the GroovyShell constructor.

This property is a map where I put some objects relevant to my application:

HashMap<String, Object> plx = new HashMap<String, Object>();
plx.put("element", new Element("xxx"));
Binding binding = new Binding();
binding.setProperty("plx", plx);

Imagine that Element is a Java class defined this way

class Element
{
  String name;
  public Element(String name)
  {
    this.name = name;
  }
  public String getName()
  {
    return name;
  }
 }

I can write and execute the following Groovy script with no problem

println plx.element.name

and I get the correct result: xxx

The problems I have is that I do not know how to modify the classpath the editor uses, to let it resolve the Element class to enable code completion when I am editing the script within my application.

Do you want to start a new VM each time you are executing a script in your RCP, or do you want to run your Groovy script in the same VM where your RCP is currently running. Your question is a bit too generic for me... If the latter one, make sure, you have added your Java classes to the plug-ins dependency, otherwise it will not be resolved at runtime just compile time.

If you would like to use your classes without explicitly importing them in the script you can add the imports transparently. http://www.jroller.com/melix/entry/customizing_groovy_compilation_process

You can also inject any services or instances via bindings into the script. It is basically a map of key values.

Once your Java classes are on the classpath the content assist will be available for them in your script. If you need further customization in the content assist proposals, then check this great post to get more details about the actual implementation. http://contraptionsforprogramming.blogspot.de/2009/12/extending-groovy-eclipse-for-use-with.html

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM