KickJava   Java API By Example, From Geeks To Geeks.

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


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.CoreException;
19
20 import org.eclipse.jdt.core.ICompilationUnit;
21
22 import org.eclipse.jdt.ui.PreferenceConstants;
23 import org.eclipse.jdt.ui.text.java.IInvocationContext;
24 import org.eclipse.jdt.ui.text.java.IJavaCompletionProposal;
25 import org.eclipse.jdt.ui.text.java.IProblemLocation;
26 import org.eclipse.jdt.ui.text.java.IQuickFixProcessor;
27
28 import org.eclipse.jdt.internal.ui.text.javadoc.IHtmlTagConstants;
29 import org.eclipse.jdt.internal.ui.text.javadoc.IJavaDocTagConstants;
30 import org.eclipse.jdt.internal.ui.text.spelling.engine.ISpellCheckEngine;
31 import org.eclipse.jdt.internal.ui.text.spelling.engine.ISpellChecker;
32 import org.eclipse.jdt.internal.ui.text.spelling.engine.RankedWordProposal;
33
34 /**
35  * Quick fix processor for incorrectly spelled words.
36  *
37  * @since 3.0
38  */

39 public class WordQuickFixProcessor implements IQuickFixProcessor {
40
41     /**
42      * Creates a new word quick fix processor.
43      */

44     public WordQuickFixProcessor() {
45         // For extension point
46
}
47
48     /*
49      * @see org.eclipse.jdt.ui.text.java.IQuickFixProcessor#getCorrections(org.eclipse.jdt.ui.text.java.IInvocationContext,org.eclipse.jdt.ui.text.java.IProblemLocation[])
50      */

51     public IJavaCompletionProposal[] getCorrections(IInvocationContext context, IProblemLocation[] locations) throws CoreException {
52
53         final int threshold= PreferenceConstants.getPreferenceStore().getInt(PreferenceConstants.SPELLING_PROPOSAL_THRESHOLD);
54
55         int size= 0;
56         List JavaDoc proposals= null;
57         String JavaDoc[] arguments= null;
58
59         IProblemLocation location= null;
60         RankedWordProposal proposal= null;
61         IJavaCompletionProposal[] result= null;
62
63         boolean fixed= false;
64         boolean match= false;
65         boolean sentence= false;
66
67         final ISpellCheckEngine engine= SpellCheckEngine.getInstance();
68         final ISpellChecker checker= engine.getSpellChecker();
69
70         if (checker != null) {
71
72             for (int index= 0; index < locations.length; index++) {
73
74                 location= locations[index];
75                 if (location.getProblemId() == JavaSpellingReconcileStrategy.SPELLING_PROBLEM_ID) {
76
77                     arguments= location.getProblemArguments();
78                     if (arguments != null && arguments.length > 4) {
79
80                         sentence= Boolean.valueOf(arguments[3]).booleanValue();
81                         match= Boolean.valueOf(arguments[4]).booleanValue();
82                         fixed= arguments[0].charAt(0) == IHtmlTagConstants.HTML_TAG_PREFIX || arguments[0].charAt(0) == IJavaDocTagConstants.JAVADOC_TAG_PREFIX;
83
84                         if ((sentence && match) && !fixed)
85                             result= new IJavaCompletionProposal[] { new ChangeCaseProposal(arguments, location.getOffset(), location.getLength(), context, engine.getLocale())};
86                         else {
87
88                             proposals= new ArrayList JavaDoc(checker.getProposals(arguments[0], sentence));
89                             size= proposals.size();
90
91                             if (threshold > 0 && size > threshold) {
92
93                                 Collections.sort(proposals);
94                                 proposals= proposals.subList(size - threshold - 1, size - 1);
95                                 size= proposals.size();
96                             }
97
98                             boolean extendable= !fixed ? (checker.acceptsWords() || AddWordProposal.canAskToConfigure()) : false;
99                             result= new IJavaCompletionProposal[size + (extendable ? 3 : 2)];
100
101                             for (index= 0; index < size; index++) {
102
103                                 proposal= (RankedWordProposal)proposals.get(index);
104                                 result[index]= new WordCorrectionProposal(proposal.getText(), arguments, location.getOffset(), location.getLength(), context, proposal.getRank());
105                             }
106
107                             if (extendable)
108                                 result[index++]= new AddWordProposal(arguments[0], context);
109
110                             result[index++]= new WordIgnoreProposal(arguments[0], context);
111                             result[index++]= new DisableSpellCheckingProposal(context);
112                         }
113                         break;
114                     }
115                 }
116             }
117         }
118         return result;
119     }
120
121     /*
122      * @see org.eclipse.jdt.ui.text.java.IQuickFixProcessor#hasCorrections(org.eclipse.jdt.core.ICompilationUnit,int)
123      */

124     public final boolean hasCorrections(ICompilationUnit unit, int id) {
125         return id == JavaSpellingReconcileStrategy.SPELLING_PROBLEM_ID;
126     }
127 }
128
Popular Tags