KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > java > LazyTypeCompletionItem


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.editor.java;
21
22 import com.sun.source.tree.Scope;
23 import java.awt.Color JavaDoc;
24 import java.awt.Font JavaDoc;
25 import java.awt.Graphics JavaDoc;
26 import java.awt.event.KeyEvent JavaDoc;
27 import java.util.EnumSet JavaDoc;
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 JavaDoc;
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 /**
42  *
43  * @author Dusan Balek
44  */

45 public class LazyTypeCompletionItem extends JavaCompletionItem implements LazyCompletionItem {
46     
47     public static final LazyTypeCompletionItem create(ElementHandle<TypeElement> handle, EnumSet JavaDoc<ElementKind> kinds, int substitutionOffset, JavaSource javaSource) {
48         return new LazyTypeCompletionItem(handle, kinds, substitutionOffset, javaSource);
49     }
50     
51     private ElementHandle<TypeElement> handle;
52     private EnumSet JavaDoc<ElementKind> kinds;
53     private JavaSource javaSource;
54     private String JavaDoc name;
55     private String JavaDoc simpleName;
56     private String JavaDoc pkgName;
57     private JavaCompletionItem delegate = null;
58     private LazyTypeCompletionItem nextItem = null;
59     private String JavaDoc sortText;
60     private int prefWidth = -1;
61     
62     private LazyTypeCompletionItem(ElementHandle<TypeElement> handle, EnumSet JavaDoc<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('.'); //NOI18N
69
this.simpleName = idx > -1 ? name.substring(idx + 1) : name;
70         this.pkgName = idx > -1 ? name.substring(0, idx) : ""; //NOI18N
71
}
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 JavaDoc {
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 JavaDoc t) {
95             }
96         }
97         return delegate != null;
98     }
99
100     public void defaultAction(JTextComponent JavaDoc component) {
101         if (delegate != null)
102             delegate.defaultAction(component);
103     }
104
105     public void processKeyEvent(KeyEvent JavaDoc evt) {
106         if (delegate != null)
107             delegate.processKeyEvent(evt);
108     }
109
110     public int getPreferredWidth(Graphics JavaDoc g, Font JavaDoc defaultFont) {
111         if (prefWidth < 0)
112             prefWidth = CompletionUtilities.getPreferredWidth(simpleName + " (" + pkgName + ")", null, g, defaultFont); //NOI18N
113
return prefWidth;
114     }
115
116     public void render(Graphics JavaDoc g, Font JavaDoc defaultFont, Color JavaDoc defaultColor, Color JavaDoc 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 JavaDoc 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 JavaDoc getSortText() {
144         if (sortText == null)
145             sortText = simpleName + "#" + name; //NOI18N
146
return sortText;
147     }
148
149     public CharSequence JavaDoc 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 JavaDoc<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