1 11 package org.eclipse.debug.ui.console; 12 13 14 import org.eclipse.core.resources.IFile; 15 import org.eclipse.core.runtime.CoreException; 16 import org.eclipse.core.runtime.content.IContentDescription; 17 import org.eclipse.core.runtime.content.IContentType; 18 import org.eclipse.debug.internal.ui.DebugUIPlugin; 19 import org.eclipse.jface.text.BadLocationException; 20 import org.eclipse.jface.text.IDocument; 21 import org.eclipse.jface.text.IRegion; 22 import org.eclipse.ui.IEditorDescriptor; 23 import org.eclipse.ui.IEditorInput; 24 import org.eclipse.ui.IEditorPart; 25 import org.eclipse.ui.IEditorRegistry; 26 import org.eclipse.ui.IWorkbench; 27 import org.eclipse.ui.IWorkbenchPage; 28 import org.eclipse.ui.IWorkbenchWindow; 29 import org.eclipse.ui.PartInitException; 30 import org.eclipse.ui.part.FileEditorInput; 31 import org.eclipse.ui.texteditor.IDocumentProvider; 32 import org.eclipse.ui.texteditor.ITextEditor; 33 34 42 public class FileLink implements IConsoleHyperlink { 43 44 private IFile fFile; 45 private int fFileOffset; 46 private int fFileLength; 47 private int fFileLineNumber; 48 private String fEditorId; 49 50 62 public FileLink(IFile file, String editorId, int fileOffset, int fileLength, int fileLineNumber) { 63 fFile = file; 64 fFileOffset = fileOffset; 65 fFileLength = fileLength; 66 fFileLineNumber = fileLineNumber; 67 fEditorId = editorId; 68 } 69 70 73 public void linkActivated() { 74 IWorkbenchWindow window = DebugUIPlugin.getActiveWorkbenchWindow(); 75 if (window != null) { 76 IWorkbenchPage page = window.getActivePage(); 77 if (page != null) { 78 try { 79 IEditorPart editorPart = page.openEditor(new FileEditorInput(fFile), getEditorId() , false); 80 if (fFileLineNumber > 0) { 81 ITextEditor textEditor = null; 82 if (editorPart instanceof ITextEditor) { 83 textEditor = (ITextEditor) editorPart; 84 } else { 85 textEditor = (ITextEditor) editorPart.getAdapter(ITextEditor.class); 86 } 87 if (textEditor != null) { 88 IEditorInput input = editorPart.getEditorInput(); 89 if (fFileOffset < 0) { 90 IDocumentProvider provider = textEditor.getDocumentProvider(); 91 try { 92 provider.connect(input); 93 } catch (CoreException e) { 94 DebugUIPlugin.log(e); 96 return; 97 } 98 IDocument document = provider.getDocument(input); 99 try { 100 IRegion region= document.getLineInformation(fFileLineNumber - 1); 101 fFileOffset = region.getOffset(); 102 fFileLength = region.getLength(); 103 } catch (BadLocationException e) { 104 DebugUIPlugin.log(e); 106 } 107 provider.disconnect(input); 108 } 109 if (fFileOffset >= 0 && fFileLength >=0) { 110 textEditor.selectAndReveal(fFileOffset, fFileLength); 111 } 112 } 113 } 114 } catch (PartInitException e) { 115 DebugUIPlugin.log(e); 116 } 117 } 118 } 119 } 120 121 124 public void linkEntered() { 125 } 126 127 130 public void linkExited() { 131 } 132 133 private String getEditorId() { 134 if (fEditorId == null) { 135 IWorkbench workbench= DebugUIPlugin.getDefault().getWorkbench(); 136 IEditorDescriptor desc = workbench.getEditorRegistry().getDefaultEditor(fFile.getName(), getFileContentType()); 138 if (desc == null) { 139 desc= workbench.getEditorRegistry().findEditor(IEditorRegistry.SYSTEM_EXTERNAL_EDITOR_ID); 141 } 142 fEditorId= desc.getId(); 143 } 144 return fEditorId; 145 } 146 147 private IContentType getFileContentType() { 148 try { 149 IContentDescription description= fFile.getContentDescription(); 150 if (description != null) { 151 return description.getContentType(); 152 } 153 } catch (CoreException e) { 154 } 155 return null; 156 } 157 } 158 | Popular Tags |