1 11 12 package org.eclipse.jdt.internal.ui.text.spelling; 13 14 import java.util.ArrayList ; 15 import java.util.HashMap ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 import java.util.Map ; 19 20 import org.eclipse.core.runtime.IProgressMonitor; 21 import org.eclipse.core.runtime.Platform; 22 23 import org.eclipse.jface.text.BadLocationException; 24 import org.eclipse.jface.text.IDocument; 25 import org.eclipse.jface.text.IRegion; 26 import org.eclipse.jface.text.Position; 27 import org.eclipse.jface.text.Region; 28 import org.eclipse.jface.text.reconciler.DirtyRegion; 29 import org.eclipse.jface.text.reconciler.IReconcilingStrategy; 30 import org.eclipse.jface.text.reconciler.IReconcilingStrategyExtension; 31 import org.eclipse.jface.text.source.Annotation; 32 import org.eclipse.jface.text.source.IAnnotationModel; 33 import org.eclipse.jface.text.source.IAnnotationModelExtension; 34 35 import org.eclipse.ui.IEditorInput; 36 import org.eclipse.ui.texteditor.IDocumentProvider; 37 import org.eclipse.ui.texteditor.ITextEditor; 38 import org.eclipse.ui.texteditor.spelling.ISpellingProblemCollector; 39 import org.eclipse.ui.texteditor.spelling.SpellingContext; 40 import org.eclipse.ui.texteditor.spelling.SpellingProblem; 41 42 import org.eclipse.ui.editors.text.EditorsUI; 43 44 import org.eclipse.jdt.core.compiler.IProblem; 45 46 import org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitDocumentProvider.ProblemAnnotation; 47 48 53 public class PropertiesSpellingReconcileStrategy implements IReconcilingStrategy, IReconcilingStrategyExtension { 54 55 59 private class SpellingProblemCollector implements ISpellingProblemCollector { 60 61 62 private IAnnotationModel fAnnotationModel; 63 64 65 private Map fAddAnnotations; 66 67 72 public SpellingProblemCollector(IAnnotationModel annotationModel) { 73 fAnnotationModel= annotationModel; 74 } 75 76 79 public void accept(SpellingProblem problem) { 80 try { 81 int line= fDocument.getLineOfOffset(problem.getOffset()) + 1; 82 String word= fDocument.get(problem.getOffset(), problem.getLength()); 83 boolean dictionaryMatch= false; 84 boolean sentenceStart= false; 85 if (problem instanceof JavaSpellingProblem) { 86 dictionaryMatch= ((JavaSpellingProblem)problem).isDictionaryMatch(); 87 sentenceStart= ((JavaSpellingProblem) problem).isSentenceStart(); 88 } 89 IEditorInput editorInput= fEditor.getEditorInput(); 91 if (editorInput != null) { 92 CoreSpellingProblem iProblem= new CoreSpellingProblem(problem.getOffset(), problem.getOffset() + problem.getLength() - 1, line, problem.getMessage(), word, dictionaryMatch, sentenceStart, fDocument, editorInput.getName()); 93 fAddAnnotations.put(new ProblemAnnotation(iProblem, null), new Position(problem.getOffset(), problem.getLength())); 94 } 95 } catch (BadLocationException x) { 96 } 98 } 99 100 103 public void beginCollecting() { 104 fAddAnnotations= new HashMap (); 105 } 106 107 110 public void endCollecting() { 111 List removeAnnotations= new ArrayList (); 112 for (Iterator iter= fAnnotationModel.getAnnotationIterator(); iter.hasNext();) { 113 Annotation annotation= (Annotation) iter.next(); 114 if (ProblemAnnotation.SPELLING_ANNOTATION_TYPE.equals(annotation.getType())) 115 removeAnnotations.add(annotation); 116 } 117 118 if (fAnnotationModel instanceof IAnnotationModelExtension) 119 ((IAnnotationModelExtension) fAnnotationModel).replaceAnnotations((Annotation[]) removeAnnotations.toArray(new Annotation[removeAnnotations.size()]), fAddAnnotations); 120 else { 121 for (Iterator iter= removeAnnotations.iterator(); iter.hasNext();) 122 fAnnotationModel.removeAnnotation((Annotation) iter.next()); 123 for (Iterator iter= fAddAnnotations.keySet().iterator(); iter.hasNext();) { 124 Annotation annotation= (Annotation) iter.next(); 125 fAnnotationModel.addAnnotation(annotation, (Position) fAddAnnotations.get(annotation)); 126 } 127 } 128 129 fAddAnnotations= null; 130 } 131 } 132 133 134 public static final int SPELLING_PROBLEM_ID= 0x80000000; 135 136 137 private ITextEditor fEditor; 138 139 140 private IDocument fDocument; 141 142 143 private IProgressMonitor fProgressMonitor; 144 145 156 private SpellingContext fSpellingContext; 157 158 159 164 public PropertiesSpellingReconcileStrategy(ITextEditor editor) { 165 fEditor= editor; 166 fSpellingContext= new SpellingContext(); 167 fSpellingContext.setContentType(Platform.getContentTypeManager().getContentType("org.eclipse.jdt.core.javaProperties")); } 169 170 173 public void initialReconcile() { 174 reconcile(new Region(0, fDocument.getLength())); 175 } 176 177 180 public void reconcile(DirtyRegion dirtyRegion, IRegion subRegion) { 181 reconcile(subRegion); 182 } 183 184 187 public void reconcile(IRegion region) { 188 IAnnotationModel model= getAnnotationModel(); 189 if (model == null) 190 return; 191 192 PropertiesSpellingReconcileStrategy.SpellingProblemCollector collector= new SpellingProblemCollector(model); 193 EditorsUI.getSpellingService().check(fDocument, fSpellingContext, collector, fProgressMonitor); 194 195 } 196 197 200 public void setDocument(IDocument document) { 201 fDocument= document; 202 } 203 204 207 public void setProgressMonitor(IProgressMonitor monitor) { 208 fProgressMonitor= monitor; 209 } 210 211 217 private IAnnotationModel getAnnotationModel() { 218 IDocumentProvider documentProvider= fEditor.getDocumentProvider(); 219 if (documentProvider == null) 220 return null; 221 return documentProvider.getAnnotationModel(fEditor.getEditorInput()); 222 } 223 } 224 | Popular Tags |