KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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 import java.util.Collections JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18
19 import org.eclipse.core.runtime.IProgressMonitor;
20
21
22 import org.eclipse.jface.text.BadLocationException;
23 import org.eclipse.jface.text.DocumentEvent;
24 import org.eclipse.jface.text.IDocument;
25 import org.eclipse.jface.text.IRegion;
26
27 import org.eclipse.jdt.ui.PreferenceConstants;
28 import org.eclipse.jdt.ui.text.java.IJavaCompletionProposalComputer;
29 import org.eclipse.jdt.ui.text.java.ContentAssistInvocationContext;
30
31 import org.eclipse.jdt.internal.ui.JavaPlugin;
32 import org.eclipse.jdt.internal.ui.JavaPluginImages;
33 import org.eclipse.jdt.internal.ui.text.java.JavaCompletionProposal;
34 import org.eclipse.jdt.internal.ui.text.spelling.engine.ISpellCheckEngine;
35 import org.eclipse.jdt.internal.ui.text.spelling.engine.ISpellChecker;
36 import org.eclipse.jdt.internal.ui.text.spelling.engine.RankedWordProposal;
37
38 /**
39  * Content assist processor to complete words.
40  * <strong>Note:</strong> This is currently not supported because the spelling engine
41  * cannot return word proposals but only correction proposals.
42  * <p>
43  * If we enable this again we must register the computer in <code>plugin.xml</code>:
44  * <pre>
45  * </pre>
46  * </p>
47  *
48  * @since 3.0
49  */

50 public final class WordCompletionProposalComputer implements IJavaCompletionProposalComputer {
51
52     /** The prefix rank shift */
53     private static final int PREFIX_RANK_SHIFT= 500;
54
55     /*
56      * @see org.eclipse.jface.text.contentassist.ICompletionProposalComputer#computeCompletionProposals(org.eclipse.jface.text.contentassist.TextContentAssistInvocationContext, org.eclipse.core.runtime.IProgressMonitor)
57      */

58     public List JavaDoc computeCompletionProposals(ContentAssistInvocationContext context, IProgressMonitor monitor) {
59         if (contributes()) {
60             try {
61                 IDocument document= context.getDocument();
62                 final int offset= context.getInvocationOffset();
63             
64                 final IRegion region= document.getLineInformationOfOffset(offset);
65                 final String JavaDoc content= document.get(region.getOffset(), region.getLength());
66             
67                 int index= offset - region.getOffset() - 1;
68                 while (index >= 0 && Character.isLetter(content.charAt(index)))
69                     index--;
70             
71                 final int start= region.getOffset() + index + 1;
72                 final String JavaDoc candidate= content.substring(index + 1, offset - region.getOffset());
73             
74                 if (candidate.length() > 0) {
75             
76                     final ISpellCheckEngine engine= SpellCheckEngine.getInstance();
77                     final ISpellChecker checker= engine.getSpellChecker();
78             
79                     if (checker != null) {
80             
81                         final List JavaDoc proposals= new ArrayList JavaDoc(checker.getProposals(candidate, Character.isUpperCase(candidate.charAt(0))));
82                         final List JavaDoc result= new ArrayList JavaDoc(proposals.size());
83             
84                         for (Iterator JavaDoc it= proposals.iterator(); it.hasNext();) {
85                             RankedWordProposal word= (RankedWordProposal) it.next();
86                             String JavaDoc text= word.getText();
87                             if (text.startsWith(candidate))
88                                 word.setRank(word.getRank() + PREFIX_RANK_SHIFT);
89                             
90                             result.add(new JavaCompletionProposal(text, start, candidate.length(), JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_RENAME), text, word.getRank()) {
91                                 /*
92                                  * @see org.eclipse.jdt.internal.ui.text.java.JavaCompletionProposal#validate(org.eclipse.jface.text.IDocument, int, org.eclipse.jface.text.DocumentEvent)
93                                  */

94                                 public boolean validate(IDocument doc, int validate_offset, DocumentEvent event) {
95                                     return offset == validate_offset;
96                                 }
97                             });
98                         }
99                         
100                         return result;
101                     }
102                 }
103             } catch (BadLocationException exception) {
104                 // log & ignore
105
JavaPlugin.log(exception);
106             }
107         }
108         return Collections.EMPTY_LIST;
109     }
110
111     private boolean contributes() {
112         return PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.SPELLING_ENABLE_CONTENTASSIST);
113     }
114
115     /*
116      * @see org.eclipse.jface.text.contentassist.ICompletionProposalComputer#computeContextInformation(org.eclipse.jface.text.contentassist.TextContentAssistInvocationContext, org.eclipse.core.runtime.IProgressMonitor)
117      */

118     public List JavaDoc computeContextInformation(ContentAssistInvocationContext context, IProgressMonitor monitor) {
119         return Collections.EMPTY_LIST;
120     }
121
122     /*
123      * @see org.eclipse.jface.text.contentassist.ICompletionProposalComputer#getErrorMessage()
124      */

125     public String JavaDoc getErrorMessage() {
126         return null; // no error message available
127
}
128
129     /*
130      * @see org.eclipse.jdt.ui.text.java.IJavaCompletionProposalComputer#sessionStarted()
131      */

132     public void sessionStarted() {
133     }
134
135     /*
136      * @see org.eclipse.jdt.ui.text.java.IJavaCompletionProposalComputer#sessionEnded()
137      */

138     public void sessionEnded() {
139     }
140 }
141
Popular Tags