KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > texteditor > spelling > SpellingProblem


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.ui.texteditor.spelling;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.jface.text.BadLocationException;
19 import org.eclipse.jface.text.IDocument;
20 import org.eclipse.jface.text.Position;
21 import org.eclipse.jface.text.contentassist.ICompletionProposal;
22 import org.eclipse.jface.text.source.Annotation;
23 import org.eclipse.jface.text.source.IAnnotationModel;
24 import org.eclipse.jface.text.source.IAnnotationModelExtension;
25
26 import org.eclipse.ui.IEditorInput;
27 import org.eclipse.ui.texteditor.IDocumentProvider;
28 import org.eclipse.ui.texteditor.ITextEditor;
29
30
31 /**
32  * A spelling problem as reported by the {@link SpellingService} service to the
33  * {@link ISpellingProblemCollector}.
34  * <p>
35  * This class is intended to be subclassed by clients.
36  * </p>
37  *
38  * @see SpellingService
39  * @see ISpellingProblemCollector
40  * @since 3.1
41  */

42 public abstract class SpellingProblem {
43
44     /**
45      * Removes all spelling problems that are reported
46      * for the given <code>word</code> in the active editor.
47      * <p>
48      * <em>This a workaround to fix bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=134338
49      * for 3.2 at the time where spelling still resides in JDT Text.
50      * Once we move the spell check engine along with its quick fixes
51      * down to Platform Text we need to provide the proposals with
52      * a way to access the annotation model.</em>
53      * </p>
54      *
55      * @param editor the text editor, if <code>null</code> this method is does nothing
56      * @param word the word for which to remove the problems or <code>null</code> to remove all
57      * @since 3.3
58      */

59     public static void removeAllInActiveEditor(ITextEditor editor, String JavaDoc word) {
60         if (editor == null)
61             return;
62         
63         IDocumentProvider documentProvider= editor.getDocumentProvider();
64         if (documentProvider == null)
65             return;
66     
67         IEditorInput editorInput= editor.getEditorInput();
68         if (editorInput == null)
69             return;
70         
71         IAnnotationModel model= documentProvider.getAnnotationModel(editorInput);
72         if (model == null)
73             return;
74         
75         IDocument document= documentProvider.getDocument(editorInput);
76         if (document == null)
77             return;
78     
79         boolean supportsBatchReplace= (model instanceof IAnnotationModelExtension);
80         List JavaDoc toBeRemovedAnnotations= new ArrayList JavaDoc();
81         Iterator JavaDoc iter= model.getAnnotationIterator();
82         while (iter.hasNext()) {
83             Annotation annotation= (Annotation) iter.next();
84             if (SpellingAnnotation.TYPE.equals(annotation.getType())) {
85                 boolean doRemove= word == null;
86                 if (word == null)
87                     doRemove= true;
88                 else {
89                     String JavaDoc annotationWord= null;
90                     Position pos= model.getPosition(annotation);
91                     try {
92                         annotationWord= document.get(pos.getOffset(), pos.getLength());
93                     } catch (BadLocationException e) {
94                         continue;
95                     }
96                     doRemove= word.equals(annotationWord);
97                 }
98                 if (doRemove) {
99                     if (supportsBatchReplace)
100                         toBeRemovedAnnotations.add(annotation);
101                     else
102                         model.removeAnnotation(annotation);
103                 }
104             }
105         }
106     
107         if (supportsBatchReplace && !toBeRemovedAnnotations.isEmpty()) {
108             Annotation[] annotationArray= (Annotation[])toBeRemovedAnnotations.toArray(new Annotation[toBeRemovedAnnotations.size()]);
109             ((IAnnotationModelExtension)model).replaceAnnotations(annotationArray, null);
110         }
111     }
112
113     /**
114      * Returns the offset of the incorrectly spelled region.
115      *
116      * @return the offset of the incorrectly spelled region
117      */

118     public abstract int getOffset();
119
120     /**
121      * Returns the length of the incorrectly spelled region.
122      *
123      * @return the length of the incorrectly spelled region
124      */

125     public abstract int getLength();
126
127     /**
128      * Returns a localized, human-readable message string which describes the spelling problem.
129      *
130      * @return a localized, human-readable message string which describes the spelling problem
131      */

132     public abstract String JavaDoc getMessage();
133
134     /**
135      * Returns the proposals for the incorrectly spelled region.
136      *
137      * @return the proposals for the incorrectly spelled region
138      */

139     public abstract ICompletionProposal[] getProposals();
140 }
141
Popular Tags