1 19 20 package org.netbeans.modules.java.actions; 21 22 import java.io.IOException ; 23 import java.util.logging.Logger ; 24 import javax.lang.model.element.Element; 25 import javax.lang.model.element.TypeElement; 26 import javax.swing.Icon ; 27 import org.netbeans.api.java.source.CancellableTask; 28 import org.netbeans.api.java.source.ClasspathInfo; 29 import org.netbeans.api.java.source.CompilationController; 30 import org.netbeans.api.java.source.ElementHandle; 31 import org.netbeans.api.java.source.JavaSource; 32 import org.netbeans.api.java.source.UiUtils; 33 import org.netbeans.modules.java.ui.Icons; 34 import org.openide.filesystems.FileObject; 35 import org.openide.filesystems.FileUtil; 36 import org.openide.util.Exceptions; 37 38 42 class TypeDescription implements Comparable <TypeDescription> { 43 44 private static final String EMPTY_STRING = ""; 46 private Icon icon; 47 48 private final GoToTypeAction.CacheItem cacheItem; 49 50 private final ElementHandle<TypeElement> handle; 51 private String simpleName; 52 private String outerName; 53 private String packageName; 54 55 public TypeDescription(GoToTypeAction.CacheItem cacheItem, final ElementHandle<TypeElement> handle ) { 56 this.cacheItem = cacheItem; 57 this.handle = handle; 58 init(); 59 } 60 61 public void open() { 62 if ( cacheItem.isBinary() ) { 63 final ClasspathInfo ci = ClasspathInfo.create(cacheItem.getRoot()); 64 JavaSource js = JavaSource.create( ci ); 65 final ElementHandle<TypeElement> eh = handle; 66 final Element[] el = new Element[1]; 67 try { 68 js.runUserActionTask(new CancellableTask<CompilationController>() { 69 70 public void cancel() { 71 } 72 73 public void run(CompilationController info) { 74 el[0] = eh.resolve (info); 75 UiUtils.open(ci, el[0]); 76 } 77 78 }, true); 79 } 80 catch( IOException e ) { 81 Logger.getLogger(TypeDescription.class.getName()).info("Source not found: " + eh.getBinaryName()); 82 Exceptions.printStackTrace(e); 83 } 84 } 85 else { 86 FileObject folder = cacheItem.getRoot().getFileObject(packageName.replace(".", "/")); if (folder != null) { 89 FileObject[] ch = folder.getChildren(); 90 String name = outerName == null ? simpleName : outerName; int lastDot = name.indexOf('.'); if ( lastDot != -1 ) { 93 name = name.substring(0, lastDot ); 94 } 95 for (FileObject fileObject : ch) { 96 if ( name.equals( fileObject.getName() ) && 97 "java".equals( fileObject.getExt().toLowerCase() ) ) { 98 UiUtils.open(fileObject, handle); 99 } 100 } 101 } 102 else { 103 Logger.getLogger(TypeDescription.class.getName()).info("Package " + packageName +" doesn't exist in root: " + FileUtil.getFileDisplayName(cacheItem.getRoot())); 104 } 105 } 106 } 107 108 public String getSimpleName() { 109 return simpleName; 110 } 111 112 public FileObject getFileObject() { 113 return cacheItem.getRoot(); 114 } 115 116 public String getTypeName() { 117 StringBuilder sb = new StringBuilder ( simpleName ); 118 if( outerName != null ) { 119 sb.append(" in ").append( outerName ); 120 } 121 return sb.toString(); 122 } 123 124 public String getPackageName() { 125 StringBuilder sb = new StringBuilder (); 126 sb.append( " (").append( packageName == null ? "Default Package" : packageName).append(")"); 127 return sb.toString(); 128 129 130 } 131 132 public String getProjectName() { 133 String projectName = cacheItem.getProjectName(); 134 return projectName == null ? "" : projectName; } 136 137 public Icon getProjectIcon() { 138 return cacheItem.getProjectIcon(); 139 } 140 141 private void init() { 142 final String typeName = this.handle.getBinaryName(); 143 int lastDot = typeName.lastIndexOf('.'); int lastDollar = typeName.lastIndexOf('$'); if ( lastDot == -1 ) { 146 if ( lastDollar == -1 ) { 147 simpleName = typeName; 148 } 149 else { 150 simpleName = typeName.substring(lastDollar + 1); 151 outerName = typeName.substring(0, lastDollar ).replace( '$', '.'); } 153 } 154 else { 155 packageName = typeName.substring( 0, lastDot ); 156 157 if ( lastDollar == -1 ) { 158 simpleName = typeName.substring( lastDot + 1 ).replace( '$', '.'); } 160 else { 161 simpleName = typeName.substring(lastDollar + 1); 162 outerName = typeName.substring(lastDot + 1, lastDollar ).replace( '$', '.'); } 164 165 } 166 icon = Icons.getElementIcon (handle.getKind(), null); 167 } 168 169 public String toString() { 170 171 StringBuilder sb = new StringBuilder ( simpleName ); 172 if( outerName != null ) { 173 sb.append(" in ").append( outerName ); 174 } 175 sb.append( " (").append( packageName == null ? "Default Package" : packageName).append(")"); 176 if (cacheItem.getProjectName() != null ) { 177 sb.append( " [").append( cacheItem.getProjectName()).append("]"); 178 } 179 180 return sb.toString(); 181 } 182 183 184 public int compareTo( TypeDescription td ) { 185 int cmpr = compareStrings( simpleName, td.simpleName ); 186 if ( cmpr != 0 ) { 187 return cmpr; 188 } 189 cmpr = compareStrings( outerName, td.outerName ); 190 if ( cmpr != 0 ) { 191 return cmpr; 192 } 193 return compareStrings( packageName, td.packageName ); 194 } 195 196 public synchronized Icon getIcon() { 197 return icon; 198 } 199 200 private int compareStrings(String s1, String s2) { 201 if( s1 == null ) { 202 s1 = EMPTY_STRING; 203 } 204 if ( s2 == null ) { 205 s2 = EMPTY_STRING; 206 } 207 return s1.compareTo( s2 ); 208 } 209 210 211 } 212 | Popular Tags |