+++ /dev/null
-/*\r
-Jazzy - a Java library for Spell Checking\r
-Copyright (C) 2001 Mindaugas Idzelis\r
-Full text of license can be found in LICENSE.txt\r
-\r
-This library is free software; you can redistribute it and/or\r
-modify it under the terms of the GNU Lesser General Public\r
-License as published by the Free Software Foundation; either\r
-version 2.1 of the License, or (at your option) any later version.\r
-\r
-This library is distributed in the hope that it will be useful,\r
-but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\r
-Lesser General Public License for more details.\r
-\r
-You should have received a copy of the GNU Lesser General Public\r
-License along with this library; if not, write to the Free Software\r
-Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\r
-*/\r
-package com.swabunga.spell.swing.autospell;\r
-\r
-import java.awt.Point;\r
-import java.awt.event.ActionEvent;\r
-import java.awt.event.ActionListener;\r
-import java.awt.event.MouseAdapter;\r
-import java.awt.event.MouseEvent;\r
-import java.util.ResourceBundle;\r
-\r
-import javax.swing.JEditorPane;\r
-import javax.swing.JMenuItem;\r
-import javax.swing.JPopupMenu;\r
-import javax.swing.SwingUtilities;\r
-import javax.swing.event.DocumentEvent;\r
-import javax.swing.event.DocumentListener;\r
-import javax.swing.text.AttributeSet;\r
-import javax.swing.text.Document;\r
-import javax.swing.text.Element;\r
-import javax.swing.text.Segment;\r
-import javax.swing.text.SimpleAttributeSet;\r
-import javax.swing.text.StyledDocument;\r
-\r
-import com.swabunga.spell.engine.Configuration;\r
-import com.swabunga.spell.event.DocumentWordTokenizer;\r
-import com.swabunga.spell.event.SpellChecker;\r
-\r
-/**\r
- * This class handles the actual autospelling by implementing some listeners\r
- * on the spellchecked JEditorPane and Document.\r
- * \r
- * @author Robert Gustavsson (robert@lindesign.se)\r
- *\r
- */\r
-public class AutoSpellCheckHandler extends MouseAdapter implements DocumentListener, \r
- AutoSpellConstants{\r
-\r
- private SpellChecker sCheck=null;\r
- private final Configuration config = Configuration.getConfiguration();\r
- private ResourceBundle messages=null;\r
- \r
- public AutoSpellCheckHandler(SpellChecker sc){\r
- sCheck=sc;\r
- }\r
- \r
- public AutoSpellCheckHandler(SpellChecker sc, ResourceBundle rs){\r
- this(sc);\r
- messages=rs;\r
- }\r
- \r
- public void addJEditorPane(JEditorPane pane){\r
- StyledDocument doc=(StyledDocument)pane.getDocument();\r
- markupSpelling(doc, 0, doc.getLength()-1);\r
- doc.addDocumentListener(this);\r
- pane.addMouseListener(this);\r
- }\r
- \r
- public void removeJEditorPane(JEditorPane pane){\r
- Document doc=pane.getDocument();\r
- pane.removeMouseListener(this);\r
- doc.removeDocumentListener(this);\r
- }\r
- \r
- private void markupSpelling(StyledDocument doc, int start, int end){\r
- int wordStart=-1,\r
- wordEnd=-1;\r
- String word;\r
- DocumentWordTokenizer docTok;\r
- @SuppressWarnings("unused")\r
- Segment seg=new Segment();\r
- \r
- docTok=new DocumentWordTokenizer(doc);\r
- if(start>0){\r
- docTok.posStartFullWordFrom(start);\r
- }\r
- \r
- while(docTok.hasMoreWords() && docTok.getCurrentWordPosition()<=end){\r
- word=docTok.nextWord();\r
- wordStart=docTok.getCurrentWordPosition();\r
- \r
- // Mark non word parts (spaces) as correct\r
- if(wordEnd!=-1){\r
- //System.out.println("Space:"+wordEnd+","+wordStart);\r
- markAsCorrect(doc, wordEnd, wordStart);\r
- }\r
- wordEnd=docTok.getCurrentWordEnd();\r
- \r
- if(wordEnd>doc.getLength())\r
- wordEnd=doc.getLength()-1;\r
- if(wordStart>=wordEnd)\r
- continue;\r
- //System.out.println("Word:"+wordStart+","+wordEnd);\r
- if(sCheck.isCorrect(word) || sCheck.isIgnored(word)){\r
- markAsCorrect(doc, wordStart, wordEnd);\r
- }else{\r
- markAsMisspelled(doc, wordStart, wordEnd);\r
- }\r
- }\r
- // Mark the rest (if any) as correct.\r
- if(wordEnd<end && wordEnd!=-1){\r
- //System.out.println("End:"+wordEnd+","+end);\r
- markAsCorrect(doc, wordEnd, end);\r
- }\r
- }\r
- \r
- private void markAsMisspelled(StyledDocument doc, int start, int end){\r
- SimpleAttributeSet attr;\r
- attr=new SimpleAttributeSet();\r
- attr.addAttribute(wordMisspelled, wordMisspelledTrue);\r
- doc.setCharacterAttributes(start, end-start, attr, false);\r
- }\r
- \r
- private void markAsCorrect(StyledDocument doc, int start, int end){\r
- SimpleAttributeSet attr;\r
- attr=new SimpleAttributeSet(doc.getCharacterElement((start+end)/2).getAttributes());\r
- attr.removeAttribute(wordMisspelled);\r
- if(end>=start)\r
- doc.setCharacterAttributes(start, end-start, attr, true);\r
- }\r
- \r
- private void handleDocumentChange(DocumentEvent evt){\r
- Element curElem,\r
- parElem;\r
- StyledDocument doc;\r
- int start,\r
- end;\r
- \r
- if(evt.getDocument() instanceof StyledDocument){\r
- doc=(StyledDocument)evt.getDocument();\r
- curElem=doc.getCharacterElement(evt.getOffset());\r
- parElem=curElem.getParentElement();\r
- if(parElem!=null){\r
- start=parElem.getStartOffset();\r
- end=parElem.getEndOffset();\r
- }else{\r
- start=curElem.getStartOffset();\r
- end=curElem.getEndOffset();\r
- }\r
- //System.out.println("curElem: "+curElem.getStartOffset()+", "+curElem.getEndOffset());\r
- //System.out.println("parElem: "+parElem.getStartOffset()+", "+parElem.getEndOffset());\r
- //System.out.println("change: "+start+", "+end);\r
- markupSpelling(doc,start, end);\r
- } \r
- }\r
- \r
- @SuppressWarnings("unchecked")\r
- private void showSuggestionPopup(JEditorPane pane, Point p){\r
- StyledDocument doc;\r
- JMenuItem item;\r
- AttributeSet attr;\r
- int pos = pane.viewToModel(p);\r
- DocumentWordTokenizer docTok;\r
- String word;\r
- java.util.List suggestions;\r
- JPopupMenu popup;\r
- ReplaceListener repList;\r
- \r
- if (pos >= 0) {\r
- doc=(StyledDocument)pane.getDocument();\r
- attr=doc.getCharacterElement(pos).getAttributes();\r
- if(attr.containsAttribute(wordMisspelled, wordMisspelledTrue)){\r
- docTok=new DocumentWordTokenizer(doc);\r
- docTok.posStartFullWordFrom(pos);\r
- word=docTok.nextWord();\r
- suggestions=sCheck.getSuggestions(word, config.getInteger(Configuration.SPELL_THRESHOLD));\r
- \r
- popup=new JPopupMenu();\r
- repList=new ReplaceListener(docTok);\r
- for(int i=0;i<suggestions.size();i++) {\r
- com.swabunga.spell.engine.Word w = (com.swabunga.spell.engine.Word) suggestions.get(i);\r
- item = new JMenuItem(w.getWord());\r
- item.setActionCommand(w.getWord());\r
- item.addActionListener(repList);\r
- popup.add(item);\r
- }\r
- popup.addSeparator();\r
- item = new JMenuItem();\r
- if(messages!=null)\r
- item.setText(messages.getString("IGNOREALL"));\r
- else\r
- item.setText("Ignore All");\r
- item.setActionCommand(word);\r
- item.addActionListener(new IgnoreAllListener(doc));\r
- popup.add(item);\r
- item = new JMenuItem();\r
- if(messages!=null)\r
- item.setText(messages.getString("ADD"));\r
- else\r
- item.setText("Add word to wordlist");\r
- item.setActionCommand(word);\r
- item.addActionListener(new AddToDictListener(doc));\r
- popup.add(item);\r
- popup.show(pane, p.x, p.y); \r
- }\r
- }\r
- }\r
- \r
- // DocumentListener implementation\r
- // ------------------------------------------------------------------\r
- public void changedUpdate(DocumentEvent evt){\r
- }\r
- \r
- public void insertUpdate(DocumentEvent evt){\r
- Runnable r=new SpellCheckChange(evt);\r
- SwingUtilities.invokeLater(r);\r
- }\r
- \r
- public void removeUpdate(DocumentEvent evt){\r
- Runnable r=new SpellCheckChange(evt);\r
- SwingUtilities.invokeLater(r);\r
- }\r
- \r
- // MouseListener implementation\r
- // ------------------------------------------------------------------\r
- @Override\r
- public void mouseReleased(MouseEvent evt){\r
- JEditorPane pane;\r
- if(!(evt.getComponent() instanceof JEditorPane))\r
- return;\r
- \r
- if(evt.isPopupTrigger()){\r
- pane=(JEditorPane)evt.getComponent();\r
- if(pane.isEditable())\r
- showSuggestionPopup(pane, new Point(evt.getX(), evt.getY()));\r
- }\r
- }\r
- \r
- // INNER CLASSES\r
- // ------------------------------------------------------------------\r
- private class SpellCheckChange implements Runnable{\r
- \r
- private final DocumentEvent evt;\r
- \r
- public SpellCheckChange(DocumentEvent evt){\r
- this.evt=evt;\r
- }\r
- \r
- public void run(){\r
- handleDocumentChange(evt);\r
- }\r
- \r
- }\r
- \r
- private class ReplaceListener implements ActionListener{\r
- \r
- DocumentWordTokenizer tok;\r
- \r
- public ReplaceListener(DocumentWordTokenizer tok){\r
- this.tok=tok;\r
- }\r
- \r
- public void actionPerformed(ActionEvent evt){\r
- tok.replaceWord(evt.getActionCommand());\r
- }\r
- }\r
- \r
- private class AddToDictListener implements ActionListener{\r
- \r
- private final StyledDocument doc;\r
- \r
- public AddToDictListener(StyledDocument doc){\r
- this.doc=doc;\r
- }\r
- \r
- public void actionPerformed(ActionEvent evt){\r
- sCheck.addToDictionary(evt.getActionCommand());\r
- Runnable r=new MarkUpSpellingAll(doc);\r
- SwingUtilities.invokeLater(r);\r
- }\r
- }\r
- \r
- private class IgnoreAllListener implements ActionListener{\r
- \r
- private final StyledDocument doc;\r
- \r
- public IgnoreAllListener(StyledDocument doc){\r
- this.doc=doc;\r
- }\r
- \r
- public void actionPerformed(ActionEvent evt){\r
- sCheck.ignoreAll(evt.getActionCommand());\r
- Runnable r=new MarkUpSpellingAll(doc);\r
- SwingUtilities.invokeLater(r);\r
- }\r
- }\r
- \r
- private class MarkUpSpellingAll implements Runnable{\r
- \r
- private final StyledDocument doc;\r
- \r
- public MarkUpSpellingAll(StyledDocument doc){\r
- this.doc=doc;\r
- }\r
- \r
- public void run(){\r
- markupSpelling(doc,0,doc.getLength());\r
- }\r
- }\r
- \r
-}
\ No newline at end of file