1 19 package org.netbeans.api.java.source; 20 21 import com.sun.source.tree.ClassTree; 22 import com.sun.source.tree.CompilationUnitTree; 23 import com.sun.source.tree.MethodTree; 24 import com.sun.source.tree.Tree; 25 import com.sun.source.tree.VariableTree; 26 import com.sun.source.util.TreePath; 27 import com.sun.source.util.TreePathScanner; 28 import com.sun.tools.javac.util.Context; 29 import java.io.IOException ; 30 import java.util.Collection ; 31 import javax.lang.model.element.Element; 32 import javax.lang.model.element.ElementKind; 33 import javax.lang.model.element.Modifier; 34 import javax.swing.Icon ; 35 import javax.swing.text.StyledDocument ; 36 import org.netbeans.modules.java.source.pretty.VeryPretty; 37 import org.netbeans.modules.java.ui.Icons; 38 import org.openide.ErrorManager; 39 import org.openide.cookies.EditorCookie; 40 import org.openide.cookies.LineCookie; 41 import org.openide.cookies.OpenCookie; 42 import org.openide.filesystems.FileObject; 43 import org.openide.loaders.DataObject; 44 import org.openide.text.Line; 45 import org.openide.text.NbDocument; 46 47 54 public final class UiUtils { 55 56 private UiUtils() {} 57 58 61 public static Icon getElementIcon( ElementKind elementKind, Collection <Modifier> modifiers ) { 62 return Icons.getElementIcon(elementKind, modifiers); 63 } 64 65 @Deprecated 67 public static Icon getDeclarationIcon(Element element) { 68 return getElementIcon(element.getKind(), element.getModifiers()); 69 } 70 71 72 80 public static boolean open(final ClasspathInfo cpInfo, final Element el) { 81 Object [] openInfo = getOpenInfo (cpInfo, el); 82 if (openInfo != null) { 83 assert openInfo[0] instanceof FileObject; 84 assert openInfo[1] instanceof Integer ; 85 return doOpen((FileObject)openInfo[0],(Integer )openInfo[1]); 86 } 87 return false; 88 } 89 90 public static boolean open(final FileObject toSearch, final ElementHandle<? extends Element> toOpen) { 91 if (toSearch == null || toOpen == null) { 92 throw new IllegalArgumentException ("null not supported"); 93 } 94 95 Object [] openInfo = getOpenInfo (toSearch, toOpen); 96 if (openInfo != null) { 97 assert openInfo[0] instanceof FileObject; 98 assert openInfo[1] instanceof Integer ; 99 return doOpen((FileObject)openInfo[0],(Integer )openInfo[1]); 100 } 101 return false; 102 } 103 104 private static String getMethodHeader(MethodTree tree, CompilationInfo info, String s) { 105 Context context = info.getJavacTask().getContext(); 106 VeryPretty veryPretty = new VeryPretty(context); 107 return veryPretty.getMethodHeader(tree, s); 108 } 109 110 private static String getClassHeader(ClassTree tree, CompilationInfo info, String s) { 111 Context context = info.getJavacTask().getContext(); 112 VeryPretty veryPretty = new VeryPretty(context); 113 return veryPretty.getClassHeader(tree, s); 114 } 115 private static String getVariableHeader(VariableTree tree, CompilationInfo info, String s) { 116 Context context = info.getJavacTask().getContext(); 117 VeryPretty veryPretty = new VeryPretty(context); 118 return veryPretty.getVariableHeader(tree, s); 119 } 120 121 public static final class PrintPart { 122 private PrintPart() {} 123 public static final String ANNOTATIONS = "%annotations"; public static final String NAME = "%name%"; public static final String TYPE = "%type%"; public static final String THROWS = "%throws%"; public static final String IMPLEMENTS = "%implements%"; public static final String EXTENDS = "%extends%"; public static final String TYPEPARAMETERS = "%typeparameters%"; public static final String FLAGS = "%flags%"; public static final String PARAMETERS = "%parameters%"; } 133 134 138 public static String getHeader(TreePath treePath, CompilationInfo info, String formatString) { 139 assert info != null; 140 assert treePath != null; 141 Element element = info.getTrees().getElement(treePath); 142 if (element!=null) 143 return getHeader(element, info, formatString); 144 return null; 145 } 146 147 151 public static String getHeader(Element element, CompilationInfo info, String formatString) { 152 assert element != null; 153 assert info != null; 154 assert formatString != null; 155 Tree tree = SourceUtils.treeFor(info, element); 156 if (tree != null) { 157 if (tree.getKind() == Tree.Kind.METHOD) { 158 return getMethodHeader((MethodTree) tree, info, formatString); 159 } else if (tree.getKind() == Tree.Kind.CLASS) { 160 return getClassHeader((ClassTree)tree, info, formatString); 161 } else if (tree.getKind() == Tree.Kind.VARIABLE) { 162 return getVariableHeader((VariableTree)tree, info, formatString); 163 } 164 } 165 return formatString.replaceAll(PrintPart.NAME, element.getSimpleName().toString()).replaceAll("%[a-z]*%", ""); } 167 168 176 public @Deprecated static boolean open(final FileObject fo, final int offset) { 177 return doOpen(fo, offset); 178 } 179 180 static Object [] getOpenInfo (final ClasspathInfo cpInfo, final Element el) { 181 FileObject fo = SourceUtils.getFile(el, cpInfo); 182 if (fo != null) { 183 return getOpenInfo(fo, ElementHandle.create(el)); 184 } else { 185 return null; 186 } 187 } 188 189 static Object [] getOpenInfo(final FileObject fo, final ElementHandle<? extends Element> handle) { 190 assert fo != null; 191 192 try { 193 int offset = getOffset(fo, handle); 194 return new Object [] {fo, offset}; 195 } catch (IOException e) { 196 ErrorManager.getDefault().notify(e); 197 return null; 198 } 199 } 200 201 203 public static int getDistance(String s, String t) { 204 int d[][]; int n; int m; int i; int j; char s_i; char t_j; int cost; 213 215 n = s.length (); 216 m = t.length (); 217 if (n == 0) { 218 return m; 219 } 220 if (m == 0) { 221 return n; 222 } 223 d = new int[n+1][m+1]; 224 225 227 for (i = 0; i <= n; i++) { 228 d[i][0] = i; 229 } 230 231 for (j = 0; j <= m; j++) { 232 d[0][j] = j; 233 } 234 235 237 for (i = 1; i <= n; i++) { 238 239 s_i = s.charAt (i - 1); 240 241 243 for (j = 1; j <= m; j++) { 244 245 t_j = t.charAt (j - 1); 246 247 249 if (s_i == t_j) { 250 cost = 0; 251 } 252 else { 253 cost = 1; 254 } 255 256 d[i][j] = min (d[i-1][j]+1, d[i][j-1]+1, d[i-1][j-1] + cost); 258 259 } 260 261 } 262 263 265 return d[n][m]; 266 } 267 268 private static int min (int a, int b, int c) { 269 int mi; 270 271 mi = a; 272 if (b < mi) { 273 mi = b; 274 } 275 if (c < mi) { 276 mi = c; 277 } 278 return mi; 279 280 } 281 282 284 private static boolean doOpen(FileObject fo, int offset) { 285 try { 286 DataObject od = DataObject.find(fo); 287 EditorCookie ec = (EditorCookie) od.getCookie(EditorCookie.class); 288 LineCookie lc = (LineCookie) od.getCookie(LineCookie.class); 289 290 if (ec != null && lc != null && offset != -1) { 291 StyledDocument doc = ec.openDocument(); 292 if (doc != null) { 293 int line = NbDocument.findLineNumber(doc, offset); 294 int lineOffset = NbDocument.findLineOffset(doc, line); 295 int column = offset - lineOffset; 296 297 if (line != -1) { 298 Line l = lc.getLineSet().getCurrent(line); 299 300 if (l != null) { 301 l.show(Line.SHOW_GOTO, column); 302 return true; 303 } 304 } 305 } 306 } 307 308 OpenCookie oc = (OpenCookie) od.getCookie(OpenCookie.class); 309 310 if (oc != null) { 311 oc.open(); 312 return true; 313 } 314 } catch (IOException e) { 315 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 316 } 317 318 return false; 319 } 320 321 private static int getOffset(FileObject fo, final ElementHandle<? extends Element> handle) throws IOException { 322 final int[] result = new int[] {-1}; 323 324 325 JavaSource js = JavaSource.forFileObject(fo); 326 js.runUserActionTask(new CancellableTask<CompilationController>() { 327 328 public void cancel() { 329 } 330 331 public void run(CompilationController info) { 332 try { 333 info.toPhase(JavaSource.Phase.RESOLVED); 334 } catch (IOException ioe) { 335 ErrorManager.getDefault().notify(ioe); 336 } 337 Element el = handle.resolve(info); 338 if (el == null) 339 throw new IllegalArgumentException (); 340 341 FindDeclarationVisitor v = new FindDeclarationVisitor(el, info); 342 343 CompilationUnitTree cu = info.getCompilationUnit(); 344 345 v.scan(cu, null); 346 Tree elTree = v.declTree; 347 348 if (elTree != null) 349 result[0] = (int)info.getTrees().getSourcePositions().getStartPosition(cu, elTree); 350 } 351 },true); 352 return result[0]; 353 } 354 355 357 private static class FindDeclarationVisitor extends TreePathScanner<Void , Void > { 358 359 private Element element; 360 private Tree declTree; 361 private CompilationInfo info; 362 363 public FindDeclarationVisitor(Element element, CompilationInfo info) { 364 this.element = element; 365 this.info = info; 366 } 367 368 @Override 369 public Void visitClass(ClassTree tree, Void d) { 370 handleDeclaration(); 371 super.visitClass(tree, d); 372 return null; 373 } 374 375 @Override 376 public Void visitMethod(MethodTree tree, Void d) { 377 handleDeclaration(); 378 super.visitMethod(tree, d); 379 return null; 380 } 381 382 @Override 383 public Void visitVariable(VariableTree tree, Void d) { 384 handleDeclaration(); 385 super.visitVariable(tree, d); 386 return null; 387 } 388 389 public void handleDeclaration() { 390 Element found = info.getTrees().getElement(getCurrentPath()); 391 392 if ( element.equals( found ) ) { 393 declTree = getCurrentPath().getLeaf(); 394 } 395 } 396 397 } 398 399 422 423 424 } 425 | Popular Tags |