1

I'm trying open a file in Eclipse editor from my python program.

Here is example how to do this with 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.

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 = jvm.org.eclipse.ui.PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();

I'm getting None here. Looks like Rudolf Widmann have posted answer for this question here. So the java solution will be:

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

But how can I do this from python? How to implement it with Py4j?

Community
  • 1
  • 1
Adam
  • 2,254
  • 3
  • 24
  • 42
  • That Java code will only work in an Eclipse plugin, it won't work in a 'normal' Java program. – greg-449 Sep 26 '14 at 17:00
  • Py4j have eclipse plugin which allow programmer to interact with eclipse API. Just take a look: http://py4j.wordpress.com/2010/02/18/eclipse-py4j/ – Adam Sep 26 '14 at 17:04

1 Answers1

0

Did you try to implement the 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()
Barthelemy
  • 8,277
  • 6
  • 33
  • 36