1 11 package org.eclipse.pde.internal.ui.search.dependencies; 12 13 import java.util.HashMap ; 14 15 import org.eclipse.core.resources.IFile; 16 import org.eclipse.core.resources.IMarker; 17 import org.eclipse.core.runtime.CoreException; 18 import org.eclipse.jdt.core.IJavaElement; 19 import org.eclipse.jdt.core.JavaModelException; 20 import org.eclipse.jdt.ui.JavaUI; 21 import org.eclipse.search.ui.NewSearchUI; 22 import org.eclipse.search.ui.text.Match; 23 import org.eclipse.ui.IEditorPart; 24 import org.eclipse.ui.PartInitException; 25 import org.eclipse.ui.ide.IDE; 26 import org.eclipse.ui.texteditor.ITextEditor; 27 28 29 public class JavaEditorOpener { 30 31 public static IEditorPart open(Match match, int offset, int length, boolean activate) throws PartInitException, JavaModelException { 32 IEditorPart editor = null; 33 Object element = match.getElement(); 34 if (element instanceof IJavaElement) { 35 editor = JavaUI.openInEditor((IJavaElement)element); 36 } 37 if (editor != null && activate) 38 editor.getEditorSite().getPage().activate(editor); 39 if (editor instanceof ITextEditor) { 40 ITextEditor textEditor= (ITextEditor) editor; 41 textEditor.selectAndReveal(offset, length); 42 } else if (editor != null){ 43 if (element instanceof IFile) { 44 IFile file= (IFile) element; 45 showWithMarker(editor, file, offset, length); 46 } 47 } 48 return editor; 49 } 50 51 private static void showWithMarker(IEditorPart editor, IFile file, int offset, int length) throws PartInitException { 52 try { 53 IMarker marker= file.createMarker(NewSearchUI.SEARCH_MARKER); 54 HashMap attributes= new HashMap (4); 55 attributes.put(IMarker.CHAR_START, new Integer (offset)); 56 attributes.put(IMarker.CHAR_END, new Integer (offset + length)); 57 marker.setAttributes(attributes); 58 IDE.gotoMarker(editor, marker); 59 marker.delete(); 60 } catch (CoreException e) { 61 } 62 } 63 64 } 65 | Popular Tags |