Class SpellChecker
- java.lang.Object
-
- 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:
and in an applet this will look like:SpellChecker.registerDictionaries( new URL("file", null, ""), "en,de", "de" );
After this you can register your text component that should have the spell checker features (Highlighter, context menu, spell checking dialog). This looks like:SpellChecker.registerDictionaries( getCodeBase(), "en,de", "en" );
JTextPane text = new JTextPane(); SpellChecker.register( text );
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
SpellChecker.AbtsractLanguageAction
Base class for languages change actions.private static class
SpellChecker.ActionToggleButtonModel
private static class
SpellChecker.DisableLanguageAction
Action for disable all dictionary language.private static class
SpellChecker.LanguageAction
Action for change the current dictionary language.
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.String
applicationName
private static Dictionary
currentDictionary
private static java.util.Locale
currentLocale
private static CustomDictionaryProvider
customDictionaryProvider
private static CustomUIProvider
customUIProvider
private static SpellCheckerOptions
globalOptions
private static java.util.ArrayList<SpellChecker.LanguageAction>
languages
private static java.util.Map<LanguageChangeListener,java.lang.Object>
listeners
private static MessageHandler
messageHandler
(package private) static java.lang.String
SELECTED_KEY
Duplicate of Action.SELECTED_KEY since 1.6private static UserDictionaryProvider
userDictionaryProvider
-
Constructor Summary
Constructors Modifier Constructor Description private
SpellChecker()
There is no instance needed of SpellChecker.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description 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, boolean submenu)
Enable or disable the popup menu with the menu item "Orthography" and "Languages" or only suggestion.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.private static void
fireLanguageChanged(java.util.Locale oldLocale)
Helper method to fire an Language change event.static java.lang.String
getApplicationName()
Get the title of your application.(package private) static Dictionary
getCurrentDictionary()
Get the currentDictionary
.static java.util.Locale
getCurrentLocale()
Gets the currentLocale
.static CustomDictionaryProvider
getCustomDictionaryProvider()
Gets the currently set CustomDictionaryProvider.static CustomUIProvider
getCustomUIProvider()
Gets the currently set CustomUIProvider.static MessageHandler
getMessageHandler()
Gets the currently set message handler.static SpellCheckerOptions
getOptions()
Get the default SpellCheckerOptions.static UserDictionaryProvider
getUserDictionaryProvider()
Gets the currently set UserDictionaryProvider.static boolean
isDictionaryLoaded()
If currently a Dictionary is loaded.static void
register(javax.swing.text.JTextComponent text)
Activate the spell checker for the givenJTextComponent
.static void
register(javax.swing.text.JTextComponent text, boolean hasPopup, boolean submenu, boolean hasShortKey, boolean hasAutoSpell)
Activates the spell checker for the givenJTextComponent
.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
setCurrentLocale(java.util.Locale locale)
Set the currentLocale
.static void
setCustomDictionaryProvider(CustomDictionaryProvider customDictionaryProvider)
Set a CustomDictionaryProvider.static void
setCustomUIProvider(CustomUIProvider customUIProvider)
Set a CustomUIProvider.static void
setMessageHandler(MessageHandler messageHandler)
Set the message handler used for handling errors and information messages.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.
-
-
-
Field Detail
-
languages
private static final java.util.ArrayList<SpellChecker.LanguageAction> languages
-
currentDictionary
private static Dictionary currentDictionary
-
currentLocale
private static java.util.Locale currentLocale
-
userDictionaryProvider
private static UserDictionaryProvider userDictionaryProvider
-
customDictionaryProvider
private static CustomDictionaryProvider customDictionaryProvider
-
listeners
private static final java.util.Map<LanguageChangeListener,java.lang.Object> listeners
-
applicationName
private static java.lang.String applicationName
-
globalOptions
private static final SpellCheckerOptions globalOptions
-
messageHandler
private static MessageHandler messageHandler
-
customUIProvider
private static CustomUIProvider customUIProvider
-
SELECTED_KEY
static final java.lang.String SELECTED_KEY
Duplicate of Action.SELECTED_KEY since 1.6- See Also:
- Constant Field Values
-
-
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 beforeregisterDictionaries(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.
-
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.
-
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)
-
getMessageHandler
public static MessageHandler getMessageHandler()
Gets the currently set message handler.- Returns:
- the message handler, never null
- See Also:
setMessageHandler(MessageHandler)
-
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
-
getCustomUIProvider
public static CustomUIProvider getCustomUIProvider()
Gets the currently set CustomUIProvider. If none has been set then null is returned.- See Also:
setCustomUIProvider(CustomUIProvider)
-
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
// 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 localesactiveLocale
- 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 localesactiveLocale
- 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 givenJTextComponent
. 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 givenJTextComponent
. You do not need to unregister if the JTextComponent is not needed anymore.- Parameters:
text
- the JTextComponenthasPopup
- 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 menuhasShortKey
- 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 changeenable
- 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 changeenable
- 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:
The action is only available if you have enable the short key (F7).Action action = text.getActionMap().get("spell-checking");
- Parameters:
text
- JTextComponent to checkoptions
- 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 changesubmenu
- true, menu item "Orthography" and "Languages"; false, only suggestionsenable
- 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 changeenable
- 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 changeenable
- 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 currentDictionary
. The current dictionary will be set if the user one select or on callingregisterDictionaries
.- Returns:
- the current
Dictionary
or null if not set. - See Also:
registerDictionaries(URL, String, String)
-
getCurrentLocale
public static java.util.Locale getCurrentLocale()
Gets the currentLocale
. The current Locale will be set if the user selects one, or when callingregisterDictionaries. - 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 currentLocale
. 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
-
-