KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > texteditor > spelling > SpellingReconcileStrategy


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.texteditor.spelling;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Map JavaDoc;
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 /**
40  * Reconcile strategy used for spell checking.
41  *
42  * @since 3.3
43  */

44 public class SpellingReconcileStrategy implements IReconcilingStrategy, IReconcilingStrategyExtension {
45
46     
47     /**
48      * Spelling problem collector.
49      */

50     private class SpellingProblemCollector implements ISpellingProblemCollector {
51
52         /** Annotation model. */
53         private IAnnotationModel fAnnotationModel;
54
55         /** Annotations to add. */
56         private Map JavaDoc fAddAnnotations;
57         
58         /** Lock object for modifying the annotations. */
59         private Object JavaDoc fLockObject;
60
61         /**
62          * Initializes this collector with the given annotation model.
63          *
64          * @param annotationModel the annotation model
65          */

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         /*
76          * @see org.eclipse.ui.texteditor.spelling.ISpellingProblemCollector#accept(org.eclipse.ui.texteditor.spelling.SpellingProblem)
77          */

78         public void accept(SpellingProblem problem) {
79             fAddAnnotations.put(new SpellingAnnotation(problem), new Position(problem.getOffset(), problem.getLength()));
80         }
81
82         /*
83          * @see org.eclipse.ui.texteditor.spelling.ISpellingProblemCollector#beginCollecting()
84          */

85         public void beginCollecting() {
86             fAddAnnotations= new HashMap JavaDoc();
87         }
88
89         /*
90          * @see org.eclipse.ui.texteditor.spelling.ISpellingProblemCollector#endCollecting()
91          */

92         public void endCollecting() {
93             
94             List JavaDoc toRemove= new ArrayList JavaDoc();
95             
96             synchronized (fLockObject) {
97                 Iterator JavaDoc 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     /** Text content type */
123     private static final IContentType TEXT_CONTENT_TYPE= Platform.getContentTypeManager().getContentType(IContentTypeManager.CT_TEXT);
124
125     /** The text editor to operate on. */
126     private ISourceViewer fViewer;
127
128     /** The document to operate on. */
129     private IDocument fDocument;
130
131     /** The progress monitor. */
132     private IProgressMonitor fProgressMonitor;
133
134     private SpellingService fSpellingService;
135     
136     private ISpellingProblemCollector fSpellingProblemCollector;
137     
138     /** The spelling context containing the Java source content type. */
139     private SpellingContext fSpellingContext;
140     
141     
142     /**
143      * Creates a new comment reconcile strategy.
144      *
145      * @param viewer the source viewer
146      * @param spellingService the spelling service to use
147      */

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     /*
159      * @see org.eclipse.jface.text.reconciler.IReconcilingStrategyExtension#initialReconcile()
160      */

161     public void initialReconcile() {
162         reconcile(new Region(0, fDocument.getLength()));
163     }
164
165     /*
166      * @see org.eclipse.jface.text.reconciler.IReconcilingStrategy#reconcile(org.eclipse.jface.text.reconciler.DirtyRegion,org.eclipse.jface.text.IRegion)
167      */

168     public void reconcile(DirtyRegion dirtyRegion, IRegion subRegion) {
169         reconcile(subRegion);
170     }
171
172     /*
173      * @see org.eclipse.jface.text.reconciler.IReconcilingStrategy#reconcile(org.eclipse.jface.text.IRegion)
174      */

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     /**
183      * Returns the content type of the underlying editor input.
184      *
185      * @return the content type of the underlying editor input or
186      * <code>null</code> if none could be determined
187      */

188     protected IContentType getContentType() {
189         return TEXT_CONTENT_TYPE;
190     }
191     
192     /**
193      * Returns the document which is spell checked.
194      *
195      * @return the document
196      */

197     protected final IDocument getDocument() {
198         return fDocument;
199     }
200
201     /*
202      * @see org.eclipse.jface.text.reconciler.IReconcilingStrategy#setDocument(org.eclipse.jface.text.IDocument)
203      */

204     public void setDocument(IDocument document) {
205         fDocument= document;
206         fSpellingProblemCollector= createSpellingProblemCollector();
207     }
208
209     /**
210      * Creates a new spelling problem collector.
211      *
212      * @return the collector or <code>null</code> if none is available
213      */

214     protected ISpellingProblemCollector createSpellingProblemCollector() {
215         IAnnotationModel model= getAnnotationModel();
216         if (model == null)
217             return null;
218         return new SpellingProblemCollector(model);
219     }
220
221     /*
222      * @see org.eclipse.jface.text.reconciler.IReconcilingStrategyExtension#setProgressMonitor(org.eclipse.core.runtime.IProgressMonitor)
223      */

224     public final void setProgressMonitor(IProgressMonitor monitor) {
225         fProgressMonitor= monitor;
226     }
227     
228     /**
229      * Returns the annotation model to be used by this reconcile strategy.
230      *
231      * @return the annotation model of the underlying editor input or
232      * <code>null</code> if none could be determined
233      */

234     protected IAnnotationModel getAnnotationModel() {
235         return fViewer.getAnnotationModel();
236     }
237
238 }
239
Popular Tags