1 11 12 package org.eclipse.ui.texteditor.spelling; 13 14 import java.util.ArrayList ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 18 import org.eclipse.jface.text.BadLocationException; 19 import org.eclipse.jface.text.IDocument; 20 import org.eclipse.jface.text.Position; 21 import org.eclipse.jface.text.contentassist.ICompletionProposal; 22 import org.eclipse.jface.text.source.Annotation; 23 import org.eclipse.jface.text.source.IAnnotationModel; 24 import org.eclipse.jface.text.source.IAnnotationModelExtension; 25 26 import org.eclipse.ui.IEditorInput; 27 import org.eclipse.ui.texteditor.IDocumentProvider; 28 import org.eclipse.ui.texteditor.ITextEditor; 29 30 31 42 public abstract class SpellingProblem { 43 44 59 public static void removeAllInActiveEditor(ITextEditor editor, String word) { 60 if (editor == null) 61 return; 62 63 IDocumentProvider documentProvider= editor.getDocumentProvider(); 64 if (documentProvider == null) 65 return; 66 67 IEditorInput editorInput= editor.getEditorInput(); 68 if (editorInput == null) 69 return; 70 71 IAnnotationModel model= documentProvider.getAnnotationModel(editorInput); 72 if (model == null) 73 return; 74 75 IDocument document= documentProvider.getDocument(editorInput); 76 if (document == null) 77 return; 78 79 boolean supportsBatchReplace= (model instanceof IAnnotationModelExtension); 80 List toBeRemovedAnnotations= new ArrayList (); 81 Iterator iter= model.getAnnotationIterator(); 82 while (iter.hasNext()) { 83 Annotation annotation= (Annotation) iter.next(); 84 if (SpellingAnnotation.TYPE.equals(annotation.getType())) { 85 boolean doRemove= word == null; 86 if (word == null) 87 doRemove= true; 88 else { 89 String annotationWord= null; 90 Position pos= model.getPosition(annotation); 91 try { 92 annotationWord= document.get(pos.getOffset(), pos.getLength()); 93 } catch (BadLocationException e) { 94 continue; 95 } 96 doRemove= word.equals(annotationWord); 97 } 98 if (doRemove) { 99 if (supportsBatchReplace) 100 toBeRemovedAnnotations.add(annotation); 101 else 102 model.removeAnnotation(annotation); 103 } 104 } 105 } 106 107 if (supportsBatchReplace && !toBeRemovedAnnotations.isEmpty()) { 108 Annotation[] annotationArray= (Annotation[])toBeRemovedAnnotations.toArray(new Annotation[toBeRemovedAnnotations.size()]); 109 ((IAnnotationModelExtension)model).replaceAnnotations(annotationArray, null); 110 } 111 } 112 113 118 public abstract int getOffset(); 119 120 125 public abstract int getLength(); 126 127 132 public abstract String getMessage(); 133 134 139 public abstract ICompletionProposal[] getProposals(); 140 } 141 | Popular Tags |