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.*;
45
import com.sun.source.util.SourcePositions;
46
import com.sun.source.util.TreePath;
47
import java.awt.Color;
49
import java.awt.Graphics;
50
import java.awt.event.KeyEvent;
51
import java.io.IOException;
53
import java.util.logging.Level;
54
import java.util.logging.Logger;
55
import javax.lang.model.element.*;
56
import javax.lang.model.type.*;
57
import javax.lang.model.util.ElementFilter;
58
import javax.lang.model.util.Elements;
59
import javax.swing.ImageIcon;
60
import javax.swing.SwingUtilities;
61
import javax.swing.text.BadLocationException;
62
import javax.swing.text.JTextComponent;
63
import javax.swing.text.Position;
64
import org.netbeans.api.editor.completion.Completion;
65
import org.netbeans.api.java.lexer.JavaTokenId;
66
import org.netbeans.api.java.source.*;
67
import org.netbeans.api.java.source.JavaSource.Phase;
68
import org.netbeans.api.lexer.TokenHierarchy;
69
import org.netbeans.api.lexer.TokenSequence;
70
import org.netbeans.editor.BaseDocument;
71
import org.netbeans.lib.editor.codetemplates.api.CodeTemplateManager;
72
import org.netbeans.modules.java.editor.codegen.GeneratorUtils;
73
import org.netbeans.spi.editor.completion.CompletionItem;
74
import org.netbeans.spi.editor.completion.CompletionTask;
75
import org.netbeans.spi.editor.completion.support.CompletionUtilities;
76
import org.openide.util.NbBundle;
77
import org.openide.xml.XMLUtil;
83
public abstract class JavaCompletionItem implements CompletionItem {
85
protected static int SMART_TYPE = 1000;
86
private static final String GENERATE_TEXT = NbBundle.getMessage(JavaCompletionItem.class, "generate_Lbl");
88
public static final JavaCompletionItem createKeywordItem(String kwd, String postfix, int substitutionOffset, boolean smartType) {
89
return new KeywordItem(kwd, 0, postfix, substitutionOffset, smartType);
92
public static final JavaCompletionItem createPackageItem(String pkgFQN, int substitutionOffset, boolean isDeprecated) {
93
return new PackageItem(pkgFQN, substitutionOffset, isDeprecated);
96
public static final JavaCompletionItem createTypeItem(TypeElement elem, DeclaredType type, int substitutionOffset, boolean displayPkgName, boolean isDeprecated, boolean insideNew, boolean smartType) {
97
switch (elem.getKind()) {
99
return new ClassItem(elem, type, 0, substitutionOffset, displayPkgName, isDeprecated, insideNew, smartType);
101
return new InterfaceItem(elem, type, 0, substitutionOffset, displayPkgName, isDeprecated, insideNew, smartType);
103
return new EnumItem(elem, type, 0, substitutionOffset, displayPkgName, isDeprecated, insideNew, smartType);
104
case ANNOTATION_TYPE:
105
return new AnnotationTypeItem(elem, type, 0, substitutionOffset, displayPkgName, isDeprecated, insideNew, smartType);
107
throw new IllegalArgumentException("kind=" + elem.getKind());
111
public static final JavaCompletionItem createArrayItem(ArrayType type, int substitutionOffset, Elements elements) {
113
TypeMirror tm = type;
114
while(tm.getKind() == TypeKind.ARRAY) {
115
tm = ((ArrayType)tm).getComponentType();
118
if (tm.getKind().isPrimitive())
119
return new KeywordItem(tm.toString(), dim, null, substitutionOffset, true);
120
if (tm.getKind() == TypeKind.DECLARED || tm.getKind() == TypeKind.ERROR) {
121
DeclaredType dt = (DeclaredType)tm;
122
TypeElement elem = (TypeElement)dt.asElement();
123
switch (elem.getKind()) {
125
return new ClassItem(elem, dt, dim, substitutionOffset, true, elements.isDeprecated(elem), false, true);
127
return new InterfaceItem(elem, dt, dim, substitutionOffset, true, elements.isDeprecated(elem), false, true);
129
return new EnumItem(elem, dt, dim, substitutionOffset, true, elements.isDeprecated(elem), false, true);
130
case ANNOTATION_TYPE:
131
return new AnnotationTypeItem(elem, dt, dim, substitutionOffset, true, elements.isDeprecated(elem), false, true);
134
throw new IllegalArgumentException("array element kind=" + tm.getKind());
137
public static final JavaCompletionItem createTypeParameterItem(TypeParameterElement elem, int substitutionOffset) {
138
return new TypeParameterItem(elem, substitutionOffset);
141
public static final JavaCompletionItem createVariableItem(VariableElement elem, TypeMirror type, int substitutionOffset, boolean isInherited, boolean isDeprecated, boolean smartType) {
142
switch (elem.getKind()) {
145
case EXCEPTION_PARAMETER:
146
return new VariableItem(type, elem.getSimpleName().toString(), substitutionOffset, smartType);
149
return new FieldItem(elem, type, substitutionOffset, isInherited, isDeprecated, smartType);
151
throw new IllegalArgumentException("kind=" + elem.getKind());
155
public static final JavaCompletionItem createVariableItem(String varName, int substitutionOffset, boolean smartType) {
156
return new VariableItem(null, varName, substitutionOffset, smartType);
159
public static final JavaCompletionItem createExecutableItem(ExecutableElement elem, ExecutableType type, int substitutionOffset, boolean isInherited, boolean isDeprecated, boolean inImport, boolean smartType) {
160
switch (elem.getKind()) {
162
return new MethodItem(elem, type, substitutionOffset, isInherited, isDeprecated, inImport, smartType);
164
return new ConstructorItem(elem, type, substitutionOffset, isDeprecated, smartType);
166
throw new IllegalArgumentException("kind=" + elem.getKind());
170
public static final JavaCompletionItem createOverrideMethodItem(ExecutableElement elem, ExecutableType type, int substitutionOffset, boolean implement) {
171
switch (elem.getKind()) {
173
return new OverrideMethodItem(elem, type, substitutionOffset, implement);
175
throw new IllegalArgumentException("kind=" + elem.getKind());
179
public static final JavaCompletionItem createGetterSetterMethodItem(VariableElement elem, TypeMirror type, int substitutionOffset, boolean setter) {
180
switch (elem.getKind()) {
183
return new GetterSetterMethodItem(elem, type, substitutionOffset, setter);
185
throw new IllegalArgumentException("kind=" + elem.getKind());
189
public static final JavaCompletionItem createDefaultConstructorItem(TypeElement elem, int substitutionOffset, boolean smartType) {
190
return new DefaultConstructorItem(elem, substitutionOffset, smartType);
193
public static final JavaCompletionItem createParametersItem(ExecutableElement elem, ExecutableType type, int substitutionOffset, boolean isDeprecated, int activeParamIndex) {
194
return new ParametersItem(elem, type, substitutionOffset, isDeprecated, activeParamIndex);
197
public static final JavaCompletionItem createAnnotationItem(TypeElement elem, DeclaredType type, int substitutionOffset, boolean isDeprecated) {
198
return new AnnotationItem(elem, type, substitutionOffset, isDeprecated, true);
201
public static final JavaCompletionItem createAttributeItem(ExecutableElement elem, ExecutableType type, int substitutionOffset, boolean isDeprecated) {
202
return new AttributeItem(elem, type, substitutionOffset, isDeprecated);
205
public static final JavaCompletionItem createStaticMemberItem(DeclaredType type, Element memberElem, TypeMirror memberType, int substitutionOffset, boolean isDeprecated) {
206
switch (memberElem.getKind()) {
210
return new StaticMemberItem(type, memberElem, memberType, substitutionOffset, isDeprecated);
212
throw new IllegalArgumentException("kind=" + memberElem.getKind());
216
public static final JavaCompletionItem createInitializeAllConstructorItem(Iterable<? extends VariableElement> fields, TypeElement parent, int substitutionOffset) {
217
return new InitializeAllConstructorItem(fields, parent, substitutionOffset);
220
public static final String COLOR_END = "</font>"; //NOI18N
221
public static final String STRIKE = "<s>"; //NOI18N
222
public static final String STRIKE_END = "</s>"; //NOI18N
223
public static final String BOLD = "<b>"; //NOI18N
224
public static final String BOLD_END = "</b>"; //NOI18N
226
protected int substitutionOffset;
228
protected JavaCompletionItem(int substitutionOffset) {
229
this.substitutionOffset = substitutionOffset;
232
public void defaultAction(JTextComponent component) {
233
if (component != null) {
234
Completion.get().hideDocumentation();
235
Completion.get().hideCompletion();
236
int caretOffset = component.getSelectionEnd();
237
substituteText(component, substitutionOffset, caretOffset - substitutionOffset, null);
241
public void processKeyEvent(KeyEvent evt) {
242
if (evt.getID() == KeyEvent.KEY_TYPED) {
243
switch (evt.getKeyChar()) {
248
Completion.get().hideDocumentation();
249
Completion.get().hideCompletion();
251
JTextComponent component = (JTextComponent)evt.getSource();
252
int caretOffset = component.getSelectionEnd();
253
substituteText(component, substitutionOffset, caretOffset - substitutionOffset, Character.toString(evt.getKeyChar()));
254
if (evt.getKeyChar() == '.')
255
Completion.get().showCompletion();
262
public boolean instantSubstitution(JTextComponent component) {
263
if (component != null) {
265
int caretOffset = component.getSelectionEnd();
266
if (caretOffset > substitutionOffset) {
267
String text = component.getDocument().getText(substitutionOffset, caretOffset - substitutionOffset);
268
if (!getInsertPrefix().toString().startsWith(text)) {
273
catch (BadLocationException ble) {}
275
defaultAction(component);
279
public CompletionTask createDocumentationTask() {
283
public CompletionTask createToolTipTask() {
287
public int getPreferredWidth(Graphics g, Font defaultFont) {
288
return CompletionUtilities.getPreferredWidth(getLeftHtmlText(), getRightHtmlText(), g, defaultFont);
291
public void render(Graphics g, Font defaultFont, Color defaultColor, Color backgroundColor, int width, int height, boolean selected) {
292
CompletionUtilities.renderHtml(getIcon(), getLeftHtmlText(), getRightHtmlText(), g, defaultFont, defaultColor, width, height, selected);
295
protected ImageIcon getIcon() {
299
protected String getLeftHtmlText() {
303
protected String getRightHtmlText() {
307
protected void substituteText(JTextComponent c, int offset, int len, String toAdd) {
308
BaseDocument doc = (BaseDocument)c.getDocument();
309
String text = getInsertPrefix().toString();
311
boolean completeMethod = text.length() == len && toAdd != null && "()".equals(toAdd.trim());
312
int semiPos = toAdd != null && toAdd.endsWith(";") ? findPositionForSemicolon(c) : -2; //NOI18N
314
toAdd = toAdd.length() > 1 ? toAdd.substring(0, toAdd.length() - 1) : null;
315
if (toAdd != null && !toAdd.equals("\n")) {//NOI18N
316
TokenSequence<JavaTokenId> sequence = SourceUtils.getJavaTokenSequence(TokenHierarchy.get(doc), offset + len);
317
if (sequence == null || !sequence.moveNext() && !sequence.movePrevious()) {
321
boolean added = false;
322
while(toAdd != null && toAdd.length() > 0) {
323
String tokenText = sequence.token().text().toString();
324
if (tokenText.startsWith(toAdd)) {
325
len = sequence.offset() - offset + toAdd.length();
328
} else if (toAdd.startsWith(tokenText)) {
330
len = sequence.offset() - offset;
331
text += toAdd.substring(0, tokenText.length());
332
toAdd = toAdd.substring(tokenText.length());
334
} else if (sequence.token().id() == JavaTokenId.WHITESPACE && sequence.token().text().toString().indexOf('\n') < 0) {//NOI18N
335
if (!sequence.moveNext()) {
349
String textToReplace = doc.getText(offset, len);
350
if (text.equals(textToReplace)) {
352
doc.insertString(semiPos, ";", null); //NOI18N
355
Position position = doc.createPosition(offset);
356
Position semiPosition = semiPos > -1 ? doc.createPosition(semiPos) : null;
357
doc.remove(offset, len);
358
doc.insertString(position.getOffset(), text, null);
359
if (semiPosition != null)
360
doc.insertString(semiPosition.getOffset(), ";", null);
361
else if (completeMethod)
362
c.setCaretPosition(c.getCaretPosition() - 1);
363
} catch (BadLocationException e) {
371
static class KeywordItem extends JavaCompletionItem {
373
private static final String JAVA_KEYWORD = "org/netbeans/modules/java/editor/resources/javakw_16.png"; //NOI18N
374
private static final String KEYWORD_COLOR = "<font color=#000099>"; //NOI18N
375
private static ImageIcon icon;
379
private String postfix;
380
private boolean smartType;
381
private String leftText;
383
private KeywordItem(String kwd, int dim, String postfix, int substitutionOffset, boolean smartType) {
384
super(substitutionOffset);
387
this.postfix = postfix;
388
this.smartType = smartType;
391
public int getSortPriority() {
392
return smartType ? 600 - SMART_TYPE : 600;
395
public CharSequence getSortText() {
399
public CharSequence getInsertPrefix() {
403
protected ImageIcon getIcon(){
404
if (icon == null) icon = new ImageIcon(org.openide.util.Utilities.loadImage(JAVA_KEYWORD));
408
protected String getLeftHtmlText() {
409
if (leftText == null) {
410
StringBuilder sb = new StringBuilder();
411
sb.append(KEYWORD_COLOR);
414
for(int i = 0; i < dim; i++)
415
sb.append("[]"); //NOI18N
417
sb.append(COLOR_END);
418
leftText = sb.toString();
423
protected void substituteText(JTextComponent c, int offset, int len, String toAdd) {
425
super.substituteText(c, offset, len, toAdd != null ? toAdd : postfix);
428
BaseDocument doc = (BaseDocument)c.getDocument();
429
final StringBuilder text = new StringBuilder();
430
int semiPos = toAdd != null && toAdd.endsWith(";") ? findPositionForSemicolon(c) : -2; //NOI18N
432
toAdd = toAdd.length() > 1 ? toAdd.substring(0, toAdd.length() - 1) : null;
433
if (toAdd != null && !toAdd.equals("\n")) {//NOI18N
434
TokenSequence<JavaTokenId> sequence = SourceUtils.getJavaTokenSequence(TokenHierarchy.get(doc), offset + len);
435
if (sequence == null || !sequence.moveNext() && !sequence.movePrevious()) {
439
boolean added = false;
440
while(toAdd != null && toAdd.length() > 0) {
441
String tokenText = sequence.token().text().toString();
442
if (tokenText.startsWith(toAdd)) {
443
len = sequence.offset() - offset + toAdd.length();
446
} else if (toAdd.startsWith(tokenText)) {
448
len = sequence.offset() - offset;
449
text.append(toAdd.substring(0, tokenText.length()));
450
toAdd = toAdd.substring(tokenText.length());
452
} else if (sequence.token().id() == JavaTokenId.WHITESPACE && sequence.token().text().toString().indexOf('\n') < 0) {//NOI18N
453
if (!sequence.moveNext()) {
464
StringBuilder sb = new StringBuilder();
467
for(int i = 0; i < dim; i++) {
468
sb.append("[${PAR"); //NOI18N
470
sb.append(" instanceof=\"int\" default=\"\"}]"); //NOI18N
474
Position semiPosition = semiPos > -1 ? doc.createPosition(semiPos) : null;
476
doc.remove(offset, len);
477
if (semiPosition != null)
478
doc.insertString(semiPosition.getOffset(), ";", null); //NOI18N
479
} catch (BadLocationException e) {
484
CodeTemplateManager ctm = CodeTemplateManager.get(doc);
486
ctm.createTemporary(sb.append(text).toString()).insert(c);
490
public String toString() {
495
static class PackageItem extends JavaCompletionItem {
497
private static final String PACKAGE = "org/netbeans/modules/java/editor/resources/package.gif"; // NOI18N
498
private static final String PACKAGE_COLOR = "<font color=#005600>"; //NOI18N
499
private static ImageIcon icon;
501
private boolean isDeprecated;
502
private String simpleName;
503
private String sortText;
504
private String leftText;
506
private PackageItem(String pkgFQN, int substitutionOffset, boolean isDeprecated) {
507
super(substitutionOffset);
508
this.isDeprecated = isDeprecated;
509
int idx = pkgFQN.lastIndexOf('.');
510
this.simpleName = idx < 0 ? pkgFQN : pkgFQN.substring(idx + 1);
511
this.sortText = this.simpleName + "#" + pkgFQN; //NOI18N
514
public int getSortPriority() {
518
public CharSequence getSortText() {
522
public CharSequence getInsertPrefix() {
526
protected ImageIcon getIcon(){
527
if (icon == null) icon = new ImageIcon(org.openide.util.Utilities.loadImage(PACKAGE));
531
protected String getLeftHtmlText() {
532
if (leftText == null) {
533
StringBuilder sb = new StringBuilder();
534
sb.append(PACKAGE_COLOR);
537
sb.append(simpleName);
539
sb.append(STRIKE_END);
540
sb.append(COLOR_END);
541
leftText = sb.toString();
546
public String toString() {
551
static class ClassItem extends JavaCompletionItem {
553
private static final String CLASS = "org/netbeans/modules/editor/resources/completion/class_16.png"; //NOI18N
554
private static final String CLASS_COLOR = "<font color=#560000>"; //NOI18N
555
private static final String PKG_COLOR = "<font color=#808080>"; //NOI18N
556
private static ImageIcon icon;
558
protected ElementHandle<TypeElement> elementHandle;
559
private TypeMirrorHandle<DeclaredType> typeHandle;
561
private boolean isDeprecated;
562
private boolean insideNew;
563
private boolean smartType;
564
private String simpleName;
565
private String typeName;
566
private String enclName;
567
private CharSequence sortText;
568
private String leftText;
570
private ClassItem(TypeElement elem, DeclaredType type, int dim, int substitutionOffset, boolean displayPkgName, boolean isDeprecated, boolean insideNew, boolean smartType) {
571
super(substitutionOffset);
572
TypeMirror elemType = elem.asType();
573
if (elemType.getKind() == TypeKind.ERROR) {
574
this.elementHandle = null;
575
this.typeHandle = TypeMirrorHandle.create((DeclaredType)elemType);
577
this.elementHandle = ElementHandle.create(elem);
578
this.typeHandle = TypeMirrorHandle.create(type);
581
this.isDeprecated = isDeprecated;
582
this.insideNew = insideNew;
583
this.smartType = smartType;
584
this.simpleName = elem.getSimpleName().toString();
585
this.typeName = Utilities.getTypeName(type, false).toString();
586
if (displayPkgName) {
587
this.enclName = Utilities.getElementName(elem.getEnclosingElement(), true).toString();
588
this.sortText = new ClassSortText(this.simpleName, this.enclName);
590
this.enclName = null;
591
this.sortText = this.simpleName;
595
public int getSortPriority() {
596
return smartType ? 800 - SMART_TYPE : 800;
599
public CharSequence getSortText() {
603
public CharSequence getInsertPrefix() {
607
public boolean instantSubstitution(JTextComponent component) {
611
public CompletionTask createDocumentationTask() {
612
return JavaCompletionProvider.createDocTask(elementHandle);
615
protected ImageIcon getIcon(){
616
if (icon == null) icon = new ImageIcon(org.openide.util.Utilities.loadImage(CLASS));
620
protected String getLeftHtmlText() {
621
if (leftText == null) {
622
StringBuilder sb = new StringBuilder();
623
sb.append(getColor());
626
sb.append(escape(typeName));
627
for(int i = 0; i < dim; i++)
628
sb.append("[]"); //NOI18N
630
sb.append(STRIKE_END);
631
if (enclName != null && enclName.length() > 0) {
632
sb.append(COLOR_END);
633
sb.append(PKG_COLOR);
634
sb.append(" ("); //NOI18N
636
sb.append(")"); //NOI18N
638
sb.append(COLOR_END);
639
leftText = sb.toString();
644
protected String getColor() {
648
protected void substituteText(final JTextComponent c, final int offset, int len, String toAdd) {
649
final BaseDocument doc = (BaseDocument)c.getDocument();
650
final StringBuilder text = new StringBuilder();
651
final int semiPos = toAdd != null && toAdd.endsWith(";") ? findPositionForSemicolon(c) : -2; //NOI18N
653
toAdd = toAdd.length() > 1 ? toAdd.substring(0, toAdd.length() - 1) : null;
654
if (toAdd != null && !toAdd.equals("\n")) {//NOI18N
655
TokenSequence<JavaTokenId> sequence = SourceUtils.getJavaTokenSequence(TokenHierarchy.get(doc), offset + len);
656
if (sequence == null || !sequence.moveNext() && !sequence.movePrevious()) {
660
boolean added = false;
661
while(toAdd != null && toAdd.length() > 0) {
662
String tokenText = sequence.token().text().toString();
663
if (tokenText.startsWith(toAdd)) {
664
len = sequence.offset() - offset + toAdd.length();
667
} else if (toAdd.startsWith(tokenText)) {
669
len = sequence.offset() - offset;
670
text.append(toAdd.substring(0, tokenText.length()));
671
toAdd = toAdd.substring(tokenText.length());
673
} else if (sequence.token().id() == JavaTokenId.WHITESPACE && sequence.token().text().toString().indexOf('\n') < 0) {//NOI18N
674
if (!sequence.moveNext()) {
685
final int finalLen = len;
686
JavaSource js = JavaSource.forDocument(doc);
688
js.runUserActionTask(new Task<CompilationController>() {
690
public void run(CompilationController controller) throws IOException {
691
controller.toPhase(Phase.RESOLVED);
692
TreePath path = controller.getTreeUtilities().pathFor(offset);
693
DeclaredType type = typeHandle.resolve(controller);
694
TypeElement elem = elementHandle != null ? elementHandle.resolve(controller) : (TypeElement)type.asElement();
695
boolean asTemplate = false;
696
StringBuilder sb = new StringBuilder();
698
sb.append("${PAR"); //NOI18N
700
if (type.getKind() != TypeKind.ERROR && EnumSet.range(ElementKind.PACKAGE, ElementKind.INTERFACE).contains(elem.getEnclosingElement().getKind())) {
701
sb.append(" type=\""); //NOI18N
702
sb.append(elem.getQualifiedName());
703
sb.append("\" default=\""); //NOI18N
704
sb.append(elem.getSimpleName());
706
sb.append(" default=\""); //NOI18N
707
sb.append(elem.getQualifiedName());
709
sb.append("\" editable=false}"); //NOI18N
710
Iterator<? extends TypeMirror> tas = type.getTypeArguments().iterator();
712
sb.append('<'); //NOI18N
713
while (tas.hasNext()) {
714
TypeMirror ta = tas.next();
715
sb.append("${PAR"); //NOI18N
717
if (ta.getKind() == TypeKind.TYPEVAR) {
718
TypeVariable tv = (TypeVariable)ta;
719
if (elem == tv.asElement().getEnclosingElement()) {
720
sb.append(" type=\""); //NOI18N
721
ta = tv.getUpperBound();
722
sb.append(Utilities.getTypeName(ta, true));
723
sb.append("\" default=\""); //NOI18N
724
sb.append(Utilities.getTypeName(ta, false));
726
sb.append(" editable=false default=\""); //NOI18N
727
sb.append(Utilities.getTypeName(ta, true));
730
sb.append("\"}"); //NOI18N
731
} else if (ta.getKind() == TypeKind.WILDCARD) {
732
sb.append(" type=\""); //NOI18N
733
TypeMirror bound = ((WildcardType)ta).getExtendsBound();
735
bound = ((WildcardType)ta).getSuperBound();
736
sb.append(bound != null ? Utilities.getTypeName(bound, true) : "Object"); //NOI18N
737
sb.append("\" default=\""); //NOI18N
738
sb.append(bound != null ? Utilities.getTypeName(bound, false) : "Object"); //NOI18N
739
sb.append("\"}"); //NOI18N
741
} else if (ta.getKind() == TypeKind.ERROR) {
742
sb.append(" default=\""); //NOI18N
743
sb.append(((ErrorType)ta).asElement().getSimpleName());
744
sb.append("\"}"); //NOI18N
747
sb.append(" type=\""); //NOI18N
748
sb.append(Utilities.getTypeName(ta, true));
749
sb.append("\" default=\""); //NOI18N
750
sb.append(Utilities.getTypeName(ta, false));
751
sb.append("\" editable=false}"); //NOI18N
755
sb.append(", "); //NOI18N
757
sb.append('>'); //NOI18N
759
for(int i = 0; i < dim; i++) {
760
sb.append("[${PAR"); //NOI18N
762
sb.append(" instanceof=\"int\" default=\"\"}]"); //NOI18N
767
sb.append("${cursor completionInvoke}"); //NOI18N
771
doc.remove(offset, finalLen);
772
} catch (BadLocationException e) {
778
CodeTemplateManager ctm = CodeTemplateManager.get(doc);
780
ctm.createTemporary(sb.append(text).toString()).insert(c);
785
Position semiPosition = semiPos > -1 && !insideNew ? doc.createPosition(semiPos) : null;
786
TreePath tp = controller.getTreeUtilities().pathFor(offset);
787
CharSequence cs = enclName == null ? elem.getSimpleName() : AutoImport.resolveImport(controller, tp, controller.getTypes().getDeclaredType(elem));
789
cs = text.insert(0, cs);
790
String textToReplace = doc.getText(offset, finalLen);
791
if (textToReplace.contentEquals(cs)) return;
792
doc.remove(offset, finalLen);
793
doc.insertString(offset, cs.toString(), null);
794
if (semiPosition != null)
795
doc.insertString(semiPosition.getOffset(), ";", null); //NOI18N
796
} catch (BadLocationException e) {
802
List<ExecutableElement> ctors = type.getKind() == TypeKind.DECLARED ? ElementFilter.constructorsIn(elem.getEnclosedElements()) : null;
804
final JavaCompletionItem item = ctors.size() > 1 || Utilities.hasAccessibleInnerClassConstructor(elem, controller.getTreeUtilities().scopeFor(offset), controller.getTrees())
805
? null : ctors.size() == 1
806
? createExecutableItem(ctors.get(0), (ExecutableType)controller.getTypes().asMemberOf(type, ctors.get(0)), offset, false, false, false, true)
807
: createDefaultConstructorItem(elem, offset, true);
809
final Position offPosition = doc.createPosition(offset);
810
SwingUtilities.invokeLater(new Runnable() {
813
item.substituteText(c, offPosition.getOffset(), c.getSelectionEnd() - offPosition.getOffset(), text.toString());
815
//Temporary ugly solution
816
SwingUtilities.invokeLater(new Runnable() {
818
Completion.get().showCompletion();
825
catch (BadLocationException e) {}
831
} catch (IOException ioe) {
835
public String toString() {
840
static class InterfaceItem extends ClassItem {
842
private static final String INTERFACE = "org/netbeans/modules/editor/resources/completion/interface.png"; // NOI18N
843
private static final String INTERFACE_COLOR = "<font color=#404040>"; //NOI18N
844
private static ImageIcon icon;
846
private InterfaceItem(TypeElement elem, DeclaredType type, int dim, int substitutionOffset, boolean displayPkgName, boolean isDeprecated, boolean insideNew, boolean smartType) {
847
super(elem, type, dim, substitutionOffset, displayPkgName, isDeprecated, insideNew, smartType);
850
protected ImageIcon getIcon(){
851
if (icon == null) icon = new ImageIcon(org.openide.util.Utilities.loadImage(INTERFACE));
855
protected String getColor() {
856
return INTERFACE_COLOR;
860
static class EnumItem extends ClassItem {
862
private static final String ENUM = "org/netbeans/modules/editor/resources/completion/enum.png"; // NOI18N
863
private static ImageIcon icon;
865
private EnumItem(TypeElement elem, DeclaredType type, int dim, int substitutionOffset, boolean displayPkgName, boolean isDeprecated, boolean insideNew, boolean smartType) {
866
super(elem, type, dim, substitutionOffset, displayPkgName, isDeprecated, insideNew, smartType);
869
protected ImageIcon getIcon(){
870
if (icon == null) icon = new ImageIcon(org.openide.util.Utilities.loadImage(ENUM));
875
static class AnnotationTypeItem extends ClassItem {
877
private static final String ANNOTATION = "org/netbeans/modules/editor/resources/completion/annotation_type.png"; // NOI18N
878
private static ImageIcon icon;
880
private AnnotationTypeItem(TypeElement elem, DeclaredType type, int dim, int substitutionOffset, boolean displayPkgName, boolean isDeprecated, boolean insideNew, boolean smartType) {
881
super(elem, type, dim, substitutionOffset, displayPkgName, isDeprecated, insideNew, smartType);
884
protected ImageIcon getIcon(){
885
if (icon == null) icon = new ImageIcon(org.openide.util.Utilities.loadImage(ANNOTATION));
890
static class TypeParameterItem extends JavaCompletionItem {
892
private static final String TYPE_PARAMETER_COLOR = "<font color=#000000>"; //NOI18N
894
private String simpleName;
895
private String leftText;
897
private TypeParameterItem(TypeParameterElement elem, int substitutionOffset) {
898
super(substitutionOffset);
899
this.simpleName = elem.getSimpleName().toString();
902
public int getSortPriority() {
906
public CharSequence getSortText() {
910
public CharSequence getInsertPrefix() {
914
protected String getLeftHtmlText() {
915
if (leftText == null)
916
leftText = TYPE_PARAMETER_COLOR + simpleName + COLOR_END;
920
public String toString() {
925
static class VariableItem extends JavaCompletionItem {
927
private static final String LOCAL_VARIABLE = "org/netbeans/modules/editor/resources/completion/localVariable.gif"; //NOI18N
928
private static final String PARAMETER_COLOR = "<font color=#00007c>"; //NOI18N
929
private static ImageIcon icon;
931
private String varName;
932
private boolean smartType;
933
private String typeName;
934
private String leftText;
935
private String rightText;
937
private VariableItem(TypeMirror type, String varName, int substitutionOffset, boolean smartType) {
938
super(substitutionOffset);
939
this.varName = varName;
940
this.smartType = smartType;
941
this.typeName = type != null ? Utilities.getTypeName(type, false).toString() : null;
944
public int getSortPriority() {
945
return smartType ? 200 - SMART_TYPE : 200;
948
public CharSequence getSortText() {
952
public CharSequence getInsertPrefix() {
956
protected String getLeftHtmlText() {
957
if (leftText == null)
958
leftText = PARAMETER_COLOR + BOLD + varName + BOLD_END + COLOR_END;
962
protected String getRightHtmlText() {
963
if (rightText == null)
964
rightText = escape(typeName);
968
protected ImageIcon getIcon(){
969
if (icon == null) icon = new ImageIcon(org.openide.util.Utilities.loadImage(LOCAL_VARIABLE));
973
public String toString() {
974
return (typeName != null ? typeName + " " : "") + varName; //NOI18N
978
static class FieldItem extends JavaCompletionItem {
980
private static final String FIELD_PUBLIC = "org/netbeans/modules/editor/resources/completion/field_16.png"; //NOI18N
981
private static final String FIELD_PROTECTED = "org/netbeans/modules/editor/resources/completion/field_protected_16.png"; //NOI18N
982
private static final String FIELD_PACKAGE = "org/netbeans/modules/editor/resources/completion/field_package_private_16.png"; //NOI18N
983
private static final String FIELD_PRIVATE = "org/netbeans/modules/editor/resources/completion/field_private_16.png"; //NOI18N
984
private static final String FIELD_ST_PUBLIC = "org/netbeans/modules/editor/resources/completion/field_static_16.png"; //NOI18N
985
private static final String FIELD_ST_PROTECTED = "org/netbeans/modules/editor/resources/completion/field_static_protected_16.png"; //NOI18N
986
private static final String FIELD_ST_PACKAGE = "org/netbeans/modules/editor/resources/completion/field_static_package_private_16.png"; //NOI18N
987
private static final String FIELD_ST_PRIVATE = "org/netbeans/modules/editor/resources/completion/field_static_private_16.png"; //NOI18N
988
private static final String FIELD_COLOR = "<font color=#008618>"; //NOI18N
989
private static ImageIcon icon[][] = new ImageIcon[2][4];
991
private ElementHandle<VariableElement> elementHandle;
992
private boolean isInherited;
993
private boolean isDeprecated;
994
private boolean smartType;
995
private String simpleName;
996
private Set<Modifier> modifiers;
997
private String typeName;
998
private String leftText;
999
private String rightText;
1001
private FieldItem(VariableElement elem, TypeMirror type, int substitutionOffset, boolean isInherited, boolean isDeprecated, boolean smartType) {
1002
super(substitutionOffset);
1003
this.elementHandle = ElementHandle.create(elem);
1004
this.isInherited = isInherited;
1005
this.isDeprecated = isDeprecated;
1006
this.smartType = smartType;
1007
this.simpleName = elem.getSimpleName().toString();
1008
this.modifiers = elem.getModifiers();
1009
this.typeName = Utilities.getTypeName(type, false).toString();
1012
public int getSortPriority() {
1013
return smartType ? 300 - SMART_TYPE : 300;
1016
public CharSequence getSortText() {
1020
public CharSequence getInsertPrefix() {
1024
public CompletionTask createDocumentationTask() {
1025
return JavaCompletionProvider.createDocTask(elementHandle);
1028
protected String getLeftHtmlText() {
1029
if (leftText == null) {
1030
StringBuilder sb = new StringBuilder();
1031
sb.append(FIELD_COLOR);
1036
sb.append(simpleName);
1038
sb.append(STRIKE_END);
1040
sb.append(BOLD_END);
1041
sb.append(COLOR_END);
1042
leftText = sb.toString();
1047
protected String getRightHtmlText() {
1048
if (rightText == null)
1049
rightText = escape(typeName);
1053
protected ImageIcon getIcon(){
1054
int level = getProtectionLevel(modifiers);
1055
boolean isStatic = modifiers.contains(Modifier.STATIC);
1056
ImageIcon cachedIcon = icon[isStatic?1:0][level];
1057
if (cachedIcon != null)
1060
String iconPath = FIELD_PUBLIC;
1064
iconPath = FIELD_ST_PRIVATE;
1068
iconPath = FIELD_ST_PACKAGE;
1071
case PROTECTED_LEVEL:
1072
iconPath = FIELD_ST_PROTECTED;
1076
iconPath = FIELD_ST_PUBLIC;
1082
iconPath = FIELD_PRIVATE;
1086
iconPath = FIELD_PACKAGE;
1089
case PROTECTED_LEVEL:
1090
iconPath = FIELD_PROTECTED;
1094
iconPath = FIELD_PUBLIC;
1098
ImageIcon newIcon = new ImageIcon(org.openide.util.Utilities.loadImage(iconPath));
1099
icon[isStatic?1:0][level] = newIcon;
1103
public String toString() {
1104
StringBuilder sb = new StringBuilder();
1105
for(Modifier mod : modifiers) {
1106
sb.append(mod.toString());
1109
sb.append(typeName);
1111
sb.append(simpleName);
1112
return sb.toString();
1116
static class MethodItem extends JavaCompletionItem {
1118
private static final String METHOD_PUBLIC = "org/netbeans/modules/editor/resources/completion/method_16.png"; //NOI18N
1119
private static final String METHOD_PROTECTED = "org/netbeans/modules/editor/resources/completion/method_protected_16.png"; //NOI18N
1120
private static final String METHOD_PACKAGE = "org/netbeans/modules/editor/resources/completion/method_package_private_16.png"; //NOI18N
1121
private static final String METHOD_PRIVATE = "org/netbeans/modules/editor/resources/completion/method_private_16.png"; //NOI18N
1122
private static final String METHOD_ST_PUBLIC = "org/netbeans/modules/editor/resources/completion/method_static_16.png"; //NOI18N
1123
private static final String METHOD_ST_PROTECTED = "org/netbeans/modules/editor/resources/completion/method_static_protected_16.png"; //NOI18N
1124
private static final String METHOD_ST_PRIVATE = "org/netbeans/modules/editor/resources/completion/method_static_private_16.png"; //NOI18N
1125
private static final String METHOD_ST_PACKAGE = "org/netbeans/modules/editor/resources/completion/method_static_package_private_16.png"; //NOI18N
1126
private static final String METHOD_COLOR = "<font color=#000000>"; //NOI18N
1127
private static final String PARAMETER_NAME_COLOR = "<font color=#a06001>"; //NOI18N
1128
private static ImageIcon icon[][] = new ImageIcon[2][4];
1130
protected ElementHandle<ExecutableElement> elementHandle;
1131
private boolean isInherited;
1132
private boolean isDeprecated;
1133
private boolean inImport;
1134
private boolean smartType;
1135
private String simpleName;
1136
protected Set<Modifier> modifiers;
1137
private List<ParamDesc> params;
1138
private String typeName;
1139
private boolean isPrimitive;
1140
private String sortText;
1141
private String leftText;
1142
private String rightText;
1144
private MethodItem(ExecutableElement elem, ExecutableType type, int substitutionOffset, boolean isInherited, boolean isDeprecated, boolean inImport, boolean smartType) {
1145
super(substitutionOffset);
1146
this.elementHandle = ElementHandle.create(elem);
1147
this.isInherited = isInherited;
1148
this.isDeprecated = isDeprecated;
1149
this.inImport = inImport;
1150
this.smartType = smartType;
1151
this.simpleName = elem.getSimpleName().toString();
1152
this.modifiers = elem.getModifiers();
1153
this.params = new ArrayList<ParamDesc>();
1154
Iterator<? extends VariableElement> it = elem.getParameters().iterator();
1155
Iterator<? extends TypeMirror> tIt = type.getParameterTypes().iterator();
1156
while(it.hasNext() && tIt.hasNext()) {
1157
TypeMirror tm = tIt.next();
1158
this.params.add(new ParamDesc(tm.toString(), Utilities.getTypeName(tm, false, elem.isVarArgs() && !tIt.hasNext()).toString(), it.next().getSimpleName().toString()));
1160
TypeMirror retType = type.getReturnType();
1161
this.typeName = Utilities.getTypeName(retType, false).toString();
1162
this.isPrimitive = retType.getKind().isPrimitive() || retType.getKind() == TypeKind.VOID;
1165
public int getSortPriority() {
1166
return smartType ? 500 - SMART_TYPE : 500;
1169
public CharSequence getSortText() {
1170
if (sortText == null) {
1171
StringBuilder sortParams = new StringBuilder();
1172
sortParams.append('(');
1174
for(Iterator<ParamDesc> it = params.iterator(); it.hasNext();) {
1175
ParamDesc param = it.next();
1176
sortParams.append(param.typeName);
1178
sortParams.append(',');
1182
sortParams.append(')');
1183
sortText = simpleName + "#" + ((cnt < 10 ? "0" : "") + cnt) + "#" + sortParams.toString(); //NOI18N
1188
public CharSequence getInsertPrefix() {
1192
protected String getLeftHtmlText() {
1193
if (leftText == null) {
1194
StringBuilder lText = new StringBuilder();
1195
lText.append(METHOD_COLOR);
1199
lText.append(STRIKE);
1200
lText.append(simpleName);
1202
lText.append(STRIKE_END);
1204
lText.append(BOLD_END);
1205
lText.append(COLOR_END);
1207
for (Iterator<ParamDesc> it = params.iterator(); it.hasNext();) {
1208
ParamDesc paramDesc = it.next();
1209
lText.append(escape(paramDesc.typeName));
1211
lText.append(PARAMETER_NAME_COLOR);
1212
lText.append(paramDesc.name);
1213
lText.append(COLOR_END);
1215
lText.append(", "); //NOI18N
1219
return lText.toString();
1224
protected String getRightHtmlText() {
1225
if (rightText == null)
1226
rightText = escape(typeName);
1230
public CompletionTask createDocumentationTask() {
1231
return JavaCompletionProvider.createDocTask(elementHandle);
1234
protected ImageIcon getIcon() {
1235
int level = getProtectionLevel(modifiers);
1236
boolean isStatic = modifiers.contains(Modifier.STATIC);
1237
ImageIcon cachedIcon = icon[isStatic?1:0][level];
1238
if (cachedIcon != null)
1241
String iconPath = METHOD_PUBLIC;
1245
iconPath = METHOD_ST_PRIVATE;
1249
iconPath = METHOD_ST_PACKAGE;
1252
case PROTECTED_LEVEL:
1253
iconPath = METHOD_ST_PROTECTED;
1257
iconPath = METHOD_ST_PUBLIC;
1263
iconPath = METHOD_PRIVATE;
1267
iconPath = METHOD_PACKAGE;
1270
case PROTECTED_LEVEL:
1271
iconPath = METHOD_PROTECTED;
1275
iconPath = METHOD_PUBLIC;
1279
ImageIcon newIcon = new ImageIcon(org.openide.util.Utilities.loadImage(iconPath));
1280
icon[isStatic?1:0][level] = newIcon;
1284
protected void substituteText(final JTextComponent c, int offset, int len, String toAdd) {
1285
if (toAdd == null) {
1288
final String[] ret = new String[1];
1289
JavaSource js = JavaSource.forDocument(c.getDocument());
1290
js.runUserActionTask(new Task<CompilationController>() {
1292
public void run(CompilationController controller) throws Exception {
1293
controller.toPhase(JavaSource.Phase.PARSED);
1294
TreePath tp = controller.getTreeUtilities().pathFor(c.getSelectionEnd());
1295
Tree tree = tp.getLeaf();
1296
if (tree.getKind() == Tree.Kind.IDENTIFIER || tree.getKind() == Tree.Kind.PRIMITIVE_TYPE)
1297
tp = tp.getParentPath();
1298
if (tp.getLeaf().getKind() == Tree.Kind.MEMBER_SELECT ||
1299
(tp.getLeaf().getKind() == Tree.Kind.METHOD_INVOCATION && ((MethodInvocationTree)tp.getLeaf()).getMethodSelect() == tree))
1300
tp = tp.getParentPath();
1301
if (tp.getLeaf().getKind() == Tree.Kind.EXPRESSION_STATEMENT || tp.getLeaf().getKind() == Tree.Kind.BLOCK)
1302
ret[0] = ";"; //NOI18N
1306
} catch (IOException ex) {
1310
String add = inImport ? ";" : CodeStyle.getDefault(null).spaceBeforeMethodCallParen() ? " ()" : "()"; //NOI18N
1311
if (toAdd != null && !add.startsWith(toAdd))
1313
if (inImport || params.isEmpty()) {
1314
super.substituteText(c, offset, len, add);
1316
BaseDocument doc = (BaseDocument)c.getDocument();
1317
String text = ""; //NOI18N
1318
int semiPos = add.endsWith(";") ? findPositionForSemicolon(c) : -2; //NOI18N
1320
add = add.length() > 1 ? add.substring(0, add.length() - 1) : null;
1321
TokenSequence<JavaTokenId> sequence = SourceUtils.getJavaTokenSequence(TokenHierarchy.get(doc), offset + len);
1322
if (sequence == null || !sequence.moveNext() && !sequence.movePrevious()) {
1326
boolean added = false;
1327
while(add != null && add.length() > 0) {
1328
String tokenText = sequence.token().text().toString();
1329
if (tokenText.startsWith(add)) {
1330
len = sequence.offset() - offset + add.length();
1333
} else if (add.startsWith(tokenText)) {
1334
sequence.moveNext();
1335
len = sequence.offset() - offset;
1336
text += add.substring(0, tokenText.length());
1337
add = add.substring(tokenText.length());
1339
} else if (sequence.token().id() == JavaTokenId.WHITESPACE && sequence.token().text().toString().indexOf('\n') < 0) {//NOI18N
1340
if (!sequence.moveNext()) {
1352
Position semiPosition = semiPos > -1 ? doc.createPosition(semiPos) : null;
1354
doc.remove(offset, len);
1355
if (semiPosition != null)
1356
doc.insertString(semiPosition.getOffset(), ";", null); //NOI18N
1357
} catch (BadLocationException e) {
1362
CodeTemplateManager ctm = CodeTemplateManager.get(doc);
1364
StringBuilder sb = new StringBuilder();
1365
sb.append(getInsertPrefix());
1366
sb.append("("); //NOI18N
1367
if (text.length() > 1) {
1368
for (Iterator<ParamDesc> it = params.iterator(); it.hasNext();) {
1369
ParamDesc paramDesc = it.next();
1370
sb.append("${"); //NOI18N
1371
sb.append(paramDesc.name);
1372
sb.append(" named instanceof="); //NOI18N
1373
sb.append(paramDesc.fullTypeName);
1374
sb.append("}"); //NOI18N
1376
sb.append(", "); //NOI18N
1378
sb.append(")");//NOI18N
1379
if (text.length() > 2)
1380
sb.append(text.substring(2));
1382
ctm.createTemporary(sb.toString()).insert(c);
1383
Completion.get().showToolTip();
1388
public String toString() {
1389
StringBuilder sb = new StringBuilder();
1390
for (Modifier mod : modifiers) {
1391
sb.append(mod.toString());
1394
sb.append(typeName);
1396
sb.append(simpleName);
1398
for (Iterator<ParamDesc> it = params.iterator(); it.hasNext();) {
1399
ParamDesc paramDesc = it.next();
1400
sb.append(paramDesc.typeName);
1402
sb.append(paramDesc.name);
1404
sb.append(", "); //NOI18N
1408
return sb.toString();
1412
static class OverrideMethodItem extends MethodItem {
1414
private static final String IMPL_BADGE_PATH = "org/netbeans/modules/java/editor/resources/implement_badge.png";
1415
private static final String OVRD_BADGE_PATH = "org/netbeans/modules/java/editor/resources/override_badge.png";
1417
private static final String OVERRIDE_TEXT = NbBundle.getMessage(JavaCompletionItem.class, "override_Lbl");
1418
private static final String IMPLEMENT_TEXT = NbBundle.getMessage(JavaCompletionItem.class, "implement_Lbl");
1420
private static ImageIcon implementBadge = new ImageIcon(org.openide.util.Utilities.loadImage(IMPL_BADGE_PATH));
1421
private static ImageIcon overrideBadge = new ImageIcon(org.openide.util.Utilities.loadImage(OVRD_BADGE_PATH));
1422
private static ImageIcon merged_icon[][] = new ImageIcon[2][4];
1425
private boolean implement;
1426
private String leftText;
1428
private OverrideMethodItem(ExecutableElement elem, ExecutableType type, int substitutionOffset, boolean implement) {
1429
super(elem, type, substitutionOffset, false, false, false, false);
1430
this.implement = implement;
1433
protected String getLeftHtmlText() {
1434
if (leftText == null) {
1435
leftText = super.getLeftHtmlText() + " - ";
1436
leftText += (implement ? IMPLEMENT_TEXT : OVERRIDE_TEXT);
1442
protected ImageIcon getIcon() {
1443
int level = getProtectionLevel(modifiers);
1444
ImageIcon merged = merged_icon[implement? 0 : 1][level];
1445
if ( merged != null ) {
1448
ImageIcon superIcon = super.getIcon();
1449
merged = new ImageIcon( org.openide.util.Utilities.mergeImages(
1450
superIcon.getImage(),
1451
implement ? implementBadge.getImage() : overrideBadge.getImage(),
1455
merged_icon[implement? 0 : 1][level] = merged;
1460
protected void substituteText(final JTextComponent c, final int offset, final int len, String toAdd) {
1461
BaseDocument doc = (BaseDocument)c.getDocument();
1465
doc.remove(offset, len);
1466
} catch (BadLocationException e) {
1473
JavaSource js = JavaSource.forDocument(doc);
1474
js.runModificationTask(new Task<WorkingCopy>() {
1476
public void run(WorkingCopy copy) throws IOException {
1477
copy.toPhase(Phase.ELEMENTS_RESOLVED);
1478
ExecutableElement ee = elementHandle.resolve(copy);
1481
TreePath tp = copy.getTreeUtilities().pathFor(offset);
1482
if (tp.getLeaf().getKind() == Tree.Kind.CLASS) {
1483
if (Utilities.isInMethod(tp))
1484
copy.toPhase(Phase.RESOLVED);
1486
for (Tree tree : ((ClassTree)tp.getLeaf()).getMembers()) {
1487
if (copy.getTrees().getSourcePositions().getStartPosition(tp.getCompilationUnit(), tree) < offset)
1493
GeneratorUtils.generateAbstractMethodImplementation(copy, tp, ee, idx);
1495
GeneratorUtils.generateMethodOverride(copy, tp, ee, idx);
1499
} catch (IOException ex) {
1500
Logger.getLogger("global").log(Level.WARNING, null, ex);
1504
public String toString() {
1505
StringBuilder sb = new StringBuilder();
1506
sb.append(super.toString());
1508
sb.append(implement ? IMPLEMENT_TEXT : OVERRIDE_TEXT);
1509
return sb.toString();
1512
public boolean instantSubstitution(JTextComponent component) {
1513
return false;//no instant substitution for override method item.
1517
static class GetterSetterMethodItem extends JavaCompletionItem {
1519
private static final String METHOD_PUBLIC = "org/netbeans/modules/editor/resources/completion/method_16.png"; //NOI18N
1520
private static final String GETTER_BADGE_PATH = "org/netbeans/modules/java/editor/resources/getter_badge.png"; //NOI18N
1521
private static final String SETTER_BADGE_PATH = "org/netbeans/modules/java/editor/resources/setter_badge.png"; //NOI18N
1522
private static final String METHOD_COLOR = "<font color=#000000>"; //NOI18N
1523
private static final String PARAMETER_NAME_COLOR = "<font color=#a06001>"; //NOI18N
1525
private static ImageIcon superIcon;
1526
private static ImageIcon[] merged_icons = new ImageIcon[2];
1528
protected ElementHandle<VariableElement> elementHandle;
1529
private boolean setter;
1530
private String simpleName;
1531
private String name;
1532
private String typeName;
1533
private String sortText;
1534
private String leftText;
1535
private String rightText;
1537
private GetterSetterMethodItem(VariableElement elem, TypeMirror type, int substitutionOffset, boolean setter) {
1538
super(substitutionOffset);
1539
this.elementHandle = ElementHandle.create(elem);
1540
this.setter = setter;
1541
this.simpleName = elem.getSimpleName().toString();
1543
this.name = "set" + Character.toUpperCase(simpleName.charAt(0)) + simpleName.substring(1, simpleName.length()); //NOI18N
1545
this.name = (elem.asType().getKind() == TypeKind.BOOLEAN ? "is" : "get") + Character.toUpperCase(simpleName.charAt(0)) + simpleName.substring(1, simpleName.length()); //NOI18N
1546
this.typeName = Utilities.getTypeName(type, false).toString();
1549
public int getSortPriority() {
1553
public CharSequence getSortText() {
1554
if (sortText == null) {
1555
StringBuilder sortParams = new StringBuilder();
1556
sortParams.append('('); //NOI18N
1558
sortParams.append(typeName);
1559
sortParams.append(')'); //NOI18N
1560
sortText = name + "#" + (setter ? "01" : "00") + "#" + sortParams.toString(); //NOI18N
1565
public CharSequence getInsertPrefix() {
1570
protected String getLeftHtmlText() {
1571
if (leftText == null) {
1572
StringBuilder lText = new StringBuilder();
1573
lText.append(METHOD_COLOR);
1576
lText.append(BOLD_END);
1577
lText.append(COLOR_END);
1580
lText.append(escape(typeName));
1582
lText.append(PARAMETER_NAME_COLOR);
1583
lText.append(simpleName);
1584
lText.append(COLOR_END);
1586
lText.append(") - "); //NOI18N
1587
lText.append(GENERATE_TEXT);
1588
leftText = lText.toString();
1594
protected String getRightHtmlText() {
1595
if (rightText == null)
1596
rightText = setter ? "void" : escape(typeName);
1601
protected ImageIcon getIcon() {
1602
if (merged_icons[setter ? 1 : 0] == null) {
1603
if (superIcon == null)
1604
superIcon = new ImageIcon(org.openide.util.Utilities.loadImage(METHOD_PUBLIC));
1606
ImageIcon setterBadge = new ImageIcon(org.openide.util.Utilities.loadImage(SETTER_BADGE_PATH));
1607
merged_icons[1] = new ImageIcon(org.openide.util.Utilities.mergeImages(superIcon.getImage(),
1608
setterBadge.getImage(), 16 - 8, 16 - 8));
1610
ImageIcon getterBadge = new ImageIcon(org.openide.util.Utilities.loadImage(GETTER_BADGE_PATH));
1611
merged_icons[0] = new ImageIcon(org.openide.util.Utilities.mergeImages(superIcon.getImage(),
1612
getterBadge.getImage(), 16 - 8, 16 - 8));
1615
return merged_icons[setter ? 1 : 0];
1619
protected void substituteText(final JTextComponent c, final int offset, final int len, String toAdd) {
1620
BaseDocument doc = (BaseDocument)c.getDocument();
1624
doc.remove(offset, len);
1625
} catch (BadLocationException e) {
1632
JavaSource js = JavaSource.forDocument(doc);
1633
js.runModificationTask(new Task<WorkingCopy>() {
1635
public void run(WorkingCopy copy) throws IOException {
1636
copy.toPhase(Phase.ELEMENTS_RESOLVED);
1637
VariableElement ve = elementHandle.resolve(copy);
1640
TreePath tp = copy.getTreeUtilities().pathFor(offset);
1641
if (tp.getLeaf().getKind() == Tree.Kind.CLASS) {
1642
if (Utilities.isInMethod(tp))
1643
copy.toPhase(Phase.RESOLVED);
1645
for (Tree tree : ((ClassTree)tp.getLeaf()).getMembers()) {
1646
if (copy.getTrees().getSourcePositions().getStartPosition(tp.getCompilationUnit(), tree) < offset)
1651
TypeElement te = (TypeElement)copy.getTrees().getElement(tp);
1653
GeneratorUtilities gu = GeneratorUtilities.get(copy);
1654
MethodTree method = setter ? gu.createSetter(te, ve) : gu.createGetter(te, ve);
1655
ClassTree decl = copy.getTreeMaker().insertClassMember((ClassTree)tp.getLeaf(), idx, method);
1656
copy.rewrite(tp.getLeaf(), decl);
1661
} catch (IOException ex) {
1662
Logger.getLogger("global").log(Level.WARNING, null, ex);
1667
public String toString() {
1668
StringBuilder sb = new StringBuilder();
1669
sb.append("public "); //NOI18N
1670
sb.append(setter ? "void" : typeName); //NOI18N
1675
sb.append(typeName);
1677
sb.append(simpleName);
1679
sb.append(") - "); //NOI18N
1680
sb.append(GENERATE_TEXT);
1681
return sb.toString();
1685
public boolean instantSubstitution(JTextComponent component) {
1686
return false;//no instant substitution for override method item.
1690
static class ConstructorItem extends JavaCompletionItem {
1692
private static final String CONSTRUCTOR_PUBLIC = "org/netbeans/modules/editor/resources/completion/constructor_16.png"; //NOI18N
1693
private static final String CONSTRUCTOR_PROTECTED = "org/netbeans/modules/editor/resources/completion/constructor_protected_16.png"; //NOI18N
1694
private static final String CONSTRUCTOR_PACKAGE = "org/netbeans/modules/editor/resources/completion/constructor_package_private_16.png"; //NOI18N
1695
private static final String CONSTRUCTOR_PRIVATE = "org/netbeans/modules/editor/resources/completion/constructor_private_16.png"; //NOI18N
1696
private static final String CONSTRUCTOR_COLOR = "<font color=#b28b00>"; //NOI18N
1697
private static final String PARAMETER_NAME_COLOR = "<font color=#a06001>"; //NOI18N
1698
private static ImageIcon icon[] = new ImageIcon[4];
1700
private ElementHandle<ExecutableElement> elementHandle;
1701
private boolean isDeprecated;
1702
private boolean smartType;
1703
private String simpleName;
1704
protected Set<Modifier> modifiers;
1705
private List<ParamDesc> params;
1706
private boolean isAbstract;
1707
private String sortText;
1708
private String leftText;
1710
private ConstructorItem(ExecutableElement elem, ExecutableType type, int substitutionOffset, boolean isDeprecated, boolean smartType) {
1711
super(substitutionOffset);
1712
this.elementHandle = ElementHandle.create(elem);
1713
this.isDeprecated = isDeprecated;
1714
this.smartType = smartType;
1715
this.simpleName = elem.getEnclosingElement().getSimpleName().toString();
1716
this.modifiers = elem.getModifiers();
1717
this.params = new ArrayList<ParamDesc>();
1718
Iterator<? extends VariableElement> it = elem.getParameters().iterator();
1719
Iterator<? extends TypeMirror> tIt = type.getParameterTypes().iterator();
1720
while(it.hasNext() && tIt.hasNext()) {
1721
TypeMirror tm = tIt.next();
1722
this.params.add(new ParamDesc(tm.toString(), Utilities.getTypeName(tm, false, elem.isVarArgs() && !tIt.hasNext()).toString(), it.next().getSimpleName().toString()));
1724
this.isAbstract = elem.getEnclosingElement().getModifiers().contains(Modifier.ABSTRACT);
1727
public int getSortPriority() {
1728
return smartType ? 650 - SMART_TYPE : 650;
1731
public CharSequence getSortText() {
1732
if (sortText == null) {
1733
StringBuilder sortParams = new StringBuilder();
1734
sortParams.append('(');
1736
for (Iterator<ParamDesc> it = params.iterator(); it.hasNext();) {
1737
ParamDesc paramDesc = it.next();
1738
sortParams.append(paramDesc.typeName);
1740
sortParams.append(',');
1744
sortParams.append(')');
1745
sortText = simpleName + "#" + ((cnt < 10 ? "0" : "") + cnt) + "#" + sortParams.toString(); //NOI18N
1750
public CharSequence getInsertPrefix() {
1754
protected String getLeftHtmlText() {
1755
if (leftText == null) {
1756
StringBuilder lText = new StringBuilder();
1757
lText.append(CONSTRUCTOR_COLOR);
1760
lText.append(STRIKE);
1761
lText.append(simpleName);
1763
lText.append(STRIKE_END);
1764
lText.append(BOLD_END);
1765
lText.append(COLOR_END);
1767
for (Iterator<ParamDesc> it = params.iterator(); it.hasNext();) {
1768
ParamDesc paramDesc = it.next();
1769
lText.append(escape(paramDesc.typeName));
1771
lText.append(PARAMETER_NAME_COLOR);
1772
lText.append(paramDesc.name);
1773
lText.append(COLOR_END);
1775
lText.append(", "); //NOI18N
1779
leftText = lText.toString();
1784
public CompletionTask createDocumentationTask() {
1785
return JavaCompletionProvider.createDocTask(elementHandle);
1788
protected ImageIcon getIcon() {
1789
int level = getProtectionLevel(modifiers);
1790
ImageIcon cachedIcon = icon[level];
1791
if (cachedIcon != null)
1794
String iconPath = CONSTRUCTOR_PUBLIC;
1797
iconPath = CONSTRUCTOR_PRIVATE;
1801
iconPath = CONSTRUCTOR_PACKAGE;
1804
case PROTECTED_LEVEL:
1805
iconPath = CONSTRUCTOR_PROTECTED;
1809
iconPath = CONSTRUCTOR_PUBLIC;
1812
ImageIcon newIcon = new ImageIcon(org.openide.util.Utilities.loadImage(iconPath));
1813
icon[level] = newIcon;
1817
protected void substituteText(JTextComponent c, int offset, int len, String toAdd) {
1820
int semiPos = toAdd != null && toAdd.endsWith(";") ? findPositionForSemicolon(c) : -2; //NOI18N
1822
toAdd = toAdd.length() > 1 ? toAdd.substring(0, toAdd.length() - 1) : null;
1823
BaseDocument doc = (BaseDocument)c.getDocument();
1824
TokenSequence<JavaTokenId> sequence = SourceUtils.getJavaTokenSequence(TokenHierarchy.get(doc), offset);
1825
if (sequence == null || !sequence.moveNext() && !sequence.movePrevious()) {
1826
sequence.movePrevious();
1827
if (sequence.token().id() == JavaTokenId.THIS || sequence.token().id() == JavaTokenId.SUPER) {
1830
toAdd = ";"; //NOI18N
1832
sequence.moveNext();
1834
String add = isAbstract ? "() {}" : "()"; //NOI18N
1835
if (CodeStyle.getDefault(null).spaceBeforeMethodCallParen())
1836
add = " " + add; //NOI18N
1837
if (toAdd != null && !add.startsWith(toAdd))
1839
String text = ""; //NOI18N
1840
if (sequence == null) {
1844
boolean added = false;
1845
while(add != null && add.length() > 0) {
1846
String tokenText = sequence.token().text().toString();
1847
if (tokenText.startsWith(add)) {
1848
len = sequence.offset() - offset + add.length();
1851
} else if (add.startsWith(tokenText)) {
1852
sequence.moveNext();
1853
len = sequence.offset() - offset;
1854
text += add.substring(0, tokenText.length());
1855
add = add.substring(tokenText.length());
1857
} else if (sequence.token().id() == JavaTokenId.WHITESPACE && sequence.token().text().toString().indexOf('\n') < 0) {//NOI18N
1858
if (!sequence.moveNext()) {
1869
Position position = null;
1871
Position semiPosition = semiPos > -1 ? doc.createPosition(semiPos) : null;
1872
doc.remove(offset, len);
1873
doc.insertString(offset, text, null);
1874
position = doc.createPosition(offset);
1875
if (semiPosition != null)
1876
doc.insertString(semiPosition.getOffset(), ";", null); //NOI18N
1877
else if (!isAbstract && params.isEmpty() && "()".equals(text.trim())) //NOI18N
1878
c.setCaretPosition(c.getCaretPosition() - 1);
1879
} catch (BadLocationException e) {
1883
if (isAbstract && text.length() > 3) {
1885
JavaSource js = JavaSource.forDocument(doc);
1886
final int off = offset + 4;
1887
js.runModificationTask(new Task<WorkingCopy>() {
1889
public void run(WorkingCopy copy) throws IOException {
1890
copy.toPhase(JavaSource.Phase.RESOLVED);
1891
TreePath path = copy.getTreeUtilities().pathFor(off);
1892
while (path.getLeaf() != path.getCompilationUnit()) {
1893
Tree tree = path.getLeaf();
1894
Tree parentTree = path.getParentPath().getLeaf();
1895
if (parentTree.getKind() == Tree.Kind.NEW_CLASS && tree.getKind() == Tree.Kind.CLASS) {
1896
GeneratorUtils.generateAllAbstractMethodImplementations(copy, path);
1899
path = path.getParentPath();
1903
} catch (Exception ex) {
1906
if (!params.isEmpty() && text.length() > 1) {
1907
CodeTemplateManager ctm = CodeTemplateManager.get(doc);
1909
StringBuilder sb = new StringBuilder();
1910
for (Iterator<ParamDesc> it = params.iterator(); it.hasNext();) {
1911
ParamDesc paramDesc = it.next();
1912
sb.append("${"); //NOI18N
1913
sb.append(paramDesc.name);
1914
sb.append(" named instanceof="); //NOI18N
1915
sb.append(paramDesc.fullTypeName);
1916
sb.append("}"); //NOI18N
1918
sb.append(", "); //NOI18N
1920
if (position != null)
1921
offset = position.getOffset();
1922
c.setCaretPosition(offset + 1);
1923
ctm.createTemporary(sb.toString()).insert(c);
1924
Completion.get().showToolTip();
1929
public String toString() {
1930
StringBuilder sb = new StringBuilder();
1931
for (Modifier mod : modifiers) {
1932
sb.append(mod.toString());
1935
sb.append(simpleName);
1936
sb.append('('); //NOI18N
1937
for (Iterator<ParamDesc> it = params.iterator(); it.hasNext();) {
1938
ParamDesc paramDesc = it.next();
1939
sb.append(paramDesc.typeName);
1941
sb.append(paramDesc.name);
1943
sb.append(", "); //NOI18N
1947
return sb.toString();
1951
static class DefaultConstructorItem extends JavaCompletionItem {
1953
private static final String CONSTRUCTOR = "org/netbeans/modules/java/editor/resources/new_constructor_16.png"; //NOI18N
1954
private static final String CONSTRUCTOR_COLOR = "<font color=#b28b00>"; //NOI18N
1955
private static ImageIcon icon;
1957
private boolean smartType;
1958
private String simpleName;
1959
private boolean isAbstract;
1960
private String sortText;
1961
private String leftText;
1963
private DefaultConstructorItem(TypeElement elem, int substitutionOffset, boolean smartType) {
1964
super(substitutionOffset);
1965
this.smartType = smartType;
1966
this.simpleName = elem.getSimpleName().toString();
1967
this.isAbstract = elem.getModifiers().contains(Modifier.ABSTRACT);
1970
public CharSequence getInsertPrefix() {
1974
public int getSortPriority() {
1975
return smartType ? 650 - SMART_TYPE : 650;
1978
public CharSequence getSortText() {
1979
if (sortText == null)
1980
sortText = simpleName + "#0#"; //NOI18N
1984
protected String getLeftHtmlText() {
1985
if (leftText == null)
1986
leftText = CONSTRUCTOR_COLOR + simpleName + "()" + COLOR_END; //NOI18N
1990
protected ImageIcon getIcon() {
1991
if (icon == null) icon = new ImageIcon(org.openide.util.Utilities.loadImage(CONSTRUCTOR));
1995
protected void substituteText(JTextComponent c, int offset, int len, String toAdd) {
1998
int semiPos = toAdd != null && toAdd.endsWith(";") ? findPositionForSemicolon(c) : -2; //NOI18N
2000
toAdd = toAdd.length() > 1 ? toAdd.substring(0, toAdd.length() - 1) : null;
2001
BaseDocument doc = (BaseDocument) c.getDocument();
2002
TokenSequence<JavaTokenId> sequence = SourceUtils.getJavaTokenSequence(TokenHierarchy.get(doc), offset);
2003
if (sequence == null || !sequence.moveNext() && !sequence.movePrevious()) {
2004
sequence.movePrevious();
2005
if (sequence.token().id() == JavaTokenId.THIS || sequence.token().id() == JavaTokenId.SUPER) {
2008
toAdd = ";"; //NOI18N
2010
sequence.moveNext();
2012
String add = isAbstract ? "() {}" : "()"; //NOI18N
2013
if (CodeStyle.getDefault(null).spaceBeforeMethodCallParen())
2014
add = " " + add; //NOI18N
2015
if (toAdd != null && !add.startsWith(toAdd))
2017
String text = ""; //NOI18N
2018
if (sequence == null) {
2022
boolean added = false;
2023
while(add != null && add.length() > 0) {
2024
String tokenText = sequence.token().text().toString();
2025
if (tokenText.startsWith(add)) {
2026
len = sequence.offset() - offset + add.length();
2029
} else if (add.startsWith(tokenText)) {
2030
sequence.moveNext();
2031
len = sequence.offset() - offset;
2032
text += add.substring(0, tokenText.length());
2033
add = add.substring(tokenText.length());
2035
} else if (sequence.token().id() == JavaTokenId.WHITESPACE && sequence.token().text().toString().indexOf('\n') < 0) {//NOI18N
2036
if (!sequence.moveNext()) {
2048
Position position = doc.createPosition(offset);
2049
Position semiPosition = semiPos > -1 ? doc.createPosition(semiPos) : null;
2050
doc.remove(offset, len);
2051
doc.insertString(position.getOffset(), text, null);
2052
if (semiPosition != null)
2053
doc.insertString(position.getOffset(), ";", null); //NOI18N
2054
else if (!isAbstract && "()".equals(text.trim())) //NOI18N
2055
c.setCaretPosition(c.getCaretPosition() - 1);
2056
} catch (BadLocationException e) {
2060
if (isAbstract && text.length() > 3) {
2062
JavaSource js = JavaSource.forDocument(c.getDocument());
2063
final int off = c.getSelectionEnd() - text.length() + 4;
2064
js.runModificationTask(new Task<WorkingCopy>() {
2066
public void run(WorkingCopy copy) throws IOException {
2067
copy.toPhase(JavaSource.Phase.RESOLVED);
2068
TreePath path = copy.getTreeUtilities().pathFor(off);
2069
while (path.getLeaf() != path.getCompilationUnit()) {
2070
Tree tree = path.getLeaf();
2071
Tree parentTree = path.getParentPath().getLeaf();
2072
if (parentTree.getKind() == Tree.Kind.NEW_CLASS && tree.getKind() == Tree.Kind.CLASS) {
2073
GeneratorUtils.generateAllAbstractMethodImplementations(copy, path);
2076
path = path.getParentPath();
2080
} catch (IOException ex) {
2085
public String toString() {
2086
return simpleName + "()";
2090
static class ParametersItem extends JavaCompletionItem {
2092
private static final String PARAMETERS_COLOR = "<font color=#808080>"; //NOI18N
2093
private static final String ACTIVE_PARAMETER_COLOR = "<font color=#000000>"; //NOI18N
2095
protected ElementHandle<ExecutableElement> elementHandle;
2096
private boolean isDeprecated;
2097
private int activeParamsIndex;
2098
private String simpleName;
2099
private ArrayList<ParamDesc> params;
2100
private String typeName;
2101
private String sortText;
2102
private String leftText;
2103
private String rightText;
2105
private ParametersItem(ExecutableElement elem, ExecutableType type, int substitutionOffset, boolean isDeprecated, int activeParamsIndex) {
2106
super(substitutionOffset);
2107
this.elementHandle = ElementHandle.create(elem);
2108
this.isDeprecated = isDeprecated;
2109
this.activeParamsIndex = activeParamsIndex;
2110
this.simpleName = elem.getKind() == ElementKind.CONSTRUCTOR ? elem.getEnclosingElement().getSimpleName().toString() : elem.getSimpleName().toString();
2111
this.params = new ArrayList<ParamDesc>();
2112
Iterator<? extends VariableElement> it = elem.getParameters().iterator();
2113
Iterator<? extends TypeMirror> tIt = type.getParameterTypes().iterator();
2114
while(it.hasNext() && tIt.hasNext()) {
2115
TypeMirror tm = tIt.next();
2116
this.params.add(new ParamDesc(tm.toString(), Utilities.getTypeName(tm, false, elem.isVarArgs() && !tIt.hasNext()).toString(), it.next().getSimpleName().toString()));
2118
TypeMirror retType = type.getReturnType();
2119
this.typeName = Utilities.getTypeName(retType, false).toString();
2122
public int getSortPriority() {
2123
return 100 - SMART_TYPE;
2126
public CharSequence getSortText() {
2127
if (sortText == null) {
2128
StringBuilder sortParams = new StringBuilder();
2129
sortParams.append('(');
2131
for(Iterator<ParamDesc> it = params.iterator(); it.hasNext();) {
2132
ParamDesc param = it.next();
2133
sortParams.append(param.typeName);
2135
sortParams.append(',');
2139
sortParams.append(')');
2140
sortText = "#" + ((cnt < 10 ? "0" : "") + cnt) + "#" + sortParams.toString(); //NOI18N
2145
public CharSequence getInsertPrefix() {
2149
protected String getLeftHtmlText() {
2150
if (leftText == null) {
2151
StringBuilder lText = new StringBuilder();
2152
lText.append(PARAMETERS_COLOR);
2154
lText.append(STRIKE);
2155
lText.append(simpleName);
2157
lText.append(STRIKE_END);
2159
for (int i = 0; i < params.size(); i++) {
2160
ParamDesc paramDesc = params.get(i);
2161
if (i == activeParamsIndex)
2162
lText.append(COLOR_END).append(ACTIVE_PARAMETER_COLOR).append(BOLD);
2163
lText.append(escape(paramDesc.typeName));
2165
lText.append(paramDesc.name);
2166
if (i < params.size() - 1)
2167
lText.append(", "); //NOI18N
2169
lText.append(BOLD_END).append(COLOR_END).append(PARAMETERS_COLOR);
2172
lText.append(COLOR_END);
2173
return lText.toString();
2178
protected String getRightHtmlText() {
2179
if (rightText == null)
2180
rightText = PARAMETERS_COLOR + escape(typeName) + COLOR_END;
2184
public CompletionTask createDocumentationTask() {
2185
return JavaCompletionProvider.createDocTask(elementHandle);
2188
public boolean instantSubstitution(JTextComponent component) {
2192
protected void substituteText(final JTextComponent c, int offset, int len, String toAdd) {
2193
String add = ")"; //NOI18N
2194
if (toAdd != null && !add.startsWith(toAdd))
2196
if (params.isEmpty()) {
2197
super.substituteText(c, offset, len, add);
2199
BaseDocument doc = (BaseDocument)c.getDocument();
2200
String text = ""; //NOI18N
2201
int semiPos = add.endsWith(";") ? findPositionForSemicolon(c) : -2; //NOI18N
2203
add = add.length() > 1 ? add.substring(0, add.length() - 1) : null;
2204
TokenSequence<JavaTokenId> sequence = SourceUtils.getJavaTokenSequence(TokenHierarchy.get(doc), offset + len);
2205
if (sequence == null || !sequence.moveNext() && !sequence.movePrevious()) {
2209
boolean added = false;
2210
while(add != null && add.length() > 0) {
2211
String tokenText = sequence.token().text().toString();
2212
if (tokenText.startsWith(add)) {
2213
len = sequence.offset() - offset + add.length();
2216
} else if (add.startsWith(tokenText)) {
2217
sequence.moveNext();
2218
len = sequence.offset() - offset;
2219
text += add.substring(0, tokenText.length());
2220
add = add.substring(tokenText.length());
2222
} else if (sequence.token().id() == JavaTokenId.WHITESPACE && sequence.token().text().toString().indexOf('\n') < 0) {//NOI18N
2223
if (!sequence.moveNext()) {
2235
Position semiPosition = semiPos > -1 ? doc.createPosition(semiPos) : null;
2237
doc.remove(offset, len);
2238
if (semiPosition != null)
2239
doc.insertString(semiPosition.getOffset(), ";", null); //NOI18N
2240
} catch (BadLocationException e) {
2245
CodeTemplateManager ctm = CodeTemplateManager.get(doc);
2247
StringBuilder sb = new StringBuilder();
2248
for (int i = activeParamsIndex; i < params.size(); i++) {
2249
ParamDesc paramDesc = params.get(i);
2250
sb.append("${"); //NOI18N
2251
sb.append(paramDesc.name);
2252
sb.append(" named instanceof="); //NOI18N
2253
sb.append(paramDesc.fullTypeName);
2254
sb.append("}"); //NOI18N
2255
if (i < params.size() - 1)
2256
sb.append(", "); //NOI18N
2258
if (text.length() > 0)
2260
ctm.createTemporary(sb.toString()).insert(c);
2261
Completion.get().showToolTip();
2266
public String toString() {
2267
StringBuilder sb = new StringBuilder();
2268
sb.append(typeName);
2270
sb.append(simpleName);
2272
for (Iterator<ParamDesc> it = params.iterator(); it.hasNext();) {
2273
ParamDesc paramDesc = it.next();
2274
sb.append(paramDesc.typeName);
2276
sb.append(paramDesc.name);
2278
sb.append(", "); //NOI18N
2282
return sb.toString();
2286
static class AnnotationItem extends AnnotationTypeItem {
2288
private AnnotationItem(TypeElement elem, DeclaredType type, int substitutionOffset, boolean isDeprecated, boolean smartType) {
2289
super(elem, type, 0, substitutionOffset, true, isDeprecated, false, smartType);
2292
public CharSequence getInsertPrefix() {
2293
return "@" + super.getInsertPrefix(); //NOI18N
2296
protected void substituteText(final JTextComponent c, final int offset, int len, String toAdd) {
2297
final BaseDocument doc = (BaseDocument)c.getDocument();
2298
final StringBuilder text = new StringBuilder();
2299
final int semiPos = toAdd != null && toAdd.endsWith(";") ? findPositionForSemicolon(c) : -2; //NOI18N
2301
toAdd = toAdd.length() > 1 ? toAdd.substring(0, toAdd.length() - 1) : null;
2302
if (toAdd != null && !toAdd.equals("\n")) {//NOI18N
2303
TokenSequence<JavaTokenId> sequence = SourceUtils.getJavaTokenSequence(TokenHierarchy.get(doc), offset + len);
2304
if (sequence == null || !sequence.moveNext() && !sequence.movePrevious()) {
2308
boolean added = false;
2309
while(toAdd != null && toAdd.length() > 0) {
2310
String tokenText = sequence.token().text().toString();
2311
if (tokenText.startsWith(toAdd)) {
2312
len = sequence.offset() - offset + toAdd.length();
2315
} else if (toAdd.startsWith(tokenText)) {
2316
sequence.moveNext();
2317
len = sequence.offset() - offset;
2318
text.append(toAdd.substring(0, tokenText.length()));
2319
toAdd = toAdd.substring(tokenText.length());
2321
} else if (sequence.token().id() == JavaTokenId.WHITESPACE && sequence.token().text().toString().indexOf('\n') < 0) {//NOI18N
2322
if (!sequence.moveNext()) {
2333
final int finalLen = len;
2334
JavaSource js = JavaSource.forDocument(doc);
2336
js.runUserActionTask(new Task<CompilationController>() {
2338
public void run(CompilationController controller) throws IOException {
2339
controller.toPhase(JavaSource.Phase.RESOLVED);
2340
TypeElement elem = elementHandle.resolve(controller);
2344
Position semiPosition = semiPos > -1 ? doc.createPosition(semiPos) : null;
2345
TreePath tp = controller.getTreeUtilities().pathFor(offset);
2346
text.insert(0, "@" + AutoImport.resolveImport(controller, tp, controller.getTypes().getDeclaredType(elem))); //NOI18N
2347
String textToReplace = doc.getText(offset, finalLen);
2348
if (textToReplace.contentEquals(text)) return;
2349
doc.remove(offset, finalLen);
2350
doc.insertString(offset, text.toString(), null);
2351
if (semiPosition != null)
2352
doc.insertString(semiPosition.getOffset(), ";", null); //NOI18N
2353
} catch (BadLocationException e) {
2360
} catch (IOException ioe) {
2365
static class AttributeItem extends JavaCompletionItem {
2367
private static final String ATTRIBUTE = "org/netbeans/modules/java/editor/resources/attribute_16.png"; // NOI18N
2368
private static final String ATTRIBUTE_COLOR = "<font color=#404040>"; //NOI18N
2369
private static ImageIcon icon;
2371
private ElementHandle<ExecutableElement> elementHandle;
2372
private boolean isDeprecated;
2373
private String simpleName;
2374
private String typeName;
2375
private String defaultValue;
2376
private String leftText;
2377
private String rightText;
2379
private AttributeItem(ExecutableElement elem, ExecutableType type, int substitutionOffset, boolean isDeprecated) {
2380
super(substitutionOffset);
2381
this.elementHandle = ElementHandle.create(elem);
2382
this.isDeprecated = isDeprecated;
2383
this.simpleName = elem.getSimpleName().toString();
2384
this.typeName = Utilities.getTypeName(type.getReturnType(), false).toString();
2385
AnnotationValue value = elem.getDefaultValue();
2386
this.defaultValue = value != null ? value.toString() : null;
2389
public int getSortPriority() {
2393
public CharSequence getSortText() {
2397
public CharSequence getInsertPrefix() {
2398
return simpleName + "="; //NOI18N
2401
public CompletionTask createDocumentationTask() {
2402
return JavaCompletionProvider.createDocTask(elementHandle);
2405
protected ImageIcon getIcon(){
2406
if (icon == null) icon = new ImageIcon(org.openide.util.Utilities.loadImage(ATTRIBUTE));
2410
protected String getLeftHtmlText() {
2411
if (leftText == null) {
2412
StringBuilder sb = new StringBuilder();
2413
sb.append(ATTRIBUTE_COLOR);
2414
if (defaultValue == null)
2418
sb.append(simpleName);
2420
sb.append(STRIKE_END);
2421
if (defaultValue == null) {
2422
sb.append(BOLD_END);
2424
sb.append(" = "); //NOI18N
2425
sb.append(defaultValue);
2427
sb.append(COLOR_END);
2428
leftText = sb.toString();
2433
protected String getRightHtmlText() {
2434
if (rightText == null)
2435
rightText = escape(typeName);
2439
public String toString() {
2444
static class StaticMemberItem extends JavaCompletionItem {
2446
private static final String FIELD_ST_PUBLIC = "org/netbeans/modules/editor/resources/completion/field_static_16.png"; //NOI18N
2447
private static final String FIELD_ST_PROTECTED = "org/netbeans/modules/editor/resources/completion/field_static_protected_16.png"; //NOI18N
2448
private static final String FIELD_ST_PACKAGE = "org/netbeans/modules/editor/resources/completion/field_static_package_private_16.png"; //NOI18N
2449
private static final String FIELD_COLOR = "<font color=#0000b2>"; //NOI18N
2450
private static final String METHOD_ST_PUBLIC = "org/netbeans/modules/editor/resources/completion/method_static_16.png"; //NOI18N
2451
private static final String METHOD_ST_PROTECTED = "org/netbeans/modules/editor/resources/completion/method_static_protected_16.png"; //NOI18N
2452
private static final String METHOD_ST_PACKAGE = "org/netbeans/modules/editor/resources/completion/method_static_package_private_16.png"; //NOI18N
2453
private static final String METHOD_COLOR = "<font color=#7c0000>"; //NOI18N
2454
private static final String PARAMETER_NAME_COLOR = "<font color=#b200b2>"; //NOI18N
2455
private static ImageIcon icon[][] = new ImageIcon[2][3];
2457
private TypeMirrorHandle<DeclaredType> typeHandle;
2458
private ElementHandle<Element> memberElementHandle;
2459
private boolean isDeprecated;
2460
private String typeName;
2461
private String memberName;
2462
private String memberTypeName;
2463
private Set<Modifier> modifiers;
2464
private List<ParamDesc> params;
2465
private String sortText;
2466
private String leftText;
2467
private String rightText;
2469
private StaticMemberItem(DeclaredType type, Element memberElem, TypeMirror memberType, int substitutionOffset, boolean isDeprecated) {
2470
super(substitutionOffset);
2471
this.typeHandle = TypeMirrorHandle.create(type);
2472
this.memberElementHandle = ElementHandle.create(memberElem);
2473
this.isDeprecated = isDeprecated;
2474
this.typeName = Utilities.getTypeName(type, false).toString();
2475
this.memberName = memberElem.getSimpleName().toString();
2476
this.memberTypeName = Utilities.getTypeName(memberElem.getKind().isField() ? memberType : ((ExecutableType)memberType).getReturnType(), false).toString();
2477
this.modifiers = memberElem.getModifiers();
2478
if (!memberElem.getKind().isField()) {
2479
this.params = new ArrayList<ParamDesc>();
2480
Iterator<? extends VariableElement> it = ((ExecutableElement)memberElem).getParameters().iterator();
2481
Iterator<? extends TypeMirror> tIt = ((ExecutableType)memberType).getParameterTypes().iterator();
2482
while(it.hasNext() && tIt.hasNext()) {
2483
TypeMirror tm = tIt.next();
2484
this.params.add(new ParamDesc(tm.toString(), Utilities.getTypeName(tm, false, ((ExecutableElement)memberElem).isVarArgs() && !tIt.hasNext()).toString(), it.next().getSimpleName().toString()));
2489
public int getSortPriority() {
2490
return (params == null ? 700 : 750) - SMART_TYPE;
2493
public CharSequence getSortText() {
2494
if (sortText == null) {
2495
if (params == null) {
2496
sortText = memberName + "#" + typeName; //NOI18N
2498
StringBuilder sortParams = new StringBuilder();
2499
sortParams.append('(');
2501
for (Iterator<ParamDesc> it = params.iterator(); it.hasNext();) {
2502
ParamDesc paramDesc = it.next();
2503
sortParams.append(paramDesc.typeName);
2505
sortParams.append(',');
2509
sortParams.append(')');
2510
sortText = memberName + "#" + ((cnt < 10 ? "0" : "") + cnt) + "#" + sortParams.toString() + "#" + typeName; //NOI18N
2516
public CharSequence getInsertPrefix() {
2517
return typeName + "." + memberName; //NOI18N
2520
public CompletionTask createDocumentationTask() {
2521
return JavaCompletionProvider.createDocTask(memberElementHandle);
2524
protected String getLeftHtmlText() {
2525
if (leftText == null) {
2526
StringBuilder lText = new StringBuilder();
2527
lText.append(memberElementHandle.getKind().isField() ? FIELD_COLOR : METHOD_COLOR);
2528
lText.append(escape(typeName));
2531
lText.append(STRIKE);
2532
lText.append(memberName);
2534
lText.append(STRIKE_END);
2535
lText.append(COLOR_END);
2536
if (params != null) {
2538
for (Iterator<ParamDesc> it = params.iterator(); it.hasNext();) {
2539
ParamDesc paramDesc = it.next();
2540
lText.append(escape(paramDesc.typeName));
2541
lText.append(' '); //NOI18N
2542
lText.append(PARAMETER_NAME_COLOR);
2543
lText.append(paramDesc.name);
2544
lText.append(COLOR_END);
2546
lText.append(", "); //NOI18N
2551
leftText = lText.toString();
2556
protected String getRightHtmlText() {
2557
if (rightText == null)
2558
rightText = escape(memberTypeName);
2562
protected ImageIcon getIcon(){
2563
int level = getProtectionLevel(modifiers);
2564
boolean isField = memberElementHandle.getKind().isField();
2565
ImageIcon cachedIcon = icon[isField ? 0 : 1][level - 1];
2566
if (cachedIcon != null)
2569
String iconPath = null;
2573
iconPath = FIELD_ST_PACKAGE;
2576
case PROTECTED_LEVEL:
2577
iconPath = FIELD_ST_PROTECTED;
2581
iconPath = FIELD_ST_PUBLIC;
2587
iconPath = METHOD_ST_PACKAGE;
2590
case PROTECTED_LEVEL:
2591
iconPath = METHOD_ST_PROTECTED;
2595
iconPath = METHOD_ST_PUBLIC;
2599
if (iconPath == null)
2601
ImageIcon newIcon = new ImageIcon(org.openide.util.Utilities.loadImage(iconPath));
2602
icon[isField ? 0 : 1][level - 1] = newIcon;
2606
protected void substituteText(final JTextComponent c, final int offset, int len, String toAdd) {
2607
final BaseDocument doc = (BaseDocument)c.getDocument();
2608
final StringBuilder text = new StringBuilder();
2609
final int semiPos = toAdd != null && toAdd.endsWith(";") ? findPositionForSemicolon(c) : -2; //NOI18N
2611
toAdd = toAdd.length() > 1 ? toAdd.substring(0, toAdd.length() - 1) : null;
2612
if (toAdd != null && !toAdd.equals("\n")) { //NOI18N
2613
TokenSequence<JavaTokenId> sequence = SourceUtils.getJavaTokenSequence(TokenHierarchy.get(doc), offset + len);
2614
if (sequence == null || !sequence.moveNext() && !sequence.movePrevious()) {
2618
boolean added = false;
2619
while(toAdd != null && toAdd.length() > 0) {
2620
String tokenText = sequence.token().text().toString();
2621
if (tokenText.startsWith(toAdd)) {
2622
len = sequence.offset() - offset + toAdd.length();
2625
} else if (toAdd.startsWith(tokenText)) {
2626
sequence.moveNext();
2627
len = sequence.offset() - offset;
2628
text.append(toAdd.substring(0, tokenText.length()));
2629
toAdd = toAdd.substring(tokenText.length());
2631
} else if (sequence.token().id() == JavaTokenId.WHITESPACE && sequence.token().text().toString().indexOf('\n') < 0) {//NOI18N
2632
if (!sequence.moveNext()) {
2643
final int finalLen = len;
2644
JavaSource js = JavaSource.forDocument(doc);
2646
js.runUserActionTask(new Task<CompilationController>() {
2648
public void run(CompilationController controller) throws IOException {
2649
controller.toPhase(JavaSource.Phase.RESOLVED);
2650
DeclaredType type = typeHandle.resolve(controller);
2651
StringBuilder sb = new StringBuilder();
2653
sb.append("${PAR#"); //NOI18N
2655
sb.append(" type=\""); //NOI18N
2656
sb.append(((TypeElement)type.asElement()).getQualifiedName());
2657
sb.append("\" default=\""); //NOI18N
2658
sb.append(((TypeElement)type.asElement()).getSimpleName());
2659
sb.append("\" editable=false}"); //NOI18N
2660
Iterator<? extends TypeMirror> tas = type.getTypeArguments().iterator();
2661
if (tas.hasNext()) {
2662
sb.append('<'); //NOI18N
2663
while (tas.hasNext()) {
2664
TypeMirror ta = tas.next();
2665
sb.append("${PAR#"); //NOI18N
2667
if (ta.getKind() == TypeKind.TYPEVAR) {
2668
sb.append(" type=\""); //NOI18N
2669
ta = ((TypeVariable)ta).getUpperBound();
2670
sb.append(Utilities.getTypeName(ta, true));
2671
sb.append("\" default=\""); //NOI18N
2672
sb.append(Utilities.getTypeName(ta, false));
2673
sb.append("\"}"); //NOI18N
2674
} else if (ta.getKind() == TypeKind.WILDCARD) {
2675
sb.append(" type=\""); //NOI18N
2676
TypeMirror bound = ((WildcardType)ta).getExtendsBound();
2678
bound = ((WildcardType)ta).getSuperBound();
2679
sb.append(bound != null ? Utilities.getTypeName(bound, true) : "Object"); //NOI18N
2680
sb.append("\" default=\""); //NOI18N
2681
sb.append(bound != null ? Utilities.getTypeName(bound, false) : "Object"); //NOI18N
2682
sb.append("\"}"); //NOI18N
2683
} else if (ta.getKind() == TypeKind.ERROR) {
2684
sb.append(" default=\""); //NOI18N
2685
sb.append(((ErrorType)ta).asElement().getSimpleName());
2686
sb.append("\"}"); //NOI18N
2688
sb.append(" type=\""); //NOI18N
2689
sb.append(Utilities.getTypeName(ta, true));
2690
sb.append("\" default=\""); //NOI18N
2691
sb.append(Utilities.getTypeName(ta, false));
2692
sb.append("\" editable=false}"); //NOI18N
2695
sb.append(", "); //NOI18N
2697
sb.append('>'); //NOI18N
2699
sb.append('.'); //NOI18N
2700
sb.append(memberName);
2701
if (params != null) {
2702
sb.append("("); //NOI18N
2703
for (Iterator<ParamDesc> it = params.iterator(); it.hasNext();) {
2704
ParamDesc paramDesc = it.next();
2705
sb.append("${"); //NOI18N
2706
sb.append(paramDesc.name);
2707
sb.append(" named instanceof="); //NOI18N
2708
sb.append(paramDesc.fullTypeName);
2709
sb.append("}"); //NOI18N
2711
sb.append(", "); //NOI18N
2713
sb.append(")");//NOI18N
2718
Position semiPosition = semiPos > -1 ? doc.createPosition(semiPos) : null;
2720
doc.remove(offset, finalLen);
2721
if (semiPosition != null)
2722
doc.insertString(semiPosition.getOffset(), ";", null); //NOI18N
2723
} catch (BadLocationException e) {
2728
CodeTemplateManager ctm = CodeTemplateManager.get(doc);
2730
ctm.createTemporary(sb.toString()).insert(c);
2734
} catch (IOException ioe) {
2738
public String toString() {
2739
StringBuilder sb = new StringBuilder();
2740
for(Modifier mod : modifiers) {
2741
sb.append(mod.toString());
2742
sb.append(' '); // NOI18N
2744
sb.append(memberTypeName);
2746
sb.append(typeName);
2748
sb.append(memberName);
2749
if (params != null) {
2750
sb.append('('); //NOI18N
2751
for (Iterator<ParamDesc> it = params.iterator(); it.hasNext();) {
2752
ParamDesc paramDesc = it.next();
2753
sb.append(paramDesc.typeName);
2755
sb.append(paramDesc.name);
2757
sb.append(", "); //NOI18N
2762
return sb.toString();
2766
static class InitializeAllConstructorItem extends JavaCompletionItem {
2768
private static final String CONSTRUCTOR_PUBLIC = "org/netbeans/modules/java/editor/resources/new_constructor_16.png"; //NOI18N
2769
private static final String CONSTRUCTOR_COLOR = "<font color=#b28b00>"; //NOI18N
2770
private static final String PARAMETER_NAME_COLOR = "<font color=#b200b2>"; //NOI18N
2771
private static ImageIcon icon;
2773
private List<ElementHandle<VariableElement>> fieldHandles;
2774
private String simpleName;
2775
private List<ParamDesc> params;
2776
private String sortText;
2777
private String leftText;
2779
private InitializeAllConstructorItem(Iterable<? extends VariableElement> fields, TypeElement parent, int substitutionOffset) {
2780
super(substitutionOffset);
2781
this.fieldHandles = new ArrayList<ElementHandle<VariableElement>>();
2782
this.params = new ArrayList<ParamDesc>();
2783
for (VariableElement ve : fields) {
2784
this.fieldHandles.add(ElementHandle.create(ve));
2785
this.params.add(new ParamDesc(null, Utilities.getTypeName(ve.asType(), false).toString(), ve.getSimpleName().toString()));
2787
this.simpleName = parent.getSimpleName().toString();
2790
public int getSortPriority() {
2794
public CharSequence getSortText() {
2795
if (sortText == null) {
2796
StringBuilder sortParams = new StringBuilder();
2797
sortParams.append('('); //NOI18N
2799
for (Iterator<ParamDesc> it = params.iterator(); it.hasNext();) {
2800
ParamDesc paramDesc = it.next();
2801
sortParams.append(paramDesc.typeName);
2803
sortParams.append(','); //NOI18N
2807
sortParams.append(')'); //NOI18N
2808
sortText = simpleName + "#" + ((cnt < 10 ? "0" : "") + cnt) + "#" + sortParams.toString(); //NOI18N
2813
protected String getLeftHtmlText() {
2814
if (leftText == null) {
2815
StringBuilder lText = new StringBuilder();
2816
lText.append(CONSTRUCTOR_COLOR);
2817
lText.append(simpleName);
2818
lText.append(COLOR_END);
2819
lText.append('('); //NOI18N
2820
for (Iterator<ParamDesc> it = params.iterator(); it.hasNext();) {
2821
ParamDesc paramDesc = it.next();
2822
lText.append(escape(paramDesc.typeName));
2823
lText.append(' '); //NOI18N
2824
lText.append(PARAMETER_NAME_COLOR);
2825
lText.append(paramDesc.name);
2826
lText.append(COLOR_END);
2828
lText.append(", "); //NOI18N
2831
lText.append(") - "); //NOI18N
2832
lText.append(GENERATE_TEXT);
2833
leftText = lText.toString();
2838
protected ImageIcon getIcon() {
2840
icon = new ImageIcon(org.openide.util.Utilities.loadImage(CONSTRUCTOR_PUBLIC));
2844
public CharSequence getInsertPrefix() {
2848
protected void substituteText(final JTextComponent c, final int offset, final int len, String toAdd) {
2849
BaseDocument doc = (BaseDocument)c.getDocument();
2853
doc.remove(offset, len);
2854
} catch (BadLocationException e) {
2861
JavaSource js = JavaSource.forDocument(c.getDocument());
2862
js.runModificationTask(new Task<WorkingCopy>() {
2864
public void run(WorkingCopy copy) throws IOException {
2865
copy.toPhase(JavaSource.Phase.PARSED);
2866
TreePath tp = copy.getTreeUtilities().pathFor(offset);
2867
if (tp.getLeaf().getKind() == Tree.Kind.CLASS) {
2868
ArrayList<VariableElement> fieldElements = new ArrayList<VariableElement>();
2869
for (ElementHandle<? extends Element> handle : fieldHandles)
2870
fieldElements.add((VariableElement)handle.resolve(copy));
2872
for (Tree tree : ((ClassTree)tp.getLeaf()).getMembers()) {
2873
if (copy.getTrees().getSourcePositions().getStartPosition(tp.getCompilationUnit(), tree) < offset)
2878
GeneratorUtils.generateConstructor(copy, tp, fieldElements, null, idx);
2882
} catch (IOException ex) {
2886
public String toString() {
2887
StringBuilder sb = new StringBuilder();
2888
sb.append("public "); //NOI18N
2889
sb.append(simpleName);
2890
sb.append('('); //NOI18N
2891
for (Iterator<ParamDesc> it = params.iterator(); it.hasNext();) {
2892
ParamDesc paramDesc = it.next();
2893
sb.append(paramDesc.typeName);
2894
sb.append(' '); //NOI18N
2895
sb.append(paramDesc.name);
2897
sb.append(", "); //NOI18N
2900
sb.append(") - "); //NOI18N
2901
sb.append(GENERATE_TEXT);
2902
return sb.toString();
2905
public boolean instantSubstitution(JTextComponent component) {
2906
return false; //no instant substitution for create constructor item
2910
private static final int PUBLIC_LEVEL = 3;
2911
private static final int PROTECTED_LEVEL = 2;
2912
private static final int PACKAGE_LEVEL = 1;
2913
private static final int PRIVATE_LEVEL = 0;
2915
private static int getProtectionLevel(Set<Modifier> modifiers) {
2916
if(modifiers.contains(Modifier.PUBLIC))
2917
return PUBLIC_LEVEL;
2918
if(modifiers.contains(Modifier.PROTECTED))
2919
return PROTECTED_LEVEL;
2920
if(modifiers.contains(Modifier.PRIVATE))
2921
return PRIVATE_LEVEL;
2922
return PACKAGE_LEVEL;
2925
private static String escape(String s) {
2928
return XMLUtil.toAttributeValue(s);
2929
} catch (Exception ex) {}
2934
private static int findPositionForSemicolon(JTextComponent c) {
2935
final int[] ret = new int[] {-2};
2936
final int offset = c.getSelectionEnd();
2938
JavaSource js = JavaSource.forDocument(c.getDocument());
2939
js.runUserActionTask(new Task<CompilationController>() {
2941
public void run(CompilationController controller) throws Exception {
2942
controller.toPhase(JavaSource.Phase.PARSED);
2944
TreePath tp = controller.getTreeUtilities().pathFor(offset);
2945
while (t == null && tp != null) {
2946
switch(tp.getLeaf().getKind()) {
2947
case EXPRESSION_STATEMENT:
2948
ExpressionTree expr = ((ExpressionStatementTree)tp.getLeaf()).getExpression();
2949
if (expr != null && expr.getKind() == Tree.Kind.ERRONEOUS)
2955
t = ((ReturnTree)tp.getLeaf()).getExpression();
2958
t = ((ThrowTree)tp.getLeaf()).getExpression();
2961
tp = tp.getParentPath();
2964
SourcePositions sp = controller.getTrees().getSourcePositions();
2965
int endPos = (int)sp.getEndPosition(tp.getCompilationUnit(), t);
2966
TokenSequence<JavaTokenId> ts = findLastNonWhitespaceToken(controller, offset, endPos);
2968
ret[0] = ts.token().id() == JavaTokenId.SEMICOLON ? -1 : ts.offset() + ts.token().length();
2971
TokenSequence<JavaTokenId> ts = controller.getTokenHierarchy().tokenSequence(JavaTokenId.language());
2973
if (ts.moveNext() && ts.token().id() == JavaTokenId.SEMICOLON)
2978
} catch (IOException ex) {
2983
private static TokenSequence<JavaTokenId> findLastNonWhitespaceToken(CompilationController controller, int startPos, int endPos) {
2984
TokenSequence<JavaTokenId> ts = controller.getTokenHierarchy().tokenSequence(JavaTokenId.language());
2986
while(ts.movePrevious()) {
2987
int offset = ts.offset();
2988
if (offset < startPos)
2990
switch (ts.token().id()) {
2994
case JAVADOC_COMMENT:
3003
static class ParamDesc {
3004
private String fullTypeName;
3005
private String typeName;
3006
private String name;
3008
public ParamDesc(String fullTypeName, String typeName, String name) {
3009
this.fullTypeName = fullTypeName;
3010
this.typeName = typeName;
3015
static class ClassSortText implements CharSequence {
3017
private String name;
3018
private String pkgName;
3019
private String text;
3021
public ClassSortText(String name, String pkgName) {
3022
this.name = name + "#"; //NOI18N
3023
this.pkgName = pkgName;
3026
public int length() {
3027
return name.length() + pkgName.length() + 3;
3030
public char charAt(int index) {
3031
return index < name.length() ? name.charAt(index) : getText().charAt(index);
3034
public CharSequence subSequence(int start, int end) {
3035
return getText().subSequence(start, end);
3038
private String getText() {
3040
text = name + Utilities.getImportanceLevel(pkgName) + "#" + pkgName; //NOI18N