RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Dynamic Scripting Lands in Europa: A Profile of Eclipse Monkey : Page 2

Bring the power of JavaScript into your Eclipse workspace and learn how to make the most of it.

Scripts Metadata
Monkey allows you to associate metadata with your scripts. You place the metadata in the first multi-line comment block of the script (/* ... */), within which you can use various keywords to declare it. The "Hello World" script already showed one keyword: Menu, which defined where the script appears in the Eclipse menu. Other supported keywords include:
  • Key to declare a key binding for the script, such as in Key: ALT+SHIFT+S
  • Listener to declare JavaScript functions that will subscribe to events raised within the Eclipse platform
  • DOM to extend the object model available to the script (More on this in the next section)

Document Object Models
There is a fundamental difference between the scripts you work with in JavaScript code and the ones you use within a web page. When working with JavaScript code, you do not have a Document Object Model (DOM) that describes the context the script belongs to (such as the web page itself in the case of scripts embedded in HTML code). You are reusing the language (JavaScript) but not the data structures (DOM) that are typical of the web development environment.

This implies that traversing object graphs and accessing the data structures exposed by the Eclipse platform (such as the JDT compiler, the workspace resources, or the user preferences) can be quite cumbersome. You have to explicitly navigate through the multitude of interfaces, factories, and providers exposed by the Eclipse APIs.

To overcome this difficulty, Eclipse Monkey provides a standard way to expose DOMs called a Monkey DOM, an Eclipse plug-in that extends the org.eclipse.eclipsemonkey.dom extension point. This extension requires two elements:

  • An implementation of the org.eclipse.eclipsemonkey.dom.IMonkeyDOMFactory
  • The name of a script variable that refers to the DOM root and will be available within the scripts

The IMonkeyDOMFactory interface defines only one method: public Object getDOMroot(). Implementers must use this method to return the root DOM object. Scripts will then access the DOM and traverse it starting from the root object, which is accessible via the named variable declared in the extension point.

A script can import a DOM by using the DOM metadata keyword and declaring the DOM in the form http://<update-site>/<dom-plugin-ID>. The following code provides an example. (Later on you will learn why you need to provide information about update sites in your DOM declaration.)

 * Menu: Simple > My Simple Script
 * DOM: http://my.update.site.com/update/com.devx.monkey.doms
// script content omitted ...

Eclipse Monkey also provides some DOMs out of the box, such as ones that simplify access to the workspace and the resources subsystem.

For example, suppose you want to expose your scripts to JDT functions so that you can use JavaScript to inspect the compilation units and Java source files available in the workspace. First, you declare the plug-in that contributes the DOM. Here's the plugin.xml descriptor file:


The plugin descriptor points to the class JavaCoreDomFactory (shown in the listing below), which exposes the org.eclipse.jdt.core.JavaCore shared instance as the document root for this DOM.

package com.devx.monkey.doms;

import org.eclipse.eclipsemonkey.dom.IMonkeyDOMFactory;
import org.eclipse.jdt.core.JavaCore;

public class JavaCoreDomFactory 
   implements IMonkeyDOMFactory {

   public Object getDOMroot() {
      return JavaCore.getJavaCore();

Your scripts can now access the Java model of the workspace using the javacore global variable declared in the plug-in descriptor file. Here's a sample script that enumerates the packages within a Java project:

 * Menu: Simple > Package enumeration
 * This dom exposes the 'workspace' variable
 * DOM: http://download.eclipse.org/technology/
 * This dom exposes the 'javacore' variable
 * DOM: http://my.update.site.com/update/com.devx.monkey.doms
function main() {
   projectName = "YourProjectName"; 

   project = workspace.root.getProject(projectName);
   srcFolder = project.getFolder("src");
   srcroot = javacore.create(project).getPackageFragmentRoot(srcFolder);
   if (!srcroot.isOpen()) srcroot.open(null);
   pkgList = "" ;
   for each (pkg in srcroot.getChildren()) {
      pkgList += pkg.elementName + "\n" ;
   alert("List of packages for " + projectName + "\n" + pkgList);	

function alert(message) {

Figure 1 presents the script output. Notice the presence of DOM metadata keywords to import the required object models into the script.

Click to enlarge

Figure 1. The Script Output with the List of Packages Extracted from a Sample Project

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date