1 11 12 package org.eclipse.jdt.internal.ui.text.spelling; 13 14 import java.text.MessageFormat ; 15 import java.util.Locale ; 16 17 import org.eclipse.core.runtime.IProgressMonitor; 18 19 import org.eclipse.jface.preference.IPreferenceStore; 20 21 import org.eclipse.jface.text.BadLocationException; 22 import org.eclipse.jface.text.IDocument; 23 import org.eclipse.jface.text.IRegion; 24 import org.eclipse.jface.text.ITypedRegion; 25 import org.eclipse.jface.text.TextUtilities; 26 import org.eclipse.jface.text.reconciler.DirtyRegion; 27 import org.eclipse.jface.text.reconciler.IReconcilingStrategy; 28 import org.eclipse.jface.text.reconciler.IReconcilingStrategyExtension; 29 import org.eclipse.jface.text.source.IAnnotationModel; 30 31 import org.eclipse.ui.texteditor.ITextEditor; 32 33 import org.eclipse.jdt.core.IProblemRequestor; 34 import org.eclipse.jdt.core.compiler.IProblem; 35 36 import org.eclipse.jdt.internal.ui.JavaUIMessages; 37 import org.eclipse.jdt.internal.ui.text.spelling.engine.ISpellCheckEngine; 38 import org.eclipse.jdt.internal.ui.text.spelling.engine.ISpellCheckPreferenceKeys; 39 import org.eclipse.jdt.internal.ui.text.spelling.engine.ISpellChecker; 40 import org.eclipse.jdt.internal.ui.text.spelling.engine.ISpellEvent; 41 import org.eclipse.jdt.internal.ui.text.spelling.engine.ISpellEventListener; 42 43 48 public class SpellReconcileStrategy implements IReconcilingStrategy, IReconcilingStrategyExtension, ISpellEventListener { 49 50 53 public class SpellProblem implements IProblem { 54 55 56 public static final int Spelling= 0x80000000; 57 58 59 private int fEnd= 0; 60 61 62 private int fLine= 1; 63 64 65 private boolean fMatch; 66 67 68 private boolean fSentence= false; 69 70 71 private int fStart= 0; 72 73 74 private final String fWord; 75 76 82 protected SpellProblem(final String word) { 83 fWord= word; 84 } 85 86 89 public String [] getArguments() { 90 91 String prefix= ""; String postfix= ""; 94 try { 95 96 final IRegion line= fDocument.getLineInformationOfOffset(fStart); 97 98 prefix= fDocument.get(line.getOffset(), fStart - line.getOffset()); 99 postfix= fDocument.get(fEnd + 1, line.getOffset() + line.getLength() - fEnd); 100 101 } catch (BadLocationException exception) { 102 } 104 return new String [] { fWord, prefix, postfix, fSentence ? Boolean.toString(true) : Boolean.toString(false), fMatch ? Boolean.toString(true) : Boolean.toString(false)}; 105 } 106 107 110 public int getID() { 111 return Spelling; 112 } 113 114 117 public String getMessage() { 118 119 if (fSentence && fMatch) 120 return MessageFormat.format(JavaUIMessages.getString("Spelling.error.case.label"), new String [] { fWord }); 122 return MessageFormat.format(JavaUIMessages.getString("Spelling.error.label"), new String [] { fWord }); } 124 125 128 public char[] getOriginatingFileName() { 129 return fEditor.getEditorInput().getName().toCharArray(); 130 } 131 132 135 public final int getSourceEnd() { 136 return fEnd; 137 } 138 139 142 public final int getSourceLineNumber() { 143 return fLine; 144 } 145 146 149 public final int getSourceStart() { 150 return fStart; 151 } 152 153 159 public final boolean isDictionaryMatch() { 160 return fMatch; 161 } 162 163 166 public final boolean isError() { 167 return false; 168 } 169 170 176 public final boolean isSentenceStart() { 177 return fSentence; 178 } 179 180 183 public final boolean isWarning() { 184 return true; 185 } 186 187 194 public final void setDictionaryMatch(final boolean match) { 195 fMatch= match; 196 } 197 198 205 public final void setSentenceStart(final boolean sentence) { 206 fSentence= sentence; 207 } 208 209 212 public final void setSourceEnd(final int end) { 213 fEnd= end; 214 } 215 216 219 public final void setSourceLineNumber(final int line) { 220 fLine= line; 221 } 222 223 226 public final void setSourceStart(final int start) { 227 fStart= start; 228 } 229 } 230 231 232 private IDocument fDocument= null; 233 234 235 private final ITextEditor fEditor; 236 237 238 private Locale fLocale= SpellCheckEngine.getDefaultLocale(); 239 240 241 private final String fPartitioning; 242 243 244 private final IPreferenceStore fPreferences; 245 246 247 private IProblemRequestor fRequestor; 248 249 250 private IProgressMonitor fProgressMonitor; 251 252 253 263 public SpellReconcileStrategy(final ITextEditor editor, final String partitioning, final IPreferenceStore store) { 264 fEditor= editor; 265 fPartitioning= partitioning; 266 fPreferences= store; 267 268 updateProblemRequestor(); 269 } 270 271 276 public Locale getLocale() { 277 278 final String locale= fPreferences.getString(ISpellCheckPreferenceKeys.SPELLING_LOCALE); 279 if (locale.equals(fLocale.toString())) 280 return fLocale; 281 282 if (locale.length() >= 5) 283 return new Locale (locale.substring(0, 2), locale.substring(3, 5)); 284 285 return SpellCheckEngine.getDefaultLocale(); 286 } 287 288 291 public void handle(final ISpellEvent event) { 292 293 if (fRequestor != null) { 294 295 final SpellProblem problem= new SpellProblem(event.getWord()); 296 297 problem.setSourceStart(event.getBegin()); 298 problem.setSourceEnd(event.getEnd()); 299 problem.setSentenceStart(event.isStart()); 300 problem.setDictionaryMatch(event.isMatch()); 301 302 try { 303 problem.setSourceLineNumber(fDocument.getLineOfOffset(event.getBegin()) + 1); 304 } catch (BadLocationException x) { 305 } 307 308 fRequestor.acceptProblem(problem); 309 } 310 } 311 312 315 public void initialReconcile() { 316 reconcile(); 317 } 318 319 322 public void reconcile(DirtyRegion dirtyRegion, IRegion subRegion) { 323 reconcile(); 324 } 325 326 329 public void reconcile(IRegion region) { 330 reconcile(); 331 } 332 333 private void reconcile() { 334 335 if (fPreferences.getBoolean(ISpellCheckPreferenceKeys.SPELLING_CHECK_SPELLING) && fRequestor != null) { 336 337 try { 338 339 fRequestor.beginReporting(); 340 341 ITypedRegion partition= null; 342 final ITypedRegion[] partitions= TextUtilities.computePartitioning(fDocument, fPartitioning, 0, fDocument.getLength(), false); 343 344 final Locale locale= getLocale(); 345 final ISpellCheckEngine engine= SpellCheckEngine.getInstance(); 346 347 final ISpellChecker checker= engine.createSpellChecker(locale, fPreferences); 348 if (checker != null) { 349 try { 350 checker.addListener(this); 351 352 for (int index= 0; index < partitions.length; index++) { 353 if (fProgressMonitor != null && fProgressMonitor.isCanceled()) 354 return; 355 356 partition= partitions[index]; 357 if (!partition.getType().equals(IDocument.DEFAULT_CONTENT_TYPE)) 358 checker.execute(new SpellCheckIterator(fDocument, partition, locale)); 359 } 360 361 } finally { 362 checker.removeListener(this); 363 } 364 } 365 } catch (BadLocationException exception) { 366 } finally { 368 fRequestor.endReporting(); 369 } 370 } 371 } 372 373 376 public final void setDocument(final IDocument document) { 377 fDocument= document; 378 379 updateProblemRequestor(); 380 } 381 382 385 public final void setProgressMonitor(final IProgressMonitor monitor) { 386 fProgressMonitor= monitor; 387 } 388 389 394 private void updateProblemRequestor() { 395 final IAnnotationModel model= fEditor.getDocumentProvider().getAnnotationModel(fEditor.getEditorInput()); 396 fRequestor= (model instanceof IProblemRequestor) ? (IProblemRequestor) model : null; 397 } 398 } 399 | Popular Tags |