6d0d4adeb0f91ccaf0e752c793d615cbe0d9bef6
[phpeclipse.git] / net.sourceforge.phpeclipse / 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, IHtmlTagConstants {
34
35         /**
36          * Returns the html representation of the specified string.
37          * 
38          * @param string
39          *                   The string to return the html representation for
40          * @return The html representation for the string
41          */
42         public static String getHtmlRepresentation(final String string) {
43
44                 final int length= string.length();
45                 final StringBuffer buffer= new StringBuffer(string);
46
47                 for (int offset= length - 1; offset >= 0; offset--) {
48
49                         for (int index= 0; index < HTML_ENTITY_CHARACTERS.length; index++) {
50
51                                 if (string.charAt(offset) == HTML_ENTITY_CHARACTERS[index]) {
52
53                                         buffer.replace(offset, offset + 1, String.valueOf(HTML_ENTITY_CODES[index]));
54                                         break;
55                                 }
56                         }
57                 }
58                 return buffer.toString();
59         }
60
61         /** The invocation context */
62         private final IInvocationContext fContext;
63
64         /** The length in the document */
65         private final int fLength;
66
67         /** The line where to apply the correction */
68         private final String fLine;
69
70         /** The offset in the document */
71         private final int fOffset;
72
73         /** The relevance of this proposal */
74         private final int fRelevance;
75
76         /** The word to complete */
77         private final String fWord;
78
79         /**
80          * Creates a new word correction proposal.
81          * 
82          * @param word
83          *                   The corrected word
84          * @param arguments
85          *                   The problem arguments associated with the spelling problem
86          * @param offset
87          *                   The offset in the document where to apply the proposal
88          * @param length
89          *                   The lenght in the document to apply the proposal
90          * @param context
91          *                   The invocation context for this proposal
92          * @param relevance
93          *                   The relevance of this proposal
94          */
95         public WordCorrectionProposal(final String word, final String[] arguments, final int offset, final int length, final IInvocationContext context, final int relevance) {
96
97                 fWord= Character.isUpperCase(arguments[0].charAt(0)) ? Character.toUpperCase(word.charAt(0)) + word.substring(1) : word;
98
99                 fOffset= offset;
100                 fLength= length;
101                 fContext= context;
102                 fRelevance= relevance;
103
104                 final StringBuffer buffer= new StringBuffer(80);
105
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$
113
114                 fLine= buffer.toString();
115         }
116
117         /*
118          * @see org.eclipse.jface.text.contentassist.ICompletionProposal#apply(org.eclipse.jface.text.IDocument)
119          */
120         public final void apply(final IDocument document) {
121                 try {
122                         document.replace(fOffset, fLength, fWord);
123                 } catch (BadLocationException exception) {
124                         // Do nothing
125                 }
126         }
127
128         /*
129          * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getAdditionalProposalInfo()
130          */
131         public String getAdditionalProposalInfo() {
132                 return fLine;
133         }
134
135         /*
136          * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getContextInformation()
137          */
138         public final IContextInformation getContextInformation() {
139                 return null;
140         }
141
142         /*
143          * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getDisplayString()
144          */
145         public String getDisplayString() {
146                 return MessageFormat.format(PHPUIMessages.getString("Spelling.correct.label"), new String[] { fWord }); //$NON-NLS-1$
147         }
148
149         /*
150          * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getImage()
151          */
152         public Image getImage() {
153                 return PHPUiImages.get(PHPUiImages.IMG_CORRECTION_RENAME);
154         }
155
156         /*
157          * @see org.eclipse.jdt.ui.text.java.IJavaCompletionProposal#getRelevance()
158          */
159         public final int getRelevance() {
160                 return fRelevance;
161         }
162
163         /*
164          * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getSelection(org.eclipse.jface.text.IDocument)
165          */
166         public final Point getSelection(final IDocument document) {
167
168                 int offset= fContext.getSelectionOffset();
169                 int length= fContext.getSelectionLength();
170
171                 final int delta= fWord.length() - fLength;
172                 if (offset <= fOffset && offset + length >= fOffset)
173                         length += delta;
174                 else if (offset > fOffset && offset + length > fOffset + fLength) {
175                         offset += delta;
176                         length -= delta;
177                 } else
178                         length += delta;
179
180                 return new Point(offset, length);
181         }
182 }