
com.viaoa.jfc.text.spellcheck.SpellChecker Maven / Gradle / Ivy
/* Copyright 1999-2015 Vince Via [email protected]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.viaoa.jfc.text.spellcheck;
import java.util.*;
import java.io.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;
import com.viaoa.jfc.text.view.SpellCheckDialog;
import com.viaoa.util.*;
/**
* USed to add a spellchecker to a text component.
* @author vvia
*
*/
public abstract class SpellChecker {
protected static String SELECT_QUERY = "SELECT Data.word FROM Data WHERE Data.word = ";
protected static String SOUNDEX_QUERY = "SELECT Data.word FROM Data WHERE Data.soundex = ";
protected DocumentTokenizer m_tokenizer;
protected Hashtable hashIgnore;
protected SpellCheckDialog dlgSpellCheck;
protected JTextComponent editor;
public SpellChecker() {
}
private JFrame getFrame() {
JFrame frm = null;
if (editor != null) {
Object obj = SwingUtilities.getWindowAncestor(editor);
if (obj instanceof JFrame) {
frm = (JFrame) obj;
}
}
return frm;
}
public SpellCheckDialog getSpellCheckDialog() {
if (dlgSpellCheck == null) {
dlgSpellCheck = new SpellCheckDialog(getFrame(), this);
}
return dlgSpellCheck;
}
private int xStart;
private int xFinish;
public void spellCheck(JTextComponent editor) {
this.editor = editor;
hashIgnore = new Hashtable();
boolean bFlag = true;
try {
editor.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
Document doc = editor.getDocument();
int pos = editor.getCaretPosition();
pos = 0; // 20101107
m_tokenizer = new DocumentTokenizer(doc, pos);
String word, wordLowCase;
while (m_tokenizer.hasMoreTokens()) {
word = m_tokenizer.nextToken();
xStart = m_tokenizer.getStartPos();
xFinish = m_tokenizer.getEndPos();
if (xStart > xFinish) xFinish = xStart + word.length();
if (word.equals(word.toUpperCase())) continue;
if (word.length()<=1) continue;
if (OAString.hasDigits(word)) continue;
wordLowCase = word.toLowerCase();
if (hashIgnore.get(wordLowCase) != null) continue;
if (isWordFound(wordLowCase)) continue;
editor.select(xStart, xFinish);
if (!getSpellCheckDialog().suggest(word, getSuggestions(word), hashIgnore)) {
bFlag = false;
break;
}
}
editor.setCaretPosition(pos);
}
catch (Exception ex) {
ex.printStackTrace();
System.err.println("SpellChecker error: "+ex.toString());
}
if (bFlag) JOptionPane.showMessageDialog(getFrame(), "The spelling check is complete", "",JOptionPane.INFORMATION_MESSAGE);
editor.setCursor(Cursor.getDefaultCursor());
}
public void replaceSelection(String origWord, String replacement) {
// int xStart = m_tokenizer.getStartPos();
// int xFinish = m_tokenizer.getEndPos();
editor.select(xStart, xFinish);
editor.replaceSelection(replacement);
xFinish = xStart+replacement.length();
editor.select(xStart, xFinish);
m_tokenizer.setPosition(xFinish);
}
private String[] getSuggestions(String word) {
String[] ss = getMatches(word);
ss = (String[]) OAArray.add(String.class, ss, getSoundexMatches(word));
return ss;
}
public abstract void addNewWord(String word);
public abstract boolean isWordFound(String word);
/**
* This should be overwritten to supply the words that start with the string in text.
*/
public abstract String[] getMatches(String text);
/**
* This should be overwritten to supply the words that are Soundex the text.
*/
public abstract String[] getSoundexMatches(String text);
}
class DocumentTokenizer
{
protected Document m_doc;
protected Segment m_seg;
protected int m_startPos;
protected int m_endPos;
protected int m_currentPos;
protected boolean bSkipToFirst; // go to first word
public DocumentTokenizer(Document doc, int offset) {
m_doc = doc;
m_seg = new Segment();
setPosition(offset);
}
public boolean hasMoreTokens() {
return (m_currentPos < m_doc.getLength());
}
public static final char[] WORD_SEPARATORS = {' ', '\t', '\n',
'\r', '\f', '.', ',', ':', '-', ';', '(', ')', '[', ']', '{',
'}', '<', '>', '/', '|', '\\', '\'', '\"', '?', '!', ((char)160) };
public static boolean isSeparator(char ch) {
for (int k=0; k
© 2015 - 2025 Weber Informatics LLC | Privacy Policy