Montag, 18. April 2011

Xtext CellEditor integration

Recently, I wrote an article for the German Eclipse magazin together with my colleague Alexander about embedding an Xtext editor into a GMF editor. Although GMF comes with an (quite complex) API for integrating parsers and code completion, there are several good reasons to integrate Xtext to do the job.
Apart from syntactical parsing and code completion, Xtext offers syntax highlighing, cross-referencing, custom validation, and quickfixes to name just a few.


Especially when using complex statements like expression languages within a diagram, Xtext can help you building comfortable and robust editors, as you can see in the upcoming version 2.0 of the YAKINDU Statechart Tools I am currently working on. In this project, we integrate Xtext to specify state behavior among other things as you can see in the following screenshot.







 From the technical point of view, integrating Xtext in your diagram editor is straight forward with the API provided. All you have to do is to extend the abstract base class XtextLabelEditPart that defines two methods to set and receive the edit string, and one method to receive an instance of DirectEditManager.
This method can be implemented as follows:

@Override
    protected DirectEditManager createXTextDirectEditManager() {
        return new XtextDirectEditManager(this, getInjector(), getEditorStyles());
    }


The Guice injector used by the XTextDirectEditManager can be obtained from the Activator of the UI plug-in of your Xtext language. Possible edit styles are SWT.SINGLE or SWT.MULTI for single / multi line editors. The XtextDirectEditManager then creates an XtextCellEditor that has no dependencies to GMF. Because of this, one can use this CellEditor within a JFace StructuredViewer, as you can see in the following screenshot of a property page.


If you want to learn more about integrating Xtext into your GEF / GMF based editors you can either buy the 
 Eclipse Magazin or take a look at the YAKINDU SCT2 source code!