package com.swabunga.spell.swing;

import javax.swing.text.*;
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.util.*;
import com.swabunga.spell.event.*;
import com.swabunga.spell.engine.*;

/** This class spellchecks a JTextComponent throwing up a Dialog everytime
 *  it encounters a misspelled word.
 *
 * @author Robert Gustavsson (robert@lindesign.se)
 */

public class JTextComponentSpellChecker implements SpellCheckListener {

    public static final int SPELLCHECK_CANCEL=-1;

    private static final String COMPLETED="COMPLETED";
    private String dialogTitle=null;

    private SpellChecker spellCheck = null;
    private JSpellDialog dlg=null;
    private JTextComponent textComp=null;
    private ResourceBundle messages;
    private int spellCheckState=0;

    // Constructor
    public JTextComponentSpellChecker(SpellDictionary dict){
        this(dict, null);
    }

    // Convinient Constructors, for those lazy guys.
    public JTextComponentSpellChecker(String dictFile)
                                                        throws IOException{
        this(dictFile, null);
    }

    public JTextComponentSpellChecker(String dictFile, String title)
                                                        throws IOException{
        this(new SpellDictionary(new File(dictFile)),title);
    }

    public JTextComponentSpellChecker(String dictFile,
                                      String phoneticFile,
                                      String title)throws IOException{
        this(new SpellDictionary(new File(dictFile),
                                 new File(phoneticFile)),
             title);
    }

    public JTextComponentSpellChecker(SpellDictionary dict, String title){
        spellCheck = new SpellChecker(dict);
        spellCheck.addSpellCheckListener(this);
        dialogTitle=title;
        messages = ResourceBundle.getBundle("com.swabunga.spell.swing.messages", Locale.getDefault());
    }

    // MEMBER METHODS
    private void setupDialog(JTextComponent textComp){

        Component comp=SwingUtilities.getRoot(textComp);

        // Probably the most common situation efter the first time.
        if(dlg!=null && dlg.getOwner()==comp)
            return;

        if (comp!=null && comp instanceof Window) {
            if(comp instanceof Frame)
                dlg = new JSpellDialog((Frame)comp, dialogTitle, true);
            if(comp instanceof Dialog)
                dlg = new JSpellDialog((Dialog)comp, dialogTitle, true);
            // Put the dialog in the middle of it's parent.
            if(dlg!=null){
                Window win=(Window)comp;
                int x=(int)(win.getLocation().getX()+win.getWidth()/2-dlg.getWidth()/2);
                int y=(int)(win.getLocation().getY()+win.getHeight()/2-dlg.getHeight()/2);
                dlg.setLocation(x,y);
            }
        } else {
            dlg = new JSpellDialog((Frame)null, dialogTitle, true);
        }
    }

    /**
    * Performes the actual spellchecking and returns the number of misspelled
    * words or SPELLCHECK_CANCEL (-1) if the spellcheck was cancelled.
    */
    public synchronized int spellCheck(JTextComponent textComp){
        spellCheckState=0;
        setupDialog(textComp);
        this.textComp=textComp;

        DocumentWordTokenizer tokenizer = new DocumentWordTokenizer(textComp.getDocument());
        spellCheck.checkSpelling(tokenizer);

        textComp.requestFocus();
        textComp.setCaretPosition(0);
        this.textComp=null;
        return spellCheckState;
    }

    public void spellingError(SpellCheckEvent event) {

        java.util.List suggestions = event.getSuggestions();
        int start=event.getWordContextPosition();
        int end=start+event.getInvalidWord().length();

        // Mark the invalid word in TextComponent
        textComp.requestFocus();
        textComp.setCaretPosition(0);
        textComp.setCaretPosition(start);
        textComp.moveCaretPosition(end);

        dlg.show(event);

        spellCheckState++;
        if(event.getAction()==SpellCheckEvent.CANCEL)
            spellCheckState=SPELLCHECK_CANCEL;
    }
}

@@ -472,61 +472,8 @@
 %0D%0A%0D%0A
-    public static final int SPELLCHECK_CANCEL=-1;%0D%0A%0D%0A
     
@@ -729,44 +729,8 @@
 es;%0D
-%0A    private int spellCheckState=0;%0D
 %0A%0D%0A 
@@ -2972,176 +2972,8 @@
 %0D%0A%0D%0A
-    /**%0D%0A    * Performes the actual spellchecking and returns the number of misspelled%0D%0A    * words or SPELLCHECK_CANCEL (-1) if the spellcheck was cancelled.%0D%0A    */%0D%0A
     
@@ -3038,36 +3038,8 @@
 )%7B%0D%0A
-        spellCheckState=0;%0D%0A
     
@@ -3194,32 +3194,49 @@
 ent());%0D%0A       
+ int exitStatus =
  spellCheck.chec
@@ -3377,31 +3377,26 @@
  return 
-spellCheck
+exit
 Stat
-e
+us
 ;%0D%0A    %7D
@@ -3835,24 +3835,24 @@
 on(end);%0D%0A%0D%0A
+
         dlg.
@@ -3868,141 +3868,8 @@
 t);%0D
-%0A%0D%0A        spellCheckState++;%0D%0A        if(event.getAction()==SpellCheckEvent.CANCEL)%0D%0A            spellCheckState=SPELLCHECK_CANCEL;%0D
 %0A   

Modified the return code to return the SpellChecker.checkSpelling code