2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
42
package org.netbeans.modules.editor.java;
44
import com.sun.source.tree.Scope;
45
import java.awt.Color;
47
import java.awt.Graphics;
48
import java.awt.event.KeyEvent;
49
import java.util.EnumSet;
50
import javax.lang.model.element.Element;
51
import javax.lang.model.element.ElementKind;
52
import javax.lang.model.element.TypeElement;
53
import javax.lang.model.type.DeclaredType;
54
import javax.swing.text.JTextComponent;
55
import org.netbeans.api.java.source.CompilationController;
56
import org.netbeans.api.java.source.Task;
57
import org.netbeans.api.java.source.ElementHandle;
58
import org.netbeans.api.java.source.JavaSource;
59
import org.netbeans.spi.editor.completion.CompletionTask;
60
import org.netbeans.spi.editor.completion.LazyCompletionItem;
61
import org.netbeans.spi.editor.completion.support.CompletionUtilities;
67
public class LazyTypeCompletionItem extends JavaCompletionItem implements LazyCompletionItem {
69
public static final LazyTypeCompletionItem create(ElementHandle<TypeElement> handle, EnumSet<ElementKind> kinds, int substitutionOffset, JavaSource javaSource, boolean insideNew) {
70
return new LazyTypeCompletionItem(handle, kinds, substitutionOffset, javaSource, insideNew);
73
private ElementHandle<TypeElement> handle;
74
private EnumSet<ElementKind> kinds;
75
private JavaSource javaSource;
76
private boolean insideNew;
78
private String simpleName;
79
private String pkgName;
80
private JavaCompletionItem delegate = null;
81
private CharSequence sortText;
82
private int prefWidth = -1;
84
private LazyTypeCompletionItem(ElementHandle<TypeElement> handle, EnumSet<ElementKind> kinds, int substitutionOffset, JavaSource javaSource, boolean insideNew) {
85
super(substitutionOffset);
88
this.javaSource = javaSource;
89
this.insideNew = insideNew;
90
this.name = handle.getQualifiedName();
91
int idx = name.lastIndexOf('.'); //NOI18N
92
this.simpleName = idx > -1 ? name.substring(idx + 1) : name;
93
this.pkgName = idx > -1 ? name.substring(0, idx) : ""; //NOI18N
94
this.sortText = new ClassSortText(this.simpleName, this.pkgName);
97
public boolean accept() {
104
long t = System.currentTimeMillis();
105
javaSource.runUserActionTask(new Task<CompilationController>() {
107
public void run(CompilationController controller) throws Exception {
108
controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
109
Scope scope = controller.getTrees().getScope(controller.getTreeUtilities().pathFor(substitutionOffset));
110
if (!isAnnonInner()) {
111
TypeElement e = handle.resolve(controller);
112
if (e != null && controller.getTrees().isAccessible(scope, e)) {
113
if (isOfKind(e, kinds))
114
delegate = JavaCompletionItem.createTypeItem(e, (DeclaredType)e.asType(), substitutionOffset, true, controller.getElements().isDeprecated(e), insideNew, false);
120
} catch(Throwable t) {
123
return delegate != null;
126
public void defaultAction(JTextComponent component) {
127
if (delegate != null)
128
delegate.defaultAction(component);
131
public void processKeyEvent(KeyEvent evt) {
132
if (delegate != null)
133
delegate.processKeyEvent(evt);
136
public int getPreferredWidth(Graphics g, Font defaultFont) {
138
prefWidth = CompletionUtilities.getPreferredWidth(simpleName + " (" + pkgName + ")", null, g, defaultFont); //NOI18N
142
public void render(Graphics g, Font defaultFont, Color defaultColor, Color backgroundColor, int width, int height, boolean selected) {
143
if (delegate != null)
144
delegate.render(g, defaultFont, defaultColor, backgroundColor, width, height, selected);
147
public CompletionTask createDocumentationTask() {
148
if (delegate != null)
149
return delegate.createDocumentationTask();
153
public CompletionTask createToolTipTask() {
154
if (delegate != null)
155
return delegate.createToolTipTask();
159
public int getSortPriority() {
163
public CharSequence getSortText() {
167
public CharSequence getInsertPrefix() {
171
boolean isAnnonInner() {
172
return simpleName.length() == 0 || Character.isDigit(simpleName.charAt(0));
175
private boolean isOfKind(Element e, EnumSet<ElementKind> kinds) {
176
if (kinds.contains(e.getKind()))
178
for (Element ee : e.getEnclosedElements())
179
if (isOfKind(ee, kinds))