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

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


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.




Application Security Testing: An Integral Part of DevOps

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:

<plugin> <extension point="org.eclipse.eclipsemonkey.dom"> <dom class="com.devx.monkey.doms.JavaCoreDomFactory" id="com.devx.monkey.doms.JavaCoreDomFactory" variableName="javacore"> </dom> </extension> </plugin>

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/ dash/update/org.eclipse.eclipsemonkey.doms * * 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) { Packages.org.eclipse.jface.dialogs.MessageDialog.openInformation( window.getShell(), 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

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date