Class SpellChecker


  • public class SpellChecker
    extends java.lang.Object
    This class is the major class of the spell checker JOrtho (Java Orthography Checker). In the most cases this is the only class that you need to add spell checking to your application. First you need to do a one-time registration of your dictionaries. In standalone applications this can look like:
     SpellChecker.registerDictionaries( new URL("file", null, ""), "en,de", "de" );
     
    and in an applet this will look like:
     SpellChecker.registerDictionaries( getCodeBase(), "en,de", "en" );
     
    After this you can register your text component that should have the spell checker features (Highlighter, context menu, spell checking dialog). This looks like:
     JTextPane text = new JTextPane();
     SpellChecker.register( text );
     
    • Constructor Detail

      • SpellChecker

        private SpellChecker()
        There is no instance needed of SpellChecker. All methods are static.
    • Method Detail

      • setMessageHandler

        public static void setMessageHandler​(MessageHandler messageHandler)
        Set the message handler used for handling errors and information messages.
        Parameters:
        messageHandler - the new MessageHandler or null (DefaultMessageHandler will be used)
      • setCustomUIProvider

        public static void setCustomUIProvider​(CustomUIProvider customUIProvider)
        Set a CustomUIProvider. This can be used to add an expert UI provider that constructs advances UI components for an application that needs common components that are more than just default Swing components
        Parameters:
        customUIProvider - the new CustomUIProvider or null
      • registerDictionaries

        public static void registerDictionaries​(java.net.URL baseURL,
                                                java.lang.String activeLocale)
        Registers the available dictionaries. The dictionaries' URLs must have the form "dictionary_xx.xxxxx" and must be relative to the baseURL. The available languages and extension of the dictionaries is load from a configuration file. The configuration file must also relative to the baseURL and must be named dictionaries.cnf, dictionaries.properties or dictionaries.txt. If the dictionary of the active Locale does not exist, the first dictionary is loaded. There is only one dictionary loaded in memory at a given time. You can download the dictionary files from http://sourceforge.net/projects/jortho/files/Dictionaries/ The configuration file has a Java Properties format. Currently there are the follow options:
        • languages
        • extension
        Samples:
         // Load the configuration and dictionaries from the current working directory and use the current locale or the first language as default 
         SpellChecker.registerDictionaries( null, null );
         
         // Load the configuration and dictionaries from the sub directory "dict"
         SpellChecker.registerDictionaries( new URL( "file", null, "dict" ), null );
         
         // Load the configuration and dictionaries from a web server and activate English as language 
         SpellChecker.registerDictionaries( new URL( "http://MyWebServer/dictionries/" ), "en" );
         
         // Load the configuration and dictionaries from the same web location like the applet and use the German dictionary as default 
         SpellChecker.registerDictionaries( myApplet.getCodeBase(), "de" );
         
         // Sample content from a file dictionaries.cnf
         extension=.ortho
         languages=de,en,it,fr,es,ru
         
        Parameters:
        baseURL - the base URL where the dictionaries and configuration file can be found. If null then first in the classloader root is searched. After it the URL("file", null, "") is used which is equals to the current working directory.
        activeLocale - the locale that should be loaded and made active. If null or empty then the default locale is used.
        See Also:
        setUserDictionaryProvider(UserDictionaryProvider), registerDictionaries(URL, String, String), registerDictionaries(URL, String, String, String)
      • registerDictionaries

        public static void registerDictionaries​(java.net.URL baseURL,
                                                java.lang.String availableLocales,
                                                java.lang.String activeLocale)
        Registers the available dictionaries. The dictionaries' URLs must have the form "dictionary_xx.ortho" and must be relative to the baseURL. If the dictionary of the active Locale does not exist, the first dictionary is loaded. There is only one dictionary loaded in memory at a given time. You can download the dictionary files from http://sourceforge.net/projects/jortho/files/Dictionaries/

        Samples:

         // Load the dictionaries from the current working directory and use the current locale or the first language as default 
         SpellChecker.registerDictionaries( null, "de,en", null );
         
         // Load the dictionaries from the sub directory "dict"
         SpellChecker.registerDictionaries( new URL( "file", null, "dict" ), "de,en", null );
         
         // Load the dictionaries from a web server and activate English as language 
         SpellChecker.registerDictionaries( new URL( "http://MyWebServer/dictionries/" ), "de,en", "en" );
         
         // Load the dictionaries from the same web location like the applet and use the German dictionary as default 
         SpellChecker.registerDictionaries( myApplet.getCodeBase(), "de,en", "de" );
         
         
        Parameters:
        baseURL - the base URL where the dictionaries can be found. If null then URL("file", null, "") is used.
        availableLocales - a comma separated list of locales
        activeLocale - the locale that should be loaded and made active. If null or empty then the default locale is used.
        See Also:
        setUserDictionaryProvider(UserDictionaryProvider), registerDictionaries(URL, String), registerDictionaries(URL, String, String, String)
      • registerDictionaries

        public static void registerDictionaries​(java.net.URL baseURL,
                                                java.lang.String availableLocales,
                                                java.lang.String activeLocale,
                                                java.lang.String extension)
        Registers the available dictionaries. The dictionaries' URLs must have the form "dictionary_xx.xxxxx" and must be relative to the baseURL. The extension can be set via parameter. If the dictionary of the active Locale does not exist, the first dictionary is loaded. There is only one dictionary loaded in memory at a given time. You can download the dictionary files from http://sourceforge.net/projects/jortho/files/Dictionaries/

        Samples:

         // Load the dictionaries from the current working directory 
         // and use the current locale or the first language as default.
         // The dictionaries must be named dictionary_de.ortho and dictionary_en.ortho
         SpellChecker.registerDictionaries( null, "de,en", null, ".ortho" );
         
         // Load the dictionaries from the sub directory "dict"
         // and use the current locale or the first language as default.
         // The dictionaries must be named dict/dictionary_de.ortho and dict/dictionary_en.ortho
         SpellChecker.registerDictionaries( new URL( "file", null, "dict" ), "de,en", null, ".ortho" );
         
         // Load the dictionaries from a web server and activate English as language 
         // The dictionaries must be named http://MyWebServer/dictionries/dictionary_de.bin and http://MyWebServer/dictionries/dictionary_en.bin
         SpellChecker.registerDictionaries( new URL( "http://MyWebServer/dictionries/" ), "de,en", "en", ".bin" );
         
         // Load the configuration from the same web location like the applet and use the German dictionary as default 
         // The dictionaries must be named dictionary_de.bin and dictionary_en.bin in the codebase
         SpellChecker.registerDictionaries( myApplet.getCodeBase(), "de,en", "de", ".bin" );
         
         
        Parameters:
        baseURL - the base URL where the dictionaries can be found. If null then URL("file", null, "") is used.
        availableLocales - a comma separated list of locales
        activeLocale - the locale that should be loaded and made active. If null or empty then the default locale is used.
        extension - the file extension of the dictionaries. Some web server like the IIS6 does not support the default ".ortho".
        See Also:
        setUserDictionaryProvider(UserDictionaryProvider), registerDictionaries(URL, String), registerDictionaries(URL, String, String)
      • register

        public static void register​(javax.swing.text.JTextComponent text)
                             throws java.lang.NullPointerException
        Activate the spell checker for the given JTextComponent. The call is equal to register( text, true, true ).
        Parameters:
        text - the JTextComponent
        Throws:
        java.lang.NullPointerException - if text is null
      • register

        public static void register​(javax.swing.text.JTextComponent text,
                                    boolean hasPopup,
                                    boolean submenu,
                                    boolean hasShortKey,
                                    boolean hasAutoSpell)
                             throws java.lang.NullPointerException
        Activates the spell checker for the given JTextComponent. You do not need to unregister if the JTextComponent is not needed anymore.
        Parameters:
        text - the JTextComponent
        hasPopup - if true, the JTextComponent is to have a popup menu with the menu item "Orthography" and "Languages".
        submenu - if true, the popup has a sub menu
        hasShortKey - if true, pressing the F7 key will display the spell check dialog.
        hasAutoSpell - if true, the JTextComponent has a auto spell checking.
        Throws:
        java.lang.NullPointerException - if text is null
      • unregister

        public static void unregister​(javax.swing.text.JTextComponent text)
        Removes all spell checker features from the JTextComponent. This does not need to be called if the text component is no longer needed.
        Parameters:
        text - the JTextComponent
      • enableShortKey

        public static void enableShortKey​(javax.swing.text.JTextComponent text,
                                          boolean enable)
        Enable or disable the F7 key. Pressing the F7 key will display the spell check dialog. This also register an Action with the name "spell-checking".
        Parameters:
        text - the JTextComponent that should change
        enable - true, enable the feature.
      • enableShortKey

        public static void enableShortKey​(javax.swing.text.JTextComponent text,
                                          boolean enable,
                                          SpellCheckerOptions options)
        Enable or disable the F7 key. Pressing the F7 key will display the spell check dialog. This also register an Action with the name "spell-checking".
        Parameters:
        text - the JTextComponent that should change
        enable - true, enable the feature.
        options - override the default options for this menu.
      • showSpellCheckerDialog

        public static void showSpellCheckerDialog​(javax.swing.text.JTextComponent text,
                                                  SpellCheckerOptions options)
        Show the Spell Checker dialog for the given JTextComponent. It will be do nothing if the JTextComponent is not editable or there are no dictionary loaded. The action for this method can you receive via:
         Action action = text.getActionMap().get("spell-checking");
         
        The action is only available if you have enable the short key (F7).
        Parameters:
        text - JTextComponent to check
        options - override the default options for this menu.
      • enablePopup

        public static void enablePopup​(javax.swing.text.JTextComponent text,
                                       boolean enable,
                                       boolean submenu)
        Enable or disable the popup menu with the menu item "Orthography" and "Languages" or only suggestion.
        Parameters:
        text - the JTextComponent that should change
        submenu - true, menu item "Orthography" and "Languages"; false, only suggestions
        enable - true, enable the feature.
      • enableAutoSpell

        public static void enableAutoSpell​(javax.swing.text.JTextComponent text,
                                           boolean enable)
        Enable or disable the auto spell checking feature (red zigzag line) for a text component. If you change the document then you need to reenable it.
        Parameters:
        text - the JTextComponent that should change
        enable - true, enable the feature.
      • enableAutoSpell

        public static void enableAutoSpell​(javax.swing.text.JTextComponent text,
                                           boolean enable,
                                           SpellCheckerOptions options)
        Enable or disable the auto spell checking feature (red zigzag line) for a text component. If you change the document then you need to reenable it.
        Parameters:
        text - the JTextComponent that should change
        enable - true, enable the feature.
        options - override the default options for this menu.
      • addLanguageChangeLister

        public static void addLanguageChangeLister​(LanguageChangeListener listener)
        Adds a LanguageChangeListener. You do not need to remove it if the LanguageChangeListener is not needed anymore. You need a hard reference to the listener because the SpellChecker hold only a WeakReference.
        Parameters:
        listener - listener to add
        See Also:
        LanguageChangeListener
      • removeLanguageChangeLister

        public static void removeLanguageChangeLister​(LanguageChangeListener listener)
        Removes the LanguageChangeListener.
        Parameters:
        listener - listener to remove
      • fireLanguageChanged

        private static void fireLanguageChanged​(java.util.Locale oldLocale)
        Helper method to fire an Language change event.
      • createCheckerMenu

        public static javax.swing.JMenu createCheckerMenu()
        Creates a menu item "Orthography" (or the equivalent depending on the user language) with a sub-menu that includes suggestions for a correct spelling. You can use this to add this menu item to your own popup.
        Returns:
        the new menu.
      • createCheckerMenu

        public static javax.swing.JMenu createCheckerMenu​(SpellCheckerOptions options)
        Creates a menu item "Orthography" (or the equivalent depending on the user language) with a sub-menu that includes suggestions for a correct spelling. You can use this to add this menu item to your own popup.
        Parameters:
        options - override the default options for this menu.
        Returns:
        the new menu.
      • createCheckerPopup

        public static javax.swing.JPopupMenu createCheckerPopup()
        Create a dynamic JPopupMenu with a list of suggestion. You can use the follow code sequence:
        
         JPopupMenu popup = SpellChecker.createCheckerPopup();
         text.addMouseListener( new PopupListener(popup) );
         
        Returns:
        the new JPopupMenu.
        See Also:
        createCheckerMenu()
      • createCheckerPopup

        public static javax.swing.JPopupMenu createCheckerPopup​(SpellCheckerOptions options)
        Create a dynamic JPopupMenu with a list of suggestion. You can use the follow code sequence:
        
         JPopupMenu popup = SpellChecker.createCheckerPopup( null );
         text.addMouseListener( new PopupListener(popup) );
         
        Returns:
        the new JPopupMenu.
        See Also:
        createCheckerMenu(SpellCheckerOptions)
      • createLanguagesMenu

        public static javax.swing.JMenu createLanguagesMenu()
        Creates a menu item "Languages" (or the equivalent depending on the user language) with a sub-menu that lists all available dictionary languages. You can use this to add this menu item to your own popup or to your menu bar.
         JPopupMenu popup = new JPopupMenu();
         popup.add( SpellChecker.createLanguagesMenu() );
         
        Returns:
        the new menu.
      • createLanguagesMenu

        public static javax.swing.JMenu createLanguagesMenu​(SpellCheckerOptions options)
        Creates a menu item "Languages" (or the equivalent depending on the user language) with a sub-menu that lists all available dictionary languages. You can use this to add this menu item to your own popup or to your menu bar.
         JPopupMenu popup = new JPopupMenu();
         popup.add( SpellChecker.createLanguagesMenu() );
         
        Parameters:
        options - override the default options for this menu.
        Returns:
        the new menu.
      • getCurrentDictionary

        static Dictionary getCurrentDictionary()
        Get the current Dictionary. The current dictionary will be set if the user one select or on calling registerDictionaries.
        Returns:
        the current Dictionary or null if not set.
        See Also:
        registerDictionaries(URL, String, String)
      • getCurrentLocale

        public static java.util.Locale getCurrentLocale()
        Gets the current Locale. The current Locale will be set if the user selects one, or when calling registerDictionaries.
        Returns:
        the current Locale or null if none is set.
        See Also:
        registerDictionaries(URL, String, String), isDictionaryLoaded()
      • setCurrentLocale

        public static void setCurrentLocale​(java.util.Locale locale)
                                     throws java.lang.IllegalArgumentException
        Set the current Locale. The call is asynchronous.
        Parameters:
        locale - the new locale, must be registered.
        Throws:
        java.lang.IllegalArgumentException - if the locale was not registered as available.
        See Also:
        registerDictionaries(URL, String, String), isDictionaryLoaded(), getCurrentLocale()
      • isDictionaryLoaded

        public static boolean isDictionaryLoaded()
        If currently a Dictionary is loaded.
        Returns:
        true, if a dictionary is loaded and include at minimum one word.
      • setApplicationName

        public static void setApplicationName​(java.lang.String name)
        Set the title of your application. This valuse is used as title for info boxes (JOptionPane). If not set then the translated "Spelling" is used.
      • getApplicationName

        public static java.lang.String getApplicationName()
        Get the title of your application.
      • getOptions

        public static SpellCheckerOptions getOptions()
        Get the default SpellCheckerOptions. This object is a singleton. That there is no set method.
        Returns:
        the default SpellCheckerOptions