KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-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.text.MessageFormat JavaDoc;
15 import java.util.Locale JavaDoc;
16
17 import org.eclipse.core.runtime.IProgressMonitor;
18
19 import org.eclipse.jface.preference.IPreferenceStore;
20
21 import org.eclipse.jface.text.BadLocationException;
22 import org.eclipse.jface.text.IDocument;
23 import org.eclipse.jface.text.IRegion;
24 import org.eclipse.jface.text.ITypedRegion;
25 import org.eclipse.jface.text.TextUtilities;
26 import org.eclipse.jface.text.reconciler.DirtyRegion;
27 import org.eclipse.jface.text.reconciler.IReconcilingStrategy;
28 import org.eclipse.jface.text.reconciler.IReconcilingStrategyExtension;
29 import org.eclipse.jface.text.source.IAnnotationModel;
30
31 import org.eclipse.ui.texteditor.ITextEditor;
32
33 import org.eclipse.jdt.core.IProblemRequestor;
34 import org.eclipse.jdt.core.compiler.IProblem;
35
36 import org.eclipse.jdt.internal.ui.JavaUIMessages;
37 import org.eclipse.jdt.internal.ui.text.spelling.engine.ISpellCheckEngine;
38 import org.eclipse.jdt.internal.ui.text.spelling.engine.ISpellCheckPreferenceKeys;
39 import org.eclipse.jdt.internal.ui.text.spelling.engine.ISpellChecker;
40 import org.eclipse.jdt.internal.ui.text.spelling.engine.ISpellEvent;
41 import org.eclipse.jdt.internal.ui.text.spelling.engine.ISpellEventListener;
42
43 /**
44  * Reconcile strategy to spell-check comments.
45  *
46  * @since 3.0
47  */

