60aaeb06466b7b87115429dcd561399649751ea6
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpdt / internal / ui / text / spelling / WordCorrectionProposal.java
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11
12 package net.sourceforge.phpdt.internal.ui.text.spelling;
13
14 import java.text.MessageFormat;
15
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;
21
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;
27
28 /**
29  * Proposal to correct the incorrectly spelled word.
30  * 
31  * @since 3.0
32  */
33 public class WordCorrectionProposal implements IPHPCompletionProposal,
34                 IHtmlTagConstants {
35
36         /**
37          * Returns the html representation of the specified string.
38          * 
39          * @param string
40          *            The string to return the html representation for
41          * @return The html representation for the string
42          */
43         public static String getHtmlRepresentation(final String string) {
44
45                 final int length = string.length();
46                 final StringBuffer buffer = new StringBuffer(string);
47
48                 for (int offset = length - 1; offset >= 0; offset--) {
49
50                         for (int index = 0; index < HTML_ENTITY_CHARACTERS.length; index++) {
51
52                                 if (string.charAt(offset) == HTML_ENTITY_CHARACTERS[index]) {
53
54                                         buffer.replace(offset, offset + 1, String
55                                                         .valueOf(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 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 fWord;
80
81         /**
82          * Creates a new word correction proposal.
83          * 
84          * @param word
85          *            The corrected word
86          * @param arguments
87          *            The problem arguments associated with the spelling problem
88          * @param offset
89          *            The offset in the document where to apply the proposal
90          * @param length
91          *            The lenght in the document to apply the proposal
92          * @param context
93          *            The invocation context for this proposal
94          * @param relevance
95          *            The relevance of this proposal
96          */
97         public WordCorrectionProposal(final String word, final String[] arguments,
98                         final int offset, final int length,
99                         final IInvocationContext context, final int relevance) {
100
101                 fWord = Character.isUpperCase(arguments[0].charAt(0)) ? Character
102                                 .toUpperCase(word.charAt(0))
103                                 + word.substring(1) : word;
104
105                 fOffset = offset;
106                 fLength = length;
107                 fContext = context;
108                 fRelevance = relevance;
109
110                 final StringBuffer buffer = new StringBuffer(80);
111
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$
119
120                 fLine = buffer.toString();
121         }
122
123         /*
124          * @see org.eclipse.jface.text.contentassist.ICompletionProposal#apply(org.eclipse.jface.text.IDocument)
125          */
126         public final void apply(final IDocument document) {
127                 try {
128                         document.replace(fOffset, fLength, fWord);
129                 } catch (BadLocationException exception) {
130                         // Do nothing
131                 }
132         }
133
134         /*
135          * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getAdditionalProposalInfo()
136          */
137         public String getAdditionalProposalInfo() {
138                 return fLine;
139         }
140
141         /*
142          * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getContextInformation()
143          */
144         public final IContextInformation getContextInformation() {
145                 return null;
146         }
147
148         /*
149          * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getDisplayString()
150          */
151         public String getDisplayString() {
152                 return MessageFormat.format(PHPUIMessages
153                                 .getString("Spelling.correct.label"), new String[] { fWord }); //$NON-NLS-1$
154         }
155
156         /*
157          * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getImage()
158          */
159         public Image getImage() {
160                 return PHPUiImages.get(PHPUiImages.IMG_CORRECTION_RENAME);
161         }
162
163         /*
164          * @see net.sourceforge.phpdt.ui.text.java.IJavaCompletionProposal#getRelevance()
165          */
166         public final int getRelevance() {
167                 return fRelevance;
168         }
169
170         /*
171          * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getSelection(org.eclipse.jface.text.IDocument)
172          */
173         public final Point getSelection(final IDocument document) {
174
175                 int offset = fContext.getSelectionOffset();
176                 int length = fContext.getSelectionLength();
177
178                 final int delta = fWord.length() - fLength;
179                 if (offset <= fOffset && offset + length >= fOffset)
180                         length += delta;
181                 else if (offset > fOffset && offset + length > fOffset + fLength) {
182                         offset += delta;
183                         length -= delta;
184                 } else
185                         length += delta;
186
187                 return new Point(offset, length);
188         }
189 }