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
9
* IBM Corporation - initial API and implementation
10
* Sergey Prigogin (Google)
11
*******************************************************************************/
13
package org.eclipse.cdt.internal.ui.text.spelling;
15
import org.eclipse.jface.text.BadLocationException;
16
import org.eclipse.jface.text.IDocument;
17
import org.eclipse.jface.text.contentassist.IContextInformation;
18
import org.eclipse.swt.graphics.Image;
19
import org.eclipse.swt.graphics.Point;
21
import org.eclipse.cdt.ui.text.ICCompletionProposal;
22
import org.eclipse.cdt.ui.text.IInvocationContext;
24
import org.eclipse.cdt.internal.ui.CPluginImages;
25
import org.eclipse.cdt.internal.ui.text.IHtmlTagConstants;
28
* Proposal to correct the incorrectly spelled word.
30
public class WordCorrectionProposal implements ICCompletionProposal {
31
/** The invocation context */
32
private final IInvocationContext fContext;
34
/** The length in the document */
35
private final int fLength;
37
/** The line where to apply the correction */
38
private final String fLine;
40
/** The offset in the document */
41
private final int fOffset;
43
/** The relevance of this proposal */
44
private final int fRelevance;
46
/** The word to complete */
47
private final String fWord;
50
* Creates a new word correction proposal.
52
* @param word the corrected word
53
* @param arguments the problem arguments associated with the spelling problem
54
* @param offset the offset in the document where to apply the proposal
55
* @param length the length in the document to apply the proposal
56
* @param context the invocation context for this proposal
57
* @param relevance the relevance of this proposal
59
public WordCorrectionProposal(final String word, final String[] arguments, final int offset,
60
final int length, final IInvocationContext context, final int relevance) {
61
fWord= Character.isUpperCase(arguments[0].charAt(0)) ?
62
Character.toUpperCase(word.charAt(0)) + word.substring(1) : word;
67
fRelevance= relevance;
69
final StringBuffer buffer= new StringBuffer(80);
71
buffer.append("...<br>"); //$NON-NLS-1$
72
buffer.append(getHtmlRepresentation(arguments[1]));
73
buffer.append("<b>"); //$NON-NLS-1$
74
buffer.append(getHtmlRepresentation(fWord));
75
buffer.append("</b>"); //$NON-NLS-1$
76
buffer.append(getHtmlRepresentation(arguments[2]));
77
buffer.append("<br>..."); //$NON-NLS-1$
79
fLine= buffer.toString();
83
* @see org.eclipse.jface.text.contentassist.ICompletionProposal#apply(org.eclipse.jface.text.IDocument)
85
public final void apply(final IDocument document) {
87
document.replace(fOffset, fLength, fWord);
88
} catch (BadLocationException exception) {
94
* @see org.eclipse.jface.text.contentassist.ICompletionProposal#getAdditionalProposalInfo()
96
public String getAdditionalProposalInfo() {
101
* @see org.eclipse.jface.text.contentassist.ICompletionProposal#getContextInformation()
103
public final IContextInformation getContextInformation() {
108
* @see org.eclipse.jface.text.contentassist.ICompletionProposal#getDisplayString()
110
public String getDisplayString() {
111
return Messages.bind(Messages.Spelling_correct_label, fWord);
115
* @see org.eclipse.jface.text.contentassist.ICompletionProposal#getImage()
117
public Image getImage() {
118
return CPluginImages.get(CPluginImages.IMG_CORRECTION_RENAME);
122
* @see org.eclipse.cdt.ui.text.java.IJavaCompletionProposal#getRelevance()
124
public final int getRelevance() {
129
* @see org.eclipse.jface.text.contentassist.ICompletionProposal#getSelection(org.eclipse.jface.text.IDocument)
131
public final Point getSelection(final IDocument document) {
132
int offset= fContext.getSelectionOffset();
133
int length= fContext.getSelectionLength();
135
final int delta= fWord.length() - fLength;
136
if (offset <= fOffset && offset + length >= fOffset) {
138
} else if (offset > fOffset && offset + length > fOffset + fLength) {
145
return new Point(offset, length);
149
* Returns the html representation of the specified string.
151
* @param string The string to return the html representation for
152
* @return The html representation for the string
154
public static String getHtmlRepresentation(final String string) {
155
final int length= string.length();
156
final StringBuffer buffer= new StringBuffer(string);
158
for (int offset= length - 1; offset >= 0; offset--) {
159
for (int index= 0; index < IHtmlTagConstants.HTML_ENTITY_CHARACTERS.length; index++) {
160
if (string.charAt(offset) == IHtmlTagConstants.HTML_ENTITY_CHARACTERS[index]) {
161
buffer.replace(offset, offset + 1, String.valueOf(IHtmlTagConstants.HTML_ENTITY_CODES[index]));
166
return buffer.toString();
169
public String getIdString() {