【问题标题】:Opening Eclipse editor from python with Py4J使用 Py4J 从 python 打开 Eclipse 编辑器
【发布时间】:2014-09-26 16:56:19
【问题描述】:

我正在尝试通过我的 python 程序在 Eclipse 编辑器中打开一个文件。

以下是如何使用 Java 执行此操作的示例:

import java.io.File;
import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.ide.IDE;

File fileToOpen = new File("externalfile.xml");

if (fileToOpen.exists() && fileToOpen.isFile()) {
    IFileStore fileStore = EFS.getLocalFileSystem().getStore(fileToOpen.toURI());
    IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();

    try {
        IDE.openEditorOnFileStore( page, fileStore );
    } catch ( PartInitException e ) {
        //Put your exception handler here if you wish to
    }
} else {
    //Do something if the file does not exist
}

我正在尝试通过 Py4j 使用 python 中的这个 API。

from py4j.java_gateway import JavaGateway, java_import

gateway = JavaGateway()
jvm = gateway.jvm

java_import(jvm, 'org.eclipse.core.filesystem.EFS')
java_import(jvm, 'org.eclipse.ui.PlatformUI')

fileToOpen = jvm.java.io.File('c:/test.txt')

fileStore = jvm.org.eclipse.core.filesystem.EFS.getLocalFileSystem().getStore(fileToOpen.toURI());

这很好用。但我已经获得了page

page = jvm.org.eclipse.ui.PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();

我在这里没有。看起来鲁道夫·威德曼已经发布了这个问题的答案here。所以java解决方案将是:

Display.getDefault().asyncExec(new Runnable() {
    @Override
    public void run() {
        IWorkbenchWindow iw = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
    }
});

但是我怎样才能从 python 中做到这一点?如何用 Py4j 实现?

【问题讨论】:

  • Java 代码只能在 Eclipse 插件中运行,而不能在“普通”Java 程序中运行。
  • Py4j 有 eclipse 插件,允许程序员与 eclipse API 交互。看看吧:py4j.wordpress.com/2010/02/18/eclipse-py4j

标签: python eclipse py4j


【解决方案1】:

你有没有尝试实现Runnable interface in Python?

class PythonRunner(object):
    def __init__(self, gateway):
        self.gateway = gateway
        self.iw

    def run(self):
        self.iw = gateway.jvm.org.eclipse.ui.PlatformUI.getWorkbench().getActiveWorkbenchWindow()

    class Java:
        implements = ['java.lang.Runnable']

gateway = JavaGateway(start_callback_server=True)
runner = PythonRunner(gateway)
gateway.jvm.org.eclipse.swt.widgets.Display.getDefault().asyncExec(runner)
# Replace with a synchronization primitive
time.sleep(2) 
page = runner.iw.getActivePage()

【讨论】:

    猜你喜欢
    • 2021-12-21
    • 1970-01-01
    • 2016-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-16
    • 1970-01-01
    • 2015-04-09
    相关资源
    最近更新 更多