About Me

My photo
Author of Groovy modules: GBench, GProf, Co-author of a Java book パーフェクトJava, Game Developer at GREE

Monday, February 28, 2011

Groovy Monkey for a better Eclipse life

If you're a heavy Eclipse user, I recommend that you have a Groovy Monkey. The curious monkeys provide a way to add features to your Eclipse easily.


First of all, get a monkey from the update site. Though pets are expensive as far as I know, luckily, the monkeys are free :-)


Got a monkey? The monkey appears in your Eclipse menubar? Then push him to create the example project that the Groovy Monkey team provides. The project has good examples to help your understanding of Groovy Monkey.







I'll take up the simplest example of them:
----
/*
 * Menu: Open Dialog > Groovy
 * Script-Path: /GroovyMonkeyScripts/monkey/OpenDialog_Groovy.gm
 * Kudos: ervinja
 * License: EPL 1.0
 * Job: UIJob
 * DOM: http://groovy-monkey.sourceforge.net/update/plugins/net.sf.groovyMonkey.dom
 */


out.println( 'hello world from Groovy' )
org.eclipse.jface.dialogs.MessageDialog.openInformation( window.getShell(),
                                                         'Monkey Dialog',
                                                         'Hello World from Groovy' )
----
So simple, isn't it?


Groovy Monkey scripts have metadata in its header. Important metadatas in this example are Menu and Job. Menu expresses the position of the menu item to execute the script. Like this:
















Job expresses the thread to execute the script on. There are three Jobs, Job, UIJob and WorkspaceJob that are defined in Eclipse Jobs API (please refer here for details). In the example, UIJob is specified because the script accesses UI.


Do you wonder where "out" and "window" came from? These are predefined objects, called DOM (Domain Object Model). "out", called Console DOM, is a object of org.eclipse.ui.console.MessageConsoleStream to access the Eclipse console that Groovy Monkey uses. "window", called Window DOM, is a object of org.eclipse.ui.IWorkbenchWindow to access the active Eclipse window. If there are no DOMs, the example would be like the following:
----
import org.eclipse.ui.*
import org.eclipse.ui.console.*


def console = new MessageConsole('Groovy Monkey', null)
ConsolePlugin.default.consoleManager.with {
addConsoles([console] as IConsole[])
showConsoleView(console)
}
def out = console.newMessageStream()
def window = PlatformUI.workbench.activeWorkbenchWindow


out.println( 'hello world from Groovy' )
org.eclipse.jface.dialogs.MessageDialog.openInformation( window.getShell(),
                                                         'Monkey Dialog',
                                                         'Hello World from Groovy' )
----


Groovy Monkey provides more DOMs. You can see them in the Outline View or the Installed DOMs View.


I'll show one more example which I wrote. This example add a file downloading feature to your Eclipse and shows how to use workspace resources from Groovy Monkey scripts with Included metadata. To try the example, you need to get URLConnectionBuilder.
----
/*
 * Menu: Download File...
 * Script-Path: /GroovyMonkeyScripts/monkey/DownloadFile.gm
 * Kudos: Nagai Masato
 * License: EPL 1.0
 * Job: UIJob
 * Include: /GroovyMonkeyScripts/toybox/urlconnbuilder-0.0.2.jar
 */


import net.sourceforge.urlconnbuilder.*


def download = { url, filename ->
new URLConnectionBuilder().url(url) {
       connect {
           configure(requestProperties: ['User-Agent': 'George'])
           communicate(input: { conn, stream ->
               new File(filename).newOutputStream() << stream
           })
       }
}
}


shell = jface.shell(text: 'Download File') {
    def urlTxf, filenameTxf
    gridLayout(numColumns: 2)
    label(text: 'From:')
    urlTxf = text() {
        gridData(widthHint: 300)
    }
    label(text: 'To:')
    filenameTxf = text() {
        gridData(widthHint: 300)
    }
    button(text: 'Download') {
        gridData(
            horizontalSpan: 2,
            horizontalAlignment: org.eclipse.swt.layout.GridData.END
        )
        onEvent(type: 'Selection', closure: {
            download(urlTxf.text, filenameTxf.text)
            shell.close()
        })
    }
}
shell.pack()
shell.open()
----


   

2 comments:

  1. I have installed it from the update site but no monkey has been added to my eclipse, do you have any idea?

    ReplyDelete
    Replies
    1. Hi, Ali.

      I tried to install Groovy Monkey 0.6.1 to Eclipse 3.7.1 on my Windows 32bit machine and I had a success.

      Have you checked Groovy Monkey's installation status (Help > About Eclpse > Installation Details)? Has Groovy Monkey been successfully installed?

      Delete