2013-02-15 88 views
2

我有一個處理程序,我想從工作臺中的活動編輯器中獲取文本。從下面的屏幕截圖中,我想獲得Test.java中的所有內容(「public class Test ...」)。如何從Eclipse中的活動編輯器獲取文本PDE

Test class screenshot

我的「源」菜單下的成功添加了新的命令。只是不知道現在從哪裏獲得活動編輯器的文本。這裏是我到目前爲止,我試圖讓文本(它只是在一個彈出窗口中顯示的文件名):

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; 
    } 
} 
+1

可能重複的[Eclipse插件:從編輯器獲取內容](http://stackoverflow.com/questions/6661382/eclipse-plugin-get-content-from-editor) – 2015-11-13 15:58:06

回答

2

一旦你的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

相關問題