KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > texteditor > spelling > SpellingService


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.ui.texteditor.spelling;
13
14 import org.eclipse.core.runtime.CoreException;
15 import org.eclipse.core.runtime.IProgressMonitor;
16 import org.eclipse.core.runtime.ISafeRunnable;
17 import org.eclipse.core.runtime.SafeRunner;
18
19 import org.eclipse.jface.preference.IPreferenceStore;
20
21 import org.eclipse.jface.text.IDocument;
22 import org.eclipse.jface.text.IRegion;
23 import org.eclipse.jface.text.Region;
24
25 import org.eclipse.ui.internal.texteditor.TextEditorPlugin;
26 import org.eclipse.ui.internal.texteditor.spelling.SpellingEngineRegistry;
27
28 /**
29  * System wide spelling service.
30  * <p>
31  * This class is not intended to be subclassed by clients.
32  * </p>
33  *
34  * @since 3.1
35  */

36 public class SpellingService {
37
38     /**
39      * A named preference that controls if spelling is enabled or disabled.
40      * <p>
41      * Value is of type <code>Boolean</code>.
42      * </p>
43      */

44     public static final String JavaDoc PREFERENCE_SPELLING_ENABLED= "spellingEnabled"; //$NON-NLS-1$
45

46     /**
47      * A named preference that controls which spelling engine is used.
48      * The value is the spelling engine's extension id.
49      * <p>
50      * Value is of type <code>String</code>.
51      * </p>
52      */

53     public static final String JavaDoc PREFERENCE_SPELLING_ENGINE= "spellingEngine"; //$NON-NLS-1$
54

55     /** Preferences */
56     private IPreferenceStore fPreferences;
57
58     /**
59      * Initializes the spelling service with the given preferences.
60      *
61      * @param preferences the preferences
62      * @see SpellingService#PREFERENCE_SPELLING_ENABLED
63      * @see SpellingService#PREFERENCE_SPELLING_ENGINE
64      */

65     public SpellingService(IPreferenceStore preferences) {
66         fPreferences= preferences;
67     }
68
69     /**
70      * Checks the given document. Reports all found spelling problems to the
71      * collector. The spelling engine is chosen based on the settings
72      * from the given preferences.
73      *
74      * @param document the document to check
75      * @param context the context
76      * @param collector the problem collector
77      * @param monitor the progress monitor, can be <code>null</code>
78      */

79     public void check(IDocument document, SpellingContext context, ISpellingProblemCollector collector, IProgressMonitor monitor) {
80         check(document, new IRegion[] { new Region(0, document.getLength()) }, context, collector, monitor);
81     }
82
83     /**
84      * Checks the given regions in the given document. Reports all found
85      * spelling problems to the collector. The spelling engine is chosen
86      * based on the settings from the given preferences.
87      *
88      * @param document the document to check
89      * @param regions the regions to check
90      * @param context the context
91      * @param collector the problem collector
92      * @param monitor the progress monitor, can be <code>null</code>
93      */

94     public void check(final IDocument document, final IRegion[] regions, final SpellingContext context, final ISpellingProblemCollector collector, final IProgressMonitor monitor) {
95         try {
96             collector.beginCollecting();
97             if (fPreferences.getBoolean(PREFERENCE_SPELLING_ENABLED))
98                 try {
99                     final ISpellingEngine engine= createEngine(fPreferences);
100                     if (engine != null) {
101                         ISafeRunnable runnable= new ISafeRunnable() {
102                             public void run() throws Exception JavaDoc {
103                                 engine.check(document, regions, context, collector, monitor);
104                             }
105                             public void handleException(Throwable JavaDoc x) {
106                             }
107                         };
108                         SafeRunner.run(runnable);
109                     }
110                 } catch (CoreException x) {
111                     TextEditorPlugin.getDefault().getLog().log(x.getStatus());
112                 }
113         } finally {
114             collector.endCollecting();
115         }
116     }
117
118     /**
119      * Returns all spelling engine descriptors from extensions to the
120      * spelling engine extension point.
121      *
122      * @return all spelling engine descriptors
123      */

124     public SpellingEngineDescriptor[] getSpellingEngineDescriptors() {
125         SpellingEngineRegistry registry= getSpellingEngineRegistry();
126         if (registry == null)
127             return new SpellingEngineDescriptor[0];
128         return registry.getDescriptors();
129     }
130
131     /**
132      * Returns the default spelling engine descriptor from extensions to
133      * the spelling engine extension point.
134      *
135      * @return the default spelling engine descriptor or
136      * <code>null</code> if none could be found
137      */

138     public SpellingEngineDescriptor getDefaultSpellingEngineDescriptor() {
139         SpellingEngineRegistry registry= getSpellingEngineRegistry();
140         if (registry == null)
141             return null;
142         return registry.getDefaultDescriptor();
143     }
144
145     /**
146      * Returns the descriptor of the active spelling engine based on the
147      * value of the <code>PREFERENCE_SPELLING_ENGINE</code> preference
148      * in the given preferences.
149      *
150      * @param preferences the preferences
151      * @return the descriptor of the active spelling engine or
152      * <code>null</code> if none could be found
153      * @see SpellingService#PREFERENCE_SPELLING_ENGINE
154      */

155     public SpellingEngineDescriptor getActiveSpellingEngineDescriptor(IPreferenceStore preferences) {
156         SpellingEngineRegistry registry= getSpellingEngineRegistry();
157         if (registry == null)
158             return null;
159
160         SpellingEngineDescriptor descriptor= null;
161         if (preferences.contains(PREFERENCE_SPELLING_ENGINE))
162             descriptor= registry.getDescriptor(preferences.getString(PREFERENCE_SPELLING_ENGINE));
163         if (descriptor == null)
164             descriptor= registry.getDefaultDescriptor();
165         return descriptor;
166     }
167
168     /**
169      * Creates a spelling engine based on the value of the
170      * <code>PREFERENCE_SPELLING_ENGINE</code> preference in the given
171      * preferences.
172      *
173      * @param preferences the preferences
174      * @return the created spelling engine or <code>null</code> if none
175      * could be created
176      * @throws CoreException if the creation failed
177      * @see SpellingService#PREFERENCE_SPELLING_ENGINE
178      */

179     private ISpellingEngine createEngine(IPreferenceStore preferences) throws CoreException {
180         SpellingEngineDescriptor descriptor= getActiveSpellingEngineDescriptor(preferences);
181         if (descriptor != null)
182             return descriptor.createEngine();
183         return null;
184     }
185
186     /**
187      * Returns the spelling engine registry.
188      *
189      * @return the spelling engine registry or <code>null</code> if the plug-in has been shutdown
190      */

191     private SpellingEngineRegistry getSpellingEngineRegistry() {
192         return TextEditorPlugin.getDefault().getSpellingEngineRegistry();
193     }
194 }
195
Popular Tags