如何从Eclipse中的活动编辑器获取文本PDE
问题描述:
我有一个处理程序,我想从工作台中的活动编辑器中获取文本。从下面的屏幕截图中,我想获得Test.java中的所有内容(“public class Test ...”)。如何从Eclipse中的活动编辑器获取文本PDE
我的“源”菜单下的成功添加了新的命令。只是不知道现在从哪里获得活动编辑器的文本。这里是我到目前为止,我试图让文本(它只是在一个弹出窗口中显示的文件名):
package generatebuilderproject.handlers;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.jface.dialogs.MessageDialog;
public class GenerateBuilderHandler extends AbstractHandler {
public GenerateBuilderHandler() {
}
public Object execute(ExecutionEvent event) throws ExecutionException {
IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
IEditorPart editorPart = HandlerUtil.getActiveEditor(event);
MessageDialog.openInformation(
window.getShell(),
"GenerateBuilderProject",
editorPart.getEditorInput().getName());
return null;
}
}
答
一旦你的IEditorPart
,你可以尝试以下方法:
IEditorInput input = editorPart.getEditorInput();
if (input instanceof FileEditorInput) {
IFile file = ((FileEditorInput) input).getFile();
InputStream is = file.getContents();
// TODO get contents from InputStream
}
+0
完美 - 谢谢你 – 2013-02-15 16:12:20
可能重复的[Eclipse插件:从编辑器获取内容](http://stackoverflow.com/questions/6661382/eclipse-plugin-get-content-from-editor) – 2015-11-13 15:58:06