0

Following this question Click Here. I thought of creating a simple IDE for groovy and Java. Code is reproduced here for easy reference:

import groovy.swing.SwingBuilder
import java.awt.BorderLayout as BL
import static javax.swing.JFrame.EXIT_ON_CLOSE
import org.fife.ui.rsyntaxtextarea.*

RSyntaxTextArea textArea = new RSyntaxTextArea()
textArea.syntaxEditingStyle = SyntaxConstants.SYNTAX_STYLE_JAVA

swing =  new SwingBuilder()
frame = swing.frame(title:"test", defaultCloseOperation:EXIT_ON_CLOSE, size:[600,400], show:true ) {
  borderLayout()
  panel( constraints:BL.CENTER ) {
    borderLayout()
    scrollPane( constraints:BL.CENTER ) {
      widget textArea
    }
  }
}

Now I have all the codings entered by the user in textarea which is an Object of RSynataxTextArea, how i should perform compilation for all the code written by the user? Is there any class for this purpose or any ways of doing it in Groovy?

Thanks in advance.

Community
  • 1
  • 1
Ant's
  • 13,545
  • 27
  • 98
  • 148

1 Answers1

1

I you look in the src/main/groovy/ui folder of the source download for Groovy, you'll see the code which makes the groovyConsole work

If you look inside the ConsoleSupport class, you'll see the way the console does it:

protected Object evaluate(String text) {
    String name = "Script" + counter++;
    try {
        return getShell().evaluate(text, name);
    }
    catch (Exception e) {
        handleException(text, e);
        return null;
    }
}

where getShell() is:

public GroovyShell getShell() {
    if (shell == null) {
        shell = new GroovyShell();
    }
    return shell;
}

So it returns a new GroovyShell or the exiting one if one already exists

tim_yates
  • 167,322
  • 27
  • 342
  • 338
  • Couldn't get you.. So what exactly i have to do with my code? – Ant's Jul 29 '11 at 13:31
  • Get the text from the RSyntaxTextArea, and `evaluate()` it with an instance of `GroovyShell`? – tim_yates Jul 29 '11 at 13:45
  • I have tried adding this line `GroovyShell anto = ConsoleSupport.evaluate(textArea.getText())`. But I'm getting an error as `No signature of method: static groovy.ui.ConsoleSupport.evaluate() is applicable for argument types: (java.lang.String) values: []` .. Where I went Wrong? – Ant's Jul 29 '11 at 14:42
  • 1
    ?! The code in `ConsoleSupport` was showing you how the Groovy console does it (which is basically exactly what you are trying to write). You would need something like: `new GroovyShell().evaluate( textArea.text, "Script${System.currentTimeMillis()}" )` – tim_yates Jul 29 '11 at 14:52