001: package com.xoetrope.carousel.langed;
002:
003: import java.io.File;
004: import java.io.IOException;
005: import java.util.Locale;
006: import java.util.ResourceBundle;
007:
008: import java.awt.Component;
009: import java.awt.Dialog;
010: import java.awt.Frame;
011: import java.awt.Window;
012: import javax.swing.SwingUtilities;
013: import javax.swing.text.JTextComponent;
014:
015: import com.swabunga.spell.engine.SpellDictionary;
016: import com.swabunga.spell.event.DocumentWordTokenizer;
017: import com.swabunga.spell.event.SpellCheckEvent;
018: import com.swabunga.spell.event.SpellCheckListener;
019: import com.swabunga.spell.event.SpellChecker;
020:
021: /** This class spellchecks a JTextComponent throwing up a Dialog everytime
022: * it encounters a misspelled word.
023: *
024: * @author Robert Gustavsson (robert@lindesign.se)
025: */
026: public class JTextComponentSpellChecker implements SpellCheckListener {
027:
028: private static final String COMPLETED = "COMPLETED";
029: private String dialogTitle = null;
030:
031: private SpellChecker spellCheck = null;
032: private JSpellDialog dlg = null;
033: private JTextComponent textComp = null;
034:
035: // Constructor
036: public JTextComponentSpellChecker(SpellDictionary dict) {
037: this (dict, null);
038: }
039:
040: // Convinient Constructors, for those lazy guys.
041: public JTextComponentSpellChecker(String dictFile)
042: throws IOException {
043: this (dictFile, null);
044: }
045:
046: public JTextComponentSpellChecker(String dictFile, String title)
047: throws IOException {
048: this (new SpellDictionary(new File(dictFile)), title);
049: }
050:
051: public JTextComponentSpellChecker(String dictFile,
052: String phoneticFile, String title) throws IOException {
053: this (new SpellDictionary(new File(dictFile), new File(
054: phoneticFile)), title);
055: }
056:
057: public JTextComponentSpellChecker(SpellDictionary dict, String title) {
058: spellCheck = new SpellChecker(dict);
059: spellCheck.addSpellCheckListener(this );
060: dialogTitle = title;
061: }
062:
063: // MEMBER METHODS
064: private void setupDialog(JTextComponent textComp) {
065:
066: Component comp = SwingUtilities.getRoot(textComp);
067:
068: // Probably the most common situation efter the first time.
069: if (dlg != null && dlg.getOwner() == comp)
070: return;
071:
072: if (comp != null && comp instanceof Window) {
073: if (comp instanceof Frame)
074: dlg = new JSpellDialog((Frame) comp, dialogTitle, true);
075: if (comp instanceof Dialog)
076: dlg = new JSpellDialog((Dialog) comp, dialogTitle, true);
077: // Put the dialog in the middle of it's parent.
078: if (dlg != null) {
079: Window win = (Window) comp;
080: int x = (int) (win.getLocation().getX()
081: + win.getWidth() / 2 - dlg.getWidth() / 2);
082: int y = (int) (win.getLocation().getY()
083: + win.getHeight() / 2 - dlg.getHeight() / 2);
084: dlg.setLocation(x, y);
085: }
086: } else {
087: dlg = new JSpellDialog((Frame) null, dialogTitle, true);
088: }
089: }
090:
091: public synchronized int spellCheck(JTextComponent textComp) {
092: setupDialog(textComp);
093: this .textComp = textComp;
094:
095: DocumentWordTokenizer tokenizer = new DocumentWordTokenizer(
096: textComp.getDocument());
097: int exitStatus = spellCheck.checkSpelling(tokenizer);
098:
099: textComp.requestFocus();
100: textComp.setCaretPosition(0);
101: this .textComp = null;
102: return exitStatus;
103: }
104:
105: public void spellingError(SpellCheckEvent event) {
106:
107: java.util.List suggestions = event.getSuggestions();
108: int start = event.getWordContextPosition();
109: int end = start + event.getInvalidWord().length();
110:
111: // Mark the invalid word in TextComponent
112: textComp.requestFocus();
113: textComp.setCaretPosition(0);
114: textComp.setCaretPosition(start);
115: textComp.moveCaretPosition(end);
116:
117: dlg.show(event);
118: }
119: }
|