KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.eclipse.core.runtime.Platform;
15 import org.eclipse.core.runtime.content.IContentType;
16
17 import org.eclipse.jface.text.BadLocationException;
18 import org.eclipse.jface.text.IDocument;
19 import org.eclipse.jface.text.IRegion;
20 import org.eclipse.jface.text.source.IAnnotationModel;
21 import org.eclipse.jface.text.source.ISourceViewer;
22
23 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;
30
31 import org.eclipse.ui.editors.text.EditorsUI;
32
33 import org.eclipse.jdt.core.IProblemRequestor;
34 import org.eclipse.jdt.core.JavaCore;
35 import org.eclipse.jdt.core.compiler.IProblem;
36
37 /**
38  * Reconcile strategy for spell checking comments.
39  *
40  * @since 3.1
41  */

42 public class JavaSpellingReconcileStrategy extends SpellingReconcileStrategy {
43
44     
45     /**
46      * Spelling problem collector that forwards {@link SpellingProblem}s as
47      * {@link IProblem}s to the {@link IProblemRequestor}.
48      */

49     private class SpellingProblemCollector implements ISpellingProblemCollector {
50
51         /*
52          * @see org.eclipse.ui.texteditor.spelling.ISpellingProblemCollector#accept(org.eclipse.ui.texteditor.spelling.SpellingProblem)
53          */

54         public void accept(SpellingProblem problem) {
55             IProblemRequestor requestor= fRequestor;
56             if (requestor != null) {
57                 try {
58                     int line= getDocument().getLineOfOffset(problem.getOffset()) + 1;
59                     String JavaDoc word= getDocument().get(problem.getOffset(), problem.getLength());
60                     boolean dictionaryMatch= false;
61                     boolean sentenceStart= false;
62                     if (problem instanceof JavaSpellingProblem) {
63                         dictionaryMatch= ((JavaSpellingProblem)problem).isDictionaryMatch();
64                         sentenceStart= ((JavaSpellingProblem) problem).isSentenceStart();
65                     }
66                     // see: https://bugs.eclipse.org/bugs/show_bug.cgi?id=81514
67
IEditorInput editorInput= fEditor.getEditorInput();
68                     if (editorInput != null) {
69                         CoreSpellingProblem iProblem= new CoreSpellingProblem(problem.getOffset(), problem.getOffset() + problem.getLength() - 1, line, problem.getMessage(), word, dictionaryMatch, sentenceStart, getDocument(), editorInput.getName());
70                         requestor.acceptProblem(iProblem);
71                     }
72                 } catch (BadLocationException x) {
73                     // drop this SpellingProblem
74
}
75             }
76         }
77
78         /*
79          * @see org.eclipse.ui.texteditor.spelling.ISpellingProblemCollector#beginCollecting()
80          */

81         public void beginCollecting() {
82             if (fRequestor != null)
83                 fRequestor.beginReporting();
84         }
85
86         /*
87          * @see org.eclipse.ui.texteditor.spelling.ISpellingProblemCollector#endCollecting()
88          */

89         public void endCollecting() {
90             if (fRequestor != null)
91                 fRequestor.endReporting();
92         }
93     }
94
95     
96     /** The id of the problem */
97     public static final int SPELLING_PROBLEM_ID= 0x80000000;
98
99     /** Properties file content type */
100     private static final IContentType JAVA_CONTENT_TYPE= Platform.getContentTypeManager().getContentType(JavaCore.JAVA_SOURCE_CONTENT_TYPE);
101
102     /** The text editor to operate on. */
103     private ITextEditor fEditor;
104
105     /** The problem requester. */
106     private IProblemRequestor fRequestor;
107
108     
109     /**
110      * Creates a new comment reconcile strategy.
111      *
112      * @param viewer the source viewer
113      * @param editor the text editor to operate on
114      */

115     public JavaSpellingReconcileStrategy(ISourceViewer viewer, ITextEditor editor) {
116         super(viewer, EditorsUI.getSpellingService());
117         fEditor= editor;
118     }
119
120     /*
121      * @see org.eclipse.jface.text.reconciler.IReconcilingStrategy#reconcile(org.eclipse.jface.text.IRegion)
122      */

123     public void reconcile(IRegion region) {
124         if (fRequestor != null && isSpellingEnabled())
125             super.reconcile(region);
126     }
127     
128     private boolean isSpellingEnabled() {
129         return EditorsUI.getPreferenceStore().getBoolean(SpellingService.PREFERENCE_SPELLING_ENABLED);
130     }
131
132     /*
133      * @see org.eclipse.ui.texteditor.spelling.SpellingReconcileStrategy#createSpellingProblemCollector()
134      * @since 3.3
135      */

136     protected ISpellingProblemCollector createSpellingProblemCollector() {
137         return new SpellingProblemCollector();
138     }
139     
140     /*
141      * @see org.eclipse.ui.texteditor.spelling.SpellingReconcileStrategy#getContentType()
142      * @since 3.3
143      */

144     protected IContentType getContentType() {
145         return JAVA_CONTENT_TYPE;
146     }
147
148     /*
149      * @see org.eclipse.jface.text.reconciler.IReconcilingStrategy#setDocument(org.eclipse.jface.text.IDocument)
150      */

151     public void setDocument(IDocument document) {
152         super.setDocument(document);
153         updateProblemRequester();
154     }
155
156     /**
157      * Update the problem requester based on the current editor
158      */

159     private void updateProblemRequester() {
160         IAnnotationModel model= getAnnotationModel();
161         fRequestor= (model instanceof IProblemRequestor) ? (IProblemRequestor) model : null;
162     }
163     
164     /*
165      * @see org.eclipse.ui.texteditor.spelling.SpellingReconcileStrategy#getAnnotationModel()
166      * @since 3.3
167      */

168     protected IAnnotationModel getAnnotationModel() {
169         final IDocumentProvider documentProvider= fEditor.getDocumentProvider();
170         if (documentProvider == null)
171             return null;
172         return documentProvider.getAnnotationModel(fEditor.getEditorInput());
173     }
174 }
175
Popular Tags