KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > text > spelling > WordCompletionProcessor


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.jdt.internal.ui.text.spelling;
13
14 import java.util.ArrayList JavaDoc;
15
16 import org.eclipse.core.resources.IFile;
17
18 import org.eclipse.jface.text.BadLocationException;
19 import org.eclipse.jface.text.IDocument;
20 import org.eclipse.jface.text.IRegion;
21 import org.eclipse.jface.text.ITextViewer;
22 import org.eclipse.jface.text.contentassist.ICompletionProposal;
23 import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
24 import org.eclipse.jface.text.contentassist.IContextInformation;
25 import org.eclipse.jface.text.contentassist.IContextInformationValidator;
26
27 import org.eclipse.ui.IEditorInput;
28 import org.eclipse.ui.part.FileEditorInput;
29
30 import org.eclipse.jdt.core.ICompilationUnit;
31
32 import org.eclipse.jdt.ui.JavaUI;
33 import org.eclipse.jdt.ui.PreferenceConstants;
34 import org.eclipse.jdt.ui.text.java.IJavaCompletionProposal;
35 import org.eclipse.jdt.ui.text.java.IJavadocCompletionProcessor;
36 import org.eclipse.jdt.internal.ui.text.spelling.engine.ISpellCheckEngine;
37 import org.eclipse.jdt.internal.ui.text.spelling.engine.ISpellChecker;
38 import org.eclipse.jdt.internal.ui.text.spelling.engine.RankedWordProposal;
39
40 import org.eclipse.jdt.internal.ui.JavaPluginImages;
41 import org.eclipse.jdt.internal.ui.text.java.JavaCompletionProposal;
42
43 /**
44  * Content assist processor to complete words.
45  *
46  * @since 3.0
47  */

48 public class WordCompletionProcessor implements IContentAssistProcessor, IJavadocCompletionProcessor {
49
50     /** The prefix rank shift */
51     protected static final int PREFIX_RANK_SHIFT= 4096;
52
53     /** The error message */
54     private String JavaDoc fError= null;
55
56     /*
57      * @see org.eclipse.jdt.ui.text.java.IJavadocCompletionProcessor#computeCompletionProposals(org.eclipse.jdt.core.ICompilationUnit,int,int,int)
58      */

59     public final IJavaCompletionProposal[] computeCompletionProposals(final ICompilationUnit cu, final int offset, final int length, final int flags) {
60         if (contributes()) {
61             IEditorInput editorInput= new FileEditorInput((IFile) cu.getResource());
62             IDocument document= JavaUI.getDocumentProvider().getDocument(editorInput);
63             return computeCompletionProposals(document, offset);
64         }
65         return null;
66     }
67
68     /*
69      * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#computeCompletionProposals(org.eclipse.jface.text.ITextViewer,int)
70      */

71     public ICompletionProposal[] computeCompletionProposals(final ITextViewer viewer, final int offset) {
72         if (contributes())
73             return computeCompletionProposals(viewer.getDocument(), offset);
74         return null;
75     }
76
77     private boolean contributes() {
78         return PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.SPELLING_ENABLE_CONTENTASSIST);
79     }
80
81     private IJavaCompletionProposal[] computeCompletionProposals(final IDocument document, final int offset) {
82         IJavaCompletionProposal[] result= null;
83         try {
84
85             final IRegion region= document.getLineInformationOfOffset(offset);
86             final String JavaDoc content= document.get(region.getOffset(), region.getLength());
87
88             int index= offset - region.getOffset() - 1;
89             while (index >= 0 && Character.isLetter(content.charAt(index)))
90                 index--;
91
92             final int start= region.getOffset() + index + 1;
93             final String JavaDoc candidate= content.substring(index + 1, offset - region.getOffset());
94
95             if (candidate.length() > 0) {
96
97                 final ISpellCheckEngine engine= SpellCheckEngine.getInstance();
98                 final ISpellChecker checker= engine.createSpellChecker(engine.getLocale(), PreferenceConstants.getPreferenceStore());
99
100                 if (checker != null) {
101
102                     final ArrayList JavaDoc proposals= new ArrayList JavaDoc(checker.getProposals(candidate, Character.isUpperCase(candidate.charAt(0))));
103                     result= new IJavaCompletionProposal[proposals.size()];
104
105                     RankedWordProposal word= null;
106                     for (int proposal= 0; proposal < result.length; proposal++) {
107
108                         word= (RankedWordProposal)proposals.get(proposal);
109                         if (word.getText().startsWith(candidate))
110                             word.setRank(word.getRank() + PREFIX_RANK_SHIFT);
111                     }
112
113 // Collections.sort(proposals);
114

115                     String JavaDoc text= null;
116                     for (int proposal= 0; proposal < result.length; proposal++) {
117
118                         word= (RankedWordProposal)proposals.get(proposal);
119                         text= word.getText();
120
121 // result[result.length - proposal - 1]= new JavaCompletionProposal(text, start, candidate.length(), JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_RENAME), text, 0);
122
result[proposal]= new JavaCompletionProposal(text, start, candidate.length(), JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_RENAME), text, word.getRank());
123                     }
124                 }
125             }
126         } catch (BadLocationException exception) {
127             fError= (result == null) ? exception.getLocalizedMessage() : null;
128         }
129         return result;
130     }
131
132     /*
133      * @see org.eclipse.jdt.ui.text.java.IJavadocCompletionProcessor#computeContextInformation(org.eclipse.jdt.core.ICompilationUnit,int)
134      */

135     public final IContextInformation[] computeContextInformation(final ICompilationUnit cu, final int offset) {
136         return null;
137     }
138
139     /*
140      * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#computeContextInformation(org.eclipse.jface.text.ITextViewer,int)
141      */

142     public final IContextInformation[] computeContextInformation(final ITextViewer viewer, final int offset) {
143         return null;
144     }
145
146     /*
147      * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getCompletionProposalAutoActivationCharacters()
148      */

149     public final char[] getCompletionProposalAutoActivationCharacters() {
150         return null;
151     }
152
153     /*
154      * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getContextInformationAutoActivationCharacters()
155      */

156     public final char[] getContextInformationAutoActivationCharacters() {
157         return null;
158     }
159
160     /*
161      * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getContextInformationValidator()
162      */

163     public final IContextInformationValidator getContextInformationValidator() {
164         return null;
165     }
166
167     /*
168      * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getErrorMessage()
169      */

170     public final String JavaDoc getErrorMessage() {
171         return fError;
172     }
173 }
174
Popular Tags