1 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 36 public class SpellingService { 37 38 44 public static final String PREFERENCE_SPELLING_ENABLED= "spellingEnabled"; 46 53 public static final String PREFERENCE_SPELLING_ENGINE= "spellingEngine"; 55 56 private IPreferenceStore fPreferences; 57 58 65 public SpellingService(IPreferenceStore preferences) { 66 fPreferences= preferences; 67 } 68 69 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 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 { 103 engine.check(document, regions, context, collector, monitor); 104 } 105 public void handleException(Throwable 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 124 public SpellingEngineDescriptor[] getSpellingEngineDescriptors() { 125 SpellingEngineRegistry registry= getSpellingEngineRegistry(); 126 if (registry == null) 127 return new SpellingEngineDescriptor[0]; 128 return registry.getDescriptors(); 129 } 130 131 138 public SpellingEngineDescriptor getDefaultSpellingEngineDescriptor() { 139 SpellingEngineRegistry registry= getSpellingEngineRegistry(); 140 if (registry == null) 141 return null; 142 return registry.getDefaultDescriptor(); 143 } 144 145 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 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 191 private SpellingEngineRegistry getSpellingEngineRegistry() { 192 return TextEditorPlugin.getDefault().getSpellingEngineRegistry(); 193 } 194 } 195 | Popular Tags |