2

I want to write a eclipse plugin which reads the contents from the editor window and displays it to me. This should've been a very simple task after I found how to do this on this post Adding item to Eclipse text viewer context menu

Before I state my problem, lemme state what I did till now:

I am developing on eclipse 3.7 and I created a simple hello world plugin whose code was auto-generated by eclipse for me.

Here is the run function:

public void run(IAction action) {
        MessageDialog.openInformation(
            window.getShell(),
            "AnirudhPlugin",
            "Hello, Eclipse world");


        try {               
            IEditorPart part = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();

            if(part instanceof IReusableEditor){

                    System.out.println("is of type reusable editor");

                final IReusableEditor editor = (IReusableEditor)part;

                if(editor instanceof AbstractTextEditor){
                    System.out.println("abstract");
                }
                if(editor instanceof StatusTextEditor){
                    System.out.println("status");
                }
                if(editor instanceof TextEditor){
                    System.out.println("text");
                }
                if(editor instanceof AbstractDecoratedTextEditor){
                    System.out.println("abs dec");
                }               
                if(editor instanceof CommonSourceNotFoundEditor){
                    System.out.println("comm");
                }

            }


            if ( part instanceof ITextEditor ) {
                final ITextEditor editor = (ITextEditor)part;
                IDocumentProvider prov = editor.getDocumentProvider();
                IDocument doc = prov.getDocument( editor.getEditorInput() );
                ISelection sel = editor.getSelectionProvider().getSelection();
                if ( sel instanceof TextSelection ) {
                    final TextSelection textSel = (TextSelection)sel;
                    String newText = "/*" + textSel.getText() + "*/";
                    MessageDialog.openInformation(
                            window.getShell(),
                            "AnirudhPlugin",
                            newText);
                    doc.replace( textSel.getOffset(), textSel.getLength(), newText );
                }
            }else{
                MessageDialog.openInformation(
                        window.getShell(),
                        "AnirudhPlugin",
                        "Not ITextEditor");
            }
        } catch ( Exception ex ) {
            ex.printStackTrace();
        }

    }

Now when I run this code, I get the output that its of type "reusable editor" unlike what it should've been "ITextEditor" (please correct me I am making some silly statement since I am a newbie in eclipse Plugin dev). I tried to check IReusableEditor instance type(as you can see in the code above, I have tried to check by using "instance of") but to my surprise itsnot pointing to any of the classes which implements IReusableEditor interface.

The editor from which I was trying to read was a simple editor window in which I had written some java code.

Also I get doc2 variable value as null for the following

final IDocument doc2 = (IDocument) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor().getAdapter(IDocument.class);

Please let me know where am I doing the mistake, thanks in advance.

Community
  • 1
  • 1
Durin
  • 2,070
  • 5
  • 23
  • 37

1 Answers1

2

Okay I got how to read from the editor window

if (editor instanceof CompilationUnitEditor) {

            CompilationUnitEditor compEditor = (CompilationUnitEditor)editor;

            IEditorInput input = compEditor.getEditorInput();

            IDocumentProvider provider = compEditor.getDocumentProvider();

            IDocument document = provider.getDocument(input);

            if (document != null) {

                   System.out.println("document contents:" + document.get());

            }

     }
Durin
  • 2,070
  • 5
  • 23
  • 37
  • A `CompilationUnitEditor` is an instance of `ITextEditor`. I'm not sure why your first code would fail. – Paul Webster Nov 28 '11 at 13:17
  • I think you're overlooking that since a Class can implement more than one Interface, a large if-then-else such as this is simply going to proceed with the first test that succeeds. Odds are that the search scope just didn't include enough plug-ins to find the implementors--one of which is AbstractTextEditor. – nitind Nov 29 '11 at 06:35