1 8 9 package de.loskutov.bco.ui; 10 11 import org.eclipse.jdt.core.IJavaElement; 12 import org.eclipse.jface.text.ITextSelection; 13 import org.eclipse.jface.viewers.ISelection; 14 import org.eclipse.jface.viewers.ISelectionProvider; 15 import org.eclipse.ui.IEditorInput; 16 import org.eclipse.ui.IEditorPart; 17 import org.eclipse.ui.IWorkbenchPage; 18 import org.eclipse.ui.IWorkbenchWindow; 19 import org.eclipse.ui.texteditor.ITextEditor; 20 21 import de.loskutov.bco.BytecodeOutlinePlugin; 22 23 26 public class EclipseUtils { 27 28 31 private EclipseUtils() { 32 super(); 33 } 34 35 38 public static IEditorPart getActiveEditor() { 39 IWorkbenchWindow window = BytecodeOutlinePlugin.getDefault() 40 .getWorkbench().getActiveWorkbenchWindow(); 41 if (window != null) { 42 IWorkbenchPage page = window.getActivePage(); 43 if (page != null) { 44 return page.getActiveEditor(); 45 } 46 } 47 return null; 48 } 49 50 54 public static IJavaElement getJavaInput(IEditorPart part) { 55 IEditorInput editorInput = part.getEditorInput(); 56 if (editorInput != null) { 57 IJavaElement input = (IJavaElement) editorInput 58 .getAdapter(IJavaElement.class); 59 return input; 60 } 61 return null; 62 } 63 64 69 public static void selectInEditor(ITextEditor editor, int offset, int length) { 70 IEditorPart active = getActiveEditor(); 71 if (active != editor) { 72 editor.getSite().getPage().activate(editor); 73 } 74 editor.selectAndReveal(offset, length); 75 } 76 77 81 public static ITextSelection getSelection( 82 ISelectionProvider selectionProvider) { 83 ISelection selection = selectionProvider.getSelection(); 84 if (selection instanceof ITextSelection) { 85 return (ITextSelection) selection; 86 } 87 return null; 88 } 89 90 94 public static String getJavaPackageName(IJavaElement resource) { 95 String name = resource == null 96 ? null : resource.getElementName(); if (name == null) { 98 return ""; } 100 int type = resource.getElementType(); 101 if (type == IJavaElement.PACKAGE_FRAGMENT 102 || type == IJavaElement.PACKAGE_FRAGMENT_ROOT) { 103 return name; 104 } 105 IJavaElement ancestor = resource 106 .getAncestor(IJavaElement.PACKAGE_FRAGMENT); 107 if (ancestor != null) { 108 return ancestor.getElementName(); 109 } 110 return ""; } 112 113 } | Popular Tags |