KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.eclipse.swt.graphics.Image;
15 import org.eclipse.swt.graphics.Point;
16
17 import org.eclipse.jface.text.BadLocationException;
18 import org.eclipse.jface.text.IDocument;
19 import org.eclipse.jface.text.contentassist.IContextInformation;
20
21 import org.eclipse.jdt.internal.corext.util.Messages;
22
23 import org.eclipse.jdt.ui.text.java.IInvocationContext;
24 import org.eclipse.jdt.ui.text.java.IJavaCompletionProposal;
25
26 import org.eclipse.jdt.internal.ui.JavaPluginImages;
27 import org.eclipse.jdt.internal.ui.JavaUIMessages;
28 import org.eclipse.jdt.internal.ui.text.javadoc.IHtmlTagConstants;
29
30 /**
31  * Proposal to correct the incorrectly spelled word.
32  *
33  * @since 3.0
34  */

35 public class WordCorrectionProposal implements IJavaCompletionProposal {
36
37     /**
38      * Returns the html representation of the specified string.
39      *
40      * @param string
41      * The string to return the html representation for
42      * @return The html representation for the string
43      */

44     public static String JavaDoc getHtmlRepresentation(final String JavaDoc string) {
45
46         final int length= string.length();
47         final StringBuffer JavaDoc buffer= new StringBuffer JavaDoc(string);
48
49         for (int offset= length - 1; offset >= 0; offset--) {
50
51             for (int index= 0; index < IHtmlTagConstants.HTML_ENTITY_CHARACTERS.length; index++) {
52
53                 if (string.charAt(offset) == IHtmlTagConstants.HTML_ENTITY_CHARACTERS[index]) {
54
55                     buffer.replace(offset, offset + 1, String.valueOf(IHtmlTagConstants.HTML_ENTITY_CODES[index]));
56                     break;
57                 }
58             }
59         }
60         return buffer.toString();
61     }
62
63     /** The invocation context */
64     private final IInvocationContext fContext;
65
66     /** The length in the document */
67     private final int fLength;
68
69     /** The line where to apply the correction */
70     private final String JavaDoc fLine;
71
72     /** The offset in the document */
73     private final int fOffset;
74
75     /** The relevance of this proposal */
76     private final int fRelevance;
77
78     /** The word to complete */
79     private final String JavaDoc fWord;
80
81     /**
82      * Creates a new word correction proposal.
83      *
84      * @param word the corrected word
85      * @param arguments the problem arguments associated with the spelling problem
86      * @param offset the offset in the document where to apply the proposal
87      * @param length the lenght in the document to apply the proposal
88      * @param context the invocation context for this proposal
89      * @param relevance the relevance of this proposal
90      */

91     public WordCorrectionProposal(final String JavaDoc word, final String JavaDoc[] arguments, final int offset, final int length, final IInvocationContext context, final int relevance) {
92
93         fWord= Character.isUpperCase(arguments[0].charAt(0)) ? Character.toUpperCase(word.charAt(0)) + word.substring(1) : word;
94
95         fOffset= offset;
96         fLength= length;
97         fContext= context;
98         fRelevance= relevance;
99
100         final StringBuffer JavaDoc buffer= new StringBuffer JavaDoc(80);
101
102         buffer.append("...<br>"); //$NON-NLS-1$
103
buffer.append(getHtmlRepresentation(arguments[1]));
104         buffer.append("<b>"); //$NON-NLS-1$
105
buffer.append(getHtmlRepresentation(fWord));
106         buffer.append("</b>"); //$NON-NLS-1$
107
buffer.append(getHtmlRepresentation(arguments[2]));
108         buffer.append("<br>..."); //$NON-NLS-1$
109

110         fLine= buffer.toString();
111     }
112
113     /*
114      * @see org.eclipse.jface.text.contentassist.ICompletionProposal#apply(org.eclipse.jface.text.IDocument)
115      */

116     public final void apply(final IDocument document) {
117         try {
118             document.replace(fOffset, fLength, fWord);
119         } catch (BadLocationException exception) {
120             // Do nothing
121
}
122     }
123
124     /*
125      * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getAdditionalProposalInfo()
126      */

127     public String JavaDoc getAdditionalProposalInfo() {
128         return fLine;
129     }
130
131     /*
132      * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getContextInformation()
133      */

134     public final IContextInformation getContextInformation() {
135         return null;
136     }
137
138     /*
139      * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getDisplayString()
140      */

141     public String JavaDoc getDisplayString() {
142         return Messages.format(JavaUIMessages.Spelling_correct_label, new String JavaDoc[] { fWord });
143     }
144
145     /*
146      * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getImage()
147      */

148     public Image getImage() {
149         return JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_RENAME);
150     }
151
152     /*
153      * @see org.eclipse.jdt.ui.text.java.IJavaCompletionProposal#getRelevance()
154      */

155     public final int getRelevance() {
156         return fRelevance;
157     }
158
159     /*
160      * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getSelection(org.eclipse.jface.text.IDocument)
161      */

162     public final Point getSelection(final IDocument document) {
163
164         int offset= fContext.getSelectionOffset();
165         int length= fContext.getSelectionLength();
166
167         final int delta= fWord.length() - fLength;
168         if (offset <= fOffset && offset + length >= fOffset)
169             length += delta;
170         else if (offset > fOffset && offset + length > fOffset + fLength) {
171             offset += delta;
172             length -= delta;
173         } else
174             length += delta;
175
176         return new Point(offset, length);
177     }
178 }
179
Popular Tags