Archived Coffee-Bytes.com Content

Scripting: Javascript to Java Integration (Part 1)

Recently, I discussed how you can compile Javascript written for the Java 6 scripting engine to make it a faster, pre-parsed script . In this tip I'd like to extend that base knowledge by discussing how you can interact with Java code with the Rhino Javascript engine.

One of the nicest features of Rhino is it's ability to call back into Java anytime you want. As an example, I'll reproduce one of the many SWT snippets (Snippet 223).

Scripting: Javascript to Java Integration (Part 2)

Last time I discussed some of the main features of Javascript to Java integration in the Java 6 scripting engine. As you get into more advanced interaction with Java, however, there are some things you have to know to 'cross over'.

Implementing Interfaces

An important component of interacting with Java is the ability to implement Java interfaces. You can pass Javascript objects masquerading as Java objects into Java code, or even return the interface as a completed variable from the script.

Scripting: Compiling Scripts in Java 6

Java 6 introduces scripting engine support in to Java, and this new support provides a link between the Java we all know and hopefully love and the dynamic, delayed binding world of scripting languages.

3.3: Non-Modal Local Refactorings

3.3 M4 includes the introduction of several 'local' refactorings as quick assists (aka Ctrl+1) that work in linked mode. There are some definitions I should supply just to make sure we're all on the same page. By 'local', I mean isolated to the currently active editor. Linked mode refers to the special boxes you get around editable areas in the editor when applying a template or some other similar task:

Java 6: Copying Typed Arrays

One of the neat tricks of the Collection.toArray(Object[]) method is that it will always return an array matching the passed in type, irrespective of whether or not the one you passed in can be used to hold all of the objects in the array. That's why so often you see this idiom:

Lucene: Escaping Special Characters

When integrating Lucene into an application so it can directly take user input, it is often valuable to use the QueryParser class. This class is a very handy user-readable-text to functional query converter; perfect for taking user input without a lot of work on your part, but if you don't properly handle special characters, it will fail with a nasty-gram exception:

Camel-Case Searching and Code Assist

Eclipse 3.2 provides extensive support for what is commonly referred to as 'Camel Case' searching, or searching via the capital letter patterns commonly used in naming conventions for classes. This means that if you want to find the class 'ArrayIndexOutOfBoundsException' I can search with 'AIOOBE', or for that matter, any shortened variant of that ('A', 'AI', 'AIO', 'AIOO', etc). Here is an example using the open type dialog (Ctrl+T):

Testing: Running Tests Against a Relational Database

Unit tests are an extremely valuable component to most application development. Generally speaking, being able to develop isolated unit tests that focus on one component of Java code is ideal.

SWT: Using the Font Dialog

Many applications have the need to provide a font-selection ability. Whether it is for a rich-style text editor, or simply so the user can customize the appearance of the application, font selection is a useful facility. Thankfully, it does come built in to SWT.

The class in question is the org.eclipse.swt.widgets.FontDialog class, and using it is quite straightforward. It is simply a matter of creating a new font dialog object, and then asking it to open.