Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

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

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


advertisement
"Shareability"
One of the principal aspects of Eclipse Monkey involves the ability to share scripts among developers. The goal is to facilitate and simplify the diffusion of scripts that developers can acquire from the web and integrate into their workspace in seconds.

Eclipse Monkey offers two mechanisms to achieve this. First, you can right click on a script and choose "Copy for publication" to copy the script to the clipboard in a format that you can publish to a variety of destinations (e-mails, blogs, bugzilla entries, etc.). Eclipse Monkey will handle the conversions required by the selected destination, such as converting the script into valid HTML content if you are targeting a web page or blog entry. In the same way, Eclipse Monkey can extract the contents of your clipboard (where you may have copied a web page that contains a script published by another developer) and convert them back into scripts.

Secondly, Eclipse Monkey automatically downloads DOMs from the web if you still don't have them on your local machine. By analyzing information from the DOM metadata within the script, Eclipse Monkey can extract the update site the DOM comes from and fire up the Eclipse Update Manager to download it.



Use Case: Counting Lines of Code (LOCs)
Consider an example: creating a script that extracts some metrics from the projects that are in your Eclipse workspace and then displays them in a nice graphical way. The script will count the lines of code (LOCs) in one of your project's source files and display them in a pie chart grouped by package. (Find the whole example in the source code attached to this article.)

To analyze source files, the script reuses the javacore DOM described previously. To create the charts, it uses a custom DOM that wraps the open source JFreeChart library (other charting libraries should work fine too). The DOM is exposed with the jfreechart script variable and offers methods to create a new dataset for pie charts and show charts on the screen. Here's the complete script where these methods are invoked:

/* * Menu: SLOC > SLOC by Package * DOM: http://download.eclipse.org/technology/ dash/update/org.eclipse.eclipsemonkey.doms * DOM: http://my.update.site.com/update/com.devx.monkey.doms */ var text = "" ; var i = 0 ; function main() { project = workspace.root.getProject("com.devx.monkey.doms"); srcFolder = project.getFolder("src"); srcroot = javacore.create(project).getPackageFragmentRoot(srcFolder); p = jfreechart.dataSet.newPie(); // frg iterates over the packages of the project for each (frg in openIfNeeded(srcroot).getChildren()) { count = 0; // cu iterates over the compilation units (source files) for each( cu in openIfNeeded(frg).getCompilationUnits()){ source = cu.openable.buffer.contents; sr = new Packages.java.io.StringReader(source); lr = new Packages.java.io.LineNumberReader(sr); while ( lr.readLine() != null) {} count += lr.getLineNumber() ; lr.close(); } if (count != 0) { // store the value into the chart p.setValue(frg.elementName + "(" + count + ")",count); } } chart = jfreechart.createPieChart3D("SLOC Chart",p); jfreechart.show(chart); } function openIfNeeded(o) { if (!o.isOpen()) o.open(null); return o; }

The calls to the methods jfreechart.dataset.newPie() and jfreechart.createPieChart3D() map to objects exposed through the DOM. The objects ultimately belong to the JFreeChart library, as shown here:

public JFreeChart createPieChart3D(String title, PieDataset dataset) { JFreeChart chart = ChartFactory.createPieChart3D( title, dataset, false, false, false); return chart; }

The method jfreechart.show(), invoked at the end of the script, triggers the update() method of a custom Eclipse View that displays the chart. The following listing contains the relevant parts of the View source code:

public class ChartView extends ViewPart implements Observer { // ChartComposite is a SWT widget to display charts // provided by JFreechart private ChartComposite chartComposite ; private Composite parent; private Label l; @Override public void createPartControl(Composite parent) { this.parent = parent ; l = new Label(parent, SWT.NONE); l.setText("No chart to display"); } public void update(Observable observable, Object obj) { final JFreeChart chart = (JFreeChart) obj ; if (chartComposite == null) { l.dispose(); chartComposite = new ChartComposite(parent, SWT.NONE); chartComposite.setLayoutData( new GridData(GridData.FILL_BOTH)); } chartComposite.setChart(chart); chartComposite.redraw(); parent.layout(); } // other non-relevant parts omitted... }

Figure 2 shows the final result of the example.

Click to enlarge

Figure 2. This Chart Counts Lines of Code Shown on the Workbench

Extend Eclipse with Your Own Scripts
With Eclipse Monkey, you can integrate interpreted scripts into Eclipse and leverage the benefits of an interpreted programming language to contribute components to the platform. By using a try-test-retry approach in developing new tools for your workspace, you avoid the complexities of Eclipse plug-in development as well as long debugging sessions. This is especially useful for quick and dirty tools like the ones shown in the article's examples. In addition, Eclipse Monkey allows you to extend the base Eclipse offering with additional DOMs and distribute your scripts without any effort from other colleagues and developers.



Riccardo Govoni has been working since 2003 as a J2EE developer on a variety of applications ranging from financial services to middleware in the IP television field. He currently works for Google as a software engineer. He is a Sun Certified Java Programmer (SCJP).
Comment and Contribute

 

 

 

 

 


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

 

 

Sitemap