在其中进行编辑/键入时如何读取Eclipse编辑器的内容?

时间:2019-01-19 06:31:45

标签: java eclipse plugins eclipse-plugin

我正在开发一个Eclipse插件,它需要每分钟读取编辑器的内容。因此,我想要一个按钮来启动此过程,而要停止它。由于我是插件开发的新手,所以我尝试着用Eclipse中提供的模板工作。但是运行时应用程序崩溃了。

由于我是插件开发的新手,所以我尝试着用eclipse中给出的模板工作。但是运行时应用程序崩溃了。

public class SampleHandler extends AbstractHandler {

    int filename=0; 

    @Override
    public Object execute(ExecutionEvent event) throws ExecutionException {
        //IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
        for (int i=0; i<10;i++) {

            /*MessageDialog.openInformation(
                window.getShell(),
                "TestEditorReader",
                "Running in background"); */// function call to print in the window

            PrintWriter writer = null;

            try {
                writer = new PrintWriter(filename+"file.txt", "UTF-8");
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            filename++;
            writer.println(getCurrentEditorContent()); // function call to store it in files
            writer.close();

            try {
                Thread.sleep(10000);
            }
            catch (InterruptedException ie) {

            }
        }

        return null;


    }

    public String getCurrentEditorContent() {
        final IEditorPart editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
            .getActiveEditor();
        if (!(editor instanceof ITextEditor)) return null;
        ITextEditor ite = (ITextEditor)editor;
        IDocument doc = ite.getDocumentProvider().getDocument(ite.getEditorInput());
        return doc.get();
    }

}

0 个答案:

没有答案