]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-after/ui/org/eclipse/jdt/internal/ui/text/spelling/JavaSpellingReconcileStrategy.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-after / ui / org / eclipse / jdt / internal / ui / text / spelling / JavaSpellingReconcileStrategy.java
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
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.text.spelling;
12
13 import org.eclipse.core.runtime.Platform;
14 import org.eclipse.core.runtime.content.IContentType;
15
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;
21
22 import org.eclipse.ui.IEditorInput;
23
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 /**
39  * Reconcile strategy for spell checking comments.
40  *
41  * @since 3.1
42  */
43 public class JavaSpellingReconcileStrategy extends SpellingReconcileStrategy {
44
45
46         /**
47          * Spelling problem collector that forwards {@link SpellingProblem}s as
48          * {@link IProblem}s to the {@link IProblemRequestor}.
49          */
50         private class SpellingProblemCollector implements ISpellingProblemCollector {
51
52                 /*
53                  * @see org.eclipse.ui.texteditor.spelling.ISpellingProblemCollector#accept(org.eclipse.ui.texteditor.spelling.SpellingProblem)
54                  */
55                 public void accept(SpellingProblem problem) {
56                         IProblemRequestor requestor= fRequestor;
57                         if (requestor != null) {
58                                 try {
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();
66                                         }
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);
72                                         }
73                                 } catch (BadLocationException x) {
74                                         // drop this SpellingProblem
75                                 }
76                         }
77                 }
78
79                 /*
80                  * @see org.eclipse.ui.texteditor.spelling.ISpellingProblemCollector#beginCollecting()
81                  */
82                 public void beginCollecting() {
83                         if (fRequestor != null)
84                                 fRequestor.beginReporting();
85                 }
86
87                 /*
88                  * @see org.eclipse.ui.texteditor.spelling.ISpellingProblemCollector#endCollecting()
89                  */
90                 public void endCollecting() {
91                         if (fRequestor != null)
92                                 fRequestor.endReporting();
93                 }
94         }
95
96
97         /** The id of the problem */
98         public static final int SPELLING_PROBLEM_ID= 0x80000000;
99
100         /** Properties file content type */
101         private static final IContentType JAVA_CONTENT_TYPE= Platform.getContentTypeManager().getContentType(JavaCore.JAVA_SOURCE_CONTENT_TYPE);
102
103         /** The text editor to operate on. */
104         private ITextEditor fEditor;
105
106         /** The problem requester. */
107         private IProblemRequestor fRequestor;
108
109
110         /**
111          * Creates a new comment reconcile strategy.
112          *
113          * @param viewer the source viewer
114          * @param editor the text editor to operate on
115          */
116         public JavaSpellingReconcileStrategy(ISourceViewer viewer, ITextEditor editor) {
117                 super(viewer, EditorsUI.getSpellingService());
118                 fEditor= editor;
119         }
120
121         /*
122          * @see org.eclipse.jface.text.reconciler.IReconcilingStrategy#reconcile(org.eclipse.jface.text.IRegion)
123          */
124         @Override
125         public void reconcile(IRegion region) {
126                 if (fRequestor != null && isSpellingEnabled())
127                         super.reconcile(region);
128         }
129
130         private boolean isSpellingEnabled() {
131                 return EditorsUI.getPreferenceStore().getBoolean(SpellingService.PREFERENCE_SPELLING_ENABLED);
132         }
133
134         /*
135          * @see org.eclipse.ui.texteditor.spelling.SpellingReconcileStrategy#createSpellingProblemCollector()
136          * @since 3.3
137          */
138         @Override
139         protected ISpellingProblemCollector createSpellingProblemCollector() {
140                 return new SpellingProblemCollector();
141         }
142
143         /*
144          * @see org.eclipse.ui.texteditor.spelling.SpellingReconcileStrategy#getContentType()
145          * @since 3.3
146          */
147         @Override
148         protected IContentType getContentType() {
149                 return JAVA_CONTENT_TYPE;
150         }
151
152         /*
153          * @see org.eclipse.jface.text.reconciler.IReconcilingStrategy#setDocument(org.eclipse.jface.text.IDocument)
154          */
155         @Override
156         public void setDocument(IDocument document) {
157                 super.setDocument(document);
158                 updateProblemRequester();
159         }
160
161         /**
162          * Update the problem requester based on the current editor
163          */
164         private void updateProblemRequester() {
165                 IAnnotationModel model= getAnnotationModel();
166                 fRequestor= (model instanceof IProblemRequestor) ? (IProblemRequestor) model : null;
167         }
168
169         /*
170          * @see org.eclipse.ui.texteditor.spelling.SpellingReconcileStrategy#getAnnotationModel()
171          * @since 3.3
172          */
173         @Override
174         protected IAnnotationModel getAnnotationModel() {
175                 final IDocumentProvider documentProvider= fEditor.getDocumentProvider();
176                 if (documentProvider == null)
177                         return null;
178                 return documentProvider.getAnnotationModel(fEditor.getEditorInput());
179         }
180 }