1 11 package org.eclipse.jdt.internal.ui.javadocexport; 12 13 import org.eclipse.core.runtime.IPath; 14 import org.eclipse.core.runtime.Path; 15 16 import org.eclipse.core.resources.IFile; 17 import org.eclipse.core.resources.ResourcesPlugin; 18 19 import org.eclipse.jface.text.BadLocationException; 20 import org.eclipse.jface.text.IDocument; 21 import org.eclipse.jface.text.IRegion; 22 23 import org.eclipse.ui.IEditorPart; 24 import org.eclipse.ui.PartInitException; 25 import org.eclipse.ui.texteditor.ITextEditor; 26 27 import org.eclipse.ui.console.IHyperlink; 28 29 import org.eclipse.debug.ui.console.IConsole; 30 import org.eclipse.debug.ui.console.IConsoleLineTracker; 31 32 import org.eclipse.jdt.core.IJavaElement; 33 import org.eclipse.jdt.core.JavaCore; 34 import org.eclipse.jdt.core.JavaModelException; 35 36 import org.eclipse.jdt.ui.JavaUI; 37 38 import org.eclipse.jdt.internal.ui.JavaPlugin; 39 40 public class JavadocConsoleLineTracker implements IConsoleLineTracker { 41 42 private static class JavadocConsoleHyperLink implements IHyperlink { 43 44 private IPath fExternalPath; 45 private int fLineNumber; 46 47 public JavadocConsoleHyperLink(IPath externalPath, int lineNumber) { 48 fExternalPath= externalPath; 49 fLineNumber= lineNumber; 50 } 51 52 55 public void linkEntered() { 56 } 57 58 61 public void linkExited() { 62 } 63 64 67 public void linkActivated() { 68 try { 69 IFile[] files= ResourcesPlugin.getWorkspace().getRoot().findFilesForLocation(fExternalPath); 70 if (files.length > 0) { 71 for (int i = 0; i < files.length; i++) { 72 IFile curr= files[0]; 73 IJavaElement element= JavaCore.create(curr); 74 if (element != null && element.exists()) { 75 IEditorPart part= JavaUI.openInEditor(element, true, false); 76 if (part instanceof ITextEditor) { 77 revealLine((ITextEditor) part, fLineNumber); 78 } 79 return; 80 } 81 } 82 } 83 } catch (BadLocationException e) { 84 JavaPlugin.log(e); 85 } catch (PartInitException e) { 86 JavaPlugin.log(e); 87 } catch (JavaModelException e) { 88 JavaPlugin.log(e); 89 } 90 } 91 92 private void revealLine(ITextEditor editor, int lineNumber) throws BadLocationException { 93 IDocument document= editor.getDocumentProvider().getDocument(editor.getEditorInput()); 94 IRegion region= document.getLineInformation(lineNumber - 1); 95 editor.selectAndReveal(region.getOffset(), 0); 96 } 97 98 } 99 100 101 private IConsole fConsole; 102 103 106 public JavadocConsoleLineTracker() { 107 } 108 109 112 public void init(IConsole console) { 113 fConsole= console; 114 } 115 116 119 public void lineAppended(IRegion line) { 120 try { 121 int offset = line.getOffset(); 122 int length = line.getLength(); 123 String text = fConsole.getDocument().get(offset, length); 124 125 int index1= text.indexOf(':'); 126 if (index1 == -1) { 127 return; 128 } 129 130 int lineNumber= -1; 131 IPath path= null; 132 int index2= text.indexOf(':', index1 + 1); 133 while ((index2 != -1) && (path == null)) { 134 if (index1 < index2) { 135 try { 136 String substr= text.substring(index1 + 1, index2); 137 lineNumber= Integer.parseInt(substr); 138 path= Path.fromOSString(text.substring(0, index1)); 139 } catch (NumberFormatException e) { 140 } 142 } 143 index1= index2; 144 index2= text.indexOf(':', index1 + 1); 145 } 146 147 if (lineNumber != -1) { 148 JavadocConsoleHyperLink link= new JavadocConsoleHyperLink(path, lineNumber); 149 fConsole.addLink(link, line.getOffset(), index1); 150 151 } 152 } catch (BadLocationException e) { 153 } 155 } 156 157 158 159 162 public void dispose() { 163 fConsole = null; 164 } 165 166 } 167 | Popular Tags |