1 /*******************************************************************************
2 * Copyright (c) 2000, 2003 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
12 package net.sourceforge.phpdt.internal.ui.text.spelling;
14 import java.text.MessageFormat;
16 import net.sourceforge.phpdt.internal.ui.PHPUIMessages;
17 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
18 import net.sourceforge.phpdt.internal.ui.text.java.IInvocationContext;
19 import net.sourceforge.phpdt.internal.ui.text.java.IPHPCompletionProposal;
20 import net.sourceforge.phpdt.internal.ui.text.phpdoc.IHtmlTagConstants;
22 import org.eclipse.jface.text.BadLocationException;
23 import org.eclipse.jface.text.IDocument;
24 import org.eclipse.jface.text.contentassist.IContextInformation;
25 import org.eclipse.swt.graphics.Image;
26 import org.eclipse.swt.graphics.Point;
29 * Proposal to correct the incorrectly spelled word.
33 public class WordCorrectionProposal implements IPHPCompletionProposal,
37 * Returns the html representation of the specified string.
40 * The string to return the html representation for
41 * @return The html representation for the string
43 public static String getHtmlRepresentation(final String string) {
45 final int length = string.length();
46 final StringBuffer buffer = new StringBuffer(string);
48 for (int offset = length - 1; offset >= 0; offset--) {
50 for (int index = 0; index < HTML_ENTITY_CHARACTERS.length; index++) {
52 if (string.charAt(offset) == HTML_ENTITY_CHARACTERS[index]) {
54 buffer.replace(offset, offset + 1, String
55 .valueOf(HTML_ENTITY_CODES[index]));
60 return buffer.toString();
63 /** The invocation context */
64 private final IInvocationContext fContext;
66 /** The length in the document */
67 private final int fLength;
69 /** The line where to apply the correction */
70 private final String fLine;
72 /** The offset in the document */
73 private final int fOffset;
75 /** The relevance of this proposal */
76 private final int fRelevance;
78 /** The word to complete */
79 private final String fWord;
82 * Creates a new word correction proposal.
87 * The problem arguments associated with the spelling problem
89 * The offset in the document where to apply the proposal
91 * The lenght in the document to apply the proposal
93 * The invocation context for this proposal
95 * The relevance of this proposal
97 public WordCorrectionProposal(final String word, final String[] arguments,
98 final int offset, final int length,
99 final IInvocationContext context, final int relevance) {
101 fWord = Character.isUpperCase(arguments[0].charAt(0)) ? Character
102 .toUpperCase(word.charAt(0))
103 + word.substring(1) : word;
108 fRelevance = relevance;
110 final StringBuffer buffer = new StringBuffer(80);
112 buffer.append("...<br>"); //$NON-NLS-1$
113 buffer.append(getHtmlRepresentation(arguments[1]));
114 buffer.append("<b>"); //$NON-NLS-1$
115 buffer.append(getHtmlRepresentation(fWord));
116 buffer.append("</b>"); //$NON-NLS-1$
117 buffer.append(getHtmlRepresentation(arguments[2]));
118 buffer.append("<br>..."); //$NON-NLS-1$
120 fLine = buffer.toString();
124 * @see org.eclipse.jface.text.contentassist.ICompletionProposal#apply(org.eclipse.jface.text.IDocument)
126 public final void apply(final IDocument document) {
128 document.replace(fOffset, fLength, fWord);
129 } catch (BadLocationException exception) {
135 * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getAdditionalProposalInfo()
137 public String getAdditionalProposalInfo() {
142 * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getContextInformation()
144 public final IContextInformation getContextInformation() {
149 * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getDisplayString()
151 public String getDisplayString() {
152 return MessageFormat.format(PHPUIMessages
153 .getString("Spelling.correct.label"), new String[] { fWord }); //$NON-NLS-1$
157 * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getImage()
159 public Image getImage() {
160 return PHPUiImages.get(PHPUiImages.IMG_CORRECTION_RENAME);
164 * @see net.sourceforge.phpdt.ui.text.java.IJavaCompletionProposal#getRelevance()
166 public final int getRelevance() {
171 * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getSelection(org.eclipse.jface.text.IDocument)
173 public final Point getSelection(final IDocument document) {
175 int offset = fContext.getSelectionOffset();
176 int length = fContext.getSelectionLength();
178 final int delta = fWord.length() - fLength;
179 if (offset <= fOffset && offset + length >= fOffset)
181 else if (offset > fOffset && offset + length > fOffset + fLength) {
187 return new Point(offset, length);