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, IHtmlTagConstants {
36 * Returns the html representation of the specified string.
39 * The string to return the html representation for
40 * @return The html representation for the string
42 public static String getHtmlRepresentation(final String string) {
44 final int length= string.length();
45 final StringBuffer buffer= new StringBuffer(string);
47 for (int offset= length - 1; offset >= 0; offset--) {
49 for (int index= 0; index < HTML_ENTITY_CHARACTERS.length; index++) {
51 if (string.charAt(offset) == HTML_ENTITY_CHARACTERS[index]) {
53 buffer.replace(offset, offset + 1, String.valueOf(HTML_ENTITY_CODES[index]));
58 return buffer.toString();
61 /** The invocation context */
62 private final IInvocationContext fContext;
64 /** The length in the document */
65 private final int fLength;
67 /** The line where to apply the correction */
68 private final String fLine;
70 /** The offset in the document */
71 private final int fOffset;
73 /** The relevance of this proposal */
74 private final int fRelevance;
76 /** The word to complete */
77 private final String fWord;
80 * Creates a new word correction proposal.
85 * The problem arguments associated with the spelling problem
87 * The offset in the document where to apply the proposal
89 * The lenght in the document to apply the proposal
91 * The invocation context for this proposal
93 * The relevance of this proposal
95 public WordCorrectionProposal(final String word, final String[] arguments, final int offset, final int length, final IInvocationContext context, final int relevance) {
97 fWord= Character.isUpperCase(arguments[0].charAt(0)) ? Character.toUpperCase(word.charAt(0)) + word.substring(1) : word;
102 fRelevance= relevance;
104 final StringBuffer buffer= new StringBuffer(80);
106 buffer.append("...<br>"); //$NON-NLS-1$
107 buffer.append(getHtmlRepresentation(arguments[1]));
108 buffer.append("<b>"); //$NON-NLS-1$
109 buffer.append(getHtmlRepresentation(fWord));
110 buffer.append("</b>"); //$NON-NLS-1$
111 buffer.append(getHtmlRepresentation(arguments[2]));
112 buffer.append("<br>..."); //$NON-NLS-1$
114 fLine= buffer.toString();
118 * @see org.eclipse.jface.text.contentassist.ICompletionProposal#apply(org.eclipse.jface.text.IDocument)
120 public final void apply(final IDocument document) {
122 document.replace(fOffset, fLength, fWord);
123 } catch (BadLocationException exception) {
129 * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getAdditionalProposalInfo()
131 public String getAdditionalProposalInfo() {
136 * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getContextInformation()
138 public final IContextInformation getContextInformation() {
143 * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getDisplayString()
145 public String getDisplayString() {
146 return MessageFormat.format(PHPUIMessages.getString("Spelling.correct.label"), new String[] { fWord }); //$NON-NLS-1$
150 * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getImage()
152 public Image getImage() {
153 return PHPUiImages.get(PHPUiImages.IMG_CORRECTION_RENAME);
157 * @see org.eclipse.jdt.ui.text.java.IJavaCompletionProposal#getRelevance()
159 public final int getRelevance() {
164 * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getSelection(org.eclipse.jface.text.IDocument)
166 public final Point getSelection(final IDocument document) {
168 int offset= fContext.getSelectionOffset();
169 int length= fContext.getSelectionLength();
171 final int delta= fWord.length() - fLength;
172 if (offset <= fOffset && offset + length >= fOffset)
174 else if (offset > fOffset && offset + length > fOffset + fLength) {
180 return new Point(offset, length);