Chapter 3. Editing Programs

Table of Contents

Definitions Pane
Multiple Documents
Source Navigation
Predictive Input Dialogs
Detachable Tabbed Panes

DrJava's core component is an editor for writing Java source code. Like most text editors, it supports a wide range of editing features such as "Find/Replace", "Go to Line", "Go to File", while also providing more advanced features like syntax coloring, automatic indentation, brace matching, and even a limited notion of auto-completion.

Definitions Pane

The Definitions Pane is the main window of DrJava, displaying the currently active source file. As you edit files in this window, DrJava helps out with several useful features.

Syntax Coloring. DrJava colors special types of text differently to help make the structure of the program more apparent. Comments appear in green, while Java keywords and types appear in blue. Strings are colored red and characters are colored magenta, with all other text colored black. These colors are all configurable (see Configuring DrJava).

One notable difference between syntax coloring in DrJava and other common editors (such as Emacs) is that DrJava uses fully correct coloring as the document is edited. For example, simply typing the beginning of a block comment ("/*") will immediately update the coloring of the entire document, unlike some other editors which will only update the color of a line when that line is edited. Having an accurate view of the program is an important aspect of understanding its structure.

Automatic Indentation. The key to indenting code in DrJava is the Tab key. Rather than simply inserting a tab or spaces, pressing Tab properly indents the current line (or selected text) using common coding conventions. As you type multiple lines of code into the Definitions Pane, DrJava automatically indents each line using the same technique. By default, two spaces are used for each indentation level, although this can be configured in the Preferences window. (In DrJava, code is always indented with spaces, and never with actual tab characters.)

Brace Matching. To help you match open and close braces, DrJava highlights the region enclosed by a pair of braces. If you place the cursor immediately after a close brace, parenthesis, or bracket, all text between that character and the corresponding open brace is highlighted in another color. Like syntax coloring, brace matching is also done in a fully correct manner, updated with each keystroke. In addition, when the cursor is right after a closing curly brace, the line containing the matching open brace is displayed in the lower status bar.

Commenting / Uncommenting. To help you easily write multi-line comments, DrJava automatically adds spaces and an asterisk on each new line. In addition, there is an option in the "Miscellaneous" section of the Preferences window that will tell DrJava to automatically close multi-line comments for you. Commands in the Edit menu are also available to comment out or uncomment a block of selected code using winged comments ("//"). The key bindings for these commands default to Ctrl+Slash and Ctrl+Shift+Slash respectively. Commenting out a block of code will place "//" markers at the start of each line in the block, preserving the indentation of the code.

Context Menu. The Definitions Pane has a context menu, which can be used by right-clicking in the pane. (Mac users should use Ctrl+Click or Option+Click.) This menu provides shortcuts to useful features such as cut, copy, and paste, as well as indenting, commenting, and setting breakpoints and bookmarks.

Auto-Completion. DrJava supports a very limited notion of auto-completion that is nonetheless useful. This feature is accessible as "Auto-Complete Word Under Cursor" in the Edit menu, and it is also bound to the keyboard shortcut Ctrl-Shift-Space by default. When invoked, DrJava will look at the word to the left of the cursor and attempt to auto-complete it, based on a list of documents currently open. If there is no unique auto-completion match, DrJava displays a predictive input dialog with the auto-completion candidates.

When a project is open, DrJava can also be configured to scan all class files after a compile to obtain the auto-completion information (see Configuring DrJava). With that option set, DrJava can auto-complete the names of all classes in your project, even those of inner classes.

On the "Auto-Complete Word Under Cursor" diaog, there is a checkbox labeled "Java API". If this is checked, then DrJava will also use the class names from the Standard Java API, JUnit 3.8.2 and the user-specified additional libraries as suggestions for auto-completion. If it is disabled, only class names from your own source files are used.

Next to the "OK" button is the "Fully Qualified" button. If the class "Integer" is selected, and the user presses "OK", DrJava will auto-complete the word to "Integer". If, however, "Fully Qualified" is used to close the dialog, DrJava will enter the entire fully qualified class name, "java.lang.Integer" in this case.

Please note that auto-completion currently only works for class names, and completely ignores all context except for the word to the left of the cursor (i.e. it may generate code that does not compile).

Clipboard History. Any text you copy or cut out of DrJava documents will be placed in the clipboard history, and the last 10 entries are kept (that number is configurable, see Configuring DrJava). To access one of the entries in the history, use the "Paste from History" command in the Edit menu or press Ctrl+Shift+V. In the dialog that opens up, you can browse the history and select the entry to paste. In addition to inserting the text at the cursor, the selected entry will also be moved to the top of the clipboard history, and will therefore subsequently be available with the regular paste command.

The clipboard history is a great tool to minimize scrolling and document switching: Instead of going back and forth several times, you can just "copy, copy, copy" several pieces of code in a row, then go to another place in the code and do "paste, paste from history, paste from history".

Right Margin. DrJava can display a line after a specified number of columns, representing the right margin of the document. By default, this line is displayed after 120 columns. You can type past this line, and it has no effect on the saved files or executed programs, but the right margin line can help you format your source code more uniformly.

You can enable or disable the right margin line in the Display category of the Preferences window. The color of the line can be changed in the Colors category of the Preferences. (See Configuring DrJava.)