Skip to content

Instantly share code, notes, and snippets.

@louismrose
Created March 2, 2015 15:15
Show Gist options
  • Save louismrose/c1cc5306b27605d4cdcf to your computer and use it in GitHub Desktop.
Save louismrose/c1cc5306b27605d4cdcf to your computer and use it in GitHub Desktop.
AutoSpellChecker.java
/*
* Based on JOrtho 05.11.2005, by i-net software (GNU General Public License)
*/
package com.inet.jortho;
import java.util.Locale;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.text.Highlighter.Highlight;
/**
* This class check a <code>JTextComponent</code> automatically (in the background) for orthography. Spell error are
* highlighted with a red zigzag line.
*
* @author Volker Berlin
*/
class AutoSpellChecker implements DocumentListener, LanguageChangeListener {
private static final RedZigZagPainter painter = new RedZigZagPainter();
private final JTextComponent jText;
private final SpellCheckerOptions options;
private Dictionary dictionary;
private Locale locale;
public AutoSpellChecker(JTextComponent text, SpellCheckerOptions options){
this.jText = text;
this.options = options == null ? SpellChecker.getOptions() : options;
jText.getDocument().addDocumentListener( this );
SpellChecker.addLanguageChangeLister( this );
dictionary = SpellChecker.getCurrentDictionary();
locale = SpellChecker.getCurrentLocale();
}
/**
* Check the Elements on the given position.
*/
public void checkElements( int offset, int length ) {
int end = offset + length;
Document document = jText.getDocument();
Element element;
do{
try {
// We need to use a ParagraphElement because a CharacterElement produce problems with formating in a word
element = ((AbstractDocument)document).getParagraphElement( offset );
} catch( java.lang.Exception ex ) {
return;
}
checkElement( element );
int endOffset = element.getEndOffset();
offset = endOffset > offset ? endOffset : offset + 1;
}while( offset <= end && offset < document.getLength() );
}
/**
* Check the spelling of the text of an element.
*
* @param element
* the to checking Element
*/
private void checkElement( javax.swing.text.Element element ) {
try {
int i = element.getStartOffset();
int j = element.getEndOffset();
Highlighter highlighter = jText.getHighlighter();
Highlight[] highlights = highlighter.getHighlights();
for( int k = highlights.length; --k >= 0; ) {
Highlight highlight = highlights[k];
int hlStartOffset = highlight.getStartOffset();
int hlEndOffset = highlight.getEndOffset();
if( (i <= hlStartOffset && hlStartOffset <= j) ||
(i <= hlEndOffset && hlEndOffset <= j) ) {
if( highlight.getPainter() == painter ) {
highlighter.removeHighlight( highlight );
}
}
}
int l = ((AbstractDocument)jText.getDocument()).getLength();
j = Math.min( j, l );
if( i >= j )
return;
// prevent a NPE if the dictionary is currently not loaded.
Dictionary dic = dictionary;
Locale loc = locale;
if( dic == null || loc == null ){
return;
}
Tokenizer tok = new Tokenizer( jText, dic, loc, i, j, options );
String word;
while( (word = tok.nextInvalidWord()) != null ) {
int wordOffset = tok.getWordOffset();
highlighter.addHighlight( wordOffset, wordOffset + word.length(), painter );
}
} catch( BadLocationException e ) {
SpellChecker.getMessageHandler().handleException( e );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment