2014-09-26 1 views
1

Python 프로그램에서 Eclipse 편집기로 파일을 열려고합니다. 내가 Py4j의 사용과 파이썬에서이 API를 사용하기 위해 노력하고있어Python에서 Python의 Eclipse 편집기 열기

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 
} 

: 여기

자바와 함께이 작업을 수행하는 방법을 예입니다.

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

여기있어. Rudolf Widmann이이 질문에 대한 답변을 게시 한 것처럼 보입니다 here. 따라서 자바 솔루션은 다음과 같습니다.

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

그러나 어떻게 이것을 파이썬에서 할 수 있습니까? Py4j로 구현하는 방법은 무엇입니까?

+0

을 구현하기 위해 노력했다. –

+0

Py4j에는 프로그래머가 Eclipse API와 상호 작용할 수있는 Eclipse 플러그인이 있습니다. 그냥보십시오 : http://py4j.wordpress.com/2010/02/18/eclipse-py4j/ – Adam

답변

0

은, 그것이 '정상'자바 프로그램에서 작동하지 않습니다 자바 코드는 Eclipse 플러그인에서 작동하는 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()