48 public class SpellReconcileStrategy implements IReconcilingStrategy, IReconcilingStrategyExtension, ISpellEventListener {
49
50     /**
51      * Spelling problem to be accepted by problem requestors.
52      */

53     public class SpellProblem implements IProblem {
54
55         /** The id of the problem */
56         public static final int Spelling= 0x80000000;
57
58         /** The end offset of the problem */
59         private int fEnd= 0;
60
61         /** The line number of the problem */
62         private int fLine= 1;
63
64         /** Was the word found in the dictionary? */
65         private boolean fMatch;
66
67         /** Does the word start a new sentence? */
68         private boolean fSentence= false;
69
70         /** The start offset of the problem */
71         private int fStart= 0;
72
73         /** The word which caused the problem */
74         private final String JavaDoc fWord;
75
76         /**
77          * Creates a new spelling problem
78          *
79          * @param word
80          * The word which caused the problem
81          */

82         protected SpellProblem(final String JavaDoc word) {
83             fWord= word;
84         }
85
86         /*
87          * @see org.eclipse.jdt.core.compiler.IProblem#getArguments()
88          */

89         public String JavaDoc[] getArguments() {
90
91             String JavaDoc prefix= ""; //$NON-NLS-1$
92
String JavaDoc postfix= ""; //$NON-NLS-1$
93

94             try {
95
96                 final IRegion line= fDocument.getLineInformationOfOffset(fStart);
97
98                 prefix= fDocument.get(line.getOffset(), fStart - line.getOffset());
99                 postfix= fDocument.get(fEnd + 1, line.getOffset() + line.getLength() - fEnd);
100
101             } catch (BadLocationException exception) {
102                 // Do nothing
103
}
104             return new String JavaDoc[] { fWord, prefix, postfix, fSentence ? Boolean.toString(true) : Boolean.toString(false), fMatch ? Boolean.toString(true) : Boolean.toString(false)};
105         }
106
107         /*
108          * @see org.eclipse.jdt.core.compiler.IProblem#getID()
109          */

110         public int getID() {
111             return Spelling;
112         }
113
114         /*
115          * @see org.eclipse.jdt.core.compiler.IProblem#getMessage()
116          */

117         public String JavaDoc getMessage() {
118
119             if (fSentence && fMatch)
120                 return MessageFormat.format(JavaUIMessages.getString("Spelling.error.case.label"), new String JavaDoc[] { fWord }); //$NON-NLS-1$
121

122             return MessageFormat.format(JavaUIMessages.getString("Spelling.error.label"), new String JavaDoc[] { fWord }); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
123
}
124
125         /*
126          * @see org.eclipse.jdt.core.compiler.IProblem#getOriginatingFileName()
127          */

128         public char[] getOriginatingFileName() {
129             return fEditor.getEditorInput().getName().toCharArray();
130         }
131
132         /*
133          * @see org.eclipse.jdt.core.compiler.IProblem#getSourceEnd()
134          */

135         public final int getSourceEnd() {
136             return fEnd;
137         }
138
139         /*
140          * @see org.eclipse.jdt.core.compiler.IProblem#getSourceLineNumber()
141          */

142         public final int getSourceLineNumber() {
143             return fLine;
144         }
145
146         /*
147          * @see org.eclipse.jdt.core.compiler.IProblem#getSourceStart()
148          */

149         public final int getSourceStart() {
150             return fStart;
151         }
152
153         /**
154          * Was the problem word found in the dictionary?
155          *
156          * @return <code>true</code> iff the word was found, <code>false</code>
157          * otherwise
158          */

159         public final boolean isDictionaryMatch() {
160             return fMatch;
161         }
162
163         /*
164          * @see org.eclipse.jdt.core.compiler.IProblem#isError()
165          */

166         public final boolean isError() {
167             return false;
168         }
169
170         /**
171          * Does the problem word start a new sentence?
172          *
173          * @return <code>true</code> iff it starts a new sentence, <code>false</code>
174          * otherwise
175          */

176         public final boolean isSentenceStart() {
177             return fSentence;
178         }
179
180         /*
181          * @see org.eclipse.jdt.core.compiler.IProblem#isWarning()
182          */

183         public final boolean isWarning() {
184             return true;
185         }
186
187         /**
188          * Sets whether the problem word was found in the dictionary.
189          *
190          * @param match
191          * <code>true</code> iff the word was found, <code>false</code>
192          * otherwise
193          */

194         public final void setDictionaryMatch(final boolean match) {
195             fMatch= match;
196         }
197
198         /**
199          * Sets whether the problem word starts a new sentence.
200          *
201          * @param sentence
202          * <code>true</code> iff the word starts a new sentence,
203          * <code>false</code> otherwise.
204          */

205         public final void setSentenceStart(final boolean sentence) {
206             fSentence= sentence;
207         }
208
209         /*
210          * @see org.eclipse.jdt.core.compiler.IProblem#setSourceEnd(int)
211          */

212         public final void setSourceEnd(final int end) {
213             fEnd= end;
214         }
215
216         /*
217          * @see org.eclipse.jdt.core.compiler.IProblem#setSourceLineNumber(int)
218          */

219         public final void setSourceLineNumber(final int line) {
220             fLine= line;
221         }
222
223         /*
224          * @see org.eclipse.jdt.core.compiler.IProblem#setSourceStart(int)
225          */

226         public final void setSourceStart(final int start) {
227             fStart= start;
228         }
229     }
230
231     /** The document to operate on. */
232     private IDocument fDocument= null;
233
234     /** The text editor to operate on. */
235     private final ITextEditor fEditor;
236
237     /** The current locale. */
238     private Locale JavaDoc fLocale= SpellCheckEngine.getDefaultLocale();
239
240     /** The partitioning of the document. */
241     private final String JavaDoc fPartitioning;
242
243     /** The preference store to use. */
244     private final IPreferenceStore fPreferences;
245
246     /** The problem requestor. */
247     private IProblemRequestor fRequestor;
248
249     /** The progress monitor. */
250     private IProgressMonitor fProgressMonitor;
251     
252
253     /**
254      * Creates a new comment reconcile strategy.
255      *
256      * @param editor
257      * The text editor to operate on
258      * @param partitioning
259      * The partitioning of the document
260      * @param store
261      * The preference store to get the preferences from
262      */

263     public SpellReconcileStrategy(final ITextEditor editor, final String JavaDoc partitioning, final IPreferenceStore store) {
264         fEditor= editor;
265         fPartitioning= partitioning;
266         fPreferences= store;
267
268         updateProblemRequestor();
269     }
270
271     /**
272      * Returns the current locale of the spell checking preferences.
273      *
274      * @return The current locale of the spell checking preferences
275      */

276     public Locale JavaDoc getLocale() {
277
278         final String JavaDoc locale= fPreferences.getString(ISpellCheckPreferenceKeys.SPELLING_LOCALE);
279         if (locale.equals(fLocale.toString()))
280             return fLocale;
281
282         if (locale.length() >= 5)
283             return new Locale JavaDoc(locale.substring(0, 2), locale.substring(3, 5));
284
285         return SpellCheckEngine.getDefaultLocale();
286     }
287
288     /*
289      * @see org.eclipse.jdt.internal.ui.text.spelling.engine.ISpellEventListener#handle(org.eclipse.jdt.internal.ui.text.spelling.engine.ISpellEvent)
290      */

291     public void handle(final ISpellEvent event) {
292         
293         if (fRequestor != null) {
294             
295             final SpellProblem problem= new SpellProblem(event.getWord());
296             
297             problem.setSourceStart(event.getBegin());
298             problem.setSourceEnd(event.getEnd());
299             problem.setSentenceStart(event.isStart());
300             problem.setDictionaryMatch(event.isMatch());
301             
302             try {
303                 problem.setSourceLineNumber(fDocument.getLineOfOffset(event.getBegin()) + 1);
304             } catch (BadLocationException x) {
305                 // Do nothing
306
}
307             
308             fRequestor.acceptProblem(problem);
309         }
310     }
311
312     /*
313      * @see org.eclipse.jface.text.reconciler.IReconcilingStrategyExtension#initialReconcile()
314      */

315     public void initialReconcile() {
316         reconcile();
317     }
318
319     /*
320      * @see org.eclipse.jface.text.reconciler.IReconcilingStrategy#reconcile(org.eclipse.jface.text.reconciler.DirtyRegion,org.eclipse.jface.text.IRegion)
321      */

322     public void reconcile(DirtyRegion dirtyRegion, IRegion subRegion) {
323         reconcile();
324     }
325
326     /*
327      * @see org.eclipse.jface.text.reconciler.IReconcilingStrategy#reconcile(org.eclipse.jface.text.IRegion)
328      */

329     public void reconcile(IRegion region) {
330         reconcile();
331     }
332     
333     private void reconcile() {
334
335         if (fPreferences.getBoolean(ISpellCheckPreferenceKeys.SPELLING_CHECK_SPELLING) && fRequestor != null) {
336
337             try {
338
339                 fRequestor.beginReporting();
340
341                 ITypedRegion partition= null;
342                 final ITypedRegion[] partitions= TextUtilities.computePartitioning(fDocument, fPartitioning, 0, fDocument.getLength(), false);
343
344                 final Locale JavaDoc locale= getLocale();
345                 final ISpellCheckEngine engine= SpellCheckEngine.getInstance();
346
347                 final ISpellChecker checker= engine.createSpellChecker(locale, fPreferences);
348                 if (checker != null) {
349                     try {
350                         checker.addListener(this);
351                         
352                         for (int index= 0; index < partitions.length; index++) {
353                             if (fProgressMonitor != null && fProgressMonitor.isCanceled())
354                                 return;
355                             
356                             partition= partitions[index];
357                             if (!partition.getType().equals(IDocument.DEFAULT_CONTENT_TYPE))
358                                 checker.execute(new SpellCheckIterator(fDocument, partition, locale));
359                         }
360                         
361                     } finally {
362                         checker.removeListener(this);
363                     }
364                 }
365             } catch (BadLocationException exception) {
366                 // Do nothing
367
} finally {
368                 fRequestor.endReporting();
369             }
370         }
371     }
372
373     /*
374      * @see org.eclipse.jface.text.reconciler.IReconcilingStrategy#setDocument(org.eclipse.jface.text.IDocument)
375      */

376     public final void setDocument(final IDocument document) {
377         fDocument= document;
378         
379         updateProblemRequestor();
380     }
381
382     /*
383      * @see org.eclipse.jface.text.reconciler.IReconcilingStrategyExtension#setProgressMonitor(org.eclipse.core.runtime.IProgressMonitor)
384      */

385     public final void setProgressMonitor(final IProgressMonitor monitor) {
386         fProgressMonitor= monitor;
387     }
388
389     /**
390      * Update the problem requestor based on the current editor
391      *
392      * @since 3.0
393      */

394     private void updateProblemRequestor() {
395         final IAnnotationModel model= fEditor.getDocumentProvider().getAnnotationModel(fEditor.getEditorInput());
396         fRequestor= (model instanceof IProblemRequestor) ? (IProblemRequestor) model : null;
397     }
398 }
399
Popular Tags