简体   繁体   English

使用Py4J从python打开Eclipse编辑器

[英]Opening Eclipse editor from python with Py4J

I'm trying open a file in Eclipse editor from my python program. 我正在尝试从python程序在Eclipse编辑器中打开文件。

Here is example how to do this with Java: 这是使用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
}

I'm trying to use this API from python with use of Py4j. 我正在尝试通过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());

This works fine. 这很好。 But I have stacked with getting page . 但是我堆积了获取page

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

I'm getting None here. 我在这里什么都没有。 Looks like Rudolf Widmann have posted answer for this question here . 看起来Rudolf Widmann 在这里发布了此问题的答案。 So the java solution will be: 因此,java解决方案将是:

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

But how can I do this from python? 但是如何从python做到这一点? How to implement it with Py4j? 如何用Py4j实现它?

Did you try to implement the Runnable interface in Python? 您是否尝试在Python中实现Runnable接口?

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM