KickJava   Java API By Example, From Geeks To Geeks.

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


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.jdt.internal.ui.text.spelling;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Collections JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.core.runtime.Assert;
19
20 import org.eclipse.jface.text.BadLocationException;
21 import org.eclipse.jface.text.IDocument;
22 import org.eclipse.jface.text.IRegion;
23 import org.eclipse.jface.text.contentassist.ICompletionProposal;
24
25 import org.eclipse.ui.texteditor.spelling.SpellingProblem;
26
27 import org.eclipse.jdt.internal.corext.util.Messages;
28
29 import org.eclipse.jdt.ui.PreferenceConstants;
30 import org.eclipse.jdt.ui.text.java.IInvocationContext;
31 import org.eclipse.jdt.ui.text.java.IJavaCompletionProposal;
32
33 import org.eclipse.jdt.internal.ui.JavaUIMessages;
34 import org.eclipse.jdt.internal.ui.text.correction.AssistContext;
35 import org.eclipse.jdt.internal.ui.text.javadoc.IHtmlTagConstants;
36 import org.eclipse.jdt.internal.ui.text.javadoc.IJavaDocTagConstants;
37 import org.eclipse.jdt.internal.ui.text.spelling.engine.ISpellCheckEngine;
38 import org.eclipse.jdt.internal.ui.text.spelling.engine.ISpellChecker;
39 import org.eclipse.jdt.internal.ui.text.spelling.engine.ISpellEvent;
40 import org.eclipse.jdt.internal.ui.text.spelling.engine.RankedWordProposal;
41
42 /**
43  * A {@link SpellingProblem} that adapts a {@link ISpellEvent}.
44  * <p>
45  * TODO: remove {@link ISpellEvent} notification mechanism
46  * </p>
47  */

