001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.texteditor.spelling;
011:
012: import org.eclipse.core.runtime.CoreException;
013: import org.eclipse.core.runtime.IProgressMonitor;
014: import org.eclipse.core.runtime.ISafeRunnable;
015: import org.eclipse.core.runtime.SafeRunner;
016:
017: import org.eclipse.jface.preference.IPreferenceStore;
018:
019: import org.eclipse.jface.text.IDocument;
020: import org.eclipse.jface.text.IRegion;
021: import org.eclipse.jface.text.Region;
022:
023: import org.eclipse.ui.internal.texteditor.TextEditorPlugin;
024: import org.eclipse.ui.internal.texteditor.spelling.SpellingEngineRegistry;
025:
026: /**
027: * System wide spelling service.
028: * <p>
029: * This class is not intended to be subclassed by clients.
030: * </p>
031: *
032: * @since 3.1
033: */
034: public class SpellingService {
035:
036: /**
037: * A named preference that controls if spelling is enabled or disabled.
038: * <p>
039: * Value is of type <code>Boolean</code>.
040: * </p>
041: */
042: public static final String PREFERENCE_SPELLING_ENABLED = "spellingEnabled"; //$NON-NLS-1$
043:
044: /**
045: * A named preference that controls which spelling engine is used.
046: * The value is the spelling engine's extension id.
047: * <p>
048: * Value is of type <code>String</code>.
049: * </p>
050: */
051: public static final String PREFERENCE_SPELLING_ENGINE = "spellingEngine"; //$NON-NLS-1$
052:
053: /** Preferences */
054: private IPreferenceStore fPreferences;
055:
056: /**
057: * Initializes the spelling service with the given preferences.
058: *
059: * @param preferences the preferences
060: * @see SpellingService#PREFERENCE_SPELLING_ENABLED
061: * @see SpellingService#PREFERENCE_SPELLING_ENGINE
062: */
063: public SpellingService(IPreferenceStore preferences) {
064: fPreferences = preferences;
065: }
066:
067: /**
068: * Checks the given document. Reports all found spelling problems to the
069: * collector. The spelling engine is chosen based on the settings
070: * from the given preferences.
071: *
072: * @param document the document to check
073: * @param context the context
074: * @param collector the problem collector
075: * @param monitor the progress monitor, can be <code>null</code>
076: */
077: public void check(IDocument document, SpellingContext context,
078: ISpellingProblemCollector collector,
079: IProgressMonitor monitor) {
080: check(document, new IRegion[] { new Region(0, document
081: .getLength()) }, context, collector, monitor);
082: }
083:
084: /**
085: * Checks the given regions in the given document. Reports all found
086: * spelling problems to the collector. The spelling engine is chosen
087: * based on the settings from the given preferences.
088: *
089: * @param document the document to check
090: * @param regions the regions to check
091: * @param context the context
092: * @param collector the problem collector
093: * @param monitor the progress monitor, can be <code>null</code>
094: */
095: public void check(final IDocument document,
096: final IRegion[] regions, final SpellingContext context,
097: final ISpellingProblemCollector collector,
098: final IProgressMonitor monitor) {
099: try {
100: collector.beginCollecting();
101: if (fPreferences.getBoolean(PREFERENCE_SPELLING_ENABLED))
102: try {
103: final ISpellingEngine engine = createEngine(fPreferences);
104: if (engine != null) {
105: ISafeRunnable runnable = new ISafeRunnable() {
106: public void run() throws Exception {
107: engine.check(document, regions,
108: context, collector, monitor);
109: }
110:
111: public void handleException(Throwable x) {
112: }
113: };
114: SafeRunner.run(runnable);
115: }
116: } catch (CoreException x) {
117: TextEditorPlugin.getDefault().getLog().log(
118: x.getStatus());
119: }
120: } finally {
121: collector.endCollecting();
122: }
123: }
124:
125: /**
126: * Returns all spelling engine descriptors from extensions to the
127: * spelling engine extension point.
128: *
129: * @return all spelling engine descriptors
130: */
131: public SpellingEngineDescriptor[] getSpellingEngineDescriptors() {
132: SpellingEngineRegistry registry = getSpellingEngineRegistry();
133: if (registry == null)
134: return new SpellingEngineDescriptor[0];
135: return registry.getDescriptors();
136: }
137:
138: /**
139: * Returns the default spelling engine descriptor from extensions to
140: * the spelling engine extension point.
141: *
142: * @return the default spelling engine descriptor or
143: * <code>null</code> if none could be found
144: */
145: public SpellingEngineDescriptor getDefaultSpellingEngineDescriptor() {
146: SpellingEngineRegistry registry = getSpellingEngineRegistry();
147: if (registry == null)
148: return null;
149: return registry.getDefaultDescriptor();
150: }
151:
152: /**
153: * Returns the descriptor of the active spelling engine based on the
154: * value of the <code>PREFERENCE_SPELLING_ENGINE</code> preference
155: * in the given preferences.
156: *
157: * @param preferences the preferences
158: * @return the descriptor of the active spelling engine or
159: * <code>null</code> if none could be found
160: * @see SpellingService#PREFERENCE_SPELLING_ENGINE
161: */
162: public SpellingEngineDescriptor getActiveSpellingEngineDescriptor(
163: IPreferenceStore preferences) {
164: SpellingEngineRegistry registry = getSpellingEngineRegistry();
165: if (registry == null)
166: return null;
167:
168: SpellingEngineDescriptor descriptor = null;
169: if (preferences.contains(PREFERENCE_SPELLING_ENGINE))
170: descriptor = registry.getDescriptor(preferences
171: .getString(PREFERENCE_SPELLING_ENGINE));
172: if (descriptor == null)
173: descriptor = registry.getDefaultDescriptor();
174: return descriptor;
175: }
176:
177: /**
178: * Creates a spelling engine based on the value of the
179: * <code>PREFERENCE_SPELLING_ENGINE</code> preference in the given
180: * preferences.
181: *
182: * @param preferences the preferences
183: * @return the created spelling engine or <code>null</code> if none
184: * could be created
185: * @throws CoreException if the creation failed
186: * @see SpellingService#PREFERENCE_SPELLING_ENGINE
187: */
188: private ISpellingEngine createEngine(IPreferenceStore preferences)
189: throws CoreException {
190: SpellingEngineDescriptor descriptor = getActiveSpellingEngineDescriptor(preferences);
191: if (descriptor != null)
192: return descriptor.createEngine();
193: return null;
194: }
195:
196: /**
197: * Returns the spelling engine registry.
198: *
199: * @return the spelling engine registry or <code>null</code> if the plug-in has been shutdown
200: */
201: private SpellingEngineRegistry getSpellingEngineRegistry() {
202: return TextEditorPlugin.getDefault()
203: .getSpellingEngineRegistry();
204: }
205: }
|