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;
25 import net.sourceforge.phpeclipse.phpeditor.php.PHPDocumentPartitioner;
27 import org.eclipse.core.runtime.IProgressMonitor;
28 import org.eclipse.jface.preference.IPreferenceStore;
29 import org.eclipse.jface.text.BadLocationException;
30 import org.eclipse.jface.text.IDocument;
31 import org.eclipse.jface.text.IRegion;
32 import org.eclipse.jface.text.ITypedRegion;
33 import org.eclipse.jface.text.TextUtilities;
34 import org.eclipse.jface.text.reconciler.DirtyRegion;
35 import org.eclipse.jface.text.reconciler.IReconcilingStrategy;
36 import org.eclipse.jface.text.reconciler.IReconcilingStrategyExtension;
37 import org.eclipse.jface.text.source.IAnnotationModel;
38 import org.eclipse.ui.texteditor.ITextEditor;
41 * Reconcile strategy to spell-check comments.
45 public class SpellReconcileStrategy implements IReconcilingStrategy, IReconcilingStrategyExtension, ISpellEventListener {
48 * Spelling problem to be accepted by problem requestors.
50 public class SpellProblem implements IProblem {
52 /** The id of the problem */
53 public static final int Spelling= 0x80000000;
55 /** The end offset of the problem */
58 /** The line number of the problem */
61 /** Was the word found in the dictionary? */
62 private boolean fMatch;
64 /** Does the word start a new sentence? */
65 private boolean fSentence= false;
67 /** The start offset of the problem */
68 private int fStart= 0;
70 /** The word which caused the problem */
71 private final String fWord;
74 * Creates a new spelling problem
77 * The word which caused the problem
79 protected SpellProblem(final String word) {
84 * @see net.sourceforge.phpdt.core.compiler.IProblem#getArguments()
86 public String[] getArguments() {
88 String prefix= ""; //$NON-NLS-1$
89 String postfix= ""; //$NON-NLS-1$
93 final IRegion line= fDocument.getLineInformationOfOffset(fStart);
95 prefix= fDocument.get(line.getOffset(), fStart - line.getOffset());
96 postfix= fDocument.get(fEnd + 1, line.getOffset() + line.getLength() - fEnd);
98 } catch (BadLocationException exception) {
101 return new String[] { fWord, prefix, postfix, fSentence ? Boolean.toString(true) : Boolean.toString(false), fMatch ? Boolean.toString(true) : Boolean.toString(false)};
105 * @see net.sourceforge.phpdt.core.compiler.IProblem#getID()
112 * @see net.sourceforge.phpdt.core.compiler.IProblem#getMessage()
114 public String getMessage() {
116 if (fSentence && fMatch)
117 return MessageFormat.format(PHPUIMessages.getString("Spelling.error.case.label"), new String[] { fWord }); //$NON-NLS-1$
119 return MessageFormat.format(PHPUIMessages.getString("Spelling.error.label"), new String[] { fWord }); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
123 * @see net.sourceforge.phpdt.core.compiler.IProblem#getOriginatingFileName()
125 public char[] getOriginatingFileName() {
126 return fEditor.getEditorInput().getName().toCharArray();
130 * @see net.sourceforge.phpdt.core.compiler.IProblem#getSourceEnd()
132 public final int getSourceEnd() {
137 * @see net.sourceforge.phpdt.core.compiler.IProblem#getSourceLineNumber()
139 public final int getSourceLineNumber() {
144 * @see net.sourceforge.phpdt.core.compiler.IProblem#getSourceStart()
146 public final int getSourceStart() {
151 * Was the problem word found in the dictionary?
153 * @return <code>true</code> iff the word was found, <code>false</code>
156 public final boolean isDictionaryMatch() {
161 * @see net.sourceforge.phpdt.core.compiler.IProblem#isError()
163 public final boolean isError() {
168 * Does the problem word start a new sentence?
170 * @return <code>true</code> iff it starts a new sentence, <code>false</code>
173 public final boolean isSentenceStart() {
178 * @see net.sourceforge.phpdt.core.compiler.IProblem#isWarning()
180 public final boolean isWarning() {
185 * Sets whether the problem word was found in the dictionary.
188 * <code>true</code> iff the word was found, <code>false</code>
191 public final void setDictionaryMatch(final boolean match) {
196 * Sets whether the problem word starts a new sentence.
199 * <code>true</code> iff the word starts a new sentence,
200 * <code>false</code> otherwise.
202 public final void setSentenceStart(final boolean sentence) {
207 * @see net.sourceforge.phpdt.core.compiler.IProblem#setSourceEnd(int)
209 public final void setSourceEnd(final int end) {
214 * @see net.sourceforge.phpdt.core.compiler.IProblem#setSourceLineNumber(int)
216 public final void setSourceLineNumber(final int line) {
221 * @see net.sourceforge.phpdt.core.compiler.IProblem#setSourceStart(int)
223 public final void setSourceStart(final int start) {
228 /** The document to operate on */
229 private IDocument fDocument= null;
231 /** The text editor to operate on */
232 private final ITextEditor fEditor;
234 /** The current locale */
235 private Locale fLocale= SpellCheckEngine.getDefaultLocale();
237 /** The partitioning of the document */
238 private final String fPartitioning;
240 /** The preference store to use */
241 private final IPreferenceStore fPreferences;
243 /** The problem requestor */
244 private IProblemRequestor fRequestor;
247 * Creates a new comment reconcile strategy.
250 * The text editor to operate on
251 * @param partitioning
252 * The partitioning of the document
254 * The preference store to get the preferences from
256 public SpellReconcileStrategy(final ITextEditor editor, final String partitioning, final IPreferenceStore store) {
258 fPartitioning= partitioning;
261 updateProblemRequestor();
265 * Returns the current locale of the spell checking preferences.
267 * @return The current locale of the spell checking preferences
269 public Locale getLocale() {
271 final String locale= fPreferences.getString(ISpellCheckPreferenceKeys.SPELLING_LOCALE);
272 if (locale.equals(fLocale.toString()))
275 if (locale.length() >= 5)
276 return new Locale(locale.substring(0, 2), locale.substring(3, 5));
278 return SpellCheckEngine.getDefaultLocale();
282 * @see net.sourceforge.phpdt.internal.ui.text.spelling.engine.ISpellEventListener#handle(net.sourceforge.phpdt.internal.ui.text.spelling.engine.ISpellEvent)
284 public void handle(final ISpellEvent event) {
286 if (fRequestor != null) {
288 final SpellProblem problem= new SpellProblem(event.getWord());
290 problem.setSourceStart(event.getBegin());
291 problem.setSourceEnd(event.getEnd());
292 problem.setSentenceStart(event.isStart());
293 problem.setDictionaryMatch(event.isMatch());
296 problem.setSourceLineNumber(fDocument.getLineOfOffset(event.getBegin()) + 1);
297 } catch (BadLocationException x) {
301 fRequestor.acceptProblem(problem);
306 * @see org.eclipse.jface.text.reconciler.IReconcilingStrategyExtension#initialReconcile()
308 public void initialReconcile() {
312 * @see org.eclipse.jface.text.reconciler.IReconcilingStrategy#reconcile(org.eclipse.jface.text.reconciler.DirtyRegion,org.eclipse.jface.text.IRegion)
314 public void reconcile(final DirtyRegion dirtyRegion, final IRegion subRegion) {
315 reconcile(subRegion);
319 * @see org.eclipse.jface.text.reconciler.IReconcilingStrategy#reconcile(org.eclipse.jface.text.IRegion)
321 public void reconcile(final IRegion region) {
323 if (fPreferences.getBoolean(ISpellCheckPreferenceKeys.SPELLING_CHECK_SPELLING) && fRequestor != null) {
327 fRequestor.beginReporting();
329 ITypedRegion partition= null;
330 final ITypedRegion[] partitions= TextUtilities.computePartitioning(fDocument, fPartitioning, 0, fDocument.getLength(), false);
332 final Locale locale= getLocale();
333 final ISpellCheckEngine engine= SpellCheckEngine.getInstance();
335 final ISpellChecker checker= engine.createSpellChecker(locale, fPreferences);
336 if (checker != null) {
338 checker.addListener(this);
340 for (int index= 0; index < partitions.length; index++) {
341 partition= partitions[index];
342 if (!partition.getType().equals(IDocument.DEFAULT_CONTENT_TYPE) &&
343 !partition.getType().equals(PHPDocumentPartitioner.PHP_SCRIPT_CODE))
344 checker.execute(new SpellCheckIterator(fDocument, partition, locale));
348 checker.removeListener(this);
351 } catch (BadLocationException exception) {
354 fRequestor.endReporting();
360 * @see org.eclipse.jface.text.reconciler.IReconcilingStrategy#setDocument(org.eclipse.jface.text.IDocument)
362 public final void setDocument(final IDocument document) {
365 updateProblemRequestor();
369 * @see org.eclipse.jface.text.reconciler.IReconcilingStrategyExtension#setProgressMonitor(org.eclipse.core.runtime.IProgressMonitor)
371 public final void setProgressMonitor(final IProgressMonitor monitor) {
376 * Update the problem requestor based on the current editor
380 private void updateProblemRequestor() {
381 final IAnnotationModel model= fEditor.getDocumentProvider().getAnnotationModel(fEditor.getEditorInput());
382 fRequestor= (model instanceof IProblemRequestor) ? (IProblemRequestor) model : null;