Created
November 7, 2013 09:43
-
-
Save zxkane/7351924 to your computer and use it in GitHub Desktop.
open a file from Eclipse project after triggering a command on a Java method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.windriver.installer.testtool.handlers; | |
import java.io.File; | |
import org.eclipse.core.commands.AbstractHandler; | |
import org.eclipse.core.commands.ExecutionEvent; | |
import org.eclipse.core.commands.ExecutionException; | |
import org.eclipse.core.filesystem.EFS; | |
import org.eclipse.core.filesystem.IFileStore; | |
import org.eclipse.core.resources.IProject; | |
import org.eclipse.core.resources.IResource; | |
import org.eclipse.core.runtime.IPath; | |
import org.eclipse.ui.IEditorInput; | |
import org.eclipse.ui.IEditorPart; | |
import org.eclipse.ui.IFileEditorInput; | |
import org.eclipse.ui.IWorkbenchPage; | |
import org.eclipse.ui.IWorkbenchWindow; | |
import org.eclipse.ui.PartInitException; | |
import org.eclipse.ui.PlatformUI; | |
import org.eclipse.ui.handlers.HandlerUtil; | |
import org.eclipse.ui.ide.IDE; | |
import org.eclipse.ui.texteditor.ITextEditor; | |
import org.eclipse.jdt.core.ICompilationUnit; | |
import org.eclipse.jdt.core.IJavaElement; | |
import org.eclipse.jdt.core.JavaModelException; | |
import org.eclipse.jdt.ui.JavaUI; | |
import org.eclipse.jface.dialogs.MessageDialog; | |
import org.eclipse.jface.text.ITextSelection; | |
/** | |
* Our sample handler extends AbstractHandler, an IHandler base class. | |
* | |
* @see org.eclipse.core.commands.IHandler | |
* @see org.eclipse.core.commands.AbstractHandler | |
*/ | |
public class TestResultHandler extends AbstractHandler { | |
/** | |
* The constructor. | |
*/ | |
public TestResultHandler() { | |
} | |
IResource extractResource(IEditorPart editor) { | |
IEditorInput input = editor.getEditorInput(); | |
if (!(input instanceof IFileEditorInput)) | |
return null; | |
return ((IFileEditorInput)input).getFile(); | |
} | |
/** | |
* the command has been executed, so extract extract the needed information | |
* from the application context. | |
*/ | |
public Object execute(ExecutionEvent event) throws ExecutionException { | |
ITextEditor editor = (ITextEditor) PlatformUI.getWorkbench() | |
.getActiveWorkbenchWindow().getActivePage().getActiveEditor(); | |
if (editor != null) { | |
ITextSelection selection = (ITextSelection) editor | |
.getSelectionProvider().getSelection(); | |
IEditorInput editorInput = editor.getEditorInput(); | |
IJavaElement elem = JavaUI.getEditorInputJavaElement(editorInput); | |
if (elem instanceof ICompilationUnit) { | |
ICompilationUnit unit = (ICompilationUnit) elem; | |
try { | |
IJavaElement selected = unit.getElementAt(selection | |
.getOffset()); | |
if (selected != null) { | |
String methodName = selected.getElementName(); | |
IProject project = extractResource(editor).getProject(); | |
File rtFile = new File(project.getLocation().toFile(), methodName + ".txt"); | |
if (rtFile.exists()) { | |
IFileStore fileStore = EFS.getLocalFileSystem().getStore(rtFile.toURI()); | |
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); | |
try { | |
IDE.openEditorOnFileStore( page, fileStore ); | |
} catch ( PartInitException e ) { | |
//Put your exception handler here if you wish to | |
} | |
} | |
} | |
} catch (JavaModelException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
} | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment