Tip: Spellchecking in Eclipse

Ever wanted to add spelling checking to a dialog, form or some editor in your Eclipse-based application?

spell1

Well, I needed to do this recently for a project and thought I would share with people how it can be done (also, I had people emailing me to write more tips ;p). The spell checking infrastructure in Eclipse is handled by the text editor framework. Specifically, there’s an extension point where you can add your own spellchecking engines if you so desire:

spell2

The Java Development Tools (JDT) provides an engine by default that gets activated for things like java source files, properties files and ‘text’ files. To take advantage of this functionality, we need to hook into the text editing framework inside of Eclipse. I’ve provided a simple example to show you how you can do it, but here are the basics:

...
final SourceViewer sourceViewer = new SourceViewer(
     composite, null, null, true, SWT.MULTI | SWT.V_SCROLL | SWT.WRAP);
// grab the text widget from the source viewer
StyledText fTextField = sourceViewer.getTextWidget();
// this is where the magic happens for spellchecking
// see TextSourceViewerConfiguration#getReconciler
Document document = new Document(text);
SourceViewerConfiguration config =
     new TextSourceViewerConfiguration(EditorsUI.getPreferenceStore());
sourceViewer.configure(config);
sourceViewer.setDocument(document, annotationModel);
...

If you ever have written an editor within Eclipse, this code should look familiar to you. If not, well, welcome to text editing framework within Eclipse. You’ll notice that by default, everything is spellchecked. This is because of how the TextSourceViewerConfiguration sets up reconciliation by default (see getReconciler):

...
SpellingService spellingService= EditorsUI.getSpellingService();
IReconcilingStrategy strategy= new SpellingReconcileStrategy(sourceViewer, spellingService);
MonoReconciler reconciler= new MonoReconciler(strategy, false);
...

If you need to only spellcheck certain areas of a document, like say comment headers, you need to provide your own reconciler and partitions. Then you need to setup your partitions that you want to have a spellchecking strategy (SpellingReconcileStrategy) associated with.

5 Responses to “Tip: Spellchecking in Eclipse”

  1. Ray says:

    Thanks alot !

    Additional tipp: If you want to use this in a stand alone RCP app,
    don’t forget to include org.eclipse.jdt.ui as a dependency.
    Otherwise the DefaultSpellingEngine is missing.

  2. Marc Mazas says:

    Hi
    the example zip file gives a 404 !
    I’d like to have it !
    TIA

  3. Hi Chris,

    Can you upload back the spell example archive file.

    Thanking you in advance

    Mickael

  4. Dijan says:

    Hi,

    I don’t know if that file is still giving a 404 or again.. but I’d appreciate it, if you could bring it back online.

    Thanks in advance.

  5. SirWayne says:

    Hi,

    nothing is spellchecked in my SourceViewer =(.

    What do i need for annotationModel?
    Could you upload the sources?

    Thanks =)

5 responses so far

Written by . Published in Categories: EclipseSource News

Looking for a job?

X
Karlsruhe / Remote
JavaScript
Mobile
Karlsruhe / Victoria / Remote
Windows
Mobile
Karlsruhe / Victoria / Remote
Java
Android
Mobile