1 /*****************************************************************************
2 * Copyright (c) 2000, 2003 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
9 * IBM Corporation - initial API and implementation
10 *****************************************************************************/
12 package net.sourceforge.phpdt.internal.ui.text.spelling;
14 import java.text.MessageFormat;
15 import java.util.Locale;
17 import net.sourceforge.phpdt.core.IProblemRequestor;
18 import net.sourceforge.phpdt.core.compiler.IProblem;
19 import net.sourceforge.phpdt.internal.ui.PHPUIMessages;
20 import net.sourceforge.phpdt.internal.ui.text.spelling.engine.ISpellCheckEngine;
21 import net.sourceforge.phpdt.internal.ui.text.spelling.engine.ISpellCheckPreferenceKeys;
22 import net.sourceforge.phpdt.internal.ui.text.spelling.engine.ISpellChecker;
23 import net.sourceforge.phpdt.internal.ui.text.spelling.engine.ISpellEvent;
24 import net.sourceforge.phpdt.internal.ui.text.spelling.engine.ISpellEventListener;
26 import org.eclipse.core.runtime.IProgressMonitor;
27 import org.eclipse.jface.preference.IPreferenceStore;
28 import org.eclipse.jface.text.BadLocationException;
29 import org.eclipse.jface.text.IDocument;
30 import org.eclipse.jface.text.IRegion;
31 import org.eclipse.jface.text.ITypedRegion;
32 import org.eclipse.jface.text.TextUtilities;
33 import org.eclipse.jface.text.reconciler.DirtyRegion;
34 import org.eclipse.jface.text.reconciler.IReconcilingStrategy;
35 import org.eclipse.jface.text.reconciler.IReconcilingStrategyExtension;
36 import org.eclipse.jface.text.source.IAnnotationModel;
37 import org.eclipse.ui.texteditor.ITextEditor;
40 * Reconcile strategy to spell-check comments.
44 public class SpellReconcileStrategy implements IReconcilingStrategy, IReconcilingStrategyExtension, ISpellEventListener {
47 * Spelling problem to be accepted by problem requestors.
49 public class SpellProblem implements IProblem {
51 /** The id of the problem */
52 public static final int Spelling= 0x80000000;
54 /** The end offset of the problem */
57 /** The line number of the problem */
60 /** Was the word found in the dictionary? */
61 private boolean fMatch;
63 /** Does the word start a new sentence? */
64 private boolean fSentence= false;
66 /** The start offset of the problem */
67 private int fStart= 0;
69 /** The word which caused the problem */
70 private final String fWord;
73 * Creates a new spelling problem
76 * The word which caused the problem
78 protected SpellProblem(final String word) {
83 * @see org.eclipse.jdt.core.compiler.IProblem#getArguments()
85 public String[] getArguments() {
87 String prefix= ""; //$NON-NLS-1$
88 String postfix= ""; //$NON-NLS-1$
92 final IRegion line= fDocument.getLineInformationOfOffset(fStart);
94 prefix= fDocument.get(line.getOffset(), fStart - line.getOffset());
95 postfix= fDocument.get(fEnd + 1, line.getOffset() + line.getLength() - fEnd);
97 } catch (BadLocationException exception) {
100 return new String[] { fWord, prefix, postfix, fSentence ? Boolean.toString(true) : Boolean.toString(false), fMatch ? Boolean.toString(true) : Boolean.toString(false)};
104 * @see org.eclipse.jdt.core.compiler.IProblem#getID()
111 * @see org.eclipse.jdt.core.compiler.IProblem#getMessage()
113 public String getMessage() {
115 if (fSentence && fMatch)
116 return MessageFormat.format(PHPUIMessages.getString("Spelling.error.case.label"), new String[] { fWord }); //$NON-NLS-1$
118 return MessageFormat.format(PHPUIMessages.getString("Spelling.error.label"), new String[] { fWord }); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
122 * @see org.eclipse.jdt.core.compiler.IProblem#getOriginatingFileName()
124 public char[] getOriginatingFileName() {
125 return fEditor.getEditorInput().getName().toCharArray();
129 * @see org.eclipse.jdt.core.compiler.IProblem#getSourceEnd()
131 public final int getSourceEnd() {
136 * @see org.eclipse.jdt.core.compiler.IProblem#getSourceLineNumber()
138 public final int getSourceLineNumber() {
143 * @see org.eclipse.jdt.core.compiler.IProblem#getSourceStart()
145 public final int getSourceStart() {
150 * Was the problem word found in the dictionary?
152 * @return <code>true</code> iff the word was found, <code>false</code>
155 public final boolean isDictionaryMatch() {
160 * @see org.eclipse.jdt.core.compiler.IProblem#isError()
162 public final boolean isError() {
167 * Does the problem word start a new sentence?
169 * @return <code>true</code> iff it starts a new sentence, <code>false</code>
172 public final boolean isSentenceStart() {
177 * @see org.eclipse.jdt.core.compiler.IProblem#isWarning()
179 public final boolean isWarning() {
184 * Sets whether the problem word was found in the dictionary.
187 * <code>true</code> iff the word was found, <code>false</code>
190 public final void setDictionaryMatch(final boolean match) {
195 * Sets whether the problem word starts a new sentence.
198 * <code>true</code> iff the word starts a new sentence,
199 * <code>false</code> otherwise.
201 public final void setSentenceStart(final boolean sentence) {
206 * @see org.eclipse.jdt.core.compiler.IProblem#setSourceEnd(int)
208 public final void setSourceEnd(final int end) {
213 * @see org.eclipse.jdt.core.compiler.IProblem#setSourceLineNumber(int)
215 public final void setSourceLineNumber(final int line) {
220 * @see org.eclipse.jdt.core.compiler.IProblem#setSourceStart(int)
222 public final void setSourceStart(final int start) {
227 /** The document to operate on */
228 private IDocument fDocument= null;
230 /** The text editor to operate on */
231 private final ITextEditor fEditor;
233 /** The current locale */
234 private Locale fLocale= SpellCheckEngine.getDefaultLocale();
236 /** The partitioning of the document */
237 private final String fPartitioning;
239 /** The preference store to use */
240 private final IPreferenceStore fPreferences;
242 /** The problem requestor */
243 private IProblemRequestor fRequestor;
246 * Creates a new comment reconcile strategy.
249 * The text editor to operate on
250 * @param partitioning
251 * The partitioning of the document
253 * The preference store to get the preferences from
255 public SpellReconcileStrategy(final ITextEditor editor, final String partitioning, final IPreferenceStore store) {
257 fPartitioning= partitioning;
260 updateProblemRequestor();
264 * Returns the current locale of the spell checking preferences.
266 * @return The current locale of the spell checking preferences
268 public Locale getLocale() {
270 final String locale= fPreferences.getString(ISpellCheckPreferenceKeys.SPELLING_LOCALE);
271 if (locale.equals(fLocale.toString()))
274 if (locale.length() >= 5)
275 return new Locale(locale.substring(0, 2), locale.substring(3, 5));
277 return SpellCheckEngine.getDefaultLocale();
281 * @see org.eclipse.jdt.internal.ui.text.spelling.engine.ISpellEventListener#handle(org.eclipse.jdt.internal.ui.text.spelling.engine.ISpellEvent)
283 public void handle(final ISpellEvent event) {
285 if (fRequestor != null) {
287 final SpellProblem problem= new SpellProblem(event.getWord());
289 problem.setSourceStart(event.getBegin());
290 problem.setSourceEnd(event.getEnd());
291 problem.setSentenceStart(event.isStart());
292 problem.setDictionaryMatch(event.isMatch());
295 problem.setSourceLineNumber(fDocument.getLineOfOffset(event.getBegin()) + 1);
296 } catch (BadLocationException x) {
300 fRequestor.acceptProblem(problem);
305 * @see org.eclipse.jface.text.reconciler.IReconcilingStrategyExtension#initialReconcile()
307 public void initialReconcile() {
311 * @see org.eclipse.jface.text.reconciler.IReconcilingStrategy#reconcile(org.eclipse.jface.text.reconciler.DirtyRegion,org.eclipse.jface.text.IRegion)
313 public void reconcile(final DirtyRegion dirtyRegion, final IRegion subRegion) {
314 reconcile(subRegion);
318 * @see org.eclipse.jface.text.reconciler.IReconcilingStrategy#reconcile(org.eclipse.jface.text.IRegion)
320 public void reconcile(final IRegion region) {
322 if (fPreferences.getBoolean(ISpellCheckPreferenceKeys.SPELLING_CHECK_SPELLING) && fRequestor != null) {
326 fRequestor.beginReporting();
328 ITypedRegion partition= null;
329 final ITypedRegion[] partitions= TextUtilities.computePartitioning(fDocument, fPartitioning, 0, fDocument.getLength(), false);
331 final Locale locale= getLocale();
332 final ISpellCheckEngine engine= SpellCheckEngine.getInstance();
334 final ISpellChecker checker= engine.createSpellChecker(locale, fPreferences);
335 if (checker != null) {
337 checker.addListener(this);
339 for (int index= 0; index < partitions.length; index++) {
340 partition= partitions[index];
341 if (!partition.getType().equals(IDocument.DEFAULT_CONTENT_TYPE))
342 checker.execute(new SpellCheckIterator(fDocument, partition, locale));
346 checker.removeListener(this);
349 } catch (BadLocationException exception) {
352 fRequestor.endReporting();
358 * @see org.eclipse.jface.text.reconciler.IReconcilingStrategy#setDocument(org.eclipse.jface.text.IDocument)
360 public final void setDocument(final IDocument document) {
363 updateProblemRequestor();
367 * @see org.eclipse.jface.text.reconciler.IReconcilingStrategyExtension#setProgressMonitor(org.eclipse.core.runtime.IProgressMonitor)
369 public final void setProgressMonitor(final IProgressMonitor monitor) {
374 * Update the problem requestor based on the current editor
378 private void updateProblemRequestor() {
379 final IAnnotationModel model= fEditor.getDocumentProvider().getAnnotationModel(fEditor.getEditorInput());
380 fRequestor= (model instanceof IProblemRequestor) ? (IProblemRequestor) model : null;