KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > text > spelling > PropertiesSpellingReconcileStrategy


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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
12 package org.eclipse.jdt.internal.ui.text.spelling;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Map JavaDoc;
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 /**
49  * Reconcile strategy for spell checking comments.
50  *
51  * @since 3.1
52  */

53 public class PropertiesSpellingReconcileStrategy implements IReconcilingStrategy, IReconcilingStrategyExtension {
54
55     /**
56      * Spelling problem collector that forwards {@link SpellingProblem}s as
57      * {@link IProblem}s to the {@link org.eclipse.jdt.core.IProblemRequestor}.
58      */

59     private class SpellingProblemCollector implements ISpellingProblemCollector {
60
61         /** Annotation model */
62         private IAnnotationModel fAnnotationModel;
63
64         /** Annotations to add */
65         private Map JavaDoc fAddAnnotations;
66
67         /**
68          * Initializes this collector with the given annotation model.
69          *
70          * @param annotationModel the annotation model
71          */

72         public SpellingProblemCollector(IAnnotationModel annotationModel) {
73             fAnnotationModel= annotationModel;
74         }
75
76         /*
77          * @see org.eclipse.ui.texteditor.spelling.ISpellingProblemCollector#accept(org.eclipse.ui.texteditor.spelling.SpellingProblem)
78          */

79         public void accept(SpellingProblem problem) {
80             try {
81                 int line= fDocument.getLineOfOffset(problem.getOffset()) + 1;
82                 String JavaDoc 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                 // see: https://bugs.eclipse.org/bugs/show_bug.cgi?id=81514
90
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                 // drop this SpellingProblem
97
}
98         }
99
100         /*
101          * @see org.eclipse.ui.texteditor.spelling.ISpellingProblemCollector#beginCollecting()
102          */

103         public void beginCollecting() {
104             fAddAnnotations= new HashMap JavaDoc();
105         }
106
107         /*
108          * @see org.eclipse.ui.texteditor.spelling.ISpellingProblemCollector#endCollecting()
109          */

110         public void endCollecting() {
111             List JavaDoc removeAnnotations= new ArrayList JavaDoc();
112             for (Iterator JavaDoc 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 JavaDoc iter= removeAnnotations.iterator(); iter.hasNext();)
122                     fAnnotationModel.removeAnnotation((Annotation) iter.next());
123                 for (Iterator JavaDoc 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     /** The id of the problem */
134     public static final int SPELLING_PROBLEM_ID= 0x80000000;
135
136     /** The text editor to operate on. */
137     private ITextEditor fEditor;
138
139     /** The document to operate on. */
140     private IDocument fDocument;
141
142     /** The progress monitor. */
143     private IProgressMonitor fProgressMonitor;
144     
145     /**
146      * The spelling context containing the Java properties
147      * content type.
148      * <p>
149      * Since his reconcile strategy is for the Properties File
150      * editor which normally edits Java properties files we always
151      * use the Java properties file content type for performance
152      * reasons.
153      * </p>
154      * @since 3.2
155      */

156     private SpellingContext fSpellingContext;
157
158
159     /**
160      * Creates a new comment reconcile strategy.
161      *
162      * @param editor the text editor to operate on
163      */

164     public PropertiesSpellingReconcileStrategy(ITextEditor editor) {
165         fEditor= editor;
166         fSpellingContext= new SpellingContext();
167         fSpellingContext.setContentType(Platform.getContentTypeManager().getContentType("org.eclipse.jdt.core.javaProperties")); //$NON-NLS-1$
168
}
169
170     /*
171      * @see org.eclipse.jface.text.reconciler.IReconcilingStrategyExtension#initialReconcile()
172      */

173     public void initialReconcile() {
174         reconcile(new Region(0, fDocument.getLength()));
175     }
176
177     /*
178      * @see org.eclipse.jface.text.reconciler.IReconcilingStrategy#reconcile(org.eclipse.jface.text.reconciler.DirtyRegion,org.eclipse.jface.text.IRegion)
179      */

180     public void reconcile(DirtyRegion dirtyRegion, IRegion subRegion) {
181         reconcile(subRegion);
182     }
183
184     /*
185      * @see org.eclipse.jface.text.reconciler.IReconcilingStrategy#reconcile(org.eclipse.jface.text.IRegion)
186      */

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     /*
198      * @see org.eclipse.jface.text.reconciler.IReconcilingStrategy#setDocument(org.eclipse.jface.text.IDocument)
199      */

200     public void setDocument(IDocument document) {
201         fDocument= document;
202     }
203
204     /*
205      * @see org.eclipse.jface.text.reconciler.IReconcilingStrategyExtension#setProgressMonitor(org.eclipse.core.runtime.IProgressMonitor)
206      */

207     public void setProgressMonitor(IProgressMonitor monitor) {
208         fProgressMonitor= monitor;
209     }
210
211     /**
212      * Returns the annotation model of the underlying editor input.
213      *
214      * @return the annotation model of the underlying editor input or
215      * <code>null</code> if none could be determined
216      */

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