1 package org.lucane.client.widgets.jazzy; 2 3 import com.swabunga.spell.engine.SpellDictionary; 4 import com.swabunga.spell.engine.SpellDictionaryHashMap; 5 import com.swabunga.spell.event.DocumentWordTokenizer; 6 import com.swabunga.spell.event.SpellCheckEvent; 7 import com.swabunga.spell.event.SpellCheckListener; 8 import com.swabunga.spell.event.SpellChecker; 9 10 import javax.swing.*; 11 import javax.swing.text.JTextComponent ; 12 13 14 import java.awt.*; 15 import java.io.File ; 16 import java.io.IOException ; 17 import java.util.Locale ; 18 import java.util.ResourceBundle ; 19 20 25 26 public class JTextComponentSpellChecker implements SpellCheckListener { 27 28 private String dialogTitle = null; 30 31 private SpellChecker spellCheck = null; 32 private JSpellDialog dlg = null; 33 private JTextComponent textComp = null; 34 36 public JTextComponentSpellChecker(SpellDictionary dict) { 38 this(dict, null); 39 } 40 41 public JTextComponentSpellChecker(String dictFile) throws IOException { 43 this(dictFile, null); 44 } 45 46 public JTextComponentSpellChecker(String dictFile, String title) throws IOException { 47 this(new SpellDictionaryHashMap(new File (dictFile)), title); 48 } 49 50 public JTextComponentSpellChecker(String dictFile, String phoneticFile, String title) throws IOException { 51 this(new SpellDictionaryHashMap(new File (dictFile), new File (phoneticFile)), title); 52 } 53 54 public JTextComponentSpellChecker(SpellDictionary dict, String title) { 55 spellCheck = new SpellChecker(dict); 56 spellCheck.addSpellCheckListener(this); 57 dialogTitle = title; 58 } 60 61 63 66 public void setUserDictionary(SpellDictionary dictionary) { 67 if (spellCheck != null) 68 spellCheck.setUserDictionary(dictionary); 69 } 70 71 private void setupDialog(JTextComponent textComp) { 72 73 Component comp = SwingUtilities.getRoot(textComp); 74 75 if (dlg != null && dlg.getOwner() == comp) 77 return; 78 79 if (comp != null && comp instanceof Window) { 80 if (comp instanceof Frame) 81 dlg = new JSpellDialog((Frame) comp, dialogTitle, true); 82 if (comp instanceof Dialog) 83 dlg = new JSpellDialog((Dialog) comp, dialogTitle, true); 84 if (dlg != null) { 86 Window win = (Window) comp; 87 int x = (int) (win.getLocation().getX() + win.getWidth() / 2 - dlg.getWidth() / 2); 88 int y = (int) (win.getLocation().getY() + win.getHeight() / 2 - dlg.getHeight() / 2); 89 dlg.setLocation(x, y); 90 } 91 } else { 92 dlg = new JSpellDialog((Frame) null, dialogTitle, true); 93 } 94 } 95 96 public synchronized int spellCheck(JTextComponent textComp) { 97 setupDialog(textComp); 98 this.textComp = textComp; 99 100 DocumentWordTokenizer tokenizer = new DocumentWordTokenizer(textComp.getDocument()); 101 int exitStatus = spellCheck.checkSpelling(tokenizer); 102 103 textComp.requestFocus(); 104 textComp.setCaretPosition(0); 105 this.textComp = null; 106 return exitStatus; 107 } 108 109 public void spellingError(SpellCheckEvent event) { 110 111 event.getSuggestions(); 113 int start = event.getWordContextPosition(); 114 int end = start + event.getInvalidWord().length(); 115 116 textComp.requestFocus(); 118 textComp.setCaretPosition(0); 119 textComp.setCaretPosition(start); 120 textComp.moveCaretPosition(end); 121 122 dlg.show(event); 123 } 124 } 125 | Popular Tags |