1 19 20 package org.netbeans.modules.retouche.editor.completion; 21 22 23 import java.awt.Color ; 24 import java.awt.Font ; 25 import java.awt.Graphics ; 26 import java.awt.event.KeyEvent ; 27 import java.util.*; 28 import javax.swing.ImageIcon ; 29 import javax.swing.text.BadLocationException ; 30 import javax.swing.text.JTextComponent ; 31 import org.netbeans.api.editor.completion.Completion; 32 import org.netbeans.api.gsf.CompletionProposal; 33 import org.netbeans.api.gsf.Modifier; 34 import org.netbeans.api.retouche.source.CompilationInfo; 35 import org.netbeans.editor.BaseDocument; 36 import org.netbeans.spi.editor.completion.CompletionItem; 37 import org.netbeans.spi.editor.completion.CompletionTask; 38 import org.netbeans.spi.editor.completion.support.CompletionUtilities; 39 40 47 public abstract class GsfCompletionItem implements CompletionItem { 48 protected CompilationInfo info; 49 50 private static class DelegatedItem extends GsfCompletionItem { 51 private org.netbeans.api.gsf.CompletionProposal item; 52 private static final String METHOD_PUBLIC = "org/netbeans/modules/editor/resources/completion/method_16.png"; private static final String METHOD_PROTECTED = "org/netbeans/modules/editor/resources/completion/method_protected_16.png"; private static final String METHOD_PACKAGE = "org/netbeans/modules/editor/resources/completion/method_package_private_16.png"; private static final String METHOD_PRIVATE = "org/netbeans/modules/editor/resources/completion/method_private_16.png"; private static final String METHOD_ST_PUBLIC = "org/netbeans/modules/editor/resources/completion/method_static_16.png"; private static final String METHOD_ST_PROTECTED = "org/netbeans/modules/editor/resources/completion/method_static_protected_16.png"; private static final String METHOD_ST_PRIVATE = "org/netbeans/modules/editor/resources/completion/method_static_private_16.png"; private static final String METHOD_ST_PACKAGE = "org/netbeans/modules/editor/resources/completion/method_static_package_private_16.png"; private static final String METHOD_COLOR = "<font color=#000000>"; private static final String PARAMETER_NAME_COLOR = "<font color=#a06001>"; private static ImageIcon icon[][] = new ImageIcon [2][4]; 63 64 private DelegatedItem(CompilationInfo info, org.netbeans.api.gsf.CompletionProposal item) { 65 super(item.getAnchorOffset()); 66 this.item = item; 67 this.info = info; 68 } 69 70 public int getSortPriority() { 71 switch (item.getKind()) { 72 case CONSTRUCTOR: return 400; 73 case MODULE: return 900; 74 case CLASS: return 800; 75 case METHOD: return 500; 76 case ATTRIBUTE: 77 case FIELD: return 300; 78 case CONSTANT: 79 case VARIABLE: return 200; 80 case KEYWORD: return 600; 81 case OTHER: 82 default: 83 return 999; 84 } 85 } 86 87 public CharSequence getSortText() { 88 return item.getSortText(); 89 } 90 91 public CharSequence getInsertPrefix() { 92 return item.getInsertPrefix(); 93 } 94 95 protected String getLeftHtmlText() { 96 return item.getLhsHtml(); 97 } 98 99 public String toString() { 100 return item.getName(); 101 } 102 103 protected String getRightHtmlText() { 104 String rhs = item.getRhsHtml(); 105 106 String lhs = item.getLhsHtml(); 108 boolean inTag = false; 109 int length = 0; 110 for (int i = 0, n = lhs.length(); i < n; i++) { 111 char c = lhs.charAt(i); 112 if (inTag) { 113 if (c == '>') { 114 inTag = false; 115 } 116 } else if (c == '<') { 117 inTag = true; 118 } else { 119 length++; 120 } 121 } 122 123 return truncateRhs(rhs, length); 124 } 125 126 public CompletionTask createDocumentationTask() { 127 if (item.getElement() != null) { 128 return GsfCompletionProvider.createDocTask(item.getElement(), info); 129 } 130 131 return null; 132 } 133 134 protected ImageIcon getIcon() { 135 ImageIcon ic = item.getIcon(); 136 if (ic != null) { 137 return ic; 138 } 139 140 ImageIcon icon = org.netbeans.modules.retouche.navigation.Icons.getElementIcon(item.getKind(), item.getModifiers()); 141 return icon; 143 } 168 169 } 223 224 225 public static final GsfCompletionItem createItem(CompletionProposal proposal, CompilationInfo info) { 226 return new DelegatedItem(info, proposal); 227 } 228 229 public static final String COLOR_END = "</font>"; public static final String STRIKE = "<s>"; public static final String STRIKE_END = "</s>"; public static final String BOLD = "<b>"; public static final String BOLD_END = "</b>"; 235 protected int substitutionOffset; 236 237 private GsfCompletionItem(int substitutionOffset) { 238 this.substitutionOffset = substitutionOffset; 239 } 240 241 public void defaultAction(JTextComponent component) { 242 if (component != null) { 243 Completion.get().hideDocumentation(); 244 Completion.get().hideCompletion(); 245 int caretOffset = component.getSelectionEnd(); 246 substituteText(component, substitutionOffset, caretOffset - substitutionOffset, null); 247 } 248 } 249 250 public void processKeyEvent(KeyEvent evt) { 251 if (evt.getID() == KeyEvent.KEY_TYPED) { 252 switch (evt.getKeyChar()) { 253 case ';': 254 case ',': 255 case '(': 256 case '.': 257 Completion.get().hideDocumentation(); 258 Completion.get().hideCompletion(); 259 } 266 } 267 } 268 269 public boolean instantSubstitution(JTextComponent component) { 270 defaultAction(component); 271 return true; 272 } 273 274 public CompletionTask createDocumentationTask() { 275 return null; 276 } 277 278 public CompletionTask createToolTipTask() { 279 return null; 280 } 281 282 public int getPreferredWidth(Graphics g, Font defaultFont) { 283 return CompletionUtilities.getPreferredWidth(getLeftHtmlText(), getRightHtmlText(), g, defaultFont); 284 } 285 286 public void render(Graphics g, Font defaultFont, Color defaultColor, Color backgroundColor, int width, int height, boolean selected) { 287 CompletionUtilities.renderHtml(getIcon(), getLeftHtmlText(), getRightHtmlText(), g, defaultFont, defaultColor, width, height, selected); 288 } 289 290 protected abstract ImageIcon getIcon(); 291 292 protected String getLeftHtmlText() { 293 return null; 294 } 295 296 protected String getRightHtmlText() { 297 return null; 298 } 299 300 protected void substituteText(JTextComponent c, int offset, int len, String toAdd) { 301 BaseDocument doc = (BaseDocument)c.getDocument(); 302 String text = getInsertPrefix().toString(); 303 if (text != null) { 304 doc.atomicLock(); 338 try { 339 String textToReplace = doc.getText(offset, len); 340 if (text.equals(textToReplace)) return; 341 342 doc.remove(offset, len); 343 doc.insertString(offset, text, null); 344 } catch (BadLocationException e) { 345 } finally { 347 doc.atomicUnlock(); 348 } 349 } 350 } 351 352 private static String truncateRhs(String rhs, int left) { 353 if (rhs != null) { 354 final int MAX_SIZE = 40; 355 int size = MAX_SIZE-left; 356 if (size < 10) { 357 size = 10; 358 } 359 if (rhs != null && rhs.length() > size) { 360 rhs = rhs.substring(0,size-3) + "<b>></b>"; } 362 } 363 return rhs; 364 } 365 366 367 377 378 379 private static final int PUBLIC_LEVEL = 3; 380 private static final int PROTECTED_LEVEL = 2; 381 private static final int PACKAGE_LEVEL = 1; 382 private static final int PRIVATE_LEVEL = 0; 383 384 private static int getProtectionLevel(Set<Modifier> modifiers) { 385 if(modifiers.contains(Modifier.PUBLIC)) 386 return PUBLIC_LEVEL; 387 if(modifiers.contains(Modifier.PROTECTED)) 388 return PROTECTED_LEVEL; 389 if(modifiers.contains(Modifier.PRIVATE)) 390 return PRIVATE_LEVEL; 391 return PACKAGE_LEVEL; 392 } 393 } 394 | Popular Tags |