KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2006, 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 package org.eclipse.ui.texteditor.spelling;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.jface.text.Position;
19 import org.eclipse.jface.text.contentassist.ICompletionProposal;
20 import org.eclipse.jface.text.quickassist.IQuickAssistInvocationContext;
21 import org.eclipse.jface.text.quickassist.IQuickAssistProcessor;
22 import org.eclipse.jface.text.source.Annotation;
23 import org.eclipse.jface.text.source.IAnnotationModel;
24 import org.eclipse.jface.text.source.ISourceViewer;
25 import org.eclipse.jface.text.source.TextInvocationContext;
26
27 import org.eclipse.ui.internal.texteditor.spelling.NoCompletionsProposal;
28
29
30 /**
31  * Spelling correction processor used to show quick
32  * fixes for spelling problems.
33  *
34  * @since 3.3
35  */

36 public final class SpellingCorrectionProcessor implements IQuickAssistProcessor {
37     
38
39     private static final ICompletionProposal[] fgNoSuggestionsProposal= new ICompletionProposal[] { new NoCompletionsProposal() };
40     
41
42     /*
43      * @see IContentAssistProcessor#computeCompletionProposals(ITextViewer, int)
44      */

45     public ICompletionProposal[] computeQuickAssistProposals(IQuickAssistInvocationContext quickAssistContext) {
46         ISourceViewer viewer= quickAssistContext.getSourceViewer();
47         int documentOffset= quickAssistContext.getOffset();
48
49         int length= viewer != null ? viewer.getSelectedRange().y : 0;
50         TextInvocationContext context= new TextInvocationContext(viewer, documentOffset, length);
51
52
53         IAnnotationModel model= viewer.getAnnotationModel();
54         if (model == null)
55             return fgNoSuggestionsProposal;
56
57         List JavaDoc proposals= computeProposals(context, model);
58         if (proposals.isEmpty())
59             return fgNoSuggestionsProposal;
60
61         return (ICompletionProposal[]) proposals.toArray(new ICompletionProposal[proposals.size()]);
62     }
63
64     private boolean isAtPosition(int offset, Position pos) {
65         return (pos != null) && (offset >= pos.getOffset() && offset <= (pos.getOffset() + pos.getLength()));
66     }
67
68     private List JavaDoc computeProposals(IQuickAssistInvocationContext context, IAnnotationModel model) {
69         int offset= context.getOffset();
70         ArrayList JavaDoc annotationList= new ArrayList JavaDoc();
71         Iterator JavaDoc iter= model.getAnnotationIterator();
72         while (iter.hasNext()) {
73             Annotation annotation= (Annotation)iter.next();
74             if (canFix(annotation)) {
75                 Position pos= model.getPosition(annotation);
76                 if (isAtPosition(offset, pos)) {
77                     collectSpellingProblems(annotation, pos, annotationList);
78                 }
79             }
80         }
81         SpellingProblem[] spellingProblems= (SpellingProblem[]) annotationList.toArray(new SpellingProblem[annotationList.size()]);
82         return computeProposals(context, spellingProblems);
83     }
84
85     private void collectSpellingProblems(Annotation annotation, Position pos, List JavaDoc problems) {
86         if (annotation instanceof SpellingAnnotation)
87             problems.add(((SpellingAnnotation)annotation).getSpellingProblem());
88     }
89
90     private List JavaDoc computeProposals(IQuickAssistInvocationContext context, SpellingProblem[] spellingProblems) {
91         List JavaDoc proposals= new ArrayList JavaDoc();
92         for (int i= 0; i < spellingProblems.length; i++)
93             proposals.addAll(Arrays.asList(spellingProblems[i].getProposals()));
94         
95         return proposals;
96     }
97
98     /*
99      * @see IContentAssistProcessor#getErrorMessage()
100      */

101     public String JavaDoc getErrorMessage() {
102         return null;
103     }
104
105     /*
106      * @see org.eclipse.jface.text.quickassist.IQuickAssistProcessor#canFix(org.eclipse.jface.text.source.Annotation)
107      */

108     public boolean canFix(Annotation annotation) {
109         return annotation instanceof SpellingAnnotation && !annotation.isMarkedDeleted();
110     }
111
112     /*
113      * @see org.eclipse.jface.text.quickassist.IQuickAssistProcessor#canAssist(org.eclipse.jface.text.quickassist.IQuickAssistInvocationContext)
114      */

115     public boolean canAssist(IQuickAssistInvocationContext invocationContext) {
116         return false;
117     }
118
119 }
120
Popular Tags