1 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 42 public class JavaSpellingReconcileStrategy extends SpellingReconcileStrategy { 43 44 45 49 private class SpellingProblemCollector implements ISpellingProblemCollector { 50 51 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 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 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 } 75 } 76 } 77 78 81 public void beginCollecting() { 82 if (fRequestor != null) 83 fRequestor.beginReporting(); 84 } 85 86 89 public void endCollecting() { 90 if (fRequestor != null) 91 fRequestor.endReporting(); 92 } 93 } 94 95 96 97 public static final int SPELLING_PROBLEM_ID= 0x80000000; 98 99 100 private static final IContentType JAVA_CONTENT_TYPE= Platform.getContentTypeManager().getContentType(JavaCore.JAVA_SOURCE_CONTENT_TYPE); 101 102 103 private ITextEditor fEditor; 104 105 106 private IProblemRequestor fRequestor; 107 108 109 115 public JavaSpellingReconcileStrategy(ISourceViewer viewer, ITextEditor editor) { 116 super(viewer, EditorsUI.getSpellingService()); 117 fEditor= editor; 118 } 119 120 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 136 protected ISpellingProblemCollector createSpellingProblemCollector() { 137 return new SpellingProblemCollector(); 138 } 139 140 144 protected IContentType getContentType() { 145 return JAVA_CONTENT_TYPE; 146 } 147 148 151 public void setDocument(IDocument document) { 152 super.setDocument(document); 153 updateProblemRequester(); 154 } 155 156 159 private void updateProblemRequester() { 160 IAnnotationModel model= getAnnotationModel(); 161 fRequestor= (model instanceof IProblemRequestor) ? (IProblemRequestor) model : null; 162 } 163 164 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 |