1 11 package org.eclipse.ui.texteditor.spelling; 12 13 import java.util.ArrayList ; 14 import java.util.HashMap ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 import java.util.Map ; 18 19 import org.eclipse.core.runtime.Assert; 20 import org.eclipse.core.runtime.IProgressMonitor; 21 import org.eclipse.core.runtime.Platform; 22 import org.eclipse.core.runtime.content.IContentType; 23 import org.eclipse.core.runtime.content.IContentTypeManager; 24 25 import org.eclipse.jface.text.IDocument; 26 import org.eclipse.jface.text.IRegion; 27 import org.eclipse.jface.text.ISynchronizable; 28 import org.eclipse.jface.text.Position; 29 import org.eclipse.jface.text.Region; 30 import org.eclipse.jface.text.reconciler.DirtyRegion; 31 import org.eclipse.jface.text.reconciler.IReconcilingStrategy; 32 import org.eclipse.jface.text.reconciler.IReconcilingStrategyExtension; 33 import org.eclipse.jface.text.source.Annotation; 34 import org.eclipse.jface.text.source.IAnnotationModel; 35 import org.eclipse.jface.text.source.IAnnotationModelExtension; 36 import org.eclipse.jface.text.source.ISourceViewer; 37 38 39 44 public class SpellingReconcileStrategy implements IReconcilingStrategy, IReconcilingStrategyExtension { 45 46 47 50 private class SpellingProblemCollector implements ISpellingProblemCollector { 51 52 53 private IAnnotationModel fAnnotationModel; 54 55 56 private Map fAddAnnotations; 57 58 59 private Object fLockObject; 60 61 66 public SpellingProblemCollector(IAnnotationModel annotationModel) { 67 Assert.isLegal(annotationModel != null); 68 fAnnotationModel= annotationModel; 69 if (fAnnotationModel instanceof ISynchronizable) 70 fLockObject= ((ISynchronizable)fAnnotationModel).getLockObject(); 71 else 72 fLockObject= fAnnotationModel; 73 } 74 75 78 public void accept(SpellingProblem problem) { 79 fAddAnnotations.put(new SpellingAnnotation(problem), new Position(problem.getOffset(), problem.getLength())); 80 } 81 82 85 public void beginCollecting() { 86 fAddAnnotations= new HashMap (); 87 } 88 89 92 public void endCollecting() { 93 94 List toRemove= new ArrayList (); 95 96 synchronized (fLockObject) { 97 Iterator iter= fAnnotationModel.getAnnotationIterator(); 98 while (iter.hasNext()) { 99 Annotation annotation= (Annotation)iter.next(); 100 if (SpellingAnnotation.TYPE.equals(annotation.getType())) 101 toRemove.add(annotation); 102 } 103 Annotation[] annotationsToRemove= (Annotation[])toRemove.toArray(new Annotation[toRemove.size()]); 104 105 if (fAnnotationModel instanceof IAnnotationModelExtension) 106 ((IAnnotationModelExtension)fAnnotationModel).replaceAnnotations(annotationsToRemove, fAddAnnotations); 107 else { 108 for (int i= 0; i < annotationsToRemove.length; i++) 109 fAnnotationModel.removeAnnotation(annotationsToRemove[i]); 110 for (iter= fAddAnnotations.keySet().iterator(); iter.hasNext();) { 111 Annotation annotation= (Annotation)iter.next(); 112 fAnnotationModel.addAnnotation(annotation, (Position)fAddAnnotations.get(annotation)); 113 } 114 } 115 } 116 117 fAddAnnotations= null; 118 } 119 } 120 121 122 123 private static final IContentType TEXT_CONTENT_TYPE= Platform.getContentTypeManager().getContentType(IContentTypeManager.CT_TEXT); 124 125 126 private ISourceViewer fViewer; 127 128 129 private IDocument fDocument; 130 131 132 private IProgressMonitor fProgressMonitor; 133 134 private SpellingService fSpellingService; 135 136 private ISpellingProblemCollector fSpellingProblemCollector; 137 138 139 private SpellingContext fSpellingContext; 140 141 142 148 public SpellingReconcileStrategy(ISourceViewer viewer, SpellingService spellingService) { 149 Assert.isNotNull(viewer); 150 Assert.isNotNull(spellingService); 151 fViewer= viewer; 152 fSpellingService= spellingService; 153 fSpellingContext= new SpellingContext(); 154 fSpellingContext.setContentType(getContentType()); 155 156 } 157 158 161 public void initialReconcile() { 162 reconcile(new Region(0, fDocument.getLength())); 163 } 164 165 168 public void reconcile(DirtyRegion dirtyRegion, IRegion subRegion) { 169 reconcile(subRegion); 170 } 171 172 175 public void reconcile(IRegion region) { 176 if (getAnnotationModel() == null || fSpellingProblemCollector == null) 177 return; 178 179 fSpellingService.check(fDocument, fSpellingContext, fSpellingProblemCollector, fProgressMonitor); 180 } 181 182 188 protected IContentType getContentType() { 189 return TEXT_CONTENT_TYPE; 190 } 191 192 197 protected final IDocument getDocument() { 198 return fDocument; 199 } 200 201 204 public void setDocument(IDocument document) { 205 fDocument= document; 206 fSpellingProblemCollector= createSpellingProblemCollector(); 207 } 208 209 214 protected ISpellingProblemCollector createSpellingProblemCollector() { 215 IAnnotationModel model= getAnnotationModel(); 216 if (model == null) 217 return null; 218 return new SpellingProblemCollector(model); 219 } 220 221 224 public final void setProgressMonitor(IProgressMonitor monitor) { 225 fProgressMonitor= monitor; 226 } 227 228 234 protected IAnnotationModel getAnnotationModel() { 235 return fViewer.getAnnotationModel(); 236 } 237 238 } 239 | Popular Tags |