Groovy Console in NetBeans IDE
By Geertjan-Oracle on Mar 18, 2007
Before going into the remaining problems, let's first look at what it took to get to the current result. First, the good people at Groovy did the world a big favor by creating a single JAR file that includes everything needed for working with Groovy in Java. In the Groovy distribution, get embeddable/groovy-all-1.0.jar, and you're good to go. Wrap that in a library wrapper module project, attached to a module suite, add your functionality module to the suite, declare a dependency on the library wrapper module, and you can start coding for Groovy in Java. Next, you want Groovy files to be opened in a TopComponent. Readers of this blog will know how to do that (use the New File Type wizard and the New Window Component wizard, then create an implementation of the NetBeans API OpenSupport class and change the TopComponent to CloneableTopComponent).
Up to this point, everything is completely standard NetBeans API stuff, as described above. Next, you need to embed the Groovy Console in the TopComponent. That's the tricky bit and is still not perfect. Unfortunately, unlike third-party objects such as the FlashPanel, described here, the Groovy Console is not a JPanel. It is something built on top of a JFrame. So, how to embed a JFrame in a TopComponent? Not easy. The Console object is a single, encapsulated object, we can't move its contents around as we would do if we could open its sources as a JFrame in NetBeans IDE. However, there's a handy method on the Console, called Console.getFrame. That's our way in. That method returns a JFrame, from which we can get the content pane, which we then add as a Container to the TopComponent.
However, for some reason we can only get the content pane after the Groovy Console is running. I don't know why this is the case. Therefore, I call Console.run, then I get the content pane and add it to the TopComponent, and then I call Console.exit. So, when you open a Groovy file in the embedded Groovy Console, you first see a quick flashy thing, which is the Groovy Console opening and closing as a JFrame, while the content pane is added to the TopComponent. Not perfect, but workable. Hope it can be fixed somehow.
The next problem, unresolved currently, is the biggest problem. The method Console.setInputArea(JTextArea) is used to send the content of the Groovy file to the input area of the embedded console. However, once the embedded Groovy Console is opened, the sent text isn't visible! But I know it is there, somewhere in the background or hidden or something like that, because when I press Ctrl-Enter, the content of the input area is run and the result is displayed in the output area. So, if the content of my Groovy file is println "hello world", and I then open the Groovy file in the embedded Groovy Console, I don't see anything in the input area. However, when I press Ctrl-Enter, the line println "hello world" is executed. Very strange.
The final issue, currently, is related to the menu items in the Groovy Console that I am transferring to a toolbar in NetBeans IDE. (If you look in the screenshot above, I have transferred the Run, Previous, and Next items so far.) If I could simply grab the entire menu from the Groovy Console and stick it in the TopComponent, I would rather do that, but grabbing the menu seems impossible. However, the real problem is that even though I can press Ctrl-Enter successfully (i.e., the script executes with the required result), I don't know what to call from the Java code. In contrast, for example, to the "Previous" and "Next" buttons, which call Console.historyPrev and Console.historyNext respectively, there doesn't appear to be something like Console.run(inputArea). One would think that Console.runScript would help, but I haven't figured out how to use that, despite googling and crying and so on. I've tried playing with Robot.keyPress, suggested by Michel Graciano on the firstname.lastname@example.org mailing list, but it hasn't worked for me so far. Therefore, this is currently another big problem for this embedded Groovy Console solution.
However, in combination with the Schliemann solution that I blogged about in the past few days, it seems clear that this embedded Groovy Console points to the possibility of there being good support for Groovy in NetBeans IDE. And, if someone can help me out with the above problems, I would be extremely thankful.
In other news. Quick! Don't miss this opportunity... here on Amazon.com you can now buy the NetBeans Platform book together with "Harry Potter and the Deathly Hallows (Book 7)". Can life get any better than this?