|
What this is
Other links
The source code/* * Sun Public License Notice * * The contents of this file are subject to the Sun Public License * Version 1.0 (the "License"). You may not use this file except in * compliance with the License. A copy of the License is available at * http://www.sun.com/ * * The Original Code is NetBeans. The Initial Developer of the Original * Code is Sun Microsystems, Inc. Portions Copyright 1997-2004 Sun * Microsystems, Inc. All Rights Reserved. */ package org.netbeans.modules.editor.java; import org.netbeans.editor.BaseDocument; import org.netbeans.editor.Formatter; import org.netbeans.editor.ext.CompletionQuery; import org.netbeans.editor.ext.ExtFormatter; import org.netbeans.editor.ext.java.JCExpression; import org.netbeans.editor.ext.java.JavaCompletion; import org.netbeans.editor.ext.java.JavaSettingsNames; import org.netbeans.jmi.javamodel.*; import java.awt.Color; import java.awt.Component; import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Comparator; import javax.swing.text.BadLocationException; import javax.swing.text.JTextComponent; /** * * @author Dusan Balek */ public abstract class NbJMIResultItem implements CompletionQuery.ResultItem { public abstract String getItemText(); protected abstract Component getPaintComponent(boolean isSelected); protected abstract Object getAssociatedObject(); protected static Color getTypeColor(Type typ) { return (typ instanceof PrimitiveType) ? NbJMIPaintComponent.KEYWORD_COLOR : NbJMIPaintComponent.TYPE_COLOR; } protected static String getTypeName(Type typ) { if (typ instanceof Array) return getTypeName(((Array)typ).getType()) + "[]"; // NOI18N if (typ instanceof JavaClass) return ((JavaClass)typ).getSimpleName(); return typ.getName(); } public boolean substituteCommonText(JTextComponent c, int offset, int len, int subLen) { // [PENDING] not enough info in parameters... // commonText // substituteExp return false; } public boolean substituteText(JTextComponent c, int offset, int len, boolean shift) { BaseDocument doc = (BaseDocument)c.getDocument(); String text = getItemText(); int selectionStartOffset = -1; int selectionEndOffset = -1; if (text != null) { // Update the text doc.atomicLock(); try { String textToReplace = doc.getText(offset, len); if (text.equals(textToReplace)) return false; doc.remove(offset, len); doc.insertString(offset, text, null); if (selectionStartOffset >= 0) { c.select(offset + selectionStartOffset, offset + selectionEndOffset); } } catch (BadLocationException e) { // Can't update } finally { doc.atomicUnlock(); } } return true; } public Component getPaintComponent(javax.swing.JList list, boolean isSelected, boolean cellHasFocus) { Component ret = getPaintComponent(isSelected); if (ret==null) return null; if (isSelected) { ret.setBackground(list.getSelectionBackground()); ret.setForeground(list.getSelectionForeground()); } else { ret.setBackground(list.getBackground()); ret.setForeground(list.getForeground()); } ret.getAccessibleContext().setAccessibleName(getItemText()); ret.getAccessibleContext().setAccessibleDescription(getItemText()); return ret; } public String toString() { return getItemText(); } public static class VarResultItem extends NbJMIResultItem { private Type type; private String typeName; private Color typeColor; private String varName; private int modifiers; private static NbJMIPaintComponent.NbFieldPaintComponent fieldComponent = null; public VarResultItem(String varName, Type type, int modifiers){ this.type = type; this.varName = varName; this.modifiers = modifiers | JavaCompletion.LOCAL_MEMBER_BIT; this.typeName = getTypeName(type); this.typeColor = getTypeColor(type); } public String getItemText() { return varName; } public Component getPaintComponent(boolean isSelected) { if (fieldComponent == null) { fieldComponent = new NbJMIPaintComponent.NbFieldPaintComponent(true); } fieldComponent.setTypeName(typeName); fieldComponent.setTypeColor(typeColor); fieldComponent.setFieldName(varName); fieldComponent.setModifiers(modifiers); fieldComponent.setSelected(isSelected); return fieldComponent; } protected Object getAssociatedObject() { return null; } public Type getType() { return type; } public String toString() { String mods = Modifier.toString(modifiers) + " "; // NOI18N return (mods.length() > 1 ? mods : "") + typeName + " " + varName; // NOI18N } } public static class FieldResultItem extends NbJMIResultItem{ private Field fld; private String typeName; private Color typeColor; private String fldName; private int modifiers; private boolean isDeprecated; private static NbJMIPaintComponent.NbFieldPaintComponent fieldComponent = null; public FieldResultItem(Field fld, JavaClass context){ this.fld = fld; this.fldName = fld.getName(); this.modifiers = fld.getModifiers(); if (fld.getDeclaringClass() == (context instanceof ParameterizedType ? ((ParameterizedType)context).getDefinition() : context)) { this.modifiers |= JavaCompletion.LOCAL_MEMBER_BIT; } this.typeName = getTypeName(fld.getType()); this.typeColor = getTypeColor(fld.getType()); this.isDeprecated = fld.isDeprecated(); } public String getItemText() { return fldName; } public String getTypeName() { return typeName; } public int getModifiers() { return modifiers; } public String getFieldName() { return fldName; } public Component getPaintComponent(boolean isSelected) { if (fieldComponent == null) { fieldComponent = new NbJMIPaintComponent.NbFieldPaintComponent(false); } fieldComponent.setTypeName(typeName); fieldComponent.setFieldName(fldName); fieldComponent.setTypeColor(typeColor); fieldComponent.setModifiers(modifiers); fieldComponent.setSelected(isSelected); fieldComponent.setDeprecated(isDeprecated); return fieldComponent; } protected Object getAssociatedObject() { return fld; } public String toString() { String mods = Modifier.toString(modifiers) + " "; // NOI18N return (mods.length() > 1 ? mods : "") + typeName + " " + fldName; // NOI18N } } public static class MethodResultItem extends CallableFeatureResultItem { private static NbJMIPaintComponent.NbMethodPaintComponent mtdComponent = null; public MethodResultItem(Method mtd, JCExpression substituteExp, JavaClass context) { super(mtd, substituteExp, context); } public Component getPaintComponent(boolean isSelected) { if (mtdComponent == null) { mtdComponent = new NbJMIPaintComponent.NbMethodPaintComponent(); } mtdComponent.setFeatureName(getName()); mtdComponent.setModifiers(getModifiers()); mtdComponent.setTypeName(getTypeName()); mtdComponent.setTypeColor(getTypeColor()); mtdComponent.setParams(getParams()); mtdComponent.setExceptions(getExceptions()); mtdComponent.setDeprecated(isDeprecated()); mtdComponent.setSelected(isSelected); return mtdComponent; } public String toString() { String mods = Modifier.toString(getModifiers()) + " "; // NOI18N return (mods.length() > 1 ? mods : "") + getTypeName() + " " + getName() + printParams() + printExceptions(); // NOI18N } } public static class ConstructorResultItem extends CallableFeatureResultItem { private static NbJMIPaintComponent.NbConstructorPaintComponent ctrComponent = null; private Object declaringClass = null; public ConstructorResultItem(Constructor con, JCExpression substituteExp) { super(con, substituteExp, null); } public ConstructorResultItem(JavaClass type, JCExpression substituteExp) { super(type.getSimpleName(), substituteExp); this.declaringClass = type; } public Object getDeclaringClass() { return declaringClass; } public String getName() { return getTypeName(); } public Component getPaintComponent(boolean isSelected) { if (ctrComponent == null) { ctrComponent = new NbJMIPaintComponent.NbConstructorPaintComponent(); } ctrComponent.setFeatureName(getName()); ctrComponent.setModifiers(getModifiers()); ctrComponent.setParams(getParams()); ctrComponent.setExceptions(getExceptions()); ctrComponent.setDeprecated(isDeprecated()); ctrComponent.setSelected(isSelected); return ctrComponent; } public String toString() { String mods = Modifier.toString(getModifiers()) + " "; // NOI18N return (mods.length() > 1 ? mods : "") + getName() + printParams() + printExceptions(); } } public abstract static class CallableFeatureResultItem extends NbJMIResultItem { private CallableFeature cf; private JCExpression substituteExp; private List params = new ArrayList(); private List excs = new ArrayList(); private int modifiers; private String cfName, typeName; private Color typeColor; private boolean isDeprecated; public CallableFeatureResultItem(CallableFeature cf, JCExpression substituteExp, JavaClass context) { this.cf = cf; this.substituteExp = substituteExp; this.modifiers = cf.getModifiers(); if (cf.getDeclaringClass() == (context instanceof ParameterizedType ? ((ParameterizedType)context).getDefinition() : context)) { modifiers |= JavaCompletion.LOCAL_MEMBER_BIT; } cfName = cf.getName(); typeName = getTypeName(cf.getType()); typeColor = getTypeColor(cf.getType()); isDeprecated = cf.isDeprecated(); for (Iterator it = cf.getParameters().iterator(); it.hasNext();) { Parameter prm = (Parameter) it.next(); Type type = prm.getType(); params.add(new ParamStr(type.getName(), getTypeName(type), prm.getName(), prm.isVarArg(), getTypeColor(type))); } for (Iterator it = cf.getExceptions().iterator(); it.hasNext();) { JavaClass ex = (JavaClass) it.next(); excs.add(new ExcStr(ex.getSimpleName(), getTypeColor(ex))); } } protected CallableFeatureResultItem(String typeName, JCExpression substituteExp) { this.typeName = typeName; this.substituteExp = substituteExp; this.modifiers = Modifier.PUBLIC | JavaCompletion.LOCAL_MEMBER_BIT; } public String getItemText() { return getName(); } public String getTypeName() { return typeName; } public Color getTypeColor() { return typeColor; } public int getModifiers() { return modifiers; } public boolean isDeprecated() { return isDeprecated; } public String getName() { return cfName; } /** Returns List of ParamStr */ public List getParams() { return params; } /** Returns List of ExcStr */ public List getExceptions() { return excs; } protected Object getAssociatedObject() { return cf; } public boolean substituteText(JTextComponent c, int offset, int len, boolean shift) { BaseDocument doc = (BaseDocument)c.getDocument(); String text = null; int selectionStartOffset = -1; int selectionEndOffset = -1; boolean addParams = true; JCExpression exp = substituteExp; while(exp != null) { if (exp.getExpID() == JCExpression.IMPORT) { addParams = false; break; } exp = exp.getParent(); } switch ((substituteExp != null) ? substituteExp.getExpID() : -1) { case JCExpression.METHOD: // no subst break; case JCExpression.METHOD_OPEN: int parmsCnt = params.size(); if (parmsCnt == 0) { text = ")"; // NOI18N } else { // one or more parameters int ind = substituteExp.getParameterCount(); boolean addSpace = false; Formatter f = doc.getFormatter(); if (f instanceof ExtFormatter) { Object o = ((ExtFormatter)f).getSettingValue(JavaSettingsNames.JAVA_FORMAT_SPACE_AFTER_COMMA); if ((o instanceof Boolean) && ((Boolean)o).booleanValue()) { addSpace = true; } } try { if (addSpace && (ind == 0 || (offset > 0 && Character.isWhitespace(doc.getText(offset - 1, 1).charAt(0))))) { addSpace = false; } } catch (BadLocationException e) { } boolean isVarArg = parmsCnt > 0 ? ((ParamStr)params.get(parmsCnt - 1)).isVarArg() : false; if (ind < parmsCnt || isVarArg) { text = addSpace ? " " : ""; // NOI18N selectionStartOffset = text.length(); text += ((ParamStr)params.get(ind < parmsCnt ? ind : parmsCnt - 1)).getName(); selectionEndOffset = text.length(); } } break; default: text = getItemText(); boolean addSpace = false; Formatter f = doc.getFormatter(); if (f instanceof ExtFormatter) { Object o = ((ExtFormatter)f).getSettingValue(JavaSettingsNames.JAVA_FORMAT_SPACE_BEFORE_PARENTHESIS); if ((o instanceof Boolean) && ((Boolean)o).booleanValue()) { addSpace = true; } } if (addParams) { if (addSpace) { text += ' '; } text += '('; if (params.size() > 0) { selectionStartOffset = text.length(); text += ((ParamStr)params.get(0)).getName(); selectionEndOffset = text.length(); } text += ")"; // NOI18N } break; } if (text != null) { // Update the text doc.atomicLock(); try { String textToReplace = doc.getText(offset, len); if (text.equals(textToReplace)) return false; doc.remove(offset, len); doc.insertString(offset, text, null); if (selectionStartOffset >= 0) { c.select(offset + selectionStartOffset, offset + selectionEndOffset); } } catch (BadLocationException e) { // Can't update } finally { doc.atomicUnlock(); } } return true; } protected String printParams() { StringBuffer sb = new StringBuffer(); sb.append("("); // NOI18N for (Iterator it = params.iterator(); it.hasNext();) { ParamStr ps = (ParamStr)it.next(); sb.append(ps.getSimpleTypeName()); if (ps.isVarArg()) { sb.append("..."); // NOI18N } String name = ps.getName(); if (name != null && name.length() > 0) { sb.append(" "); // NOI18N sb.append(name); } if (it.hasNext()) { sb.append(", "); // NOI18N } } sb.append(")"); // NOI18N return sb.toString(); } protected String printExceptions() { StringBuffer sb = new StringBuffer(); if (excs.size() > 0) { sb.append(" throws "); // NOI18N for (Iterator it = excs.iterator(); it.hasNext();) { ExcStr ex = (ExcStr) it.next(); sb.append(ex.getName()); if (it.hasNext()) { sb.append(", "); // NOI18N } } } return sb.toString(); } } public static class PackageResultItem extends NbJMIResultItem { private boolean displayFullPackagePath; private JavaPackage pkg; private String pkgName; private static NbJMIPaintComponent.NbPackagePaintComponent pkgComponent = null; public PackageResultItem(JavaPackage pkg, boolean displayFullPackagePath){ this.pkg = pkg; this.displayFullPackagePath = displayFullPackagePath; this.pkgName = pkg.getName(); } public String getItemText() { return displayFullPackagePath ? pkgName : pkgName.substring(pkgName.lastIndexOf('.') + 1); } public Component getPaintComponent(boolean isSelected) { if (pkgComponent == null) { pkgComponent = new NbJMIPaintComponent.NbPackagePaintComponent(); } pkgComponent.setSelected(isSelected); pkgComponent.setPackageName(pkgName); pkgComponent.setDisplayFullPackagePath(displayFullPackagePath); return pkgComponent; } protected Object getAssociatedObject() { return pkg; } } public static class ClassResultItem extends NbJMIResultItem { private JavaClass cls; private boolean isInterface; private boolean isDeprecated; private String name = null; private static NbJMIPaintComponent.NbInterfacePaintComponent interfaceComponent = null; private static NbJMIPaintComponent.NbClassPaintComponent classComponent = null; private static NbJMIPaintComponent.NbEnumPaintComponent enumComponent = null; private static NbJMIPaintComponent.NbAnnotationPaintComponent annotationComponent = null; public ClassResultItem(JavaClass cls, boolean displayFQN){ this.cls = cls; this.name = displayFQN ? cls.getSimpleName() + " (" + cls.getName() + ")" : cls.getSimpleName(); //NOI18N this.isInterface = cls.isInterface(); this.isDeprecated = cls.isDeprecated(); } public String getItemText() { return name; } public Component getPaintComponent(boolean isSelected) { if (cls instanceof AnnotationType) { if (annotationComponent == null) { annotationComponent = new NbJMIPaintComponent.NbAnnotationPaintComponent(); } annotationComponent.setSelected(isSelected); annotationComponent.setDeprecated(isDeprecated); annotationComponent.setFormatClassName(name); return annotationComponent; } else if (cls instanceof JavaEnum) { if (enumComponent == null) { enumComponent = new NbJMIPaintComponent.NbEnumPaintComponent(); } enumComponent.setSelected(isSelected); enumComponent.setDeprecated(isDeprecated); enumComponent.setFormatClassName(name); return enumComponent; } else if (isInterface) { if (interfaceComponent == null) { interfaceComponent = new NbJMIPaintComponent.NbInterfacePaintComponent(); } interfaceComponent.setSelected(isSelected); interfaceComponent.setDeprecated(isDeprecated); interfaceComponent.setFormatClassName(name); return interfaceComponent; } else { if (classComponent == null) { classComponent = new NbJMIPaintComponent.NbClassPaintComponent(); } classComponent.setSelected(isSelected); classComponent.setDeprecated(isDeprecated); classComponent.setFormatClassName(name); return classComponent; } } protected Object getAssociatedObject() { return cls; } } public static class StringResultItem extends NbJMIResultItem { private String str; private static NbJMIPaintComponent.NbStringPaintComponent stringComponent = null; public StringResultItem(String str) { this.str = str; } public String getItemText() { return str; } public Component getPaintComponent(boolean isSelected) { if (stringComponent == null) { stringComponent = new NbJMIPaintComponent.NbStringPaintComponent(); } stringComponent.setSelected(isSelected); stringComponent.setString(str); return stringComponent; } protected Object getAssociatedObject() { return str; } } static class ParamStr { private String type, simpleType, prm; private boolean isVarArg; private Color typeColor; public ParamStr(String type, String simpleType, String prm, boolean isVarArg, Color typeColor) { this.type = type; this.simpleType = simpleType; this.prm = prm; this.isVarArg = isVarArg; this.typeColor = typeColor; } public String getTypeName() { return type; } public String getSimpleTypeName() { return simpleType; } public String getName() { return prm; } public boolean isVarArg() { return isVarArg; } public Color getTypeColor() { return typeColor; } } static class ExcStr { private String name; private Color typeColor; public ExcStr(String name, Color typeColor) { this.name = name; this.typeColor = typeColor; } public String getName() { return name; } public Color getTypeColor() { return typeColor; } } } |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.