~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to java/editor/src/org/netbeans/modules/editor/java/LazyTypeCompletionItem.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
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]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
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.
 
29
 *
 
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.
 
40
 */
 
41
 
 
42
package org.netbeans.modules.editor.java;
 
43
 
 
44
import com.sun.source.tree.Scope;
 
45
import java.awt.Color;
 
46
import java.awt.Font;
 
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;
 
62
 
 
63
/**
 
64
 *
 
65
 * @author Dusan Balek
 
66
 */
 
67
public class LazyTypeCompletionItem extends JavaCompletionItem implements LazyCompletionItem {
 
68
    
 
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);
 
71
    }
 
72
    
 
73
    private ElementHandle<TypeElement> handle;
 
74
    private EnumSet<ElementKind> kinds;
 
75
    private JavaSource javaSource;
 
76
    private boolean insideNew;
 
77
    private String name;
 
78
    private String simpleName;
 
79
    private String pkgName;
 
80
    private JavaCompletionItem delegate = null;
 
81
    private CharSequence sortText;
 
82
    private int prefWidth = -1;
 
83
    
 
84
    private LazyTypeCompletionItem(ElementHandle<TypeElement> handle, EnumSet<ElementKind> kinds, int substitutionOffset, JavaSource javaSource, boolean insideNew) {
 
85
        super(substitutionOffset);
 
86
        this.handle = handle;
 
87
        this.kinds = kinds;
 
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);
 
95
    }
 
96
    
 
97
    public boolean accept() {
 
98
        if (handle != null) {
 
99
            if (isAnnonInner()) {
 
100
                handle = null;
 
101
                return false;
 
102
            }
 
103
            try {
 
104
                long t = System.currentTimeMillis();
 
105
                javaSource.runUserActionTask(new Task<CompilationController>() {
 
106
 
 
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);
 
115
                            }
 
116
                        }
 
117
                        handle = null;
 
118
                    }
 
119
                }, true);
 
120
            } catch(Throwable t) {
 
121
            }
 
122
        }
 
123
        return delegate != null;
 
124
    }
 
125
 
 
126
    public void defaultAction(JTextComponent component) {
 
127
        if (delegate != null)
 
128
            delegate.defaultAction(component);
 
129
    }
 
130
 
 
131
    public void processKeyEvent(KeyEvent evt) {
 
132
        if (delegate != null)
 
133
            delegate.processKeyEvent(evt);
 
134
    }
 
135
 
 
136
    public int getPreferredWidth(Graphics g, Font defaultFont) {
 
137
        if (prefWidth < 0)
 
138
            prefWidth = CompletionUtilities.getPreferredWidth(simpleName + " (" + pkgName + ")", null, g, defaultFont); //NOI18N
 
139
        return prefWidth;
 
140
    }
 
141
 
 
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);
 
145
    }
 
146
 
 
147
    public CompletionTask createDocumentationTask() {
 
148
        if (delegate != null)
 
149
            return delegate.createDocumentationTask();
 
150
        return null;
 
151
    }
 
152
 
 
153
    public CompletionTask createToolTipTask() {
 
154
        if (delegate != null)
 
155
            return delegate.createToolTipTask();
 
156
        return null;
 
157
    }
 
158
 
 
159
    public int getSortPriority() {
 
160
        return 700;
 
161
    }
 
162
 
 
163
    public CharSequence getSortText() {
 
164
        return sortText;
 
165
    }
 
166
 
 
167
    public CharSequence getInsertPrefix() {
 
168
        return simpleName;
 
169
    }
 
170
    
 
171
    boolean isAnnonInner() {
 
172
        return simpleName.length() == 0 || Character.isDigit(simpleName.charAt(0));
 
173
    }
 
174
    
 
175
    private boolean isOfKind(Element e, EnumSet<ElementKind> kinds) {
 
176
        if (kinds.contains(e.getKind()))
 
177
            return true;
 
178
        for (Element ee : e.getEnclosedElements())
 
179
            if (isOfKind(ee, kinds))
 
180
                return true;
 
181
        return false;
 
182
    }
 
183
}