1 /*******************************************************************************
2 * Copyright (c) 2000, 2011 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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.text.spelling;
13 import org.eclipse.core.runtime.Platform;
14 import org.eclipse.core.runtime.content.IContentType;
16 import org.eclipse.jface.text.BadLocationException;
17 import org.eclipse.jface.text.IDocument;
18 import org.eclipse.jface.text.IRegion;
19 import org.eclipse.jface.text.source.IAnnotationModel;
20 import org.eclipse.jface.text.source.ISourceViewer;
22 import org.eclipse.ui.IEditorInput;
24 import org.eclipse.ui.texteditor.IDocumentProvider;
25 import org.eclipse.ui.texteditor.ITextEditor;
26 import org.eclipse.ui.texteditor.spelling.ISpellingProblemCollector;
27 import org.eclipse.ui.texteditor.spelling.SpellingProblem;
28 import org.eclipse.ui.texteditor.spelling.SpellingReconcileStrategy;
29 import org.eclipse.ui.texteditor.spelling.SpellingService;
31 import org.eclipse.ui.editors.text.EditorsUI;
33 import org.eclipse.jdt.core.IProblemRequestor;
34 import org.eclipse.jdt.core.JavaCore;
35 import org.eclipse.jdt.core.compiler.IProblem;
39 * Reconcile strategy for spell checking comments.
43 public class JavaSpellingReconcileStrategy extends SpellingReconcileStrategy {
47 * Spelling problem collector that forwards {@link SpellingProblem}s as
48 * {@link IProblem}s to the {@link IProblemRequestor}.
50 private class SpellingProblemCollector implements ISpellingProblemCollector {
53 * @see org.eclipse.ui.texteditor.spelling.ISpellingProblemCollector#accept(org.eclipse.ui.texteditor.spelling.SpellingProblem)
55 public void accept(SpellingProblem problem) {
56 IProblemRequestor requestor= fRequestor;
57 if (requestor != null) {
59 int line= getDocument().getLineOfOffset(problem.getOffset()) + 1;
60 String word= getDocument().get(problem.getOffset(), problem.getLength());
61 boolean dictionaryMatch= false;
62 boolean sentenceStart= false;
63 if (problem instanceof JavaSpellingProblem) {
64 dictionaryMatch= ((JavaSpellingProblem)problem).isDictionaryMatch();
65 sentenceStart= ((JavaSpellingProblem) problem).isSentenceStart();
67 // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=81514
68 IEditorInput editorInput= fEditor.getEditorInput();
69 if (editorInput != null) {
70 CoreSpellingProblem iProblem= new CoreSpellingProblem(problem.getOffset(), problem.getOffset() + problem.getLength() - 1, line, problem.getMessage(), word, dictionaryMatch, sentenceStart, getDocument(), editorInput.getName());
71 requestor.acceptProblem(iProblem);
73 } catch (BadLocationException x) {
74 // drop this SpellingProblem
80 * @see org.eclipse.ui.texteditor.spelling.ISpellingProblemCollector#beginCollecting()
82 public void beginCollecting() {
83 if (fRequestor != null)
84 fRequestor.beginReporting();
88 * @see org.eclipse.ui.texteditor.spelling.ISpellingProblemCollector#endCollecting()
90 public void endCollecting() {
91 if (fRequestor != null)
92 fRequestor.endReporting();
97 /** The id of the problem */
98 public static final int SPELLING_PROBLEM_ID= 0x80000000;
100 /** Properties file content type */
101 private static final IContentType JAVA_CONTENT_TYPE= Platform.getContentTypeManager().getContentType(JavaCore.JAVA_SOURCE_CONTENT_TYPE);
103 /** The text editor to operate on. */
104 private ITextEditor fEditor;
106 /** The problem requester. */
107 private IProblemRequestor fRequestor;
111 * Creates a new comment reconcile strategy.
113 * @param viewer the source viewer
114 * @param editor the text editor to operate on
116 public JavaSpellingReconcileStrategy(ISourceViewer viewer, ITextEditor editor) {
117 super(viewer, EditorsUI.getSpellingService());
122 * @see org.eclipse.jface.text.reconciler.IReconcilingStrategy#reconcile(org.eclipse.jface.text.IRegion)
125 public void reconcile(IRegion region) {
126 if (fRequestor != null && isSpellingEnabled())
127 super.reconcile(region);
130 private boolean isSpellingEnabled() {
131 return EditorsUI.getPreferenceStore().getBoolean(SpellingService.PREFERENCE_SPELLING_ENABLED);
135 * @see org.eclipse.ui.texteditor.spelling.SpellingReconcileStrategy#createSpellingProblemCollector()
139 protected ISpellingProblemCollector createSpellingProblemCollector() {
140 return new SpellingProblemCollector();
144 * @see org.eclipse.ui.texteditor.spelling.SpellingReconcileStrategy#getContentType()
148 protected IContentType getContentType() {
149 return JAVA_CONTENT_TYPE;
153 * @see org.eclipse.jface.text.reconciler.IReconcilingStrategy#setDocument(org.eclipse.jface.text.IDocument)
156 public void setDocument(IDocument document) {
157 super.setDocument(document);
158 updateProblemRequester();
162 * Update the problem requester based on the current editor
164 private void updateProblemRequester() {
165 IAnnotationModel model= getAnnotationModel();
166 fRequestor= (model instanceof IProblemRequestor) ? (IProblemRequestor) model : null;
170 * @see org.eclipse.ui.texteditor.spelling.SpellingReconcileStrategy#getAnnotationModel()
174 protected IAnnotationModel getAnnotationModel() {
175 final IDocumentProvider documentProvider= fEditor.getDocumentProvider();
176 if (documentProvider == null)
178 return documentProvider.getAnnotationModel(fEditor.getEditorInput());