The Java Developer's Guide to EclipseThe Java Developer's Guide to Eclipse

In Chapter 26, Building a Custom Text Editor with JFace Text, you learned how JFace Text provides a framework for creating, displaying, and editing text documents. The framework is domain-independent, extensible and rich with support for building editors of syntax-oriented text, such as programming language source, XML, HTML, and SQL. It is not surprising, then, that Eclipse’s default text editor and Java editor are built using this framework.

To begin, import the example project, com.ibm.jdg2e.editor.jfacetext.sql. It demonstrates key features of the JFace Text framework, as shown below.

SQL editor

Running the Example

  1. In the Plug-in Development perspective, launch the runtime Workbench (Run > Run As > Eclipse Application).
  2. Create a new project in the workspace of the runtime Workbench.
  3. Copy the test.sql file from the com.ibm.jdg2e.editor.jfacetext.sql project in the development (host) workspace into the new project in the runtime workspace.
  4. Open the test.sql file.
  5. Use the the content assist; type the first few letters of the SELECT keyword ("sel") and then Ctrl+Space, and the content proposals will appear.

    Content assist

  6. Type in the word where in lower case. Notice the color syntax highlighting:

    Syntax highlighting

  7. Select Edit > Content Format and notice that "where" is turned to uppercase "WHERE".
  8. Define a code folding region by selecting some of the SQL Editor's text. Then select Define Folding Region from the editor's context menu. When collapsed, the folded region is denoted by the small triangle in the left vertical ruler.

    Define folding region

Roadmap of the Example

The SQL Editor implements three editor customizations:

  1. SQL keywords are available using content assist.
  2. Using syntax highlighting, SQL keywords are colorized.
  3. The formatter folds all SQL keywords to upper case.

The implementation phases begins with the creation of an editor with only the basic text editing functions. Here are the highlights of the basic text editing implementation.

Basic Editor Classes

Class (All) Description
SQLEditor This class is responsible for configuring the SQL editor.
SQLEditorContributor Manages the installation and deinstallation of actions for the SQL editor.
SQLEditorDocumentParticipant This is the expected class for extensions provided for the org.eclipse.core.filebuffers.documentSetup extension point.
SQLEditorPlugin The main plug-in class.
SQLEditorSourceViewerConfiguration This class defines the editor add-ons; content assist, content formatter, highlighting, auto-indent strategy, double click strategy.

Content Assistant

In this part of the implementation, a content assistant provides completion proposals for SQL keywords. Here are the highlights of the contents assist implementation.

SQL Specific Implementations

Class (All) Description
ISQLSyntax Contains the SQL syntax words (upper and lower cases).
SQLCodeScanner The SQLCodeScanner is a RuleBaseScanner.This class finds SQL comments and keywords, as the user edits the document. It is "programmed" with a sequence of rules that evaluates and returns the offset and the length of the last found token.
SQLCompletionProcessor The SQL content assist processor. This content assist processor proposes completions and computes context information for a SQL content type.
SQLDoubleClickStrategy Process double clicks in the SQL content.
SQLPartitionScanner The SQLPartitionScanner is a RulesBasedPartitionScanner. The SQL document partitions are computed dynamically as events signal that the document has changed. The document partitions are based on tokens that represent comments and SQL code sections.

Syntax Highlighting

This part of the implementation adds the syntax highlighting of SQL keywords. Here are the highlights of this phase of development.

SQL Plug-in Utilities

Class (All) Description
ColorProvider Colors used in the SQL editor.
SQLWhiteSpaceDetector A class that determines if character is a SQL white space character.
SQLWordDetector Determines whether a given character is valid as part of a SQL keywords in the current context.
WordPartDetector Scans and detects parts of a word.

Content Formatting

This part of the implementation adds the content formatting of SQL keywords. Here are the highlights of this phase of development.

Formatting Specific Implementation

Class (All) Description
SQLWordStrategy The formatting strategy that converts SQL keywords to upper case.

Class Diagram

The classes involved in the solution and their relationships are shown in the figure below (click a class to see more information).

This class defines code folding regions.  See SQLEditor.java

© Copyright International Business Machines Corporation, 2003, 2004, 2006. All Rights Reserved.
Code or samples provided herein are provided without warranty of any kind.