OSDN Git Service

Added primitive spell checker & English dictionaries.
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / gui / BrowserWindow.java
index a1e4c43..8ac0cc5 100644 (file)
@@ -20,6 +20,8 @@
 package cx.fbn.nevernote.gui;\r
 \r
 import java.io.File;\r
+import java.io.FileNotFoundException;\r
+import java.io.IOException;\r
 import java.net.FileNameMap;\r
 import java.net.URI;\r
 import java.net.URLConnection;\r
@@ -33,6 +35,7 @@ import java.util.Date;
 import java.util.GregorianCalendar;\r
 import java.util.HashMap;\r
 import java.util.List;\r
+import java.util.Locale;\r
 \r
 import com.evernote.edam.limits.Constants;\r
 import com.evernote.edam.type.Data;\r
@@ -41,6 +44,13 @@ import com.evernote.edam.type.Notebook;
 import com.evernote.edam.type.Resource;\r
 import com.evernote.edam.type.ResourceAttributes;\r
 import com.evernote.edam.type.Tag;\r
+import com.swabunga.spell.engine.SpellDictionary;\r
+import com.swabunga.spell.engine.SpellDictionaryHashMap;\r
+import com.swabunga.spell.engine.Word;\r
+import com.swabunga.spell.event.SpellCheckEvent;\r
+import com.swabunga.spell.event.SpellCheckListener;\r
+import com.swabunga.spell.event.SpellChecker;\r
+import com.swabunga.spell.event.StringWordTokenizer;\r
 import com.trolltech.qt.core.QByteArray;\r
 import com.trolltech.qt.core.QDataStream;\r
 import com.trolltech.qt.core.QDateTime;\r
@@ -91,6 +101,7 @@ import cx.fbn.nevernote.dialog.EnCryptDialog;
 import cx.fbn.nevernote.dialog.EnDecryptDialog;\r
 import cx.fbn.nevernote.dialog.GeoDialog;\r
 import cx.fbn.nevernote.dialog.InsertLinkDialog;\r
+import cx.fbn.nevernote.dialog.SpellCheck;\r
 import cx.fbn.nevernote.dialog.TableDialog;\r
 import cx.fbn.nevernote.dialog.TagAssign;\r
 import cx.fbn.nevernote.evernote.EnCrypt;\r
