com.inet.jortho
Class SpellChecker

java.lang.Object
  extended by com.inet.jortho.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 );
 


Method Summary
static void addLanguageChangeLister(LanguageChangeListener listener)
          Adds a LanguageChangeListener.
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.
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.
static javax.swing.JPopupMenu createCheckerPopup()
          Create a dynamic JPopupMenu with a list of suggestion.
static javax.swing.JPopupMenu createCheckerPopup(SpellCheckerOptions options)
          Create a dynamic JPopupMenu with a list of suggestion.
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.
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.
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.
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.
static void enablePopup(javax.swing.text.JTextComponent text, boolean enable)
          Enable or disable the popup menu with the menu item "Orthography" and "Languages".
static void enableShortKey(javax.swing.text.JTextComponent text, boolean enable)
          Enable or disable the F7 key.
static void enableShortKey(javax.swing.text.JTextComponent text, boolean enable, SpellCheckerOptions options)
          Enable or disable the F7 key.
static java.lang.String getApplicationName()
          Get the title of your application.
static java.util.Locale getCurrentLocale()
          Gets the current Locale.
static CustomDictionaryProvider getCustomDictionaryProvider()
          Gets the currently set CustomDictionaryProvider.
static SpellCheckerOptions getOptions()
          Get the default SpellCheckerOptions.
static UserDictionaryProvider getUserDictionaryProvider()
          Gets the currently set UserDictionaryProvider.
static void register(javax.swing.text.JTextComponent text)
          Activate the spell checker for the given JTextComponent.
static void register(javax.swing.text.JTextComponent text, boolean hasPopup, boolean hasShortKey, boolean hasAutoSpell)
          Activates the spell checker for the given JTextComponent.
static void registerDictionaries(java.net.URL baseURL, java.lang.String activeLocale)
          Registers the available dictionaries.
static void registerDictionaries(java.net.URL baseURL, java.lang.String availableLocales, java.lang.String activeLocale)
          Registers the available dictionaries.
static void registerDictionaries(java.net.URL baseURL, java.lang.String availableLocales, java.lang.String activeLocale, java.lang.String extension)
          Registers the available dictionaries.
static void removeLanguageChangeLister(LanguageChangeListener listener)
          Removes the LanguageChangeListener.
static void setApplicationName(java.lang.String name)
          Set the title of your application.
static void setCustomDictionaryProvider(CustomDictionaryProvider customDictionaryProvider)
          Set a CustomDictionaryProvider.
static void setUserDictionaryProvider(UserDictionaryProvider userDictionaryProvider)
          Sets the UserDictionaryProvider.
static void showSpellCheckerDialog(javax.swing.text.JTextComponent text, SpellCheckerOptions options)
          Show the Spell Checker dialog for the given JTextComponent.
static void unregister(javax.swing.text.JTextComponent text)
          Removes all spell checker features from the JTextComponent.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

setUserDictionaryProvider

public static void setUserDictionaryProvider(UserDictionaryProvider userDictionaryProvider)
Sets the UserDictionaryProvider. This is needed if the user should be able to add their own words. This method must be called before registerDictionaries(URL, String, String).

Parameters:
userDictionaryProvider - the new UserDictionaryProvider or null
See Also:
setCustomDictionaryProvider(CustomDictionaryProvider), getUserDictionaryProvider(), registerDictionaries(URL, String, String)

getUserDictionaryProvider

public static UserDictionaryProvider getUserDictionaryProvider()
Gets the currently set UserDictionaryProvider. If none has been set then null is returned.

See Also:
setUserDictionaryProvider(UserDictionaryProvider)

setCustomDictionaryProvider

public static void setCustomDictionaryProvider(CustomDictionaryProvider customDictionaryProvider)
Set a CustomDictionaryProvider. This can be used to add an expert dictionary like a medical dictionary or a chemical dictionary.

Parameters:
customDictionaryProvider - the new CustomDictionaryProvider or null
See Also:
setUserDictionaryProvider(UserDictionaryProvider), registerDictionaries(URL, String, String)

getCustomDictionaryProvider

public static CustomDictionaryProvider getCustomDictionaryProvider()
Gets the currently set CustomDictionaryProvider. If none has been set then null is returned.

See Also:
setCustomDictionaryProvider(CustomDictionaryProvider)

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. The configuration file has a Java Properties format. Currently there are the follow options:

Parameters:
baseURL - the base URL where the dictionaries and configuration file can be found. If null then URL("file", null, "") is used.
activeLocale - the locale that should be loaded and made active. If null or empty then the default locale is used.

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.

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

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.

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)

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 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".
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)
Enable or disable the popup menu with the menu item "Orthography" and "Languages".

Parameters:
text - the JTextComponent that should change
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

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.

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)

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 get method.

Returns:
the default SpellCheckerOptions


Copyright © 2005 - 2009 i-net software. All Rights Reserved.