1 19 20 package org.netbeans.modules.editor.java; 21 22 import com.sun.source.tree.Scope; 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.EnumSet ; 28 import javax.lang.model.element.Element; 29 import javax.lang.model.element.ElementKind; 30 import javax.lang.model.element.TypeElement; 31 import javax.lang.model.type.DeclaredType; 32 import javax.swing.text.JTextComponent ; 33 import org.netbeans.api.java.source.CompilationController; 34 import org.netbeans.api.java.source.CancellableTask; 35 import org.netbeans.api.java.source.ElementHandle; 36 import org.netbeans.api.java.source.JavaSource; 37 import org.netbeans.spi.editor.completion.CompletionTask; 38 import org.netbeans.spi.editor.completion.LazyCompletionItem; 39 import org.netbeans.spi.editor.completion.support.CompletionUtilities; 40 41 45 public class LazyTypeCompletionItem extends JavaCompletionItem implements LazyCompletionItem { 46 47 public static final LazyTypeCompletionItem create(ElementHandle<TypeElement> handle, EnumSet <ElementKind> kinds, int substitutionOffset, JavaSource javaSource) { 48 return new LazyTypeCompletionItem(handle, kinds, substitutionOffset, javaSource); 49 } 50 51 private ElementHandle<TypeElement> handle; 52 private EnumSet <ElementKind> kinds; 53 private JavaSource javaSource; 54 private String name; 55 private String simpleName; 56 private String pkgName; 57 private JavaCompletionItem delegate = null; 58 private LazyTypeCompletionItem nextItem = null; 59 private String sortText; 60 private int prefWidth = -1; 61 62 private LazyTypeCompletionItem(ElementHandle<TypeElement> handle, EnumSet <ElementKind> kinds, int substitutionOffset, JavaSource javaSource) { 63 super(substitutionOffset); 64 this.handle = handle; 65 this.kinds = kinds; 66 this.javaSource = javaSource; 67 this.name = handle.getQualifiedName(); 68 int idx = name.lastIndexOf('.'); this.simpleName = idx > -1 ? name.substring(idx + 1) : name; 70 this.pkgName = idx > -1 ? name.substring(0, idx) : ""; } 72 73 public boolean accept() { 74 if (handle != null) { 75 if (simpleName.length() == 0 || Character.isDigit(simpleName.charAt(0))) { 76 handle = null; 77 return false; 78 } 79 try { 80 javaSource.runUserActionTask(new CancellableTask<CompilationController>() { 81 public void cancel() { 82 } 83 public void run(CompilationController controller) throws Exception { 84 controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); 85 Scope scope = controller.getTrees().getScope(controller.getTreeUtilities().pathFor(substitutionOffset)); 86 LazyTypeCompletionItem item = LazyTypeCompletionItem.this; 87 for (int i = 0; i < 50 && item != null;) { 88 if (item.init(controller, scope)) 89 i++; 90 item = item.nextItem; 91 } 92 } 93 }, true); 94 } catch(Throwable t) { 95 } 96 } 97 return delegate != null; 98 } 99 100 public void defaultAction(JTextComponent component) { 101 if (delegate != null) 102 delegate.defaultAction(component); 103 } 104 105 public void processKeyEvent(KeyEvent evt) { 106 if (delegate != null) 107 delegate.processKeyEvent(evt); 108 } 109 110 public int getPreferredWidth(Graphics g, Font defaultFont) { 111 if (prefWidth < 0) 112 prefWidth = CompletionUtilities.getPreferredWidth(simpleName + " (" + pkgName + ")", null, g, defaultFont); return prefWidth; 114 } 115 116 public void render(Graphics g, Font defaultFont, Color defaultColor, Color backgroundColor, int width, int height, boolean selected) { 117 if (delegate != null) 118 delegate.render(g, defaultFont, defaultColor, backgroundColor, width, height, selected); 119 } 120 121 public CompletionTask createDocumentationTask() { 122 if (delegate != null) 123 return delegate.createDocumentationTask(); 124 return null; 125 } 126 127 public CompletionTask createToolTipTask() { 128 if (delegate != null) 129 return delegate.createToolTipTask(); 130 return null; 131 } 132 133 public boolean instantSubstitution(JTextComponent component) { 134 if (delegate != null) 135 return delegate.instantSubstitution(component); 136 return false; 137 } 138 139 public int getSortPriority() { 140 return 700; 141 } 142 143 public CharSequence getSortText() { 144 if (sortText == null) 145 sortText = simpleName + "#" + name; return sortText; 147 } 148 149 public CharSequence getInsertPrefix() { 150 return simpleName; 151 } 152 153 void setNextItem(LazyTypeCompletionItem nextItem) { 154 this.nextItem = nextItem; 155 } 156 157 boolean init(CompilationController controller, Scope scope) { 158 if (simpleName.length() >= 0 && !Character.isDigit(simpleName.charAt(0))) { 159 TypeElement e = handle.resolve(controller); 160 if (e != null && controller.getTrees().isAccessible(scope, e)) { 161 if (isOfKind(e, kinds)) 162 delegate = JavaCompletionItem.createTypeItem((TypeElement)e, (DeclaredType)e.asType(), substitutionOffset, true, controller.getElements().isDeprecated(e), false); 163 } 164 } 165 handle = null; 166 return delegate != null; 167 } 168 169 private boolean isOfKind(Element e, EnumSet <ElementKind> kinds) { 170 if (kinds.contains(e.getKind())) 171 return true; 172 for (Element ee : e.getEnclosedElements()) 173 if (isOfKind(ee, kinds)) 174 return true; 175 return false; 176 } 177 } 178 | Popular Tags |