@@ -206,6 +217,44 @@ public class BrowserWindow extends QWidget {
        private final HashMap<String,Integer> previewPageList; \r
        \r
        \r
+       public static class SuggestionListener implements SpellCheckListener {\r
+               public boolean abortSpellCheck = false;\r
+               public boolean errorsFound = false;\r
+               \r
+               private final BrowserWindow parent;\r
+               public SuggestionListener(BrowserWindow parent) {\r
+                       this.parent = parent;\r
+               }\r
+               public void spellingError(SpellCheckEvent event) {\r
+                       errorsFound = true;\r
+                       SpellCheck dialog = new SpellCheck();\r
+                       dialog.setWord(event.getInvalidWord());\r
+\r
+                   List<Word> suggestions = event.getSuggestions();\r
+                   if (suggestions.isEmpty()) {\r
+                      dialog.setNoSuggestions(true);\r
+                   } else {\r
+                      dialog.setCurrentSuggestion(suggestions.get(0).getWord());\r
+                      for (int i=0; i<suggestions.size(); i++) {\r
+                         dialog.addSuggestion(suggestions.get(i).getWord());\r
+                      }\r
+                      dialog.setSelectedSuggestion(0);\r
+                   }\r
+                   dialog.exec();\r
+                   if (dialog.cancelPressed()) {\r
+                       abortSpellCheck = true;\r
+                       return;\r
+                   }\r
+                   if (dialog.replacePressed()) {\r
+                       QClipboard clipboard = QApplication.clipboard();\r
+                       clipboard.setText(dialog.getReplacementWord()); \r
+                       parent.pasteClicked();\r
+                   }\r
+                }\r
+       }\r
+\r
+       \r
+       \r
        public BrowserWindow(DatabaseConnection c) {\r
                logger = new ApplicationLogger("browser.log");\r
                logger.log(logger.HIGH, "Setting up browser");\r
@@ -892,7 +941,7 @@ public class BrowserWindow extends QWidget {
        }\r
 \r
        // Listener when PASTE is clicked\r
-       void pasteClicked() {\r
+       public void pasteClicked() {\r
                logger.log(logger.EXTREME, "Paste Clicked");\r
                if (forceTextPaste) {\r
                        pasteWithoutFormattingClicked();\r
@@ -2577,6 +2626,67 @@ public class BrowserWindow extends QWidget {
        }\r
 \r
 \r
+       // Invoke spell checker dialog\r
+       private void doSpellCheck() {\r
+\r
+               File wordList = new File(Global.getFileManager().getSpellDirPath()+Locale.getDefault()+".dic");\r
+           SpellDictionary dictionary;\r
+               try {\r
+                       dictionary = new SpellDictionaryHashMap(wordList);\r
+                       SpellChecker spellChecker = new SpellChecker(dictionary);\r
+                       SuggestionListener spellListener = new SuggestionListener(this);\r
+                       spellChecker.addSpellCheckListener(spellListener);\r
 \r
+                       String content = getBrowser().page().mainFrame().toPlainText();\r
+                       StringWordTokenizer tokenizer = new StringWordTokenizer(content);\r
+                       if (!tokenizer.hasMoreWords())\r
+                               return;\r
+                       String word = tokenizer.nextWord();\r
+                       getBrowser().page().action(WebAction.MoveToStartOfDocument);\r
+                       QWebPage.FindFlags flags = new QWebPage.FindFlags();\r
+                       flags.set(QWebPage.FindFlag.FindBackward);\r
+\r
+                       getBrowser().setFocus();\r
+                       boolean found = getBrowser().page().findText(word);\r
+                       if (!found) {\r
+                               QMessageBox.critical(this, tr("Spell Check Error"), \r
+                                               tr("An error has occurred while launching the spell check.  The most probable" +\r
+                                                               " cause is that the cursor was not at the beginning of the document.\n\n" +\r
+                                                               "Please place the cursor at the beginning & try again"));\r
+                               return;\r
+                       }\r
+                       while (found) {\r
+                               found = getBrowser().page().findText(word);\r
+                       }\r
+               \r
+                       spellChecker.checkSpelling(new StringWordTokenizer(word));\r
+                       getBrowser().setFocus();\r
+                       \r
+                       flags = new QWebPage.FindFlags();\r
+                       tokenizer = new StringWordTokenizer(content);\r
+                       \r
+                       while(tokenizer.hasMoreWords()) {\r
+                               word = tokenizer.nextWord();\r
+                               found = getBrowser().page().findText(word);\r
+                               if (found && !spellListener.abortSpellCheck) {\r
+                                       spellChecker.checkSpelling(new StringWordTokenizer(word));\r
+                                       getBrowser().setFocus();\r
+                               }\r
+                       }\r
+                       spellChecker.removeSpellCheckListener(spellListener);\r
+                       if (!spellListener.errorsFound)\r
+                               QMessageBox.information(this, tr("Spell Check Complete"), \r
+                                               tr("No spelling errors found"));\r
+               } catch (FileNotFoundException e) {\r
+                       QMessageBox.critical(this, tr("Spell Check Error"), \r
+                                       tr("Dictionary "+ Global.getFileManager().getSpellDirPath()+Locale.getDefault()+\r
+                                               ".dic was not found."));\r
+               } catch (IOException e) {\r
+                       QMessageBox.critical(this, tr("Spell Check Error"), \r
+                                       tr("Dictionary "+ Global.getFileManager().getSpellDirPath()+Locale.getDefault()+\r
+                                               ".dic is invalid."));\r
+               }\r
+\r
+    }\r
 \r
 }\r