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;
41 * Spell check engine for Java source spell checking.
45 public class SpellCheckEngine implements ISpellCheckEngine, 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];
76 url= new URL(location, locale.toString().toLowerCase() + "." + PHPUIMessages.getString("Spelling.dictionary.file.extension")); //$NON-NLS-1$ //$NON-NLS-2$
79 stream= url.openStream();
87 } catch (IOException exception) {
91 } catch (MalformedURLException exception) {
94 result.add(getDefaultLocale());
100 * Returns the default locale for this engine.
102 * @return The default locale
104 public static Locale getDefaultLocale() {
109 * Returns the dictionary location.
111 * @throws MalformedURLException
112 * if the URL could not be created
113 * @return The dictionary location, or <code>null</code> iff the location
116 public static URL getDictionaryLocation() throws MalformedURLException {
118 final PHPeclipsePlugin plugin= PHPeclipsePlugin.getDefault();
120 return plugin.getBundle().getEntry("/" + DICTIONARY_LOCATION); //$NON-NLS-1$
126 * Returns the singleton instance of the spell check engine.
128 * @return The singleton instance of the spell check engine
130 public static final synchronized ISpellCheckEngine getInstance() {
133 fEngine= new SpellCheckEngine();
138 /** The registered locale insenitive dictionaries */
139 private final Set fGlobalDictionaries= new HashSet();
141 /** The current locale */
142 private Locale fLocale= null;
144 /** The registered locale sensitive dictionaries */
145 private final Map fLocaleDictionaries= new HashMap();
147 /** The preference store where to listen */
148 private IPreferenceStore fPreferences= null;
150 /** The user dictionary */
151 private ISpellDictionary fUserDictionary= null;
154 * Creates a new spell check manager.
156 private SpellCheckEngine() {
158 fGlobalDictionaries.add(new TaskTagDictionary());
159 fGlobalDictionaries.add(new HtmlTagDictionary());
160 fGlobalDictionaries.add(new JavaDocTagDictionary());
165 final URL location= getDictionaryLocation();
167 for (final Iterator iterator= getAvailableLocales().iterator(); iterator.hasNext();) {
169 locale= (Locale)iterator.next();
170 fLocaleDictionaries.put(locale, new SpellReconcileDictionary(locale, location));
173 } catch (MalformedURLException exception) {
179 * @see org.eclipse.jdt.ui.text.spelling.engine.ISpellCheckEngine#createSpellChecker(java.util.Locale,org.eclipse.jface.preference.IPreferenceStore)
181 public final synchronized ISpellChecker createSpellChecker(final Locale locale, final IPreferenceStore store) {
183 if (fLocale != null && fLocale.equals(locale))
186 if (fChecker == null) {
188 fChecker= new DefaultSpellChecker(store);
189 store.addPropertyChangeListener(this);
193 ISpellDictionary dictionary= null;
194 for (Iterator iterator= fGlobalDictionaries.iterator(); iterator.hasNext();) {
196 dictionary= (ISpellDictionary)iterator.next();
197 fChecker.addDictionary(dictionary);
201 ISpellDictionary dictionary= null;
202 if (fLocale != null) {
204 dictionary= (ISpellDictionary)fLocaleDictionaries.get(fLocale);
205 if (dictionary != null) {
207 fChecker.removeDictionary(dictionary);
213 dictionary= (ISpellDictionary)fLocaleDictionaries.get(locale);
214 if (dictionary == null) {
216 if (!getDefaultLocale().equals(locale)) {
218 if (fPreferences != null)
219 fPreferences.removePropertyChangeListener(this);
226 fChecker.addDictionary(dictionary);
228 if (fPreferences != null)
229 propertyChange(new PropertyChangeEvent(this, ISpellCheckPreferenceKeys.SPELLING_USER_DICTIONARY, null, fPreferences.getString(ISpellCheckPreferenceKeys.SPELLING_USER_DICTIONARY)));
235 * @see org.eclipse.jdt.ui.text.spelling.engine.ISpellCheckEngine#getLocale()
237 public final Locale getLocale() {
242 * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
244 public final void propertyChange(final PropertyChangeEvent event) {
246 if (fChecker != null && event.getProperty().equals(ISpellCheckPreferenceKeys.SPELLING_USER_DICTIONARY)) {
248 if (fUserDictionary != null) {
250 fChecker.removeDictionary(fUserDictionary);
251 fUserDictionary= null;
254 final String file= (String)event.getNewValue();
255 if (file.length() > 0) {
259 final URL url= new URL("file", null, file); //$NON-NLS-1$
260 InputStream stream= url.openStream();
261 if (stream != null) {
263 fUserDictionary= new PersistentSpellDictionary(url);
264 fChecker.addDictionary(fUserDictionary);
269 } catch (MalformedURLException exception) {
271 } catch (IOException exception) {
279 * @see org.eclipse.jdt.ui.text.spelling.engine.ISpellCheckEngine#registerDictionary(org.eclipse.jdt.ui.text.spelling.engine.ISpellDictionary)
281 public synchronized final void registerDictionary(final ISpellDictionary dictionary) {
283 fGlobalDictionaries.add(dictionary);
285 if (fChecker != null)
286 fChecker.addDictionary(dictionary);
290 * @see org.eclipse.jdt.ui.text.spelling.engine.ISpellCheckEngine#registerDictionary(java.util.Locale,org.eclipse.jdt.ui.text.spelling.engine.ISpellDictionary)
292 public synchronized final void registerDictionary(final Locale locale, final ISpellDictionary dictionary) {
294 fLocaleDictionaries.put(locale, dictionary);
296 if (fChecker != null && fLocale != null && fLocale.equals(locale))
297 fChecker.addDictionary(dictionary);
301 * @see org.eclipse.jdt.ui.text.spelling.engine.ISpellCheckEngine#unload()
303 public synchronized final void unload() {
305 ISpellDictionary dictionary= null;
306 for (final Iterator iterator= fGlobalDictionaries.iterator(); iterator.hasNext();) {
308 dictionary= (ISpellDictionary)iterator.next();
312 for (final Iterator iterator= fLocaleDictionaries.values().iterator(); iterator.hasNext();) {
314 dictionary= (ISpellDictionary)iterator.next();
318 if (fPreferences != null)
319 fPreferences.removePropertyChangeListener(this);
321 fUserDictionary= null;
326 * @see org.eclipse.jdt.ui.text.spelling.engine.ISpellCheckEngine#unregisterDictionary(org.eclipse.jdt.ui.text.spelling.engine.ISpellDictionary)
328 public synchronized final void unregisterDictionary(final ISpellDictionary dictionary) {
330 fGlobalDictionaries.remove(dictionary);
331 fLocaleDictionaries.values().remove(dictionary);
333 if (fChecker != null)
334 fChecker.removeDictionary(dictionary);