|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2004 - 2006 Mylar committers 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 |
|
| 9 |
package org.eclipse.mylar.internal.tasks.ui.editors; |
| 10 |
|
| 11 |
import java.util.ArrayList; |
| 12 |
import java.util.HashMap; |
| 13 |
import java.util.Iterator; |
| 14 |
import java.util.List; |
| 15 |
import java.util.Map; |
| 16 |
|
| 17 |
import org.eclipse.core.runtime.IProgressMonitor; |
| 18 |
import org.eclipse.core.runtime.Platform; |
| 19 |
import org.eclipse.core.runtime.content.IContentTypeManager; |
| 20 |
import org.eclipse.jdt.core.compiler.IProblem; |
| 21 |
import org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitDocumentProvider.ProblemAnnotation; |
| 22 |
import org.eclipse.jdt.internal.ui.text.spelling.CoreSpellingProblem; |
| 23 |
import org.eclipse.jdt.internal.ui.text.spelling.JavaSpellingProblem; |
| 24 |
import org.eclipse.jface.text.BadLocationException; |
| 25 |
import org.eclipse.jface.text.IDocument; |
| 26 |
import org.eclipse.jface.text.IRegion; |
| 27 |
import org.eclipse.jface.text.Position; |
| 28 |
import org.eclipse.jface.text.Region; |
| 29 |
import org.eclipse.jface.text.reconciler.DirtyRegion; |
| 30 |
import org.eclipse.jface.text.reconciler.IReconcilerExtension; |
| 31 |
import org.eclipse.jface.text.reconciler.IReconcilingStrategy; |
| 32 |
import org.eclipse.jface.text.source.Annotation; |
| 33 |
import org.eclipse.jface.text.source.IAnnotationModel; |
| 34 |
import org.eclipse.ui.IEditorInput; |
| 35 |
import org.eclipse.ui.editors.text.EditorsUI; |
| 36 |
import org.eclipse.ui.forms.editor.FormEditor; |
| 37 |
import org.eclipse.ui.texteditor.spelling.ISpellingProblemCollector; |
| 38 |
import org.eclipse.ui.texteditor.spelling.SpellingContext; |
| 39 |
import org.eclipse.ui.texteditor.spelling.SpellingProblem; |
| 40 |
|
| 41 |
/** |
| 42 |
* Adapted from |
| 43 |
* org.eclipse.jdt.internal.ui.text.spelling.PropertiesSpellingReconcileStrategy |
| 44 |
* |
| 45 |
* @author Jeff Pound |
| 46 |
*/ |
| 47 |
public class TaskSpellingReconcileStrategy implements IReconcilerExtension, IReconcilingStrategy { |
| 48 |
|
| 49 |
/** |
| 50 |
* Spelling problem collector that forwards {@link SpellingProblem}s as |
| 51 |
* {@link IProblem}s to the {@link org.eclipse.jdt.core.IProblemRequestor}. |
| 52 |
*/ |
| 53 |
private class SpellingProblemCollector implements ISpellingProblemCollector { |
| 54 |
|
| 55 |
/** Annotation model */ |
| 56 |
private IAnnotationModel fAnnotationModel; |
| 57 |
|
| 58 |
/** Annotations to add */ |
| 59 |
private Map<ProblemAnnotation, Position> fAddAnnotations; |
| 60 |
|
| 61 |
/** |
| 62 |
* Initializes this collector with the given annotation model. |
| 63 |
* |
| 64 |
* @param annotationModel |
| 65 |
* the annotation model |
| 66 |
*/ |
| 67 |
public SpellingProblemCollector(IAnnotationModel annotationModel) { |
| 68 |
fAnnotationModel = annotationModel; |
| 69 |
} |
| 70 |
|
| 71 |
/* |
| 72 |
* @see org.eclipse.ui.texteditor.spelling.ISpellingProblemCollector#accept(org.eclipse.ui.texteditor.spelling.SpellingProblem) |
| 73 |
*/ |
| 74 |
public void accept(SpellingProblem problem) { |
| 75 |
try { |
| 76 |
int line = fDocument.getLineOfOffset(problem.getOffset()) + 1; |
| 77 |
String word = fDocument.get(problem.getOffset(), problem.getLength()); |
| 78 |
boolean dictionaryMatch = false; |
| 79 |
boolean sentenceStart = false; |
| 80 |
if (problem instanceof JavaSpellingProblem) { |
| 81 |
dictionaryMatch = ((JavaSpellingProblem) problem).isDictionaryMatch(); |
| 82 |
sentenceStart = ((JavaSpellingProblem) problem).isSentenceStart(); |
| 83 |
} |
| 84 |
// see: https://bugs.eclipse.org/bugs/show_bug.cgi?id=81514 |
| 85 |
IEditorInput editorInput = editor.getEditorInput(); |
| 86 |
if (editorInput != null) { |
| 87 |
CoreSpellingProblem iProblem = new CoreSpellingProblem(problem.getOffset(), problem.getOffset() |
| 88 |
+ problem.getLength() - 1, line, problem.getMessage(), word, dictionaryMatch, |
| 89 |
sentenceStart, fDocument, editorInput.getName()); |
| 90 |
System.err.println(word); |
| 91 |
fAddAnnotations.put(new ProblemAnnotation(iProblem, null), new Position(problem.getOffset(), |
| 92 |
problem.getLength())); |
| 93 |
} |
| 94 |
} catch (BadLocationException x) { |
| 95 |
// drop this SpellingProblem |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
/* |
| 100 |
* @see org.eclipse.ui.texteditor.spelling.ISpellingProblemCollector#beginCollecting() |
| 101 |
*/ |
| 102 |
public void beginCollecting() { |
| 103 |
fAddAnnotations = new HashMap<ProblemAnnotation, Position>(); |
| 104 |
} |
| 105 |
|
| 106 |
/* |
| 107 |
* @see org.eclipse.ui.texteditor.spelling.ISpellingProblemCollector#endCollecting() |
| 108 |
*/ |
| 109 |
public void endCollecting() { |
| 110 |
|
| 111 |
List<Annotation> removeAnnotations = new ArrayList<Annotation>(); |
| 112 |
for (Iterator iter = fAnnotationModel.getAnnotationIterator(); iter.hasNext();) { |
| 113 |
Annotation annotation = (Annotation) iter.next(); |
| 114 |
if (ProblemAnnotation.SPELLING_ANNOTATION_TYPE.equals(annotation.getType())) |
| 115 |
removeAnnotations.add(annotation); |
| 116 |
} |
| 117 |
|
| 118 |
// if (fAnnotationModel instanceof IAnnotationModelExtension) |
| 119 |
// ((IAnnotationModelExtension) fAnnotationModel).replaceAnnotations((Annotation[]) removeAnnotations |
| 120 |
// .toArray(new Annotation[removeAnnotations.size()]), fAddAnnotations); |
| 121 |
// else { |
| 122 |
for (Iterator iter = removeAnnotations.iterator(); iter.hasNext();) |
| 123 |
fAnnotationModel.removeAnnotation((Annotation) iter.next()); |
| 124 |
for (Iterator iter = fAddAnnotations.keySet().iterator(); iter.hasNext();) { |
| 125 |
Annotation annotation = (Annotation) iter.next(); |
| 126 |
fAnnotationModel.addAnnotation(annotation, (Position) fAddAnnotations.get(annotation)); |
| 127 |
} |
| 128 |
// } |
| 129 |
|
| 130 |
fAddAnnotations = null; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
/** The id of the problem */ |
| 135 |
public static final int SPELLING_PROBLEM_ID = 0x80000000; |
| 136 |
|
| 137 |
/** The document to operate on. */ |
| 138 |
private IDocument fDocument; |
| 139 |
|
| 140 |
/** The progress monitor. */ |
| 141 |
private IProgressMonitor fProgressMonitor; |
| 142 |
|
| 143 |
/** |
| 144 |
* The spelling context containing the Java properties content type. |
| 145 |
* <p> |
| 146 |
* Since his reconcile strategy is for the Properties File editor which |
| 147 |
* normally edits Java properties files we always use the Java properties |
| 148 |
* file content type for performance reasons. |
| 149 |
* </p> |
| 150 |
* |
| 151 |
* @since 3.2 |
| 152 |
*/ |
| 153 |
private SpellingContext fSpellingContext; |
| 154 |
|
| 155 |
private FormEditor editor; |
| 156 |
|
| 157 |
private IAnnotationModel annotationModel; |
| 158 |
|
| 159 |
public TaskSpellingReconcileStrategy(FormEditor editor) { |
| 160 |
this.editor = editor; |
| 161 |
this.annotationModel = null; |
| 162 |
fSpellingContext = new SpellingContext(); |
| 163 |
fSpellingContext.setContentType(Platform.getContentTypeManager().getContentType(IContentTypeManager.CT_TEXT)); |
| 164 |
} |
| 165 |
|
| 166 |
/* |
| 167 |
* @see org.eclipse.jface.text.reconciler.IReconcilingStrategyExtension#initialReconcile() |
| 168 |
*/ |
| 169 |
public void initialReconcile() { |
| 170 |
reconcile(new Region(0, fDocument.getLength())); |
| 171 |
} |
| 172 |
|
| 173 |
/* |
| 174 |
* @see org.eclipse.jface.text.reconciler.IReconcilingStrategy#reconcile(org.eclipse.jface.text.reconciler.DirtyRegion,org.eclipse.jface.text.IRegion) |
| 175 |
*/ |
| 176 |
public void reconcile(DirtyRegion dirtyRegion, IRegion subRegion) { |
| 177 |
reconcile(subRegion); |
| 178 |
} |
| 179 |
|
| 180 |
/* |
| 181 |
* @see org.eclipse.jface.text.reconciler.IReconcilingStrategy#reconcile(org.eclipse.jface.text.IRegion) |
| 182 |
*/ |
| 183 |
public void reconcile(IRegion region) { |
| 184 |
TaskSpellingReconcileStrategy.SpellingProblemCollector collector = new SpellingProblemCollector(annotationModel); |
| 185 |
EditorsUI.getSpellingService().check(fDocument, fSpellingContext, collector, fProgressMonitor); |
| 186 |
} |
| 187 |
|
| 188 |
/* |
| 189 |
* @see org.eclipse.jface.text.reconciler.IReconcilingStrategy#setDocument(org.eclipse.jface.text.IDocument) |
| 190 |
*/ |
| 191 |
public void setDocument(IDocument document) { |
| 192 |
fDocument = document; |
| 193 |
} |
| 194 |
|
| 195 |
/* |
| 196 |
* @see org.eclipse.jface.text.reconciler.IReconcilingStrategyExtension#setProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) |
| 197 |
*/ |
| 198 |
public void setProgressMonitor(IProgressMonitor monitor) { |
| 199 |
fProgressMonitor = monitor; |
| 200 |
} |
| 201 |
|
| 202 |
public String getDocumentPartitioning() { |
| 203 |
// ignore |
| 204 |
return null; |
| 205 |
} |
| 206 |
|
| 207 |
public void setAnnotationModel(IAnnotationModel model) { |
| 208 |
annotationModel = model; |
| 209 |
} |
| 210 |
} |