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.ui.WebUI;
33 //import net.sourceforge.phpeclipse.PHPeclipsePlugin;
35 import org.eclipse.jface.preference.IPreferenceStore;
36 import org.eclipse.jface.util.IPropertyChangeListener;
37 import org.eclipse.jface.util.PropertyChangeEvent;
40 * Spell check engine for Java source spell checking.
44 public class SpellCheckEngine implements ISpellCheckEngine,
45 IPropertyChangeListener {
47 /** The dictionary location */
48 public static final String DICTIONARY_LOCATION = "dictionaries/"; //$NON-NLS-1$
50 /** The singleton spell checker instance */
51 private static ISpellChecker fChecker = null;
53 /** The singleton engine instance */
54 private static ISpellCheckEngine fEngine = null;
57 * Returns the available locales for this spell check engine.
59 * @return The available locales for this engine
61 public static Set getAvailableLocales() {
65 InputStream stream = null;
67 final Set result = new HashSet();
70 final URL location = getDictionaryLocation();
71 final Locale[] locales = Locale.getAvailableLocales();
73 for (int index = 0; index < locales.length; index++) {
75 locale = locales[index];
78 locale.toString().toLowerCase()
79 + "." + PHPUIMessages.getString("Spelling.dictionary.file.extension")); //$NON-NLS-1$ //$NON-NLS-2$
82 stream = url.openStream();
90 } catch (IOException exception) {
94 } catch (MalformedURLException exception) {
97 result.add(getDefaultLocale());
103 * Returns the default locale for this engine.
105 * @return The default locale
107 public static Locale getDefaultLocale() {
112 * Returns the dictionary location.
114 * @throws MalformedURLException
115 * if the URL could not be created
116 * @return The dictionary location, or <code>null</code> iff the location
119 public static URL getDictionaryLocation() throws MalformedURLException {
121 final WebUI plugin = WebUI.getDefault();
123 return plugin.getBundle().getEntry("/" + DICTIONARY_LOCATION); //$NON-NLS-1$
129 * Returns the singleton instance of the spell check engine.
131 * @return The singleton instance of the spell check engine
133 public static final synchronized ISpellCheckEngine getInstance() {
136 fEngine = new SpellCheckEngine();
141 /** The registered locale insenitive dictionaries */
142 private final Set fGlobalDictionaries = new HashSet();
144 /** The current locale */
145 private Locale fLocale = null;
147 /** The registered locale sensitive dictionaries */
148 private final Map fLocaleDictionaries = new HashMap();
150 /** The preference store where to listen */
151 private IPreferenceStore fPreferences = null;
153 /** The user dictionary */
154 private ISpellDictionary fUserDictionary = null;
157 * Creates a new spell check manager.
159 private SpellCheckEngine() {
161 fGlobalDictionaries.add(new TaskTagDictionary());
162 fGlobalDictionaries.add(new HtmlTagDictionary());
163 fGlobalDictionaries.add(new JavaDocTagDictionary());
167 Locale locale = null;
168 final URL location = getDictionaryLocation();
170 for (final Iterator iterator = getAvailableLocales().iterator(); iterator
173 locale = (Locale) iterator.next();
174 fLocaleDictionaries.put(locale, new SpellReconcileDictionary(
178 } catch (MalformedURLException exception) {
184 * @see net.sourceforge.phpdt.ui.text.spelling.engine.ISpellCheckEngine#createSpellChecker(java.util.Locale,org.eclipse.jface.preference.IPreferenceStore)
186 public final synchronized ISpellChecker createSpellChecker(
187 final Locale locale, final IPreferenceStore store) {
189 if (fLocale != null && fLocale.equals(locale))
192 if (fChecker == null) {
194 fChecker = new DefaultSpellChecker(store);
195 store.addPropertyChangeListener(this);
197 fPreferences = store;
199 ISpellDictionary dictionary = null;
200 for (Iterator iterator = fGlobalDictionaries.iterator(); iterator
203 dictionary = (ISpellDictionary) iterator.next();
204 fChecker.addDictionary(dictionary);
208 ISpellDictionary dictionary = null;
209 if (fLocale != null) {
211 dictionary = (ISpellDictionary) fLocaleDictionaries.get(fLocale);
212 if (dictionary != null) {
214 fChecker.removeDictionary(dictionary);
220 dictionary = (ISpellDictionary) fLocaleDictionaries.get(locale);
221 if (dictionary == null) {
223 if (!getDefaultLocale().equals(locale)) {
225 if (fPreferences != null)
226 fPreferences.removePropertyChangeListener(this);
233 fChecker.addDictionary(dictionary);
235 if (fPreferences != null)
236 propertyChange(new PropertyChangeEvent(
238 ISpellCheckPreferenceKeys.SPELLING_USER_DICTIONARY,
241 .getString(ISpellCheckPreferenceKeys.SPELLING_USER_DICTIONARY)));
247 * @see net.sourceforge.phpdt.ui.text.spelling.engine.ISpellCheckEngine#getLocale()
249 public final Locale getLocale() {
254 * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
256 public final void propertyChange(final PropertyChangeEvent event) {
259 && event.getProperty().equals(
260 ISpellCheckPreferenceKeys.SPELLING_USER_DICTIONARY)) {
262 if (fUserDictionary != null) {
264 fChecker.removeDictionary(fUserDictionary);
265 fUserDictionary = null;
268 final String file = (String) event.getNewValue();
269 if (file.length() > 0) {
273 final URL url = new URL("file", null, file); //$NON-NLS-1$
274 InputStream stream = url.openStream();
275 if (stream != null) {
277 fUserDictionary = new PersistentSpellDictionary(url);
278 fChecker.addDictionary(fUserDictionary);
283 } catch (MalformedURLException exception) {
285 } catch (IOException exception) {
293 * @see net.sourceforge.phpdt.ui.text.spelling.engine.ISpellCheckEngine#registerDictionary(net.sourceforge.phpdt.ui.text.spelling.engine.ISpellDictionary)
295 public synchronized final void registerDictionary(
296 final ISpellDictionary dictionary) {
298 fGlobalDictionaries.add(dictionary);
300 if (fChecker != null)
301 fChecker.addDictionary(dictionary);
305 * @see net.sourceforge.phpdt.ui.text.spelling.engine.ISpellCheckEngine#registerDictionary(java.util.Locale,net.sourceforge.phpdt.ui.text.spelling.engine.ISpellDictionary)
307 public synchronized final void registerDictionary(final Locale locale,
308 final ISpellDictionary dictionary) {
310 fLocaleDictionaries.put(locale, dictionary);
312 if (fChecker != null && fLocale != null && fLocale.equals(locale))
313 fChecker.addDictionary(dictionary);
317 * @see net.sourceforge.phpdt.ui.text.spelling.engine.ISpellCheckEngine#unload()
319 public synchronized final void unload() {
321 ISpellDictionary dictionary = null;
322 for (final Iterator iterator = fGlobalDictionaries.iterator(); iterator
325 dictionary = (ISpellDictionary) iterator.next();
329 for (final Iterator iterator = fLocaleDictionaries.values().iterator(); iterator
332 dictionary = (ISpellDictionary) iterator.next();
336 if (fPreferences != null)
337 fPreferences.removePropertyChangeListener(this);
339 fUserDictionary = null;
344 * @see net.sourceforge.phpdt.ui.text.spelling.engine.ISpellCheckEngine#unregisterDictionary(net.sourceforge.phpdt.ui.text.spelling.engine.ISpellDictionary)
346 public synchronized final void unregisterDictionary(
347 final ISpellDictionary dictionary) {
349 fGlobalDictionaries.remove(dictionary);
350 fLocaleDictionaries.values().remove(dictionary);
352 if (fChecker != null)
353 fChecker.removeDictionary(dictionary);