48 public class JavaSpellingProblem extends SpellingProblem {
49
50     /** Spell event */
51     private ISpellEvent fSpellEvent;
52
53     /**
54      * The associated document.
55      *
56      * @since 3.3
57      */

58     private IDocument fDocument;
59
60     /**
61      * Initialize with the given spell event.
62      *
63      * @param spellEvent the spell event
64      * @param document the document
65      */

66     public JavaSpellingProblem(ISpellEvent spellEvent, IDocument document) {
67         Assert.isLegal(document != null);
68         Assert.isLegal(spellEvent != null);
69         fSpellEvent= spellEvent;
70         fDocument= document;
71     }
72
73     /*
74      * @see org.eclipse.ui.texteditor.spelling.SpellingProblem#getOffset()
75      */

76     public int getOffset() {
77         return fSpellEvent.getBegin();
78     }
79
80     /*
81      * @see org.eclipse.ui.texteditor.spelling.SpellingProblem#getLength()
82      */

83     public int getLength() {
84         return fSpellEvent.getEnd() - fSpellEvent.getBegin() + 1;
85     }
86
87     /*
88      * @see org.eclipse.ui.texteditor.spelling.SpellingProblem#getMessage()
89      */

90     public String JavaDoc getMessage() {
91         if (isSentenceStart() && isDictionaryMatch())
92             return Messages.format(JavaUIMessages.Spelling_error_case_label, new String JavaDoc[] { fSpellEvent.getWord() });
93
94         return Messages.format(JavaUIMessages.Spelling_error_label, new String JavaDoc[] { fSpellEvent.getWord() });
95     }
96
97     /*
98      * @see org.eclipse.ui.texteditor.spelling.SpellingProblem#getProposals()
99      */

100     public ICompletionProposal[] getProposals() {
101         String JavaDoc[] arguments= getArguments();
102         if (arguments == null)
103             return new ICompletionProposal[0];
104         
105         if (arguments[0].indexOf('&') != -1 && isIgnoringAmpersand())
106             return new ICompletionProposal[0]; // no proposals for now
107

108         final int threshold= PreferenceConstants.getPreferenceStore().getInt(PreferenceConstants.SPELLING_PROPOSAL_THRESHOLD);
109         int size= 0;
110         List JavaDoc proposals= null;
111
112         RankedWordProposal proposal= null;
113         IJavaCompletionProposal[] result= null;
114         int index= 0;
115
116         boolean fixed= false;
117         boolean match= false;
118         boolean sentence= false;
119
120         final ISpellCheckEngine engine= SpellCheckEngine.getInstance();
121         final ISpellChecker checker= engine.getSpellChecker();
122
123         if (checker != null) {
124
125             IInvocationContext context= new AssistContext(null, getOffset(),
126                     getLength());
127
128             // FIXME: this is a pretty ugly hack
129
fixed= arguments[0].charAt(0) == IHtmlTagConstants.HTML_TAG_PREFIX
130                     || arguments[0].charAt(0) == IJavaDocTagConstants.JAVADOC_TAG_PREFIX;
131
132             if ((sentence && match) && !fixed)
133                 result= new IJavaCompletionProposal[] { new ChangeCaseProposal(
134                         arguments, getOffset(), getLength(), context, engine
135                                 .getLocale()) };
136             else {
137
138                 proposals= new ArrayList JavaDoc(checker.getProposals(arguments[0],
139                         sentence));
140                 size= proposals.size();
141
142                 if (threshold > 0 && size > threshold) {
143
144                     Collections.sort(proposals);
145                     proposals= proposals
146                             .subList(size - threshold - 1, size - 1);
147                     size= proposals.size();
148                 }
149
150                 boolean extendable= !fixed ? (checker.acceptsWords() || AddWordProposal.canAskToConfigure()) : false;
151                 result= new IJavaCompletionProposal[size + (extendable ? 3 : 2)];
152
153                 for (index= 0; index < size; index++) {
154
155                     proposal= (RankedWordProposal) proposals.get(index);
156                     result[index]= new WordCorrectionProposal(proposal
157                             .getText(), arguments, getOffset(), getLength(),
158                             context, proposal.getRank());
159                 }
160
161                 if (extendable)
162                     result[index++]= new AddWordProposal(arguments[0], context);
163
164                 result[index++]= new WordIgnoreProposal(arguments[0], context);
165                 result[index++]= new DisableSpellCheckingProposal(context);
166             }
167         }
168
169         return result;
170     }
171     
172     private boolean isIgnoringAmpersand() {
173         return PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.SPELLING_IGNORE_AMPERSAND_IN_PROPERTIES);
174     }
175
176     public String JavaDoc[] getArguments() {
177
178         String JavaDoc prefix= ""; //$NON-NLS-1$
179
String JavaDoc postfix= ""; //$NON-NLS-1$
180
String JavaDoc word;
181         try {
182             word= fDocument.get(getOffset(), getLength());
183         } catch (BadLocationException e) {
184             return null;
185         }
186
187         try {
188
189             IRegion line= fDocument.getLineInformationOfOffset(getOffset());
190             int end= getOffset() + getLength();
191             prefix= fDocument.get(line.getOffset(), getOffset()
192                     - line.getOffset());
193             postfix= fDocument.get(end + 1, line.getOffset() + line.getLength()
194                     - end);
195
196         } catch (BadLocationException exception) {
197             // Do nothing
198
}
199         return new String JavaDoc[] {
200                 word,
201                 prefix,
202                 postfix,
203                 isSentenceStart() ? Boolean.toString(true) : Boolean
204                         .toString(false),
205                 isDictionaryMatch() ? Boolean.toString(true) : Boolean
206                         .toString(false) };
207     }
208
209     /**
210      * Returns <code>true</code> iff the corresponding word was found in the dictionary.
211      * <p>
212      * NOTE: to be removed, see {@link #getProposals()}
213      * </p>
214      *
215      * @return <code>true</code> iff the corresponding word was found in the dictionary
216      */

217     public boolean isDictionaryMatch() {
218         return fSpellEvent.isMatch();
219     }
220
221     /**
222      * Returns <code>true</code> iff the corresponding word starts a sentence.
223      * <p>
224      * NOTE: to be removed, see {@link #getProposals()}
225      * </p>
226      *
227      * @return <code>true</code> iff the corresponding word starts a sentence
228      */

229     public boolean isSentenceStart() {
230         return fSpellEvent.isStart();
231     }
232
233 }
234
Popular Tags