| 1 | /******************************************************************************* |
|---|---|
| 2 | * Copyright (c) 2000, 2007 IBM Corporation and others. |
| 3 | * |
| 4 | * This program and the accompanying materials |
| 5 | * are made available under the terms of the Eclipse Public License 2.0 |
| 6 | * which accompanies this distribution, and is available at |
| 7 | * https://www.eclipse.org/legal/epl-2.0/ |
| 8 | * |
| 9 | * SPDX-License-Identifier: EPL-2.0 |
| 10 | * |
| 11 | * Contributors: |
| 12 | * IBM Corporation - initial API and implementation |
| 13 | *******************************************************************************/ |
| 14 | package org.eclipse.jdt.astview; |
| 15 | |
| 16 | |
| 17 | import org.eclipse.ui.IEditorInput; |
| 18 | import org.eclipse.ui.IEditorPart; |
| 19 | import org.eclipse.ui.IWorkbenchPage; |
| 20 | import org.eclipse.ui.IWorkbenchWindow; |
| 21 | import org.eclipse.ui.PlatformUI; |
| 22 | |
| 23 | import org.eclipse.ui.texteditor.ITextEditor; |
| 24 | |
| 25 | import org.eclipse.jdt.core.IJavaElement; |
| 26 | import org.eclipse.jdt.core.ITypeRoot; |
| 27 | |
| 28 | import org.eclipse.jdt.ui.JavaUI; |
| 29 | |
| 30 | /** |
| 31 | * |
| 32 | */ |
| 33 | public class EditorUtility { |
| 34 | private EditorUtility() { |
| 35 | super(); |
| 36 | } |
| 37 | |
| 38 | public static IEditorPart getActiveEditor() { |
| 39 | IWorkbenchWindow window= PlatformUI.getWorkbench().getActiveWorkbenchWindow(); |
| 40 | if (window != null) { |
| 41 | IWorkbenchPage page= window.getActivePage(); |
| 42 | if (page != null) { |
| 43 | return page.getActiveEditor(); |
| 44 | } |
| 45 | } |
| 46 | return null; |
| 47 | } |
| 48 | |
| 49 | public static ITypeRoot getJavaInput(IEditorPart part) { |
| 50 | IEditorInput editorInput= part.getEditorInput(); |
| 51 | if (editorInput != null) { |
| 52 | IJavaElement input= JavaUI.getEditorInputJavaElement(editorInput); |
| 53 | if (input instanceof ITypeRoot) { |
| 54 | return (ITypeRoot) input; |
| 55 | } |
| 56 | } |
| 57 | return null; |
| 58 | } |
| 59 | |
| 60 | public static void selectInEditor(ITextEditor editor, int offset, int length) { |
| 61 | IEditorPart active = getActiveEditor(); |
| 62 | if (active != editor) { |
| 63 | editor.getSite().getPage().activate(editor); |
| 64 | } |
| 65 | editor.selectAndReveal(offset, length); |
| 66 | } |
| 67 | } |
| 68 |
Members