* Copyright (C) 2010 Robert Futrell
* robert_futrell at users.sourceforge.net
* http://fifesoft.com/rsyntaxtextarea
* This library is distributed under a modified BSD license. See the included
* RSTALanguageSupport.License.txt file for details.
package org.fife.rsta.ac.java;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.util.Iterator;
import org.fife.rsta.ac.java.buildpath.SourceLocation;
import org.fife.rsta.ac.java.classreader.AccessFlags;
import org.fife.rsta.ac.java.classreader.ClassFile;
import org.fife.rsta.ac.java.rjc.ast.CompilationUnit;
import org.fife.rsta.ac.java.rjc.ast.TypeDeclaration;
import org.fife.ui.autocomplete.Completion;
import org.fife.ui.autocomplete.CompletionProvider;
* Completion for a Java class, interface or enum.
class ClassCompletion extends AbstractJavaSourceCompletion {
public ClassCompletion(CompletionProvider provider, ClassFile cf) {
super(provider, cf.getClassName(false));
* Fixed error when comparing classes of the same name, which did not allow
* classes with same name but different packages.
* Thanks to Guilherme Joao Frantz and Jonatas Schuler for the patch!
public int compareTo(Completion o) {
// Check for classes with same name, but in different packages
else if(o.toString().equalsIgnoreCase(toString())) {
if (o instanceof ClassCompletion) {
ClassCompletion c2 = (ClassCompletion) o;
return getClassName(true).compareTo(c2.getClassName(true));
return super.compareTo(o);
public boolean equals(Object obj) {
return (obj instanceof ClassCompletion) &&
((ClassCompletion)obj).getReplacementText().equals(getReplacementText());
* Returns the name of the class represented by this completion.
* @param fullyQualified Whether the returned name should be fully
* @return The class name.
public String getClassName(boolean fullyQualified){
return cf.getClassName(fullyQualified);
// TODO: Add functionality to ClassFile to make this simpler.
boolean isInterface = false;
boolean isPublic = false;
//boolean isProtected = false;
//boolean isPrivate = false;
boolean isDefault = false;
int access = cf.getAccessFlags();
if ((access&AccessFlags.ACC_INTERFACE)>0) {
else if (org.fife.rsta.ac.java.classreader.Util.isPublic(access)) {
// else if (org.fife.rsta.ac.java.classreader.Util.isProtected(access)) {
// else if (org.fife.rsta.ac.java.classreader.Util.isPrivate(access)) {
IconFactory fact = IconFactory.get();
key = IconFactory.DEFAULT_INTERFACE_ICON;
key = IconFactory.INTERFACE_ICON;
key = IconFactory.DEFAULT_CLASS_ICON;
key = IconFactory.CLASS_ICON;
return fact.getIcon(key, cf.isDeprecated());
* Returns the package this class or interface is in.
* @return The package, or <code>null</code> if it is not in a package.
* @see #getClassName(boolean)
public String getPackageName() {
return cf.getPackageName();
public String getSummary() {
SourceCompletionProvider scp = (SourceCompletionProvider)getProvider();
SourceLocation loc = scp.getSourceLocForClass(cf.getClassName(true));
CompilationUnit cu = Util.getCompilationUnitFromDisk(loc, cf);
for (Iterator i=cu.getTypeDeclarationIterator(); i.hasNext(); ) {
TypeDeclaration td = (TypeDeclaration)i.next();
String typeName = td.getName();
// Avoid inner classes, etc.
if (typeName.equals(cf.getClassName(false))) {
String summary = td.getDocComment();
// Be cautious - might be no doc comment (or a bug?)
if (summary!=null && summary.startsWith("/**")) {
return Util.docCommentToHtml(summary);
// Default to the fully-qualified class name.
return cf.getClassName(true);
public String getToolTipText() {
return "class " + getReplacementText();
return getReplacementText().hashCode();
public void rendererText(Graphics g, int x, int y, boolean selected) {
String s = cf.getClassName(false);
FontMetrics fm = g.getFontMetrics();
int newX = x + fm.stringWidth(s);
int midY = y + fm.getDescent() - fm.getHeight()/2;
g.drawLine(x, midY, newX, midY);
String pkgName = cf.getClassName(true);
int lastIndexOf = pkgName.lastIndexOf('.');
if (lastIndexOf != -1) { // Class may not be in a package
pkgName = pkgName.substring(0, lastIndexOf);
Color origColor = g.getColor();
g.drawString(pkgName, x, y);