views:

456

answers:

1

Hello, please can you help me to catch the buffered text present into the editor, I have this code:

System.out.println( Workbench.getInstance().getActiveWorkbenchWindow().getActivePage().getActiveEditor().getTitle() );
System.out.println( Workbench.getInstance().getActiveWorkbenchWindow().getActivePage().getActiveEditor().getEditorInput() );

I can not follow the path indicated by the first line, and pratically re-reading the file because I need exactly the text buffer.

In the second line instead I receive always a path from the casted class org.eclipse.ui.examples.rcp.texteditor.editors.PathEditorInput (which I don't want to include @runtime in my app)

Please help me, tnx

+2  A: 

IEditorPart.getEditorInput() returns an IEditorInput representing the editor input. If the active editor is using PathEditorInput as an input you'll need to either bundle it or refactor the code to not use the example rcp editor input - the PathEditorInput you mention is an rcp example.

For example you can use one of the standard editors like org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditor or org.eclipse.ui.editors.text.TextEditor and a org.eclipse.ui.part.FileEditorInput.

Did you create your project with an example wizard? If so that would explain where the example usage has come from.

as far as getting the text, the following snippet will obtain the Editor if it is an instance of AbstractTextEditor, then will retrieve the content from the document.

Note there are some discouraged accesses in this call, if you register as a listener on the SelectionService, you can keep track of the active selection and avoid having to query the workbench for the active Editor.

AbstractTextEditor part = (AbstractTextEditor) Workbench.getInstance()
        .getActiveWorkbenchWindow().getActivePage().getActiveEditor()
        .getAdapter(AbstractTextEditor.class);

if (part != null) {

    IDocument document = part.getDocumentProvider().getDocument(
            part.getEditorInput());

    String content = document.get();

    //do something with the text
}
Rich Seller