1 /*******************************************************************************
2 * Copyright (c) 2000, 2003 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
12 package net.sourceforge.phpdt.internal.ui.text.spelling;
14 import java.io.IOException;
15 import java.io.InputStream;
16 import java.net.MalformedURLException;
18 import java.util.HashMap;
19 import java.util.HashSet;
20 import java.util.Iterator;
21 import java.util.Locale;
25 import net.sourceforge.phpdt.internal.ui.PHPUIMessages;
26 import net.sourceforge.phpdt.internal.ui.text.spelling.engine.DefaultSpellChecker;
27 import net.sourceforge.phpdt.internal.ui.text.spelling.engine.ISpellCheckEngine;
28 import net.sourceforge.phpdt.internal.ui.text.spelling.engine.ISpellCheckPreferenceKeys;
29 import net.sourceforge.phpdt.internal.ui.text.spelling.engine.ISpellChecker;
30 import net.sourceforge.phpdt.internal.ui.text.spelling.engine.ISpellDictionary;
31 import net.sourceforge.phpdt.internal.ui.text.spelling.engine.PersistentSpellDictionary;
32 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
34 import org.eclipse.jface.preference.IPreferenceStore;
35 import org.eclipse.jface.util.IPropertyChangeListener;
36 import org.eclipse.jface.util.PropertyChangeEvent;
39 * Spell check engine for Java source spell checking.
43 public class SpellCheckEngine implements ISpellCheckEngine,
44 IPropertyChangeListener {
46 /** The dictionary location */
47 public static final String DICTIONARY_LOCATION = "dictionaries/"; //$NON-NLS-1$
49 /** The singleton spell checker instance */
50 private static ISpellChecker fChecker = null;
52 /** The singleton engine instance */
53 private static ISpellCheckEngine fEngine = null;
56 * Returns the available locales for this spell check engine.
58 * @return The available locales for this engine
60 public static Set getAvailableLocales() {
64 InputStream stream = null;
66 final Set result = new HashSet();
69 final URL location = getDictionaryLocation();
70 final Locale[] locales = Locale.getAvailableLocales();
72 for (int index = 0; index < locales.length; index++) {
74 locale = locales[index];
77 locale.toString().toLowerCase()
78 + "." + PHPUIMessages.getString("Spelling.dictionary.file.extension")); //$NON-NLS-1$ //$NON-NLS-2$
81 stream = url.openStream();
89 } catch (IOException exception) {
93 } catch (MalformedURLException exception) {
96 result.add(getDefaultLocale());
102 * Returns the default locale for this engine.
104 * @return The default locale
106 public static Locale getDefaultLocale() {
111 * Returns the dictionary location.
113 * @throws MalformedURLException
114 * if the URL could not be created
115 * @return The dictionary location, or <code>null</code> iff the location
118 public static URL getDictionaryLocation() throws MalformedURLException {
120 final PHPeclipsePlugin plugin = PHPeclipsePlugin.getDefault();
122 return plugin.getBundle().getEntry("/" + DICTIONARY_LOCATION); //$NON-NLS-1$
128 * Returns the singleton instance of the spell check engine.
130 * @return The singleton instance of the spell check engine
132 public static final synchronized ISpellCheckEngine getInstance() {
135 fEngine = new SpellCheckEngine();
140 /** The registered locale insenitive dictionaries */
141 private final Set fGlobalDictionaries = new HashSet();
143 /** The current locale */
144 private Locale fLocale = null;
146 /** The registered locale sensitive dictionaries */
147 private final Map fLocaleDictionaries = new HashMap();
149 /** The preference store where to listen */
150 private IPreferenceStore fPreferences = null;
152 /** The user dictionary */
153 private ISpellDictionary fUserDictionary = null;
156 * Creates a new spell check manager.
158 private SpellCheckEngine() {
160 fGlobalDictionaries.add(new TaskTagDictionary());
161 fGlobalDictionaries.add(new HtmlTagDictionary());
162 fGlobalDictionaries.add(new JavaDocTagDictionary());
166 Locale locale = null;
167 final URL location = getDictionaryLocation();
169 for (final Iterator iterator = getAvailableLocales().iterator(); iterator
172 locale = (Locale) iterator.next();
173 fLocaleDictionaries.put(locale, new SpellReconcileDictionary(
177 } catch (MalformedURLException exception) {
183 * @see net.sourceforge.phpdt.ui.text.spelling.engine.ISpellCheckEngine#createSpellChecker(java.util.Locale,org.eclipse.jface.preference.IPreferenceStore)
185 public final synchronized ISpellChecker createSpellChecker(
186 final Locale locale, final IPreferenceStore store) {
188 if (fLocale != null && fLocale.equals(locale))
191 if (fChecker == null) {
193 fChecker = new DefaultSpellChecker(store);
194 store.addPropertyChangeListener(this);
196 fPreferences = store;
198 ISpellDictionary dictionary = null;
199 for (Iterator iterator = fGlobalDictionaries.iterator(); iterator
202 dictionary = (ISpellDictionary) iterator.next();
203 fChecker.addDictionary(dictionary);
207 ISpellDictionary dictionary = null;
208 if (fLocale != null) {
210 dictionary = (ISpellDictionary) fLocaleDictionaries.get(fLocale);
211 if (dictionary != null) {
213 fChecker.removeDictionary(dictionary);
219 dictionary = (ISpellDictionary) fLocaleDictionaries.get(locale);
220 if (dictionary == null) {
222 if (!getDefaultLocale().equals(locale)) {
224 if (fPreferences != null)
225 fPreferences.removePropertyChangeListener(this);
232 fChecker.addDictionary(dictionary);
234 if (fPreferences != null)
235 propertyChange(new PropertyChangeEvent(
237 ISpellCheckPreferenceKeys.SPELLING_USER_DICTIONARY,
240 .getString(ISpellCheckPreferenceKeys.SPELLING_USER_DICTIONARY)));
246 * @see net.sourceforge.phpdt.ui.text.spelling.engine.ISpellCheckEngine#getLocale()
248 public final Locale getLocale() {
253 * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
255 public final void propertyChange(final PropertyChangeEvent event) {
258 && event.getProperty().equals(
259 ISpellCheckPreferenceKeys.SPELLING_USER_DICTIONARY)) {
261 if (fUserDictionary != null) {
263 fChecker.removeDictionary(fUserDictionary);
264 fUserDictionary = null;
267 final String file = (String) event.getNewValue();
268 if (file.length() > 0) {
272 final URL url = new URL("file", null, file); //$NON-NLS-1$
273 InputStream stream = url.openStream();
274 if (stream != null) {
276 fUserDictionary = new PersistentSpellDictionary(url);
277 fChecker.addDictionary(fUserDictionary);
282 } catch (MalformedURLException exception) {
284 } catch (IOException exception) {
292 * @see net.sourceforge.phpdt.ui.text.spelling.engine.ISpellCheckEngine#registerDictionary(net.sourceforge.phpdt.ui.text.spelling.engine.ISpellDictionary)
294 public synchronized final void registerDictionary(
295 final ISpellDictionary dictionary) {
297 fGlobalDictionaries.add(dictionary);
299 if (fChecker != null)
300 fChecker.addDictionary(dictionary);
304 * @see net.sourceforge.phpdt.ui.text.spelling.engine.ISpellCheckEngine#registerDictionary(java.util.Locale,net.sourceforge.phpdt.ui.text.spelling.engine.ISpellDictionary)
306 public synchronized final void registerDictionary(final Locale locale,
307 final ISpellDictionary dictionary) {
309 fLocaleDictionaries.put(locale, dictionary);
311 if (fChecker != null && fLocale != null && fLocale.equals(locale))
312 fChecker.addDictionary(dictionary);
316 * @see net.sourceforge.phpdt.ui.text.spelling.engine.ISpellCheckEngine#unload()
318 public synchronized final void unload() {
320 ISpellDictionary dictionary = null;
321 for (final Iterator iterator = fGlobalDictionaries.iterator(); iterator
324 dictionary = (ISpellDictionary) iterator.next();
328 for (final Iterator iterator = fLocaleDictionaries.values().iterator(); iterator
331 dictionary = (ISpellDictionary) iterator.next();
335 if (fPreferences != null)
336 fPreferences.removePropertyChangeListener(this);
338 fUserDictionary = null;
343 * @see net.sourceforge.phpdt.ui.text.spelling.engine.ISpellCheckEngine#unregisterDictionary(net.sourceforge.phpdt.ui.text.spelling.engine.ISpellDictionary)
345 public synchronized final void unregisterDictionary(
346 final ISpellDictionary dictionary) {
348 fGlobalDictionaries.remove(dictionary);
349 fLocaleDictionaries.values().remove(dictionary);
351 if (fChecker != null)
352 fChecker.removeDictionary(dictionary);