1 11 12 package org.eclipse.jdt.internal.ui.text.spelling; 13 14 import org.eclipse.swt.graphics.Image; 15 import org.eclipse.swt.graphics.Point; 16 17 import org.eclipse.jface.text.BadLocationException; 18 import org.eclipse.jface.text.IDocument; 19 import org.eclipse.jface.text.contentassist.IContextInformation; 20 21 import org.eclipse.jdt.internal.corext.util.Messages; 22 23 import org.eclipse.jdt.ui.text.java.IInvocationContext; 24 import org.eclipse.jdt.ui.text.java.IJavaCompletionProposal; 25 26 import org.eclipse.jdt.internal.ui.JavaPluginImages; 27 import org.eclipse.jdt.internal.ui.JavaUIMessages; 28 import org.eclipse.jdt.internal.ui.text.javadoc.IHtmlTagConstants; 29 30 35 public class WordCorrectionProposal implements IJavaCompletionProposal { 36 37 44 public static String getHtmlRepresentation(final String string) { 45 46 final int length= string.length(); 47 final StringBuffer buffer= new StringBuffer (string); 48 49 for (int offset= length - 1; offset >= 0; offset--) { 50 51 for (int index= 0; index < IHtmlTagConstants.HTML_ENTITY_CHARACTERS.length; index++) { 52 53 if (string.charAt(offset) == IHtmlTagConstants.HTML_ENTITY_CHARACTERS[index]) { 54 55 buffer.replace(offset, offset + 1, String.valueOf(IHtmlTagConstants.HTML_ENTITY_CODES[index])); 56 break; 57 } 58 } 59 } 60 return buffer.toString(); 61 } 62 63 64 private final IInvocationContext fContext; 65 66 67 private final int fLength; 68 69 70 private final String fLine; 71 72 73 private final int fOffset; 74 75 76 private final int fRelevance; 77 78 79 private final String fWord; 80 81 91 public WordCorrectionProposal(final String word, final String [] arguments, final int offset, final int length, final IInvocationContext context, final int relevance) { 92 93 fWord= Character.isUpperCase(arguments[0].charAt(0)) ? Character.toUpperCase(word.charAt(0)) + word.substring(1) : word; 94 95 fOffset= offset; 96 fLength= length; 97 fContext= context; 98 fRelevance= relevance; 99 100 final StringBuffer buffer= new StringBuffer (80); 101 102 buffer.append("...<br>"); buffer.append(getHtmlRepresentation(arguments[1])); 104 buffer.append("<b>"); buffer.append(getHtmlRepresentation(fWord)); 106 buffer.append("</b>"); buffer.append(getHtmlRepresentation(arguments[2])); 108 buffer.append("<br>..."); 110 fLine= buffer.toString(); 111 } 112 113 116 public final void apply(final IDocument document) { 117 try { 118 document.replace(fOffset, fLength, fWord); 119 } catch (BadLocationException exception) { 120 } 122 } 123 124 127 public String getAdditionalProposalInfo() { 128 return fLine; 129 } 130 131 134 public final IContextInformation getContextInformation() { 135 return null; 136 } 137 138 141 public String getDisplayString() { 142 return Messages.format(JavaUIMessages.Spelling_correct_label, new String [] { fWord }); 143 } 144 145 148 public Image getImage() { 149 return JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_RENAME); 150 } 151 152 155 public final int getRelevance() { 156 return fRelevance; 157 } 158 159 162 public final Point getSelection(final IDocument document) { 163 164 int offset= fContext.getSelectionOffset(); 165 int length= fContext.getSelectionLength(); 166 167 final int delta= fWord.length() - fLength; 168 if (offset <= fOffset && offset + length >= fOffset) 169 length += delta; 170 else if (offset > fOffset && offset + length > fOffset + fLength) { 171 offset += delta; 172 length -= delta; 173 } else 174 length += delta; 175 176 return new Point(offset, length); 177 } 178 } 179 | Popular Tags |