001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.texteditor.spelling;
011:
012: import java.util.ArrayList;
013: import java.util.Iterator;
014: import java.util.List;
015:
016: import org.eclipse.core.runtime.Assert;
017:
018: import org.eclipse.jface.text.BadLocationException;
019: import org.eclipse.jface.text.IDocument;
020: import org.eclipse.jface.text.Position;
021: import org.eclipse.jface.text.contentassist.ICompletionProposal;
022: import org.eclipse.jface.text.quickassist.IQuickAssistInvocationContext;
023: import org.eclipse.jface.text.source.Annotation;
024: import org.eclipse.jface.text.source.IAnnotationModel;
025: import org.eclipse.jface.text.source.IAnnotationModelExtension;
026: import org.eclipse.jface.text.source.ISourceViewer;
027:
028: import org.eclipse.ui.IEditorInput;
029: import org.eclipse.ui.texteditor.IDocumentProvider;
030: import org.eclipse.ui.texteditor.ITextEditor;
031:
032: /**
033: * A spelling problem as reported by the {@link SpellingService} service to the
034: * {@link ISpellingProblemCollector}.
035: * <p>
036: * This class is intended to be subclassed by clients.
037: * </p>
038: *
039: * @see SpellingService
040: * @see ISpellingProblemCollector
041: * @since 3.1
042: */
043: public abstract class SpellingProblem {
044:
045: /**
046: * Removes all spelling problems that are reported
047: * for the given <code>word</code> in the active editor.
048: * <p>
049: * <em>This a workaround to fix bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=134338
050: * for 3.2 at the time where spelling still resides in JDT Text.
051: * Once we move the spell check engine along with its quick fixes
052: * down to Platform Text we need to provide the proposals with
053: * a way to access the annotation model.</em>
054: * </p>
055: *
056: * @param editor the text editor, if <code>null</code> this method does nothing
057: * @param word the word for which to remove the problems or <code>null</code> to remove all
058: * @since 3.3
059: * @deprecated As of 3.4, replaced by {@link #removeAll(ISourceViewer, String)}
060: */
061: public static void removeAllInActiveEditor(ITextEditor editor,
062: String word) {
063: if (editor == null)
064: return;
065:
066: IDocumentProvider documentProvider = editor
067: .getDocumentProvider();
068: if (documentProvider == null)
069: return;
070:
071: IEditorInput editorInput = editor.getEditorInput();
072: if (editorInput == null)
073: return;
074:
075: IAnnotationModel model = documentProvider
076: .getAnnotationModel(editorInput);
077: if (model == null)
078: return;
079:
080: IDocument document = documentProvider.getDocument(editorInput);
081: if (document == null)
082: return;
083:
084: boolean supportsBatchReplace = (model instanceof IAnnotationModelExtension);
085: List toBeRemovedAnnotations = new ArrayList();
086: Iterator iter = model.getAnnotationIterator();
087: while (iter.hasNext()) {
088: Annotation annotation = (Annotation) iter.next();
089: if (SpellingAnnotation.TYPE.equals(annotation.getType())) {
090: boolean doRemove = word == null;
091: if (word == null)
092: doRemove = true;
093: else {
094: String annotationWord = null;
095: Position pos = model.getPosition(annotation);
096: try {
097: annotationWord = document.get(pos.getOffset(),
098: pos.getLength());
099: } catch (BadLocationException e) {
100: continue;
101: }
102: doRemove = word.equals(annotationWord);
103: }
104: if (doRemove) {
105: if (supportsBatchReplace)
106: toBeRemovedAnnotations.add(annotation);
107: else
108: model.removeAnnotation(annotation);
109: }
110: }
111: }
112:
113: if (supportsBatchReplace && !toBeRemovedAnnotations.isEmpty()) {
114: Annotation[] annotationArray = (Annotation[]) toBeRemovedAnnotations
115: .toArray(new Annotation[toBeRemovedAnnotations
116: .size()]);
117: ((IAnnotationModelExtension) model).replaceAnnotations(
118: annotationArray, null);
119: }
120: }
121:
122: /**
123: * Removes all spelling problems that are reported
124: * for the given <code>word</code> in the active editor.
125: *
126: * @param sourceViewer the source viewer
127: * @param word the word for which to remove the problems or <code>null</code> to remove all
128: * @since 3.4
129: */
130: public static void removeAll(ISourceViewer sourceViewer, String word) {
131: Assert.isNotNull(sourceViewer);
132:
133: IAnnotationModel model = sourceViewer.getAnnotationModel();
134: if (model == null)
135: return;
136:
137: IDocument document = sourceViewer.getDocument();
138: if (document == null)
139: return;
140:
141: boolean supportsBatchReplace = (model instanceof IAnnotationModelExtension);
142: List toBeRemovedAnnotations = new ArrayList();
143: Iterator iter = model.getAnnotationIterator();
144: while (iter.hasNext()) {
145: Annotation annotation = (Annotation) iter.next();
146: if (SpellingAnnotation.TYPE.equals(annotation.getType())) {
147: boolean doRemove = word == null;
148: if (word == null)
149: doRemove = true;
150: else {
151: String annotationWord = null;
152: Position pos = model.getPosition(annotation);
153: try {
154: annotationWord = document.get(pos.getOffset(),
155: pos.getLength());
156: } catch (BadLocationException e) {
157: continue;
158: }
159: doRemove = word.equals(annotationWord);
160: }
161: if (doRemove) {
162: if (supportsBatchReplace)
163: toBeRemovedAnnotations.add(annotation);
164: else
165: model.removeAnnotation(annotation);
166: }
167: }
168: }
169:
170: if (supportsBatchReplace && !toBeRemovedAnnotations.isEmpty()) {
171: Annotation[] annotationArray = (Annotation[]) toBeRemovedAnnotations
172: .toArray(new Annotation[toBeRemovedAnnotations
173: .size()]);
174: ((IAnnotationModelExtension) model).replaceAnnotations(
175: annotationArray, null);
176: }
177: }
178:
179: /**
180: * Returns the offset of the incorrectly spelled region.
181: *
182: * @return the offset of the incorrectly spelled region
183: */
184: public abstract int getOffset();
185:
186: /**
187: * Returns the length of the incorrectly spelled region.
188: *
189: * @return the length of the incorrectly spelled region
190: */
191: public abstract int getLength();
192:
193: /**
194: * Returns a localized, human-readable message string which describes the spelling problem.
195: *
196: * @return a localized, human-readable message string which describes the spelling problem
197: */
198: public abstract String getMessage();
199:
200: /**
201: * Returns the proposals for the incorrectly spelled region.
202: *
203: * @return the proposals for the incorrectly spelled region
204: */
205: public abstract ICompletionProposal[] getProposals();
206:
207: /**
208: * Returns the proposals for the incorrectly spelled region.
209: *
210: * @param context the invocation context or <code>null</code> if none
211: * @return the proposals for the incorrectly spelled region
212: * @since 3.4
213: */
214: public ICompletionProposal[] getProposals(
215: IQuickAssistInvocationContext context) {
216: return getProposals();
217: }
218: }
|