KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > client > widgets > jazzy > JTextComponentSpellChecker


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 JavaDoc;
12
13
14 import java.awt.*;
15 import java.io.File JavaDoc;
16 import java.io.IOException JavaDoc;
17 import java.util.Locale JavaDoc;
18 import java.util.ResourceBundle JavaDoc;
19
20 /** This class spellchecks a JTextComponent throwing up a Dialog everytime
21  * it encounters a misspelled word.
22  *
23  * @author Robert Gustavsson (robert@lindesign.se)
24  */

25
26 public class JTextComponentSpellChecker implements SpellCheckListener {
27
28 // private static final String COMPLETED="COMPLETED";
29
private String JavaDoc dialogTitle = null;
30
31   private SpellChecker spellCheck = null;
32   private JSpellDialog dlg = null;
33   private JTextComponent JavaDoc textComp = null;
34   //private ResourceBundle messages;
35

36   // Constructor
37
public JTextComponentSpellChecker(SpellDictionary dict) {
38     this(dict, null);
39   }
40
41   // Convinient Constructors, for those lazy guys.
42
public JTextComponentSpellChecker(String JavaDoc dictFile) throws IOException JavaDoc {
43     this(dictFile, null);
44   }
45
46   public JTextComponentSpellChecker(String JavaDoc dictFile, String JavaDoc title) throws IOException JavaDoc {
47     this(new SpellDictionaryHashMap(new File JavaDoc(dictFile)), title);
48   }
49
50   public JTextComponentSpellChecker(String JavaDoc dictFile, String JavaDoc phoneticFile, String JavaDoc title) throws IOException JavaDoc {
51     this(new SpellDictionaryHashMap(new File JavaDoc(dictFile), new File JavaDoc(phoneticFile)), title);
52   }
53
54   public JTextComponentSpellChecker(SpellDictionary dict, String JavaDoc title) {
55     spellCheck = new SpellChecker(dict);
56     spellCheck.addSpellCheckListener(this);
57     dialogTitle = title;
58     //messages = ResourceBundle.getBundle("com.swabunga.spell.swing.messages", Locale.getDefault());
59
}
60
61   // MEMBER METHODS
62

63   /**
64    * Set user dictionary (used when a word is added)
65    */

66   public void setUserDictionary(SpellDictionary dictionary) {
67     if (spellCheck != null)
68       spellCheck.setUserDictionary(dictionary);
69   }
70
71   private void setupDialog(JTextComponent JavaDoc textComp) {
72
73     Component JavaDoc comp = SwingUtilities.getRoot(textComp);
74
75     // Probably the most common situation efter the first time.
76
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       // Put the dialog in the middle of it's parent.
85
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 JavaDoc 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 // java.util.List suggestions = event.getSuggestions();
112
event.getSuggestions();
113     int start = event.getWordContextPosition();
114     int end = start + event.getInvalidWord().length();
115
116     // Mark the invalid word in TextComponent
117
textComp.requestFocus();
118     textComp.setCaretPosition(0);
119     textComp.setCaretPosition(start);
120     textComp.moveCaretPosition(end);
121
122     dlg.show(event);
123   }
124 }
125
Popular Tags