Getting started with a DLTK project
For the sake of simplicity, this tutorial integrates all the Octave IDE functionality into a single plug-in. Generally, you implement a feature using multiple plug-ins: one containing core classes, one containing UI classes, one containing debug classes, and so on. But this tutorial's code only needs one plug-in, so if you have Eclipse running, click File > New > Project, select the Plug-in Project option, and click Next to begin creating the plug-in project. Follow the steps here to create a plug-in project that includes a sample editor, which is a good starting point for your new plug-in.
- Enter the project name, which in this example is
org.dworks.octaveide, as shown in Figure 3.
Figure 3. Entering the project's name
- In the Content screen, shown in Figure 4, enter
org.dworks.ocativeideas the ID if it's not already set, and click Next.
Figure 4. The Content screen
- Select the Create a plug-in using one of the templates check box, select Plug-in with an editor from the list, and click Next.
OctiveEditor, as shown in Figure 5, enter
mfor the File Extension, and then click Finish to create your plug-in project.
Figure 5. Entering the information for the editor
Your new plug-in project is created with the plugin.xml configuration file already available in your project, and you're off to a good start. You can edit the plugin.xml configuration using the built-in editor, or you can edit the source of the file directly by clicking the editor's plugin.xml tab. This tutorial shows you the listing of the plugin.xml file itself.
Before continuing with the rest of the tutorial, you should add two
dependencies that allow your plug-in code to find the DLTK classes. In
the Dependencies tab of your plugin.xml editor, click
Add, and add
org.eclipse.dltk.ui. The current version in
this tutorial is 3.0.1.
The plugin.xml file that was started when you created your plug-in project contains information about the plug-in's extensions. These extensions tell Eclipse how the plug-in intends to contribute to the workbench. To start, the plugin.xml file needs two simple extensions: one that identifies the Octave editor and one that identifies the types of files to be edited. The first extension was already created when you created the plug-in project using the template.
Listing 1 presents the first extension, which defines characteristics of the Octave editor.
Listing 1. The Octave IDE editor extension
<extension point="org.eclipse.ui.editors"> <editor id="org.dworks.octaveide.editor.OctaveEditor" class="org.dworks.octaveide.editor.OctaveEditor" contributorClass="org.dworks.octaveide.actions.OctaveActionContributor" default="true" icon="icons/oct.gif" name="OctaveEditor"> <contentTypeBinding contentTypeId="org.dworks.octaveide.octaveContentType"> </contentTypeBinding> </editor> </extension>
This extension contains several important attributes. The
class attribute defines the primary class
that embodies the editor:
which you will create shortly. The
contributorClass attribute identifies the
class that provides actions for the editor. And the
default attribute states that the
OctaveEditor should be the default editor
for associated files. The
identifies the text that will be displayed for the editor. As defined
in the plugin.properties file, the
OctaveEditor.name property corresponds to
"Octave Script Editor."
The last supplement in the extension,
contentTypeBinding, requires explanation.
Eclipse maintains a registry of content types that represent file
formats and naming conventions. By adding a
contentTypeBinding element to the editor's
extension, you can associate the editor with file suffixes, file
names, and even file aliases. But for your Octave editor, you're only
interested in *.m files, so your extension of
simple. Listing 2 shows this second extension.
Listing 2. The Octave IDE ContentType extension
<extension point="org.eclipse.core.runtime.contentTypes"> <content-type id="org.dworks.octaveide.octaveContentType" base-type="org.eclipse.core.runtime.text" file-extensions="m" name="%octaveContentType" priority="high"> </content-type> </extension>
Many more attributes are available for customizing content types, but
this declaration identifies the
content type as being text based and associated with the file suffix
states that if a file becomes associated with multiple content types,
this type should be given high priority.
A principal advantage of using the DLTK is that it's simple to
customize how IDEs work. This customization is made possible through
DLTK's language toolkit interfaces
IDLTKUILanguageToolkit, and so on). These
interfaces present methods that, when implemented, configure many
aspects of the IDE, such as parsing, color usage, user preferences,
and accessing the interpreter.
This tutorial focuses primarily on the DLTK UI, so you'll only concern
yourself with the
The methods in this interface perform one of two functions: They
either return a class that handles editor configuration or provide the
ID of a configuration object declared in plugin.xml. Seven of these
methods are listed here in Table 1.
Table 1. Methods on the IDLTKUILanguageToolkit interface
|Returns the editor's ID|
|Returns the |
|Returns the |
|Returns the IDs of the preference pages related to editor settings|
|Returns the IDs of the preference pages related to the script interpreter|
|Returns the ID of the script interpreter|
In the example code, the
OctaveUILanguageToolkit class fleshes out
these methods with objects and identifiers. The first two objects
discussed relate to the first two methods in the toolkit:
Now that you've configured the editor's extension, the first order of
business is to create the
Thanks to the DLTK, it won't require much coding: The DLTK's
ScriptEditor class (the superclass of
OctaveEditor) handles most of the editor's
operations on your behalf. All you need are a few configuration
methods. In the
OctaveEditor, the most
important of these is
ScriptTextTools class is convenient.
This central class accesses several other classes that provide the
editor's capabilities. By subclassing
ScriptTextTools, an editor can customize
how these capabilities are performed. The Octave editor creates a
provides access to the important objects shown in Table 2.
Table 2. Objects made available by OctaveTextTools
|Reads the editor text and determines partition boundaries|
|Configures the |
These objects become particularly important in implementing syntax coloring—that is, changing the color of text depending on the text's syntactic function in code. The next topic explains this feature in greater detail.